fix: try to reopen the first workspace if the workspace deletion failed (#5844)

This commit is contained in:
Lucas.Xu 2024-07-31 17:52:36 +08:00 committed by GitHub
parent d1c1449cf6
commit dce9231118
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 12 deletions

View File

@ -1,5 +1,3 @@
import 'package:flutter/foundation.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/shared/feature_flags.dart';
import 'package:appflowy/user/application/user_listener.dart';
@ -12,6 +10,7 @@ import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:collection/collection.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:protobuf/protobuf.dart';
@ -83,10 +82,19 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
emit(
state.copyWith(
currentWorkspace: currentWorkspace,
workspaces: workspaces,
),
);
// try to open the workspace if the current workspace is not the same
if (currentWorkspace != null &&
currentWorkspace.workspaceId !=
state.currentWorkspace?.workspaceId) {
Log.info(
'fetch workspaces: try to open workspace: ${currentWorkspace.workspaceId}',
);
add(OpenWorkspace(currentWorkspace.workspaceId));
}
},
createWorkspace: (name) async {
emit(
@ -159,13 +167,11 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
}
final result = await _userService.deleteWorkspaceById(workspaceId);
final workspaces = result.fold(
// remove the deleted workspace from the list instead of fetching
// the workspaces again
(s) => state.workspaces
.where((e) => e.workspaceId != workspaceId)
.toList(),
(e) => state.workspaces,
// fetch the workspaces again to check if the current workspace is deleted
final workspacesResult = await _fetchWorkspaces();
final workspaces = workspacesResult.$2;
final containsDeletedWorkspace = workspaces.any(
(e) => e.workspaceId == workspaceId,
);
result
..onSuccess((_) {
@ -177,6 +183,11 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
})
..onFailure((f) {
Log.error('delete workspace error: $f');
// if the workspace is deleted but return an error, we need to
// open the first workspace
if (!containsDeletedWorkspace) {
add(OpenWorkspace(workspaces.first.workspaceId));
}
});
emit(
state.copyWith(

View File

@ -11,6 +11,7 @@ use flowy_user_pub::workspace_service::UserWorkspaceService;
use lib_infra::async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::info;
pub struct UserDepsResolver();
@ -61,9 +62,14 @@ impl UserWorkspaceService for UserWorkspaceServiceImpl {
}
fn did_delete_workspace(&self, workspace_id: String) -> FlowyResult<()> {
self
// The remove_indices_for_workspace should not block the deletion of the workspace
// Log the error and continue
if let Err(err) = self
.folder_manager
.remove_indices_for_workspace(workspace_id)?;
.remove_indices_for_workspace(workspace_id)
{
info!("Error removing indices for workspace: {}", err);
}
Ok(())
}