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>(
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(

View File

@ -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<UserWorkspaceEvent, UserWorkspaceState> {
..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<UserWorkspaceEvent, UserWorkspaceState> {
emit(
state.copyWith(
currentWorkspace: currentWorkspace,
workspaces: result.$2,
workspaces: workspaces,
isCollabWorkspaceOn: isCollabWorkspaceOn,
actionResult: null,
),
@ -89,7 +91,10 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
});
},
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<UserWorkspaceEvent, UserWorkspaceState> {
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<UserWorkspaceEvent, UserWorkspaceState> {
currentWorkspaceInList = lastOpenedWorkspace;
}
}
currentWorkspaceInList ??= workspaces.first;
currentWorkspaceInList ??= workspaces.firstOrNull;
return (
currentWorkspaceInList,
workspaces
@ -303,6 +311,13 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
return (null, <UserWorkspacePB>[], false);
}
}
UserWorkspacePB convertWorkspacePBToUserWorkspace(WorkspacePB workspace) {
return UserWorkspacePB.create()
..workspaceId = workspace.id
..name = workspace.name
..createdAtTimestamp = workspace.createTime;
}
}
@freezed

View File

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

View File

@ -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(