(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.
This commit is contained in:
Nathan Panchout
2025-07-01 15:24:34 +02:00
parent 93def378a9
commit a0c56816f2
5 changed files with 70 additions and 7 deletions
@@ -2,9 +2,16 @@ import { User } from '@/features/auth';
export interface Access {
id: string;
max_ancestors_role: Role;
role: Role;
max_role: Role;
team: string;
user: User;
document: {
id: string;
path: string;
depth: number;
};
abilities: {
destroy: boolean;
partial_update: boolean;
@@ -21,10 +28,17 @@ export enum Role {
OWNER = 'owner',
}
export const RoleImportance = {
[Role.READER]: 1,
[Role.EDITOR]: 2,
[Role.ADMIN]: 3,
[Role.OWNER]: 4,
};
export enum LinkReach {
RESTRICTED = 'restricted',
PUBLIC = 'public',
AUTHENTICATED = 'authenticated',
PUBLIC = 'public',
}
export enum LinkRole {
@@ -43,13 +57,19 @@ export interface Doc {
created_at: string;
creator: string;
depth: number;
path: string;
is_favorite: boolean;
link_reach: LinkReach;
link_role: LinkRole;
nb_accesses_direct: number;
nb_accesses_ancestors: number;
computed_link_reach: LinkReach;
computed_link_role?: LinkRole;
ancestors_link_reach: LinkReach;
ancestors_link_role?: LinkRole;
numchild: number;
updated_at: string;
user_role: Role;
user_roles: Role[];
abilities: {
accesses_manage: boolean;
@@ -73,9 +93,16 @@ export interface Doc {
versions_destroy: boolean;
versions_list: boolean;
versions_retrieve: boolean;
link_select_options: LinkSelectOption;
};
}
export interface LinkSelectOption {
public?: LinkRole[];
authenticated?: LinkRole[];
restricted?: LinkRole[];
}
export enum DocDefaultFilter {
ALL_DOCS = 'all_docs',
MY_DOCS = 'my_docs',
@@ -22,3 +22,29 @@ export const base64ToYDoc = (base64: string) => {
export const base64ToBlocknoteXmlFragment = (base64: string) => {
return base64ToYDoc(base64).getXmlFragment('document-store');
};
export const getDocLinkReach = (doc: Doc) => {
if (doc.computed_link_reach) {
return doc.computed_link_reach;
}
return doc.link_reach;
};
export const getDocLinkRole = (doc: Doc) => {
if (doc.computed_link_role) {
return doc.computed_link_role;
}
return doc.link_role;
};
export const docLinkIsDesync = (doc: Doc) => {
// If the document has no ancestors
if (!doc.ancestors_link_reach) {
return false;
}
return (
doc.computed_link_reach !== doc.ancestors_link_reach ||
doc.computed_link_role !== doc.ancestors_link_role
);
};
@@ -15,9 +15,8 @@ export const subPageToTree = (children: Doc[]): TreeViewDataType<Doc>[] => {
};
export const isOwnerOrAdmin = (doc: Doc): boolean => {
return doc.user_roles.some(
(role) => role === Role.OWNER || role === Role.ADMIN,
);
const userRole = doc.user_role;
return userRole === Role.OWNER || userRole === Role.ADMIN;
};
export const canDrag = (doc: Doc): boolean => {
@@ -21,7 +21,7 @@ export function useDragAndDrop(
const [selectedDoc, setSelectedDoc] = useState<Doc>();
const [canDrop, setCanDrop] = useState<boolean>();
const canDrag = selectedDoc?.user_roles.some((role) => role === Role.OWNER);
const canDrag = selectedDoc?.user_role === Role.OWNER;
const mouseSensor = useSensor(MouseSensor, { activationConstraint });
const touchSensor = useSensor(TouchSensor, { activationConstraint });
@@ -1,7 +1,7 @@
import { WorkboxPlugin } from 'workbox-core';
import { Doc, DocsResponse } from '@/docs/doc-management';
import { LinkReach, LinkRole } from '@/docs/doc-management/types';
import { LinkReach, LinkRole, Role } from '@/docs/doc-management/types';
import { DBRequest, DocsDB } from '../DocsDB';
import { RequestSerializer } from '../RequestSerializer';
@@ -200,10 +200,21 @@ export class ApiPlugin implements WorkboxPlugin {
versions_destroy: true,
versions_list: true,
versions_retrieve: true,
link_select_options: {
public: [LinkRole.READER, LinkRole.EDITOR],
authenticated: [LinkRole.READER, LinkRole.EDITOR],
restricted: undefined,
},
},
link_reach: LinkReach.RESTRICTED,
link_role: LinkRole.READER,
user_roles: [],
user_roles: [Role.OWNER],
user_role: Role.OWNER,
path: '',
computed_link_reach: LinkReach.RESTRICTED,
computed_link_role: LinkRole.READER,
ancestors_link_reach: LinkReach.RESTRICTED,
ancestors_link_role: undefined,
};
await DocsDB.cacheResponse(