diff --git a/packages/frontend/core/src/components/affine/setting-modal/workspace-setting/new-workspace-setting-detail/delete-leave-workspace/index.tsx b/packages/frontend/core/src/components/affine/setting-modal/workspace-setting/new-workspace-setting-detail/delete-leave-workspace/index.tsx index ee5cc14d7e..271bcc5b90 100644 --- a/packages/frontend/core/src/components/affine/setting-modal/workspace-setting/new-workspace-setting-detail/delete-leave-workspace/index.tsx +++ b/packages/frontend/core/src/components/affine/setting-modal/workspace-setting/new-workspace-setting-detail/delete-leave-workspace/index.tsx @@ -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 ( diff --git a/packages/frontend/core/src/modules/permissions/index.ts b/packages/frontend/core/src/modules/permissions/index.ts index 495f0bc805..667ffc3fcd 100644 --- a/packages/frontend/core/src/modules/permissions/index.ts +++ b/packages/frontend/core/src/modules/permissions/index.ts @@ -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]); } diff --git a/packages/frontend/core/src/modules/permissions/services/permission.ts b/packages/frontend/core/src/modules/permissions/services/permission.ts index ca9c5ef6a5..fcf9168da6 100644 --- a/packages/frontend/core/src/modules/permissions/services/permission.ts +++ b/packages/frontend/core/src/modules/permissions/services/permission.ts @@ -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(); + } } diff --git a/packages/frontend/core/src/modules/permissions/stores/permission.ts b/packages/frontend/core/src/modules/permissions/stores/permission.ts index dfe15a9502..02f756f729 100644 --- a/packages/frontend/core/src/modules/permissions/stores/permission.ts +++ b/packages/frontend/core/src/modules/permissions/stores/permission.ts @@ -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, + }, + }); + } }