Skip to content

Commit e8147d0

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 8b896dd commit e8147d0

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,21 @@ export const DocTree = ({ initialTargetId }: DocTreeProps) => {
195195
>
196196
<Box $direction="row" $align="center" $width="100%">
197197
<SimpleDocItem doc={rootNode} showAccesses={true} />
198-
<div className="doc-tree-root-item-actions">
199-
<DocTreeItemActions
200-
doc={rootNode}
201-
onCreateSuccess={(createdDoc) => {
202-
const newDoc = {
203-
...createdDoc,
204-
children: [],
205-
childrenCount: 0,
206-
parentId: treeContext.root?.id ?? undefined,
207-
};
208-
treeContext?.treeData.addChild(null, newDoc);
209-
}}
210-
isOpen={rootActionsOpen}
211-
onOpenChange={setRootActionsOpen}
212-
/>
213-
</div>
198+
<DocTreeItemActions
199+
doc={rootNode}
200+
onCreateSuccess={(createdDoc) => {
201+
const newDoc = {
202+
...createdDoc,
203+
children: [],
204+
childrenCount: 0,
205+
parentId: treeContext.root?.id ?? undefined,
206+
};
207+
treeContext?.treeData.addChild(null, newDoc);
208+
}}
209+
isOpen={rootActionsOpen}
210+
isRoot={true}
211+
onOpenChange={setRootActionsOpen}
212+
/>
214213
</Box>
215214
</StyledLink>
216215
)}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
} from '@gouvfr-lasuite/ui-kit';
66
import { useModal } from '@openfun/cunningham-react';
77
import { useRouter } from 'next/router';
8-
import { Fragment } from 'react';
98
import { useTranslation } from 'react-i18next';
109
import { css } from 'styled-components';
1110

@@ -21,29 +20,28 @@ import {
2120
import { useCreateChildrenDoc } from '../api/useCreateChildren';
2221
import { useDetachDoc } from '../api/useDetach';
2322
import MoveDocIcon from '../assets/doc-extract-bold.svg';
24-
import { useTreeUtils } from '../hooks';
2523

2624
type DocTreeItemActionsProps = {
2725
doc: Doc;
28-
parentId?: string | null;
29-
onCreateSuccess?: (newDoc: Doc) => void;
3026
isOpen?: boolean;
27+
isRoot?: boolean;
28+
onCreateSuccess?: (newDoc: Doc) => void;
3129
onOpenChange?: (isOpen: boolean) => void;
30+
parentId?: string | null;
3231
};
3332

3433
export const DocTreeItemActions = ({
3534
doc,
36-
parentId,
37-
onCreateSuccess,
3835
isOpen,
36+
isRoot = false,
37+
onCreateSuccess,
3938
onOpenChange,
39+
parentId,
4040
}: DocTreeItemActionsProps) => {
4141
const router = useRouter();
4242
const { t } = useTranslation();
4343
const deleteModal = useModal();
44-
4544
const copyLink = useCopyDocLink(doc.id);
46-
const { isCurrentParent } = useTreeUtils(doc);
4745
const { mutate: detachDoc } = useDetachDoc();
4846
const treeContext = useTreeContext<Doc>();
4947
const { mutate: duplicateDoc } = useDuplicateDoc({
@@ -77,7 +75,7 @@ export const DocTreeItemActions = ({
7775
icon: <Icon iconName="link" $size="24px" />,
7876
callback: copyLink,
7977
},
80-
...(!isCurrentParent
78+
...(!isRoot
8179
? [
8280
{
8381
label: t('Move to my docs'),
@@ -135,7 +133,7 @@ export const DocTreeItemActions = ({
135133
};
136134

137135
return (
138-
<Fragment>
136+
<Box className="doc-tree-root-item-actions">
139137
<Box
140138
$direction="row"
141139
$align="center"
@@ -187,6 +185,6 @@ export const DocTreeItemActions = ({
187185
afterDelete={afterDelete}
188186
/>
189187
)}
190-
</Fragment>
188+
</Box>
191189
);
192190
};

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)