Skip to content

Commit

Permalink
fix(core): cant leave workspace (#6999)
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed May 21, 2024
1 parent 7fddd14 commit d5c93f1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ArrowRightSmallIcon } from '@blocksuite/icons';
import {
GlobalContextService,
useLiveData,
useService,
useServices,
WorkspaceService,
WorkspacesService,
} from '@toeverything/infra';
Expand All @@ -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) {
Expand Down Expand Up @@ -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 (
Expand Down
7 changes: 6 additions & 1 deletion packages/frontend/core/src/modules/permissions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type Framework,
WorkspaceScope,
WorkspaceService,
WorkspacesService,
} from '@toeverything/infra';

import { WorkspacePermission } from './entities/permission';
Expand All @@ -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]);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
},
});
}
}

0 comments on commit d5c93f1

Please sign in to comment.