Skip to content

Commit

Permalink
Initialize collapsed/expanded sidebar state within CollapseSidebarCon…
Browse files Browse the repository at this point in the history
…text
  • Loading branch information
sulaiman-fern committed Jul 29, 2024
1 parent f8df17c commit c3687e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
32 changes: 22 additions & 10 deletions packages/ui/app/src/sidebar/CollapseSidebarContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
useState
} from "react";
import { noop } from "ts-essentials";
import { useCallbackOne } from "use-memo-one";
Expand Down Expand Up @@ -84,11 +83,10 @@ export const CollapseSidebarProvider: FC<
// }),
// [registerListener, scrollContainerRef],
// );

const { parentIdMap, parentToChildrenMap } = useMemo(() => {
const parentIdMap = new Map<FernNavigation.NodeId, FernNavigation.NodeId[]>();
const parentToChildrenMap = new Map<FernNavigation.NodeId, FernNavigation.NodeId[]>();

if (sidebar == null) {
return { parentIdMap, parentToChildrenMap };
}
Expand All @@ -113,14 +111,28 @@ export const CollapseSidebarProvider: FC<
return { parentIdMap, parentToChildrenMap };
}, [sidebar]);

const [expanded, setExpanded] = useState<FernNavigation.NodeId[]>(() =>
selectedNodeId == null ? [] : [selectedNodeId, ...(parentIdMap.get(selectedNodeId) ?? [])],
const initializeExpandedSections = (): FernNavigation.NodeId[] => {
if(selectedNodeId == null) {
return []

Check failure on line 116 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
} else {
if(sidebar) {
const selectedNodes: FernNavigation.NodeId[] = []

Check failure on line 119 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
FernNavigation.utils.traverseNavigation(sidebar, (node, _index, parents) => {

Check failure on line 120 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

'parents' is defined but never used. Allowed unused args must match /^_/u
if(FernNavigation.isSection(node) && node.type === "section") {
if(!node?.collapsed) {
selectedNodes.push(...[node.id, ...(parentIdMap.get(node.id) ?? [])])

Check failure on line 123 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
}
}
})

Check failure on line 126 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
return selectedNodes

Check failure on line 127 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
} else {
return [selectedNodeId, ...(parentIdMap.get(selectedNodeId) ?? [])]

Check failure on line 129 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
}}
}

Check failure on line 131 in packages/ui/app/src/sidebar/CollapseSidebarContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

const [expanded, setExpanded] = useState<FernNavigation.NodeId[]>(() => initializeExpandedSections()
);

useEffect(() => {
setExpanded(selectedNodeId == null ? [] : [selectedNodeId, ...(parentIdMap.get(selectedNodeId) ?? [])]);
}, [selectedNodeId, parentIdMap]);

const checkExpanded = useCallback(
(expandableId: FernNavigation.NodeId) =>
expanded.some((id) => id === expandableId || parentIdMap.get(id)?.includes(expandableId)),
Expand Down
13 changes: 3 additions & 10 deletions packages/ui/app/src/sidebar/nodes/SidebarSectionNode.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FernNavigation } from "@fern-api/fdr-sdk";
import clsx from "clsx";
import { useCallback, useEffect, useState } from "react";
import { useCallback } from "react";
import { useCurrentNodeId } from "../../atoms";
import { useCollapseSidebar } from "../CollapseSidebarContext";
import { SidebarSlugLink } from "../SidebarLink";
Expand All @@ -14,12 +14,8 @@ interface SidebarSectionNodeProps {
}

export function SidebarSectionNode({ node, className, depth }: SidebarSectionNodeProps): React.ReactElement | null {
const [expanded, setExpanded] = useState<boolean>(false)
const { checkExpanded, toggleExpanded, checkChildSelected, registerScrolledToPathListener } = useCollapseSidebar();
const handleToggleExpand = useCallback(() => {
toggleExpanded(node.id)
setExpanded(!expanded)
}, [node.id, toggleExpanded, expanded]);
const handleToggleExpand = useCallback(() => toggleExpanded(node.id), [node.id, toggleExpanded]);
const selectedNodeId = useCurrentNodeId();

if (node.children.length === 0) {
Expand All @@ -45,10 +41,7 @@ export function SidebarSectionNode({ node, className, depth }: SidebarSectionNod
return null;
}

useEffect(() => {
setExpanded(!!node.id || checkExpanded(node.id) || (childSelected && node.overviewPageId != null) || !node?.collapsed)
}, [])

const expanded = selectedNodeId === node.id || checkExpanded(node.id) || (childSelected && node.overviewPageId != null)

Check failure on line 44 in packages/ui/app/src/sidebar/nodes/SidebarSectionNode.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
const showIndicator = childSelected && !expanded;

return (
Expand Down

0 comments on commit c3687e0

Please sign in to comment.