Skip to content

Commit c55c4fa

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 0128b14 commit c55c4fa

File tree

4 files changed

+29
-40
lines changed

4 files changed

+29
-40
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: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,42 @@ 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

1211
import { Box, BoxButton, Icon } from '@/components';
13-
1412
import {
1513
Doc,
1614
ModalRemoveDoc,
1715
Role,
1816
useCopyDocLink,
19-
} from '../../doc-management';
17+
} from '@/docs/doc-management';
18+
2019
import { useCreateChildrenDoc } from '../api/useCreateChildren';
2120
import { useDetachDoc } from '../api/useDetach';
2221
import MoveDocIcon from '../assets/doc-extract-bold.svg';
23-
import { useTreeUtils } from '../hooks';
2422

2523
type DocTreeItemActionsProps = {
2624
doc: Doc;
27-
parentId?: string | null;
28-
onCreateSuccess?: (newDoc: Doc) => void;
2925
isOpen?: boolean;
26+
isRoot?: boolean;
27+
onCreateSuccess?: (newDoc: Doc) => void;
3028
onOpenChange?: (isOpen: boolean) => void;
29+
parentId?: string | null;
3130
};
3231

3332
export const DocTreeItemActions = ({
3433
doc,
35-
parentId,
36-
onCreateSuccess,
3734
isOpen,
35+
isRoot = false,
36+
onCreateSuccess,
3837
onOpenChange,
38+
parentId,
3939
}: DocTreeItemActionsProps) => {
4040
const router = useRouter();
4141
const { t } = useTranslation();
4242
const deleteModal = useModal();
43-
4443
const copyLink = useCopyDocLink(doc.id);
45-
const { isCurrentParent } = useTreeUtils(doc);
4644
const { mutate: detachDoc } = useDetachDoc();
4745
const treeContext = useTreeContext<Doc>();
4846

@@ -71,7 +69,7 @@ export const DocTreeItemActions = ({
7169
icon: <Icon iconName="link" $size="24px" />,
7270
callback: copyLink,
7371
},
74-
...(!isCurrentParent
72+
...(!isRoot
7573
? [
7674
{
7775
label: t('Move to my docs'),
@@ -117,7 +115,7 @@ export const DocTreeItemActions = ({
117115
};
118116

119117
return (
120-
<Fragment>
118+
<Box className="doc-tree-root-item-actions">
121119
<Box
122120
$direction="row"
123121
$align="center"
@@ -169,6 +167,6 @@ export const DocTreeItemActions = ({
169167
afterDelete={afterDelete}
170168
/>
171169
)}
172-
</Fragment>
170+
</Box>
173171
);
174172
};

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';
@@ -144,9 +143,7 @@ export const DocsGridItem = ({ doc, dragMode = false }: DocsGridItemProps) => {
144143
</Box>
145144
</Box>
146145
{shareModal.isOpen && (
147-
<TreeProvider initialNodeId={doc.id}>
148-
<DocShareModal doc={doc} onClose={shareModal.close} />
149-
</TreeProvider>
146+
<DocShareModal doc={doc} onClose={shareModal.close} />
150147
)}
151148
</>
152149
);

0 commit comments

Comments
 (0)