Keep nested docs in shared sidebar collapsed by default (#5208)
Co-authored-by: Tom Moor <tom@getoutline.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import includes from "lodash/includes";
|
||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
@@ -6,14 +7,15 @@ import Collection from "~/models/Collection";
|
|||||||
import Document from "~/models/Document";
|
import Document from "~/models/Document";
|
||||||
import useStores from "~/hooks/useStores";
|
import useStores from "~/hooks/useStores";
|
||||||
import { sharedDocumentPath } from "~/utils/routeHelpers";
|
import { sharedDocumentPath } from "~/utils/routeHelpers";
|
||||||
|
import { descendants } from "~/utils/tree";
|
||||||
import Disclosure from "./Disclosure";
|
import Disclosure from "./Disclosure";
|
||||||
import SidebarLink from "./SidebarLink";
|
import SidebarLink from "./SidebarLink";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
node: NavigationNode;
|
node: NavigationNode;
|
||||||
collection?: Collection;
|
collection?: Collection;
|
||||||
activeDocumentId: string | undefined;
|
activeDocumentId?: string;
|
||||||
activeDocument: Document | undefined;
|
activeDocument?: Document;
|
||||||
isDraft?: boolean;
|
isDraft?: boolean;
|
||||||
depth: number;
|
depth: number;
|
||||||
index: number;
|
index: number;
|
||||||
@@ -41,10 +43,19 @@ function DocumentLink(
|
|||||||
const hasChildDocuments =
|
const hasChildDocuments =
|
||||||
!!node.children.length || activeDocument?.parentDocumentId === node.id;
|
!!node.children.length || activeDocument?.parentDocumentId === node.id;
|
||||||
const document = documents.get(node.id);
|
const document = documents.get(node.id);
|
||||||
|
|
||||||
const showChildren = React.useMemo(
|
const showChildren = React.useMemo(
|
||||||
() => !!hasChildDocuments,
|
() =>
|
||||||
[hasChildDocuments]
|
!!(
|
||||||
|
hasChildDocuments &&
|
||||||
|
((activeDocumentId &&
|
||||||
|
includes(
|
||||||
|
descendants(node).map((n) => n.id),
|
||||||
|
activeDocumentId
|
||||||
|
)) ||
|
||||||
|
isActiveDocument ||
|
||||||
|
depth <= 1)
|
||||||
|
),
|
||||||
|
[hasChildDocuments, activeDocumentId, isActiveDocument, depth, node]
|
||||||
);
|
);
|
||||||
|
|
||||||
const [expanded, setExpanded] = React.useState(showChildren);
|
const [expanded, setExpanded] = React.useState(showChildren);
|
||||||
@@ -55,12 +66,6 @@ function DocumentLink(
|
|||||||
}
|
}
|
||||||
}, [showChildren]);
|
}, [showChildren]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (isActiveDocument) {
|
|
||||||
setExpanded(true);
|
|
||||||
}
|
|
||||||
}, [isActiveDocument]);
|
|
||||||
|
|
||||||
const handleDisclosureClick = React.useCallback(
|
const handleDisclosureClick = React.useCallback(
|
||||||
(ev: React.SyntheticEvent) => {
|
(ev: React.SyntheticEvent) => {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
|
|||||||
Reference in New Issue
Block a user