Skip to content

Commit e01e711

Browse files
✨(frontend) enhance document management types and utilities
- Updated the `Access` and `Doc` interfaces to include new properties for role management and document link reach. - Introduced utility functions to handle document link reach and role, improving the logic for determining access levels. - Refactored the `isOwnerOrAdmin` function to simplify role checks for document ownership and admin status.
1 parent 3e137db commit e01e711

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

src/frontend/apps/impress/src/features/docs/doc-management/types.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import { User } from '@/features/auth';
22

33
export interface Access {
44
id: string;
5+
max_ancestors_role: Role;
56
role: Role;
7+
max_role: Role;
68
team: string;
79
user: User;
10+
document: {
11+
id: string;
12+
path: string;
13+
depth: number;
14+
};
815
abilities: {
916
destroy: boolean;
1017
partial_update: boolean;
@@ -21,10 +28,17 @@ export enum Role {
2128
OWNER = 'owner',
2229
}
2330

31+
export const RoleImportance = {
32+
[Role.READER]: 1,
33+
[Role.EDITOR]: 2,
34+
[Role.ADMIN]: 3,
35+
[Role.OWNER]: 4,
36+
};
37+
2438
export enum LinkReach {
2539
RESTRICTED = 'restricted',
26-
PUBLIC = 'public',
2740
AUTHENTICATED = 'authenticated',
41+
PUBLIC = 'public',
2842
}
2943

3044
export enum LinkRole {
@@ -43,13 +57,19 @@ export interface Doc {
4357
created_at: string;
4458
creator: string;
4559
depth: number;
60+
path: string;
4661
is_favorite: boolean;
4762
link_reach: LinkReach;
4863
link_role: LinkRole;
4964
nb_accesses_direct: number;
5065
nb_accesses_ancestors: number;
66+
computed_link_reach: LinkReach;
67+
computed_link_role?: LinkRole;
68+
ancestors_link_reach: LinkReach;
69+
ancestors_link_role?: LinkRole;
5170
numchild: number;
5271
updated_at: string;
72+
user_role: Role;
5373
user_roles: Role[];
5474
abilities: {
5575
accesses_manage: boolean;
@@ -74,9 +94,16 @@ export interface Doc {
7494
versions_destroy: boolean;
7595
versions_list: boolean;
7696
versions_retrieve: boolean;
97+
link_select_options: LinkSelectOption;
7798
};
7899
}
79100

101+
export interface LinkSelectOption {
102+
public?: LinkRole[];
103+
authenticated?: LinkRole[];
104+
restricted?: LinkRole[];
105+
}
106+
80107
export enum DocDefaultFilter {
81108
ALL_DOCS = 'all_docs',
82109
MY_DOCS = 'my_docs',

src/frontend/apps/impress/src/features/docs/doc-management/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,29 @@ export const base64ToYDoc = (base64: string) => {
2222
export const base64ToBlocknoteXmlFragment = (base64: string) => {
2323
return base64ToYDoc(base64).getXmlFragment('document-store');
2424
};
25+
26+
export const getDocLinkReach = (doc: Doc) => {
27+
if (doc.computed_link_reach) {
28+
return doc.computed_link_reach;
29+
}
30+
return doc.link_reach;
31+
};
32+
33+
export const getDocLinkRole = (doc: Doc) => {
34+
if (doc.computed_link_role) {
35+
return doc.computed_link_role;
36+
}
37+
return doc.link_role;
38+
};
39+
40+
export const docLinkIsDesync = (doc: Doc) => {
41+
// If the document has no ancestors
42+
if (!doc.ancestors_link_reach) {
43+
return false;
44+
}
45+
46+
return (
47+
doc.computed_link_reach !== doc.ancestors_link_reach ||
48+
doc.computed_link_role !== doc.ancestors_link_role
49+
);
50+
};

src/frontend/apps/impress/src/features/docs/doc-tree/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ export const subPageToTree = (children: Doc[]): TreeViewDataType<Doc>[] => {
1515
};
1616

1717
export const isOwnerOrAdmin = (doc: Doc): boolean => {
18-
return doc.user_roles.some(
19-
(role) => role === Role.OWNER || role === Role.ADMIN,
20-
);
18+
const userRole = doc.user_role;
19+
return userRole === Role.OWNER || userRole === Role.ADMIN;
2120
};
2221

2322
export const canDrag = (doc: Doc): boolean => {

src/frontend/apps/impress/src/features/docs/docs-grid/hooks/useDragAndDrop.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function useDragAndDrop(
2121
const [selectedDoc, setSelectedDoc] = useState<Doc>();
2222
const [canDrop, setCanDrop] = useState<boolean>();
2323

24-
const canDrag = selectedDoc?.user_roles.some((role) => role === Role.OWNER);
24+
const canDrag = selectedDoc?.user_role === Role.OWNER;
2525

2626
const mouseSensor = useSensor(MouseSensor, { activationConstraint });
2727
const touchSensor = useSensor(TouchSensor, { activationConstraint });

src/frontend/apps/impress/src/features/service-worker/plugins/ApiPlugin.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { WorkboxPlugin } from 'workbox-core';
22

33
import { Doc, DocsResponse } from '@/docs/doc-management';
4-
import { LinkReach, LinkRole } from '@/docs/doc-management/types';
4+
import { LinkReach, LinkRole, Role } from '@/docs/doc-management/types';
55

66
import { DBRequest, DocsDB } from '../DocsDB';
77
import { RequestSerializer } from '../RequestSerializer';
@@ -201,10 +201,21 @@ export class ApiPlugin implements WorkboxPlugin {
201201
versions_destroy: true,
202202
versions_list: true,
203203
versions_retrieve: true,
204+
link_select_options: {
205+
public: [LinkRole.READER, LinkRole.EDITOR],
206+
authenticated: [LinkRole.READER, LinkRole.EDITOR],
207+
restricted: undefined,
208+
},
204209
},
205210
link_reach: LinkReach.RESTRICTED,
206211
link_role: LinkRole.READER,
207-
user_roles: [],
212+
user_roles: [Role.OWNER],
213+
user_role: Role.OWNER,
214+
path: '',
215+
computed_link_reach: LinkReach.RESTRICTED,
216+
computed_link_role: LinkRole.READER,
217+
ancestors_link_reach: LinkReach.RESTRICTED,
218+
ancestors_link_role: undefined,
208219
};
209220

210221
await DocsDB.cacheResponse(

0 commit comments

Comments
 (0)