Skip to content

Commit 3ff6d25

Browse files
committed
♻️(frontend) use more reliable properties in useTreeUtils
Using the treeContext was causing issues with the current parent detection, in many places the context is not available. "depth" property is more reliable than "nb_accesses_ancestors".
1 parent 34ce276 commit 3ff6d25

File tree

4 files changed

+10
-18
lines changed

4 files changed

+10
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export const DocTree = ({ currentDoc }: DocTreeProps) => {
223223
treeContext?.treeData.addChild(null, newDoc);
224224
}}
225225
isOpen={rootActionsOpen}
226+
isRoot={true}
226227
onOpenChange={setRootActionsOpen}
227228
/>
228229
</Box>

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,28 @@ import {
2020
import { useCreateChildrenDoc } from '../api/useCreateChildren';
2121
import { useDetachDoc } from '../api/useDetach';
2222
import MoveDocIcon from '../assets/doc-extract-bold.svg';
23-
import { useTreeUtils } from '../hooks';
2423

2524
type DocTreeItemActionsProps = {
2625
doc: Doc;
2726
isOpen?: boolean;
28-
parentId?: string | null;
27+
isRoot?: boolean;
2928
onCreateSuccess?: (newDoc: Doc) => void;
3029
onOpenChange?: (isOpen: boolean) => void;
30+
parentId?: string | null;
3131
};
3232

3333
export const DocTreeItemActions = ({
3434
doc,
35-
parentId,
36-
onCreateSuccess,
3735
isOpen,
36+
isRoot = false,
37+
onCreateSuccess,
3838
onOpenChange,
39+
parentId,
3940
}: DocTreeItemActionsProps) => {
4041
const router = useRouter();
4142
const { t } = useTranslation();
4243
const deleteModal = useModal();
43-
4444
const copyLink = useCopyDocLink(doc.id);
45-
const { isCurrentParent } = useTreeUtils(doc);
4645
const { mutate: detachDoc } = useDetachDoc();
4746
const treeContext = useTreeContext<Doc | null>();
4847
const { mutate: duplicateDoc } = useDuplicateDoc({
@@ -81,7 +80,7 @@ export const DocTreeItemActions = ({
8180
icon: <Icon iconName="link" $size="24px" />,
8281
callback: copyLink,
8382
},
84-
...(!isCurrentParent
83+
...(!isRoot
8584
? [
8685
{
8786
label: t('Move to my docs'),

src/frontend/apps/impress/src/features/docs/doc-tree/hooks/useTreeUtils.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import { useTreeContext } from '@gouvfr-lasuite/ui-kit';
2-
31
import { Doc } from '@/docs/doc-management';
42

53
export const useTreeUtils = (doc: Doc) => {
6-
const treeContext = useTreeContext<Doc>();
7-
84
return {
9-
isParent: doc.nb_accesses_ancestors <= 1, // it is a parent
10-
isChild: doc.nb_accesses_ancestors > 1, // it is a child
11-
isCurrentParent: treeContext?.root?.id === doc.id || doc.depth === 1, // it can be a child but not for the current user
5+
isTopRoot: doc.depth === 1,
6+
isChild: doc.depth > 1,
127
isDesynchronized: !!(
138
doc.ancestors_link_reach &&
149
(doc.computed_link_reach !== doc.ancestors_link_reach ||

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { TreeProvider } from '@gouvfr-lasuite/ui-kit';
21
import { Tooltip, useModal } from '@openfun/cunningham-react';
32
import { DateTime } from 'luxon';
43
import { useTranslation } from 'react-i18next';
@@ -142,9 +141,7 @@ export const DocsGridItem = ({ doc, dragMode = false }: DocsGridItemProps) => {
142141
</Box>
143142
</Box>
144143
{shareModal.isOpen && (
145-
<TreeProvider initialNodeId={doc.id}>
146-
<DocShareModal doc={doc} onClose={shareModal.close} />
147-
</TreeProvider>
144+
<DocShareModal doc={doc} onClose={shareModal.close} />
148145
)}
149146
</>
150147
);

0 commit comments

Comments
 (0)