|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Link from "next/link"; |
| 4 | +import { usePathname } from "next/navigation"; |
| 5 | +import type { ReactNode } from "react"; |
| 6 | +import { Sidebar } from "@/components/ui/sidebar"; |
| 7 | +import type { SearchSuggestion } from "./docs-search"; |
| 8 | + |
| 9 | +// Structural mirror of fumadocs' page-tree nodes (typeof source.pageTree). We |
| 10 | +// only render the node shapes our docs use: pages, separators and one level of folder. |
| 11 | +type TreeItem = { type: "page"; name: ReactNode; url: string }; |
| 12 | +type TreeFolder = { type: "folder"; name: ReactNode; children: TreeNode[] }; |
| 13 | +type TreeSeparator = { type: "separator"; name?: ReactNode }; |
| 14 | +type TreeNode = TreeItem | TreeFolder | TreeSeparator; |
| 15 | + |
| 16 | +export type NavGroup = { label?: ReactNode; items: TreeItem[] }; |
| 17 | + |
| 18 | +export type DocsTree = { children: TreeNode[] }; |
| 19 | + |
| 20 | +// Pages to flag as new in the sidebar. Drop a URL once its primitive has been |
| 21 | +// out for a release or two. |
| 22 | +const NEW_PAGES = new Set(["/primitives/shell", "/primitives/nav", "/primitives/tabs"]); |
| 23 | + |
| 24 | +// A separator opens a labelled group for the root pages after it; a folder is a |
| 25 | +// group of its own. |
| 26 | +export const toGroups = (nodes: TreeNode[]): NavGroup[] => { |
| 27 | + const groups: NavGroup[] = []; |
| 28 | + let open: NavGroup | null = null; |
| 29 | + |
| 30 | + for (const node of nodes) { |
| 31 | + switch (node.type) { |
| 32 | + case "separator": |
| 33 | + open = { label: node.name, items: [] }; |
| 34 | + groups.push(open); |
| 35 | + break; |
| 36 | + case "folder": |
| 37 | + groups.push({ |
| 38 | + label: node.name, |
| 39 | + items: node.children.filter((child): child is TreeItem => child.type === "page"), |
| 40 | + }); |
| 41 | + open = null; |
| 42 | + break; |
| 43 | + case "page": |
| 44 | + if (!open) { |
| 45 | + open = { items: [] }; |
| 46 | + groups.push(open); |
| 47 | + } |
| 48 | + open.items.push(node); |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return groups; |
| 54 | +}; |
| 55 | + |
| 56 | +// The search palette opens on these before anything is typed. |
| 57 | +export const toSuggestions = (groups: NavGroup[]): SearchSuggestion[] => |
| 58 | + groups |
| 59 | + .flatMap((group) => group.items) |
| 60 | + .filter((item) => item.url.startsWith("/primitives/")) |
| 61 | + .map((item) => ({ |
| 62 | + url: item.url, |
| 63 | + title: typeof item.name === "string" ? item.name : item.url, |
| 64 | + })); |
| 65 | + |
| 66 | +const NavLink = ({ item }: { item: TreeItem }) => { |
| 67 | + const pathname = usePathname(); |
| 68 | + |
| 69 | + return ( |
| 70 | + <Sidebar.MenuItem> |
| 71 | + <Sidebar.MenuButton |
| 72 | + isActive={pathname === item.url} |
| 73 | + render={ |
| 74 | + <Link href={item.url}> |
| 75 | + <span className="min-w-0 flex-1 truncate">{item.name}</span> |
| 76 | + {NEW_PAGES.has(item.url) && ( |
| 77 | + <span className="flex h-[18px] shrink-0 items-center rounded-[5px] bg-accent-bg/10 px-1.5 font-medium text-2xs text-accent-bg dark:bg-accent-bg/15"> |
| 78 | + New |
| 79 | + </span> |
| 80 | + )} |
| 81 | + </Link> |
| 82 | + } |
| 83 | + /> |
| 84 | + </Sidebar.MenuItem> |
| 85 | + ); |
| 86 | +}; |
| 87 | + |
| 88 | +/** The docs half of the site sidebar: the page tree. */ |
| 89 | +export const DocsNav = ({ groups }: { groups: NavGroup[] }) => ( |
| 90 | + <Sidebar.Content className="gap-[18px]"> |
| 91 | + {groups.map((group, index) => ( |
| 92 | + <Sidebar.Group key={typeof group.label === "string" ? group.label : index}> |
| 93 | + {group.label && <Sidebar.GroupLabel>{group.label}</Sidebar.GroupLabel>} |
| 94 | + <Sidebar.Menu> |
| 95 | + {group.items.map((item) => ( |
| 96 | + <NavLink key={item.url} item={item} /> |
| 97 | + ))} |
| 98 | + </Sidebar.Menu> |
| 99 | + </Sidebar.Group> |
| 100 | + ))} |
| 101 | + </Sidebar.Content> |
| 102 | +); |
0 commit comments