Skip to content

Commit

Permalink
perf: space collaborators page (#1151)
Browse files Browse the repository at this point in the history
* perf: the space collaborators page supports managing base collaborators

* fix: showing space collaborators in reverse order

* feat: update usage feature keys
  • Loading branch information
Sky-FE authored Dec 9, 2024
1 parent b7fc1e5 commit b7c3b99
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class CollaboratorService {
},
},
},
orderBy: { createdTime: 'asc' },
orderBy: { createdTime: 'desc' },
});
return collaborators.map((collaborator) => ({
userId: collaborator.user.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import type { IRole } from '@teable/core';
import { canManageRole, Role } from '@teable/core';
import { Settings } from '@teable/icons';
import type { ListSpaceCollaboratorRo } from '@teable/openapi';
import type { ListSpaceCollaboratorRo, UpdateBaseCollaborateRo } from '@teable/openapi';
import {
deleteBaseCollaborator,
deleteSpaceCollaborator,
getSpaceCollaboratorList,
updateBaseCollaborator,
updateSpaceCollaborator,
} from '@teable/openapi';
import { ReactQueryKeys, useSession } from '@teable/sdk';
Expand All @@ -14,6 +16,7 @@ import { useRouter } from 'next/router';
import { useTranslation } from 'next-i18next';
import type { FC, PropsWithChildren } from 'react';
import React, { useMemo } from 'react';
import { useFilteredRoleStatic as useFilteredBaseRoleStatic } from '../base/useFilteredRoleStatic';
import { CollaboratorItem } from '../components/CollaboratorItem';
import { CollaboratorList } from '../components/CollaboratorList';
import { RoleSelect } from '../components/RoleSelect';
Expand Down Expand Up @@ -43,16 +46,50 @@ export const Collaborators: FC<PropsWithChildren<ICollaborators>> = (props) => {
});

const { mutate: updateCollaborator, isLoading: updateCollaboratorLoading } = useMutation({
mutationFn: updateSpaceCollaborator,
onSuccess: async () => {
mutationFn: ({
resourceId,
updateCollaborateRo,
isBase,
}: {
resourceId: string;
updateCollaborateRo: { userId: string; role: IRole };
isBase?: boolean;
}) =>
isBase
? updateBaseCollaborator({
baseId: resourceId,
updateBaseCollaborateRo: updateCollaborateRo as UpdateBaseCollaborateRo,
})
: updateSpaceCollaborator({
spaceId: resourceId,
updateSpaceCollaborateRo: updateCollaborateRo,
}),
onSuccess: async (_, context) => {
const { isBase, resourceId } = context;

await queryClient.invalidateQueries(ReactQueryKeys.spaceCollaboratorList(spaceId));
queryClient.invalidateQueries(ReactQueryKeys.space(spaceId));
queryClient.invalidateQueries(ReactQueryKeys.spaceList());
if (isBase) {
queryClient.invalidateQueries(ReactQueryKeys.baseCollaboratorList(resourceId));
} else {
queryClient.invalidateQueries(ReactQueryKeys.space(spaceId));
queryClient.invalidateQueries(ReactQueryKeys.spaceList());
}
},
});

const { mutate: deleteCollaborator, isLoading: deleteCollaboratorLoading } = useMutation({
mutationFn: deleteSpaceCollaborator,
mutationFn: ({
userId,
resourceId,
isBase,
}: {
userId: string;
resourceId: string;
isBase?: boolean;
}) =>
isBase
? deleteBaseCollaborator({ baseId: resourceId, userId })
: deleteSpaceCollaborator({ spaceId: resourceId, userId }),
onSuccess: async (_, context) => {
if (context.userId === user.id) {
router.push('/space');
Expand All @@ -69,6 +106,7 @@ export const Collaborators: FC<PropsWithChildren<ICollaborators>> = (props) => {
);

const filteredRoleStatic = useFilteredRoleStatic(currentRole);
const filteredBaseRoleStatic = useFilteredBaseRoleStatic(currentRole);

const goBase = (baseId: string) => {
router.push(`/base/${baseId}`);
Expand All @@ -94,9 +132,9 @@ export const Collaborators: FC<PropsWithChildren<ICollaborators>> = (props) => {
avatar={avatar}
createdTime={createdTime}
onDeleted={(userId) => {
deleteCollaborator({ spaceId, userId });
deleteCollaborator({ resourceId: base ? base.id : spaceId, userId, isBase });
}}
showDelete={canOperator && !isBase}
showDelete={canOperator}
deletable={!deleteCollaboratorLoading && canOperator}
collaboratorTips={
isBase && (
Expand All @@ -119,10 +157,14 @@ export const Collaborators: FC<PropsWithChildren<ICollaborators>> = (props) => {
<RoleSelect
className="mx-1"
value={role}
options={filteredRoleStatic}
disabled={updateCollaboratorLoading || !canOperator || isBase}
options={isBase ? filteredBaseRoleStatic : filteredRoleStatic}
disabled={updateCollaboratorLoading || !canOperator}
onChange={(role) =>
updateCollaborator({ spaceId, updateSpaceCollaborateRo: { userId, role } })
updateCollaborator({
resourceId: base ? base.id : spaceId,
updateCollaborateRo: { userId, role },
isBase,
})
}
/>
</CollaboratorItem>
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi/src/usage/get-space-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export enum UsageFeature {
NumRows = 'numRows',
AttachmentSize = 'attachmentSize',
NumDatabaseConnections = 'numDatabaseConnections',
NumCollaborators = 'numCollaborators',
}

export const usageFeatureSchema = z.object({
[UsageFeature.NumRows]: z.number(),
[UsageFeature.AttachmentSize]: z.number(),
[UsageFeature.NumDatabaseConnections]: z.number(),
[UsageFeature.NumCollaborators]: z.number(),
});

export enum UsageFeatureLimit {
Expand Down

0 comments on commit b7c3b99

Please sign in to comment.