|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { useMemo } from 'react'; |
| 10 | +import { Link, useParams } from 'react-router-dom'; |
| 11 | +import { FileText, FileQuestion, Loader2 } from 'lucide-react'; |
| 12 | +import { DocShell } from './DocShell'; |
| 13 | +import { BookSidebar } from './BookSidebar'; |
| 14 | +import { useBookData } from './use-book-data'; |
| 15 | +import { resolveBookTree, scopeDocsToBook } from './book-nav'; |
| 16 | + |
| 17 | +/** |
| 18 | + * `/docs/book/:book` — a book landing page (ADR-0046 §6). Resolves the book's |
| 19 | + * spine against the current docs and shows the grouped table of contents: the |
| 20 | + * persistent nav sidebar plus an overview that lists every section's docs. |
| 21 | + * Each doc links to the single-doc reader, which keeps this sidebar in view. |
| 22 | + */ |
| 23 | +export default function BookPage() { |
| 24 | + const { book: bookName, appName } = useParams<{ book: string; appName?: string }>(); |
| 25 | + const { books, docs, state, error } = useBookData(); |
| 26 | + |
| 27 | + const found = useMemo(() => books.find((b) => b.name === bookName), [books, bookName]); |
| 28 | + const resolved = useMemo( |
| 29 | + () => (found ? resolveBookTree(found, scopeDocsToBook(found, docs)) : null), |
| 30 | + [found, docs], |
| 31 | + ); |
| 32 | + |
| 33 | + const docHref = (name: string) => |
| 34 | + appName ? `/apps/${appName}/docs/${name}` : `/docs/${name}`; |
| 35 | + |
| 36 | + if (state === 'loading') { |
| 37 | + return ( |
| 38 | + <div className="flex h-full items-center justify-center p-10 text-muted-foreground"> |
| 39 | + <Loader2 className="h-5 w-5 animate-spin" aria-label="Loading documentation" /> |
| 40 | + </div> |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + if (state === 'error' || !found || !resolved) { |
| 45 | + return ( |
| 46 | + <DocShell breadcrumb={bookName}> |
| 47 | + <div className="mx-auto flex max-w-3xl flex-col items-center gap-3 p-10 text-center"> |
| 48 | + <FileQuestion className="h-10 w-10 text-muted-foreground" /> |
| 49 | + <h1 className="text-lg font-semibold"> |
| 50 | + {state === 'error' ? 'Failed to load documentation' : 'Book not found'} |
| 51 | + </h1> |
| 52 | + <p className="text-sm text-muted-foreground"> |
| 53 | + {error ?? ( |
| 54 | + <> |
| 55 | + No book named <code className="rounded bg-muted px-1 py-0.5">{bookName}</code> is installed. |
| 56 | + </> |
| 57 | + )} |
| 58 | + </p> |
| 59 | + </div> |
| 60 | + </DocShell> |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <DocShell breadcrumb={resolved.label ?? bookName}> |
| 66 | + <div className="mx-auto flex max-w-5xl gap-8 p-4 sm:p-6"> |
| 67 | + <aside className="hidden w-60 shrink-0 lg:block"> |
| 68 | + <div className="sticky top-20 max-h-[calc(100vh-6rem)] overflow-auto pr-1"> |
| 69 | + <BookSidebar book={resolved} docHref={docHref} /> |
| 70 | + </div> |
| 71 | + </aside> |
| 72 | + |
| 73 | + <div className="min-w-0 max-w-3xl flex-1"> |
| 74 | + <h1 className="text-2xl font-bold">{resolved.label ?? bookName}</h1> |
| 75 | + {resolved.description ? ( |
| 76 | + <p className="mt-2 text-sm text-muted-foreground">{resolved.description}</p> |
| 77 | + ) : null} |
| 78 | + |
| 79 | + <div className="mt-6 space-y-8"> |
| 80 | + {resolved.groups.map((group) => ( |
| 81 | + <section key={group.key}> |
| 82 | + <h2 className="mb-2 text-xs font-semibold uppercase tracking-wide text-muted-foreground"> |
| 83 | + {group.label} |
| 84 | + </h2> |
| 85 | + <ul className="divide-y divide-border rounded-md border border-border"> |
| 86 | + {group.entries |
| 87 | + .filter((e) => !e.separator) |
| 88 | + .map((entry, i) => |
| 89 | + entry.href ? ( |
| 90 | + <li key={`ext-${i}`}> |
| 91 | + <a |
| 92 | + href={entry.href} |
| 93 | + target="_blank" |
| 94 | + rel="noreferrer" |
| 95 | + className="flex items-start gap-3 px-3 py-3 hover:bg-muted/50" |
| 96 | + > |
| 97 | + <FileText className="mt-0.5 h-4 w-4 shrink-0 text-muted-foreground" /> |
| 98 | + <span className="text-sm font-medium">{entry.label ?? entry.href}</span> |
| 99 | + </a> |
| 100 | + </li> |
| 101 | + ) : entry.doc ? ( |
| 102 | + <li key={entry.doc}> |
| 103 | + <Link |
| 104 | + to={docHref(entry.doc)} |
| 105 | + className="flex items-start gap-3 px-3 py-3 hover:bg-muted/50" |
| 106 | + > |
| 107 | + <FileText className="mt-0.5 h-4 w-4 shrink-0 text-muted-foreground" /> |
| 108 | + <div className="min-w-0"> |
| 109 | + <div className="text-sm font-medium">{entry.label ?? entry.doc}</div> |
| 110 | + {entry.description ? ( |
| 111 | + <div className="mt-0.5 line-clamp-2 text-xs text-muted-foreground"> |
| 112 | + {entry.description} |
| 113 | + </div> |
| 114 | + ) : null} |
| 115 | + </div> |
| 116 | + </Link> |
| 117 | + </li> |
| 118 | + ) : null, |
| 119 | + )} |
| 120 | + </ul> |
| 121 | + </section> |
| 122 | + ))} |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </DocShell> |
| 127 | + ); |
| 128 | +} |
0 commit comments