fix: workspaces empty error in local mode (#5045)

* fix: workspaces empty error in local mode

* fix: not allowed the user delete the only workspace
This commit is contained in:
Lucas.Xu 2024-04-03 12:21:02 +08:00 committed by GitHub
parent a25c728866
commit e8931c5bd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 14 deletions

View File

@ -107,8 +107,7 @@ class _MobileWorkspace extends StatelessWidget {
return BlocBuilder<UserWorkspaceBloc, UserWorkspaceState>( return BlocBuilder<UserWorkspaceBloc, UserWorkspaceState>(
builder: (context, state) { builder: (context, state) {
final currentWorkspace = state.currentWorkspace; final currentWorkspace = state.currentWorkspace;
final workspaces = state.workspaces; if (currentWorkspace == null) {
if (currentWorkspace == null || workspaces.isEmpty) {
return const SizedBox.shrink(); return const SizedBox.shrink();
} }
return GestureDetector( return GestureDetector(

View File

@ -8,6 +8,7 @@ import 'package:appflowy/user/application/user_service.dart';
import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-error/code.pbenum.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-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_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_result/appflowy_result.dart'; import 'package:appflowy_result/appflowy_result.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
@ -36,10 +37,11 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
..start(); ..start();
final result = await _fetchWorkspaces(); final result = await _fetchWorkspaces();
final currentWorkspace = result.$1;
final workspaces = result.$2;
final isCollabWorkspaceOn = final isCollabWorkspaceOn =
userProfile.authenticator != AuthenticatorPB.Local && userProfile.authenticator != AuthenticatorPB.Local &&
FeatureFlag.collaborativeWorkspace.isOn; FeatureFlag.collaborativeWorkspace.isOn;
final currentWorkspace = result.$1;
if (currentWorkspace != null && result.$3 == true) { if (currentWorkspace != null && result.$3 == true) {
final result = await _userService final result = await _userService
.openWorkspace(currentWorkspace.workspaceId); .openWorkspace(currentWorkspace.workspaceId);
@ -53,7 +55,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
emit( emit(
state.copyWith( state.copyWith(
currentWorkspace: currentWorkspace, currentWorkspace: currentWorkspace,
workspaces: result.$2, workspaces: workspaces,
isCollabWorkspaceOn: isCollabWorkspaceOn, isCollabWorkspaceOn: isCollabWorkspaceOn,
actionResult: null, actionResult: null,
), ),
@ -89,7 +91,10 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
}); });
}, },
deleteWorkspace: (workspaceId) async { 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 // do not allow to delete the last workspace, otherwise the user
// cannot do create workspace again // cannot do create workspace again
final result = FlowyResult.failure( final result = FlowyResult.failure(
@ -280,6 +285,9 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
final currentWorkspace = final currentWorkspace =
await _userService.getCurrentWorkspace().getOrThrow(); await _userService.getCurrentWorkspace().getOrThrow();
final workspaces = await _userService.getWorkspaces().getOrThrow(); final workspaces = await _userService.getWorkspaces().getOrThrow();
if (workspaces.isEmpty) {
workspaces.add(convertWorkspacePBToUserWorkspace(currentWorkspace));
}
UserWorkspacePB? currentWorkspaceInList = workspaces UserWorkspacePB? currentWorkspaceInList = workspaces
.firstWhereOrNull((e) => e.workspaceId == currentWorkspace.id); .firstWhereOrNull((e) => e.workspaceId == currentWorkspace.id);
if (lastOpenedWorkspaceId != null) { if (lastOpenedWorkspaceId != null) {
@ -289,7 +297,7 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspaceInList = lastOpenedWorkspace; currentWorkspaceInList = lastOpenedWorkspace;
} }
} }
currentWorkspaceInList ??= workspaces.first; currentWorkspaceInList ??= workspaces.firstOrNull;
return ( return (
currentWorkspaceInList, currentWorkspaceInList,
workspaces workspaces
@ -303,6 +311,13 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
return (null, <UserWorkspacePB>[], false); return (null, <UserWorkspacePB>[], false);
} }
} }
UserWorkspacePB convertWorkspacePBToUserWorkspace(WorkspacePB workspace) {
return UserWorkspacePB.create()
..workspaceId = workspace.id
..name = workspace.name
..createdAtTimestamp = workspace.createTime;
}
} }
@freezed @freezed

View File

@ -219,13 +219,20 @@ class _SidebarState extends State<_Sidebar> {
// user or workspace, setting // user or workspace, setting
Padding( Padding(
padding: menuHorizontalInset, padding: menuHorizontalInset,
child: context.read<UserWorkspaceBloc>().state.isCollabWorkspaceOn child:
? SidebarWorkspace( // if the workspaces are empty, show the user profile instead
userProfile: widget.userProfile, context.read<UserWorkspaceBloc>().state.isCollabWorkspaceOn &&
) context
: SidebarUser( .read<UserWorkspaceBloc>()
userProfile: widget.userProfile, .state
), .workspaces
.isNotEmpty
? SidebarWorkspace(
userProfile: widget.userProfile,
)
: SidebarUser(
userProfile: widget.userProfile,
),
), ),
const VSpace(20), const VSpace(20),

View File

@ -161,7 +161,7 @@ class _SidebarSwitchWorkspaceButtonState
builder: (context, state) { builder: (context, state) {
final currentWorkspace = state.currentWorkspace; final currentWorkspace = state.currentWorkspace;
final workspaces = state.workspaces; final workspaces = state.workspaces;
if (currentWorkspace == null || workspaces.isEmpty) { if (currentWorkspace == null) {
return const SizedBox.shrink(); return const SizedBox.shrink();
} }
return WorkspacesMenu( return WorkspacesMenu(