♻️(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".
This commit is contained in:
@@ -223,6 +223,7 @@ export const DocTree = ({ currentDoc }: DocTreeProps) => {
|
|||||||
treeContext?.treeData.addChild(null, newDoc);
|
treeContext?.treeData.addChild(null, newDoc);
|
||||||
}}
|
}}
|
||||||
isOpen={rootActionsOpen}
|
isOpen={rootActionsOpen}
|
||||||
|
isRoot={true}
|
||||||
onOpenChange={setRootActionsOpen}
|
onOpenChange={setRootActionsOpen}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@@ -20,29 +20,28 @@ import {
|
|||||||
import { useCreateChildrenDoc } from '../api/useCreateChildren';
|
import { useCreateChildrenDoc } from '../api/useCreateChildren';
|
||||||
import { useDetachDoc } from '../api/useDetach';
|
import { useDetachDoc } from '../api/useDetach';
|
||||||
import MoveDocIcon from '../assets/doc-extract-bold.svg';
|
import MoveDocIcon from '../assets/doc-extract-bold.svg';
|
||||||
import { useTreeUtils } from '../hooks';
|
|
||||||
|
|
||||||
type DocTreeItemActionsProps = {
|
type DocTreeItemActionsProps = {
|
||||||
doc: Doc;
|
doc: Doc;
|
||||||
isOpen?: boolean;
|
isOpen?: boolean;
|
||||||
parentId?: string | null;
|
isRoot?: boolean;
|
||||||
onCreateSuccess?: (newDoc: Doc) => void;
|
onCreateSuccess?: (newDoc: Doc) => void;
|
||||||
onOpenChange?: (isOpen: boolean) => void;
|
onOpenChange?: (isOpen: boolean) => void;
|
||||||
|
parentId?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DocTreeItemActions = ({
|
export const DocTreeItemActions = ({
|
||||||
doc,
|
doc,
|
||||||
parentId,
|
|
||||||
onCreateSuccess,
|
|
||||||
isOpen,
|
isOpen,
|
||||||
|
isRoot = false,
|
||||||
|
onCreateSuccess,
|
||||||
onOpenChange,
|
onOpenChange,
|
||||||
|
parentId,
|
||||||
}: DocTreeItemActionsProps) => {
|
}: DocTreeItemActionsProps) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const deleteModal = useModal();
|
const deleteModal = useModal();
|
||||||
|
|
||||||
const copyLink = useCopyDocLink(doc.id);
|
const copyLink = useCopyDocLink(doc.id);
|
||||||
const { isCurrentParent } = useTreeUtils(doc);
|
|
||||||
const { mutate: detachDoc } = useDetachDoc();
|
const { mutate: detachDoc } = useDetachDoc();
|
||||||
const treeContext = useTreeContext<Doc | null>();
|
const treeContext = useTreeContext<Doc | null>();
|
||||||
const { mutate: duplicateDoc } = useDuplicateDoc({
|
const { mutate: duplicateDoc } = useDuplicateDoc({
|
||||||
@@ -81,7 +80,7 @@ export const DocTreeItemActions = ({
|
|||||||
icon: <Icon iconName="link" $size="24px" />,
|
icon: <Icon iconName="link" $size="24px" />,
|
||||||
callback: copyLink,
|
callback: copyLink,
|
||||||
},
|
},
|
||||||
...(!isCurrentParent
|
...(!isRoot
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
label: t('Move to my docs'),
|
label: t('Move to my docs'),
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
import { useTreeContext } from '@gouvfr-lasuite/ui-kit';
|
|
||||||
|
|
||||||
import { Doc } from '@/docs/doc-management';
|
import { Doc } from '@/docs/doc-management';
|
||||||
|
|
||||||
export const useTreeUtils = (doc: Doc) => {
|
export const useTreeUtils = (doc: Doc) => {
|
||||||
const treeContext = useTreeContext<Doc>();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isParent: doc.nb_accesses_ancestors <= 1, // it is a parent
|
isTopRoot: doc.depth === 1,
|
||||||
isChild: doc.nb_accesses_ancestors > 1, // it is a child
|
isChild: doc.depth > 1,
|
||||||
isCurrentParent: treeContext?.root?.id === doc.id || doc.depth === 1, // it can be a child but not for the current user
|
|
||||||
isDesynchronized: !!(
|
isDesynchronized: !!(
|
||||||
doc.ancestors_link_reach &&
|
doc.ancestors_link_reach &&
|
||||||
(doc.computed_link_reach !== doc.ancestors_link_reach ||
|
(doc.computed_link_reach !== doc.ancestors_link_reach ||
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { TreeProvider } from '@gouvfr-lasuite/ui-kit';
|
|
||||||
import { Tooltip, useModal } from '@openfun/cunningham-react';
|
import { Tooltip, useModal } from '@openfun/cunningham-react';
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
@@ -142,9 +141,7 @@ export const DocsGridItem = ({ doc, dragMode = false }: DocsGridItemProps) => {
|
|||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
{shareModal.isOpen && (
|
{shareModal.isOpen && (
|
||||||
<TreeProvider initialNodeId={doc.id}>
|
<DocShareModal doc={doc} onClose={shareModal.close} />
|
||||||
<DocShareModal doc={doc} onClose={shareModal.close} />
|
|
||||||
</TreeProvider>
|
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user