Skip to content

Commit 7b1e87e

Browse files
wip
1 parent 6dd44f9 commit 7b1e87e

File tree

8 files changed

+103
-20
lines changed

8 files changed

+103
-20
lines changed

src/frontend/apps/impress/src/features/docs/doc-header/components/DocHeader.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Doc,
99
LinkReach,
1010
currentDocRole,
11+
getDocLinkReach,
1112
useTrans,
1213
} from '@/docs/doc-management';
1314
import { useResponsiveStore } from '@/stores';
@@ -24,8 +25,8 @@ export const DocHeader = ({ doc }: DocHeaderProps) => {
2425
const { isDesktop } = useResponsiveStore();
2526

2627
const { t } = useTranslation();
27-
const docIsPublic = doc.link_reach === LinkReach.PUBLIC;
28-
const docIsAuth = doc.link_reach === LinkReach.AUTHENTICATED;
28+
const docIsPublic = getDocLinkReach(doc) === LinkReach.PUBLIC;
29+
const docIsAuth = getDocLinkReach(doc) === LinkReach.AUTHENTICATED;
2930

3031
const { transRole } = useTrans();
3132

src/frontend/apps/impress/src/features/docs/doc-header/components/DocToolBox.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const DocToolBoxLicence = dynamic(() =>
2323

2424
export const DocToolBox = ({ doc }: DocToolBoxProps) => {
2525
const { t } = useTranslation();
26-
const hasAccesses = doc.nb_accesses_direct > 1 && doc.abilities.accesses_view;
26+
const hasAccesses =
27+
doc.nb_accesses_direct >= 1 && doc.abilities.accesses_view;
2728
const queryClient = useQueryClient();
2829

2930
const { spacingsTokens } = useCunninghamTheme();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export enum Role {
2727
OWNER = 'owner',
2828
}
2929

30+
export const RoleImportance = {
31+
[Role.READER]: 1,
32+
[Role.EDITOR]: 2,
33+
[Role.ADMIN]: 3,
34+
[Role.OWNER]: 4,
35+
};
36+
3037
export enum LinkReach {
3138
RESTRICTED = 'restricted',
3239
AUTHENTICATED = 'authenticated',

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+
getDocLinkReach(doc) !== doc.ancestors_link_reach ||
48+
getDocLinkRole(doc) !== doc.ancestors_link_role
49+
);
50+
};

src/frontend/apps/impress/src/features/docs/doc-share/components/DocInheritedShareContent.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { createGlobalStyle } from 'styled-components';
66
import { Box, Text } from '@/components';
77
import { useCunninghamTheme } from '@/cunningham';
88

9-
import { Access, useDoc, useDocStore } from '../../doc-management';
9+
import {
10+
Access,
11+
RoleImportance,
12+
useDoc,
13+
useDocStore,
14+
} from '../../doc-management';
1015
import SimpleFileIcon from '../../docs-grid/assets/simple-document.svg';
1116

1217
import { DocShareMemberItem } from './DocShareMemberItem';
@@ -23,6 +28,28 @@ type Props = {
2328
rawAccesses: Access[];
2429
};
2530

31+
const getAncestorsRole = (access: Access) => {
32+
if (!access.max_ancestors_role) {
33+
return access.role;
34+
}
35+
const currentRoleImportance = RoleImportance[access.role];
36+
const maxAncestorsRoleImportance = RoleImportance[access.max_ancestors_role];
37+
38+
return currentRoleImportance > maxAncestorsRoleImportance
39+
? access.role
40+
: access.max_ancestors_role;
41+
};
42+
43+
const getMaxRoleBetweenAccesses = (access1: Access, access2: Access) => {
44+
const role1 = getAncestorsRole(access1);
45+
const role2 = getAncestorsRole(access2);
46+
47+
const roleImportance1 = RoleImportance[role1];
48+
const roleImportance2 = RoleImportance[role2];
49+
50+
return roleImportance1 > roleImportance2 ? role1 : role2;
51+
};
52+
2653
export const DocInheritedShareContent = ({ rawAccesses }: Props) => {
2754
const { t } = useTranslation();
2855
const { spacingsTokens } = useCunninghamTheme();
@@ -46,8 +73,19 @@ export const DocInheritedShareContent = ({ rawAccesses }: Props) => {
4673
continue;
4774
}
4875

49-
if (!members.some((member) => member.user.id === access.user.id)) {
76+
const findIndex = members.findIndex(
77+
(member) => member.user.id === access.user.id,
78+
);
79+
if (findIndex === -1) {
5080
members.push(access);
81+
} else {
82+
const accessToUpdate = members[findIndex];
83+
const currentRole = getAncestorsRole(accessToUpdate);
84+
const maxRole = getMaxRoleBetweenAccesses(accessToUpdate, access);
85+
86+
if (maxRole !== currentRole) {
87+
members[findIndex] = access;
88+
}
5189
}
5290

5391
// Check if this document has a longer path than our current candidate

src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareMemberItem.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { VariantType, useToastProvider } from '@openfun/cunningham-react';
22
import { useQueryClient } from '@tanstack/react-query';
3+
import { useMemo } from 'react';
34
import { useTranslation } from 'react-i18next';
45

56
import {
@@ -9,7 +10,13 @@ import {
910
IconOptions,
1011
} from '@/components';
1112
import { useCunninghamTheme } from '@/cunningham';
12-
import { Access, Doc, KEY_SUB_PAGE, Role } from '@/docs/doc-management/';
13+
import {
14+
Access,
15+
Doc,
16+
KEY_SUB_PAGE,
17+
Role,
18+
RoleImportance,
19+
} from '@/docs/doc-management/';
1320
import { useResponsiveStore } from '@/stores';
1421

1522
import { useDeleteDocAccess, useUpdateDocAccess } from '../api';
@@ -90,6 +97,17 @@ export const DocShareMemberItem = ({
9097
];
9198

9299
const canUpdate = isInherited ? false : doc.abilities.accesses_manage;
100+
const currentRole = useMemo(() => {
101+
if (!access.max_ancestors_role) {
102+
return access.role;
103+
}
104+
const currentRoleImportance = RoleImportance[access.role];
105+
const maxAncestorsRoleImportance =
106+
RoleImportance[access.max_ancestors_role];
107+
return currentRoleImportance > maxAncestorsRoleImportance
108+
? access.role
109+
: access.max_ancestors_role;
110+
}, [access]);
93111

94112
return (
95113
<Box
@@ -103,7 +121,7 @@ export const DocShareMemberItem = ({
103121
right={
104122
<Box $direction="row" $align="center" $gap={spacingsTokens['2xs']}>
105123
<DocRoleDropdown
106-
currentRole={access.role}
124+
currentRole={currentRole}
107125
onSelectRole={onUpdate}
108126
canUpdate={canUpdate}
109127
message={message}

src/frontend/apps/impress/src/features/docs/doc-share/components/DocVisibility.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
KEY_LIST_DOC,
2222
LinkReach,
2323
LinkRole,
24+
docLinkIsDesync,
25+
getDocLinkReach,
2426
useUpdateDocLink,
2527
} from '@/features/docs';
2628
import { useResponsiveStore } from '@/stores';
@@ -40,9 +42,7 @@ export const DocVisibility = ({ doc }: DocVisibilityProps) => {
4042
const { isDesktop } = useResponsiveStore();
4143
const { spacingsTokens, colorsTokens } = useCunninghamTheme();
4244
const canManage = doc.abilities.accesses_manage;
43-
const [linkReach, setLinkReach] = useState<LinkReach>(
44-
doc.computed_link_reach,
45-
);
45+
const [linkReach, setLinkReach] = useState<LinkReach>(getDocLinkReach(doc));
4646
const [docLinkRole, setDocLinkRole] = useState<LinkRole>(
4747
doc.computed_link_role ?? LinkRole.READER,
4848
);
@@ -154,15 +154,7 @@ export const DocVisibility = ({ doc }: DocVisibilityProps) => {
154154
};
155155

156156
const showDesync = useMemo(() => {
157-
// If the document has no ancestors, we don't show the desync
158-
if (!doc.ancestors_link_reach) {
159-
return false;
160-
}
161-
162-
return (
163-
doc.computed_link_reach !== doc.ancestors_link_reach ||
164-
doc.computed_link_role !== doc.ancestors_link_role
165-
);
157+
return docLinkIsDesync(doc);
166158
}, [doc]);
167159

168160
return (

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const DocSubPageItem = (props: Props) => {
138138
<Text $css={ItemTextCss} $size="sm" $variation="1000">
139139
{doc.title || untitledDocument}
140140
</Text>
141-
{doc.nb_accesses_direct > 1 && (
141+
{doc.nb_accesses_direct >= 1 && (
142142
<Icon
143143
variant="filled"
144144
iconName="group"

0 commit comments

Comments
 (0)