Skip to content

Commit 93d9dec

Browse files
PanchoutNathanAntoLC
authored andcommitted
✨(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 adb15de commit 93d9dec

File tree

9 files changed

+74
-32
lines changed

9 files changed

+74
-32
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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Y from 'yjs';
22

3-
import { Doc, Role } from './types';
3+
import { Doc, LinkReach, LinkRole, Role } from './types';
44

55
export const currentDocRole = (abilities: Doc['abilities']): Role => {
66
return abilities.destroy
@@ -22,3 +22,23 @@ 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): LinkReach => {
27+
return doc.computed_link_reach ?? doc.link_reach;
28+
};
29+
30+
export const getDocLinkRole = (doc: Doc): LinkRole => {
31+
return doc.computed_link_role ?? doc.link_role;
32+
};
33+
34+
export const docLinkIsDesync = (doc: Doc) => {
35+
// If the document has no ancestors
36+
if (!doc.ancestors_link_reach) {
37+
return false;
38+
}
39+
40+
return (
41+
doc.computed_link_reach !== doc.ancestors_link_reach ||
42+
doc.computed_link_role !== doc.ancestors_link_role
43+
);
44+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './useCreateChildren';
22
export * from './useDocChildren';
3+
export * from './useMove';

src/frontend/apps/impress/src/features/docs/doc-tree/components/DocTree.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import { css } from 'styled-components';
1010

1111
import { Box, StyledLink } from '@/components';
1212
import { useCunninghamTheme } from '@/cunningham';
13+
import { Doc, KEY_SUB_PAGE, useDoc, useDocStore } from '@/docs/doc-management';
14+
import { SimpleDocItem } from '@/docs/docs-grid';
1315

14-
import { Doc, KEY_SUB_PAGE, useDoc, useDocStore } from '../../doc-management';
15-
import { SimpleDocItem } from '../../docs-grid';
1616
import { useDocTree } from '../api/useDocTree';
1717
import { useMoveDoc } from '../api/useMove';
18-
import { canDrag, canDrop } from '../utils';
1918

2019
import { DocSubPageItem } from './DocSubPageItem';
2120
import { DocTreeItemActions } from './DocTreeItemActions';
@@ -211,13 +210,13 @@ export const DocTree = ({ initialTargetId }: DocTreeProps) => {
211210
}
212211
const parentDoc = parentNode?.data.value as Doc;
213212
if (!parentDoc) {
214-
return canDrop(rootNode);
213+
return rootNode?.abilities.move;
215214
}
216-
return canDrop(parentDoc);
215+
return parentDoc?.abilities.move;
217216
}}
218217
canDrag={(node) => {
219218
const doc = node.value as Doc;
220-
return canDrag(doc);
219+
return doc.abilities.move;
221220
}}
222221
rootNodeId={treeContext.root?.id ?? ''}
223222
renderNode={DocSubPageItem}

src/frontend/apps/impress/src/features/docs/doc-tree/components/DocTreeItemActions.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { useCreateChildrenDoc } from '../api/useCreateChildren';
1717
import { useDetachDoc } from '../api/useDetach';
1818
import MoveDocIcon from '../assets/doc-extract-bold.svg';
1919
import { useTreeUtils } from '../hooks';
20-
import { isOwnerOrAdmin } from '../utils';
2120

2221
type DocTreeItemActionsProps = {
2322
doc: Doc;
@@ -36,7 +35,6 @@ export const DocTreeItemActions = ({
3635
const deleteModal = useModal();
3736
const { togglePanel } = useLeftPanelStore();
3837
const copyLink = useCopyDocLink(doc.id);
39-
const canUpdate = isOwnerOrAdmin(doc);
4038
const { isCurrentParent } = useTreeUtils(doc);
4139
const { mutate: detachDoc } = useDetachDoc();
4240
const treeContext = useTreeContext<Doc>();
@@ -70,7 +68,7 @@ export const DocTreeItemActions = ({
7068
? [
7169
{
7270
label: t('Convert to doc'),
73-
isDisabled: !canUpdate,
71+
isDisabled: !doc.abilities.move,
7472
icon: (
7573
<Box
7674
$css={css`
@@ -86,7 +84,7 @@ export const DocTreeItemActions = ({
8684
: []),
8785
{
8886
label: t('Delete'),
89-
isDisabled: !canUpdate,
87+
isDisabled: !doc.abilities.destroy,
9088
icon: <Icon iconName="delete" $size="24px" />,
9189
callback: deleteModal.open,
9290
},
@@ -138,7 +136,7 @@ export const DocTreeItemActions = ({
138136
$variation="600"
139137
/>
140138
</DropdownMenu>
141-
{canUpdate && (
139+
{doc.abilities.children_create && (
142140
<BoxButton
143141
onClick={(e) => {
144142
e.stopPropagation();
Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TreeViewDataType } from '@gouvfr-lasuite/ui-kit';
22

3-
import { Doc, Role } from '../doc-management';
3+
import { Doc } from '../doc-management';
44

55
export const subPageToTree = (children: Doc[]): TreeViewDataType<Doc>[] => {
66
children.forEach((child) => {
@@ -9,17 +9,3 @@ export const subPageToTree = (children: Doc[]): TreeViewDataType<Doc>[] => {
99
});
1010
return children;
1111
};
12-
13-
export const isOwnerOrAdmin = (doc: Doc): boolean => {
14-
return doc.user_roles.some(
15-
(role) => role === Role.OWNER || role === Role.ADMIN,
16-
);
17-
};
18-
19-
export const canDrag = (doc: Doc): boolean => {
20-
return isOwnerOrAdmin(doc);
21-
};
22-
23-
export const canDrop = (doc: Doc): boolean => {
24-
return isOwnerOrAdmin(doc);
25-
};

src/frontend/apps/impress/src/features/docs/docs-grid/components/DocGridContentList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useTranslation } from 'react-i18next';
77

88
import { Box, Text } from '@/components';
99
import { Doc, KEY_LIST_DOC } from '@/docs/doc-management';
10-
import { useMoveDoc } from '@/docs/doc-tree/api/useMove';
10+
import { useMoveDoc } from '@/docs/doc-tree';
1111

1212
import { useDragAndDrop } from '../hooks/useDragAndDrop';
1313

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?.abilities.move;
24+
const canDrag = !!selectedDoc?.abilities.move;
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)