import { useMemo, useState } from "react"; import { Link, useLocation } from "react-router-dom"; import type { PageSummary } from "@ledger/shared"; import { Icon } from "./Icon"; type Space = { id: string; name: string; key: string; visibility: string; }; type PageNode = PageSummary & { children: PageNode[] }; function closeSidebarOnMobile(onClose: () => void) { if (window.innerWidth <= 920) { onClose(); } } function buildTree(pages: PageSummary[]) { const byId = new Map(); const roots: PageNode[] = []; for (const page of pages) { byId.set(page.id, { ...page, children: [] }); } for (const node of byId.values()) { if (node.parentPageId && byId.has(node.parentPageId)) { byId.get(node.parentPageId)!.children.push(node); } else { roots.push(node); } } const sortNodes = (nodes: PageNode[]) => { nodes.sort((a, b) => a.title.localeCompare(b.title)); nodes.forEach((node) => sortNodes(node.children)); }; sortNodes(roots); return roots; } function TreeNode({ node, depth, currentSlug, onClose }: { node: PageNode; depth: number; currentSlug: string | undefined; onClose: () => void; }) { const [isOpen, setIsOpen] = useState(true); const isActive = node.slug === currentSlug; const hasChildren = node.children.length > 0; return (
{hasChildren ? ( ) : ( )} closeSidebarOnMobile(onClose)}> {node.title} {node.visibility}
{hasChildren && isOpen ? (
{node.children.map((child) => ( ))}
) : null}
); } export function PageSidebar({ spaces, pagesBySpace, currentSpaceKey, currentSlug, user, isOpen, onClose }: { spaces: Space[]; pagesBySpace: Record; currentSpaceKey?: string; currentSlug?: string; user: { displayName: string; role: string } | null; isOpen: boolean; onClose: () => void; }) { const location = useLocation(); const [collapsedSpaces, setCollapsedSpaces] = useState>({}); const recentPages = useMemo( () => Object.values(pagesBySpace) .flat() .sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt)) .slice(0, 5), [pagesBySpace] ); return ( <>
); }