fix(core): cant leave workspace (#6999)

This commit is contained in:
EYHN 2024-05-21 03:41:03 +00:00
parent 7fddd14f72
commit d5c93f10ac
No known key found for this signature in database
GPG Key ID: 46C9E26A75AB276C
4 changed files with 63 additions and 12 deletions

View File

@ -8,7 +8,7 @@ import { ArrowRightSmallIcon } from '@blocksuite/icons';
import {
GlobalContextService,
useLiveData,
useService,
useServices,
WorkspaceService,
WorkspacesService,
} from '@toeverything/infra';
@ -24,25 +24,34 @@ import { WorkspaceSubPath } from '../../../../../../shared';
import { WorkspaceDeleteModal } from './delete';
export const DeleteLeaveWorkspace = () => {
const {
workspaceService,
globalContextService,
workspacePermissionService,
workspacesService,
} = useServices({
WorkspaceService,
GlobalContextService,
WorkspacePermissionService,
WorkspacesService,
});
const t = useAFFiNEI18N();
const workspace = useService(WorkspaceService).workspace;
const workspace = workspaceService.workspace;
const { jumpToSubPath, jumpToIndex } = useNavigateHelper();
// fixme: cloud regression
const [showDelete, setShowDelete] = useState(false);
const [showLeave, setShowLeave] = useState(false);
const setSettingModal = useSetAtom(openSettingModalAtom);
const workspacesService = useService(WorkspacesService);
const workspaceList = useLiveData(workspacesService.list.workspaces$);
const currentWorkspaceId = useLiveData(
useService(GlobalContextService).globalContext.workspaceId.$
globalContextService.globalContext.workspaceId.$
);
const permissionService = useService(WorkspacePermissionService);
const isOwner = useLiveData(permissionService.permission.isOwner$);
const isOwner = useLiveData(workspacePermissionService.permission.isOwner$);
useEffect(() => {
permissionService.permission.revalidate();
}, [permissionService]);
workspacePermissionService.permission.revalidate();
}, [workspacePermissionService]);
const onLeaveOrDelete = useCallback(() => {
if (isOwner !== null) {
@ -73,18 +82,24 @@ export const DeleteLeaveWorkspace = () => {
}
}
await workspacesService.deleteWorkspace(workspace.meta);
if (isOwner) {
await workspacesService.deleteWorkspace(workspace.meta);
} else {
await workspacePermissionService.leaveWorkspace();
}
notify.success({ title: t['Successfully deleted']() });
}, [
setSettingModal,
currentWorkspaceId,
workspace.id,
workspace.meta,
workspacesService,
isOwner,
t,
workspaceList,
jumpToSubPath,
jumpToIndex,
workspacesService,
workspacePermissionService,
]);
return (

View File

@ -5,6 +5,7 @@ import {
type Framework,
WorkspaceScope,
WorkspaceService,
WorkspacesService,
} from '@toeverything/infra';
import { WorkspacePermission } from './entities/permission';
@ -14,7 +15,11 @@ import { WorkspacePermissionStore } from './stores/permission';
export function configurePermissionsModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(WorkspacePermissionService)
.service(WorkspacePermissionService, [
WorkspaceService,
WorkspacesService,
WorkspacePermissionStore,
])
.store(WorkspacePermissionStore, [GraphQLService])
.entity(WorkspacePermission, [WorkspaceService, WorkspacePermissionStore]);
}

View File

@ -1,7 +1,25 @@
import type { WorkspaceService, WorkspacesService } from '@toeverything/infra';
import { Service } from '@toeverything/infra';
import { WorkspacePermission } from '../entities/permission';
import type { WorkspacePermissionStore } from '../stores/permission';
export class WorkspacePermissionService extends Service {
permission = this.framework.createEntity(WorkspacePermission);
constructor(
private readonly workspaceService: WorkspaceService,
private readonly workspacesService: WorkspacesService,
private readonly store: WorkspacePermissionStore
) {
super();
}
async leaveWorkspace() {
await this.store.leaveWorkspace(
this.workspaceService.workspace.id,
this.workspaceService.workspace.name$.value ?? ''
);
this.workspacesService.list.revalidate();
}
}

View File

@ -1,5 +1,5 @@
import type { GraphQLService } from '@affine/core/modules/cloud';
import { getIsOwnerQuery } from '@affine/graphql';
import { getIsOwnerQuery, leaveWorkspaceMutation } from '@affine/graphql';
import { Store } from '@toeverything/infra';
export class WorkspacePermissionStore extends Store {
@ -18,4 +18,17 @@ export class WorkspacePermissionStore extends Store {
return isOwner.isOwner;
}
/**
* @param workspaceName for send email
*/
async leaveWorkspace(workspaceId: string, workspaceName: string) {
await this.graphqlService.gql({
query: leaveWorkspaceMutation,
variables: {
workspaceId,
workspaceName,
},
});
}
}