diff --git a/app/components/Sidebar/components/DocumentLink.tsx b/app/components/Sidebar/components/DocumentLink.tsx index ae2372ec7b..b801a4ed81 100644 --- a/app/components/Sidebar/components/DocumentLink.tsx +++ b/app/components/Sidebar/components/DocumentLink.tsx @@ -40,6 +40,10 @@ import type UserMembership from "~/models/UserMembership"; import type GroupMembership from "~/models/GroupMembership"; import { ActionContextProvider } from "~/hooks/useActionContext"; import { useDocumentMenuAction } from "~/hooks/useDocumentMenuAction"; +import SidebarDisclosureContext, { + useSidebarDisclosure, + useSidebarDisclosureState, +} from "./SidebarDisclosureContext"; type Props = { node: NavigationNode; @@ -119,6 +123,13 @@ function InnerDocumentLink( const [expanded, setExpanded, setCollapsed] = useBoolean(showChildren); + // Context-based recursive expand/collapse for descendant DocumentLinks + const { event: disclosureEvent, onDisclosureClick } = + useSidebarDisclosureState(); + + // Subscribe to recursive expand/collapse events from an ancestor + useSidebarDisclosure(setExpanded, setCollapsed); + React.useEffect(() => { if (showChildren) { setExpanded(); @@ -132,13 +143,18 @@ function InnerDocumentLink( } }, [setCollapsed, expanded, hasChildDocuments]); - const handleDisclosureClick = React.useCallback(() => { - if (expanded) { - setCollapsed(); - } else { - setExpanded(); - } - }, [setCollapsed, setExpanded, expanded]); + const handleDisclosureClick = React.useCallback( + (ev: React.MouseEvent) => { + const willExpand = !expanded; + if (willExpand) { + setExpanded(); + } else { + setCollapsed(); + } + onDisclosureClick(willExpand, ev.altKey); + }, + [setCollapsed, setExpanded, expanded, onDisclosureClick] + ); const handlePrefetch = React.useCallback(() => { void prefetchDocument?.(node.id); @@ -467,22 +483,24 @@ function InnerDocumentLink( } /> )} - - {nodeChildren.map((childNode, childIndex) => ( - - ))} - + + + {nodeChildren.map((childNode, childIndex) => ( + + ))} + + ); } diff --git a/app/components/Sidebar/components/DraggableCollectionLink.tsx b/app/components/Sidebar/components/DraggableCollectionLink.tsx index b664d15f3e..9b47831944 100644 --- a/app/components/Sidebar/components/DraggableCollectionLink.tsx +++ b/app/components/Sidebar/components/DraggableCollectionLink.tsx @@ -13,6 +13,9 @@ import useStores from "~/hooks/useStores"; import type { DragObject } from "../hooks/useDragAndDrop"; import CollectionLink from "./CollectionLink"; import DropCursor from "./DropCursor"; +import SidebarDisclosureContext, { + useSidebarDisclosureState, +} from "./SidebarDisclosureContext"; import Relative from "./Relative"; import { useSidebarContext } from "./SidebarContext"; @@ -36,6 +39,10 @@ function DraggableCollectionLink({ ); const belowCollectionIndex = belowCollection ? belowCollection.index : null; + // Context-based recursive expand/collapse for descendant DocumentLinks + const { event: disclosureEvent, onDisclosureClick } = + useSidebarDisclosureState(); + // Drop to reorder collection const [ { isCollectionDropping, isDraggingAnyCollection }, @@ -91,15 +98,22 @@ function DraggableCollectionLink({ locationSidebarContext, ]); - const handleDisclosureClick = useCallback((ev) => { - ev?.preventDefault(); - setExpanded((e) => !e); - }, []); + const handleDisclosureClick = useCallback( + (ev) => { + ev?.preventDefault(); + setExpanded((e) => { + const willExpand = !e; + onDisclosureClick(willExpand, !!ev?.altKey); + return willExpand; + }); + }, + [onDisclosureClick] + ); const displayChildDocuments = expanded && !isDragging; return ( - <> + )} - + ); } diff --git a/app/components/Sidebar/components/GroupLink.tsx b/app/components/Sidebar/components/GroupLink.tsx index faa2a73a12..35cf4db061 100644 --- a/app/components/Sidebar/components/GroupLink.tsx +++ b/app/components/Sidebar/components/GroupLink.tsx @@ -7,6 +7,9 @@ import Folder from "./Folder"; import Relative from "./Relative"; import SharedWithMeLink from "./SharedWithMeLink"; import SidebarContext, { groupSidebarContext } from "./SidebarContext"; +import SidebarDisclosureContext, { + useSidebarDisclosureState, +} from "./SidebarDisclosureContext"; import SidebarLink from "./SidebarLink"; type Props = { @@ -21,10 +24,20 @@ const GroupLink: React.FC = ({ group }) => { locationSidebarContext === sidebarContext ); - const handleDisclosureClick = React.useCallback((ev) => { - ev?.preventDefault(); - setExpanded((e) => !e); - }, []); + const { event: disclosureEvent, onDisclosureClick } = + useSidebarDisclosureState(); + + const handleDisclosureClick = React.useCallback( + (ev) => { + ev?.preventDefault(); + setExpanded((e) => { + const willExpand = !e; + onDisclosureClick(willExpand, !!ev?.altKey); + return willExpand; + }); + }, + [onDisclosureClick] + ); React.useEffect(() => { if (locationSidebarContext === sidebarContext) { @@ -42,15 +55,17 @@ const GroupLink: React.FC = ({ group }) => { depth={0} /> - - {group.documentMemberships.map((membership) => ( - - ))} - + + + {group.documentMemberships.map((membership) => ( + + ))} + + ); diff --git a/app/components/Sidebar/components/SharedDocumentLink.tsx b/app/components/Sidebar/components/SharedDocumentLink.tsx index 2d451b7b7a..1cf7948c90 100644 --- a/app/components/Sidebar/components/SharedDocumentLink.tsx +++ b/app/components/Sidebar/components/SharedDocumentLink.tsx @@ -9,6 +9,10 @@ import type Document from "~/models/Document"; import useStores from "~/hooks/useStores"; import { sharedModelPath } from "~/utils/routeHelpers"; import { descendants } from "@shared/utils/tree"; +import SidebarDisclosureContext, { + useSidebarDisclosure, + useSidebarDisclosureState, +} from "./SidebarDisclosureContext"; import SidebarLink from "./SidebarLink"; type Props = { @@ -62,6 +66,14 @@ function DocumentLink( const [expanded, setExpanded] = React.useState(showChildren); + const { event: disclosureEvent, onDisclosureClick } = + useSidebarDisclosureState(); + + const handleExpand = React.useCallback(() => setExpanded(true), []); + const handleCollapse = React.useCallback(() => setExpanded(false), []); + + useSidebarDisclosure(handleExpand, handleCollapse); + React.useEffect(() => { if (showChildren) { setExpanded(showChildren); @@ -72,9 +84,12 @@ function DocumentLink( (ev: React.SyntheticEvent) => { ev.preventDefault(); ev.stopPropagation(); - setExpanded(!expanded); + const willExpand = !expanded; + setExpanded(willExpand); + const altKey = "altKey" in ev && (ev as React.MouseEvent).altKey; + onDisclosureClick(willExpand, !!altKey); }, - [expanded] + [expanded, onDisclosureClick] ); // since we don't have access to the collection sort here, we just put any @@ -133,22 +148,24 @@ function DocumentLink( ref={ref} isActive={() => !!isActiveDocument} /> - {expanded && - nodeChildren.map((childNode, index) => ( - - ))} + + {expanded && + nodeChildren.map((childNode, index) => ( + + ))} + ); } diff --git a/app/components/Sidebar/components/SharedWithMeLink.tsx b/app/components/Sidebar/components/SharedWithMeLink.tsx index e38c210034..63228f6c70 100644 --- a/app/components/Sidebar/components/SharedWithMeLink.tsx +++ b/app/components/Sidebar/components/SharedWithMeLink.tsx @@ -22,6 +22,10 @@ import DocumentLink from "./DocumentLink"; import DropCursor from "./DropCursor"; import Folder from "./Folder"; import Relative from "./Relative"; +import SidebarDisclosureContext, { + useSidebarDisclosure, + useSidebarDisclosureState, +} from "./SidebarDisclosureContext"; import { useSidebarContext, type SidebarContextType } from "./SidebarContext"; import SidebarLink from "./SidebarLink"; @@ -48,6 +52,12 @@ function SharedWithMeLink({ membership, depth = 0 }: Props) { isActiveDocumentInPath && locationSidebarContext === sidebarContext ); + const { event: disclosureEvent, onDisclosureClick } = + useSidebarDisclosureState(); + + // Subscribe to recursive expand/collapse events from an ancestor (e.g. GroupLink) + useSidebarDisclosure(setExpanded, setCollapsed); + React.useEffect(() => { if (isActiveDocumentInPath && locationSidebarContext === sidebarContext) { setExpanded(); @@ -76,13 +86,15 @@ function SharedWithMeLink({ membership, depth = 0 }: Props) { (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); - if (expanded) { - setCollapsed(); - } else { + const willExpand = !expanded; + if (willExpand) { setExpanded(); + } else { + setCollapsed(); } + onDisclosureClick(willExpand, ev.altKey); }, - [expanded, setExpanded, setCollapsed] + [expanded, setExpanded, setCollapsed, onDisclosureClick] ); const parentRef = React.useRef(null); @@ -174,20 +186,22 @@ function SharedWithMeLink({ membership, depth = 0 }: Props) { - - {childDocuments.map((childNode, index) => ( - - ))} - + + + {childDocuments.map((childNode, index) => ( + + ))} + + {reorderProps.isDragging && ( ( + null +); + +/** + * Hook that subscribes to recursive expand/collapse events from an ancestor + * provider. When a new event is detected, the appropriate callback is invoked. + * + * Newly mounted components will also react to the current event, which enables + * cascading: expanding a parent reveals children, which mount and see the + * expand event, then expand themselves to reveal grandchildren, and so on. + * + * @param onExpand - called when a recursive expand event is received. + * @param onCollapse - called when a recursive collapse event is received. + */ +export function useSidebarDisclosure( + onExpand: () => void, + onCollapse: () => void +): void { + const event = useContext(SidebarDisclosureContext); + const lastHandledGeneration = useRef(-1); + + useEffect(() => { + if (!event || event.generation === lastHandledGeneration.current) { + return; + } + lastHandledGeneration.current = event.generation; + + if (event.action === "expand") { + onExpand(); + } else { + onCollapse(); + } + }, [event, onExpand, onCollapse]); +} + +/** + * Hook for the producing side of the disclosure context. Returns the current + * event value (to pass to a Provider) and a single callback to handle + * alt-click expand/collapse broadcasts. + * + * This hook also reads the parent context and automatically forwards any + * incoming disclosure events so that the cascade propagates through the + * entire tree — even when intermediate nodes each create their own provider. + * + * @returns object with `event` to spread onto the Provider's value and + * `onDisclosureClick` to call from disclosure click handlers. + */ +export function useSidebarDisclosureState() { + const parentEvent = useContext(SidebarDisclosureContext); + const [event, setEvent] = useState(null); + const lastForwardedParentGeneration = useRef(-1); + + // Forward parent disclosure events into our own provider value so that + // grandchildren (and beyond) see the event even though each level creates + // its own independent provider. + useEffect(() => { + if ( + !parentEvent || + parentEvent.generation === lastForwardedParentGeneration.current + ) { + return; + } + lastForwardedParentGeneration.current = parentEvent.generation; + setEvent((prev) => ({ + action: parentEvent.action, + generation: (prev?.generation ?? 0) + 1, + })); + }, [parentEvent]); + + /** + * Call from a disclosure click handler after toggling expand/collapse state. + * When alt is held, broadcasts a recursive expand or collapse event to all + * descendants. Otherwise, clears any stale event. + * + * @param willExpand - whether the node is expanding or collapsing. + * @param altKey - whether the alt/option key was held during the click. + */ + const onDisclosureClick = useCallback( + (willExpand: boolean, altKey: boolean) => { + if (altKey) { + setEvent((prev) => ({ + action: willExpand ? "expand" : "collapse", + generation: (prev?.generation ?? 0) + 1, + })); + } else { + setEvent(null); + } + }, + [] + ); + + return { event, onDisclosureClick }; +} + +export default SidebarDisclosureContext; diff --git a/app/components/Sidebar/components/StarredLink.tsx b/app/components/Sidebar/components/StarredLink.tsx index 00081a3ed5..62655e1e3a 100644 --- a/app/components/Sidebar/components/StarredLink.tsx +++ b/app/components/Sidebar/components/StarredLink.tsx @@ -19,6 +19,9 @@ import { import { useSidebarLabelAndIcon } from "../hooks/useSidebarLabelAndIcon"; import CollectionLink from "./CollectionLink"; import DocumentLink from "./DocumentLink"; +import SidebarDisclosureContext, { + useSidebarDisclosureState, +} from "./SidebarDisclosureContext"; import DropCursor from "./DropCursor"; import Folder from "./Folder"; import Relative from "./Relative"; @@ -204,6 +207,9 @@ function StarredLink({ star }: Props) { sidebarContext === locationSidebarContext ); + const { event: disclosureEvent, onDisclosureClick } = + useSidebarDisclosureState(); + React.useEffect(() => { if ( star.documentId === ui.activeDocumentId && @@ -235,9 +241,13 @@ function StarredLink({ star }: Props) { (ev?: React.MouseEvent) => { ev?.preventDefault(); ev?.stopPropagation(); - setExpanded((prevExpanded) => !prevExpanded); + setExpanded((prevExpanded) => { + const willExpand = !prevExpanded; + onDisclosureClick(willExpand, !!ev?.altKey); + return willExpand; + }); }, - [] + [onDisclosureClick] ); const handlePrefetch = React.useCallback(() => { @@ -284,39 +294,43 @@ function StarredLink({ star }: Props) { if (documentId) { return ( - + + + ); } if (collection) { return ( - + + + ); }