|
| 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 { useEffect, useState } from 'react'; |
| 10 | +import { Link } from 'react-router-dom'; |
| 11 | +import { AlertCircle, BookOpen, Loader2 } from 'lucide-react'; |
| 12 | +import { useAdapter } from '@object-ui/app-shell'; |
| 13 | +import { type DocGroup, groupDocsByPackage } from './doc-groups'; |
| 14 | + |
| 15 | +/** |
| 16 | + * `/docs` — the platform-level documentation portal (ADR-0046). |
| 17 | + * |
| 18 | + * Lists every installed `doc` metadata item, grouped by package |
| 19 | + * namespace, each linking to the single-doc viewer at `/docs/<name>`. |
| 20 | + * The viewer route is app-independent, so this portal is the canonical |
| 21 | + * place to discover docs regardless of which app (if any) is active. |
| 22 | + * An app may additionally surface a contextual link into a specific |
| 23 | + * `/docs/<name>`, but discovery lives here. |
| 24 | + */ |
| 25 | +export default function DocsIndex() { |
| 26 | + const adapter = useAdapter(); |
| 27 | + const [groups, setGroups] = useState<DocGroup[]>([]); |
| 28 | + const [state, setState] = useState<'loading' | 'ready' | 'error'>('loading'); |
| 29 | + const [errorMessage, setErrorMessage] = useState<string>(''); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + let cancelled = false; |
| 33 | + async function load() { |
| 34 | + if (!adapter) return; |
| 35 | + const client: any = adapter.getClient(); |
| 36 | + if (!client?.meta?.getItems) { |
| 37 | + setErrorMessage('meta.getItems is not available on this client'); |
| 38 | + setState('error'); |
| 39 | + return; |
| 40 | + } |
| 41 | + setState('loading'); |
| 42 | + try { |
| 43 | + const result: any = await client.meta.getItems('doc'); |
| 44 | + const items: any[] = Array.isArray(result) |
| 45 | + ? result |
| 46 | + : Array.isArray(result?.items) |
| 47 | + ? result.items |
| 48 | + : Array.isArray(result?.value) |
| 49 | + ? result.value |
| 50 | + : []; |
| 51 | + if (cancelled) return; |
| 52 | + setGroups( |
| 53 | + groupDocsByPackage( |
| 54 | + items.map((it) => ({ name: it?.name, label: it?.label })), |
| 55 | + ), |
| 56 | + ); |
| 57 | + setState('ready'); |
| 58 | + } catch (err: any) { |
| 59 | + if (cancelled) return; |
| 60 | + setErrorMessage(err?.message ?? 'Failed to load documentation'); |
| 61 | + setState('error'); |
| 62 | + } |
| 63 | + } |
| 64 | + void load(); |
| 65 | + return () => { |
| 66 | + cancelled = true; |
| 67 | + }; |
| 68 | + }, [adapter]); |
| 69 | + |
| 70 | + if (state === 'loading') { |
| 71 | + return ( |
| 72 | + <div className="flex h-full items-center justify-center p-10 text-muted-foreground"> |
| 73 | + <Loader2 className="h-5 w-5 animate-spin" aria-label="Loading documentation" /> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + if (state === 'error') { |
| 79 | + return ( |
| 80 | + <div className="mx-auto flex max-w-3xl flex-col items-center gap-3 p-10 text-center"> |
| 81 | + <AlertCircle className="h-10 w-10 text-destructive" /> |
| 82 | + <h1 className="text-lg font-semibold">Failed to load documentation</h1> |
| 83 | + <p className="text-sm text-muted-foreground">{errorMessage}</p> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className="mx-auto max-w-3xl p-4 sm:p-6"> |
| 90 | + <div className="mb-6 flex items-center gap-2"> |
| 91 | + <BookOpen className="h-6 w-6 text-muted-foreground" /> |
| 92 | + <h1 className="text-2xl font-semibold">Documentation</h1> |
| 93 | + </div> |
| 94 | + |
| 95 | + {groups.length === 0 ? ( |
| 96 | + <div className="flex flex-col items-center gap-3 py-16 text-center text-muted-foreground"> |
| 97 | + <BookOpen className="h-10 w-10" /> |
| 98 | + <p className="text-sm"> |
| 99 | + No documentation is installed. Packages ship docs as flat |
| 100 | + <code className="mx-1 rounded bg-muted px-1 py-0.5">src/docs/*.md</code> |
| 101 | + files (ADR-0046). |
| 102 | + </p> |
| 103 | + </div> |
| 104 | + ) : ( |
| 105 | + <div className="space-y-8"> |
| 106 | + {groups.map((group) => ( |
| 107 | + <section key={group.pkg}> |
| 108 | + <h2 className="mb-2 text-xs font-semibold uppercase tracking-wide text-muted-foreground"> |
| 109 | + {group.pkg} |
| 110 | + </h2> |
| 111 | + <ul className="divide-y divide-border rounded-md border border-border"> |
| 112 | + {group.docs.map((doc) => ( |
| 113 | + <li key={doc.name}> |
| 114 | + <Link |
| 115 | + to={`/docs/${doc.name}`} |
| 116 | + className="flex items-center gap-2 px-3 py-2 text-sm hover:bg-muted/50" |
| 117 | + > |
| 118 | + <BookOpen className="h-4 w-4 shrink-0 text-muted-foreground" /> |
| 119 | + <span className="font-medium">{doc.label ?? doc.name}</span> |
| 120 | + <code className="ml-auto text-xs text-muted-foreground">{doc.name}</code> |
| 121 | + </Link> |
| 122 | + </li> |
| 123 | + ))} |
| 124 | + </ul> |
| 125 | + </section> |
| 126 | + ))} |
| 127 | + </div> |
| 128 | + )} |
| 129 | + </div> |
| 130 | + ); |
| 131 | +} |
0 commit comments