diff --git a/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_page_header.dart b/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_page_header.dart index 9407313485..c6782c0ce1 100644 --- a/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_page_header.dart +++ b/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_page_header.dart @@ -107,8 +107,7 @@ class _MobileWorkspace extends StatelessWidget { return BlocBuilder( builder: (context, state) { final currentWorkspace = state.currentWorkspace; - final workspaces = state.workspaces; - if (currentWorkspace == null || workspaces.isEmpty) { + if (currentWorkspace == null) { return const SizedBox.shrink(); } return GestureDetector( diff --git a/frontend/appflowy_flutter/lib/workspace/application/user/user_workspace_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/user/user_workspace_bloc.dart index 3f8f468aa2..92c663c614 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/user/user_workspace_bloc.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/user/user_workspace_bloc.dart @@ -8,6 +8,7 @@ import 'package:appflowy/user/application/user_service.dart'; import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/protobuf/flowy-error/code.pbenum.dart'; import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; +import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart'; import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; import 'package:appflowy_result/appflowy_result.dart'; import 'package:collection/collection.dart'; @@ -36,10 +37,11 @@ class UserWorkspaceBloc extends Bloc { ..start(); final result = await _fetchWorkspaces(); + final currentWorkspace = result.$1; + final workspaces = result.$2; final isCollabWorkspaceOn = userProfile.authenticator != AuthenticatorPB.Local && FeatureFlag.collaborativeWorkspace.isOn; - final currentWorkspace = result.$1; if (currentWorkspace != null && result.$3 == true) { final result = await _userService .openWorkspace(currentWorkspace.workspaceId); @@ -53,7 +55,7 @@ class UserWorkspaceBloc extends Bloc { emit( state.copyWith( currentWorkspace: currentWorkspace, - workspaces: result.$2, + workspaces: workspaces, isCollabWorkspaceOn: isCollabWorkspaceOn, actionResult: null, ), @@ -89,7 +91,10 @@ class UserWorkspaceBloc extends Bloc { }); }, deleteWorkspace: (workspaceId) async { - if (state.workspaces.length <= 1) { + final remoteWorkspaces = await _fetchWorkspaces().then( + (value) => value.$2, + ); + if (state.workspaces.length <= 1 || remoteWorkspaces.length <= 1) { // do not allow to delete the last workspace, otherwise the user // cannot do create workspace again final result = FlowyResult.failure( @@ -280,6 +285,9 @@ class UserWorkspaceBloc extends Bloc { final currentWorkspace = await _userService.getCurrentWorkspace().getOrThrow(); final workspaces = await _userService.getWorkspaces().getOrThrow(); + if (workspaces.isEmpty) { + workspaces.add(convertWorkspacePBToUserWorkspace(currentWorkspace)); + } UserWorkspacePB? currentWorkspaceInList = workspaces .firstWhereOrNull((e) => e.workspaceId == currentWorkspace.id); if (lastOpenedWorkspaceId != null) { @@ -289,7 +297,7 @@ class UserWorkspaceBloc extends Bloc { currentWorkspaceInList = lastOpenedWorkspace; } } - currentWorkspaceInList ??= workspaces.first; + currentWorkspaceInList ??= workspaces.firstOrNull; return ( currentWorkspaceInList, workspaces @@ -303,6 +311,13 @@ class UserWorkspaceBloc extends Bloc { return (null, [], false); } } + + UserWorkspacePB convertWorkspacePBToUserWorkspace(WorkspacePB workspace) { + return UserWorkspacePB.create() + ..workspaceId = workspace.id + ..name = workspace.name + ..createdAtTimestamp = workspace.createTime; + } } @freezed diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart index a992ea6c20..4bb0600462 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart @@ -219,13 +219,20 @@ class _SidebarState extends State<_Sidebar> { // user or workspace, setting Padding( padding: menuHorizontalInset, - child: context.read().state.isCollabWorkspaceOn - ? SidebarWorkspace( - userProfile: widget.userProfile, - ) - : SidebarUser( - userProfile: widget.userProfile, - ), + child: + // if the workspaces are empty, show the user profile instead + context.read().state.isCollabWorkspaceOn && + context + .read() + .state + .workspaces + .isNotEmpty + ? SidebarWorkspace( + userProfile: widget.userProfile, + ) + : SidebarUser( + userProfile: widget.userProfile, + ), ), const VSpace(20), diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar_workspace.dart b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar_workspace.dart index 08c4d1e270..a20818b105 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar_workspace.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar_workspace.dart @@ -161,7 +161,7 @@ class _SidebarSwitchWorkspaceButtonState builder: (context, state) { final currentWorkspace = state.currentWorkspace; final workspaces = state.workspaces; - if (currentWorkspace == null || workspaces.isEmpty) { + if (currentWorkspace == null) { return const SizedBox.shrink(); } return WorkspacesMenu(