Skip to content

Commit

Permalink
Revert "fix: respect the collapsed property on nested sections" (#1235)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Aug 1, 2024
1 parent 6b691e7 commit 5b11a42
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions packages/ui/app/src/sidebar/CollapseSidebarContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const CollapseSidebarProvider: FC<
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 @@ -112,38 +113,12 @@ export const CollapseSidebarProvider: FC<
return { parentIdMap, parentToChildrenMap };
}, [sidebar]);

const initialExpandedSections = useCallback((): FernNavigation.NodeId[] => {
if (sidebar != null) {
const expandedNodes: FernNavigation.NodeId[] = [];
FernNavigation.utils.traverseNavigation(sidebar, (node) => {
if (node.type === "section" && node.collapsed === false) {
expandedNodes.push(...[node.id, ...(parentIdMap.get(node.id) ?? [])]);
}
});
return [...expandedNodes];
} else {
return [];
}
}, [parentIdMap, sidebar]);

const defaultExpandedNodes = useMemo(() => initialExpandedSections(), [initialExpandedSections]);

const [expanded, setExpanded] = useState<FernNavigation.NodeId[]>(initialExpandedSections);
const [expanded, setExpanded] = useState<FernNavigation.NodeId[]>(() =>
selectedNodeId == null ? [] : [selectedNodeId, ...(parentIdMap.get(selectedNodeId) ?? [])],
);

useEffect(() => {
const updatedDefaultExpanded = expanded.filter((node) => {
if (defaultExpandedNodes.includes(node)) {
return node;
}
return;
});

setExpanded(
selectedNodeId == null
? updatedDefaultExpanded
: [selectedNodeId, ...updatedDefaultExpanded, ...(parentIdMap.get(selectedNodeId) ?? [])],
);
// eslint-disable-next-line react-hooks/exhaustive-deps
setExpanded(selectedNodeId == null ? [] : [selectedNodeId, ...(parentIdMap.get(selectedNodeId) ?? [])]);
}, [selectedNodeId, parentIdMap]);

const checkExpanded = useCallback(
Expand Down

0 comments on commit 5b11a42

Please sign in to comment.