|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + useEffect, |
| 5 | + useMemo, |
| 6 | + useState, |
| 7 | + useSyncExternalStore, |
| 8 | + type ComponentProps, |
| 9 | + type CSSProperties, |
| 10 | + type ReactNode, |
| 11 | +} from "react"; |
| 12 | +import { usePathname } from "next/navigation"; |
| 13 | +import { DocsLayout } from "fumadocs-ui/layouts/notebook"; |
| 14 | +import { DocsBody, DocsDescription, DocsPage, DocsTitle } from "fumadocs-ui/layouts/notebook/page"; |
| 15 | +import { EmbedFrame } from "@/components/embed"; |
| 16 | +import { gatedPageView, withGatedPages, withGatedTabs, type GatedNavItem } from "@/lib/gated-view"; |
| 17 | +import { fetchGated, hasSignedInHint, useReader } from "@/lib/reader"; |
| 18 | + |
| 19 | +// Gated docs in the browser (lib/gated-content.ts): fetched from /api/gated with the reader's |
| 20 | +// grant after the page loads, so the static HTML, and everyone without the flag, never has them. |
| 21 | +// The HTML comes from our own MDX, compiled by lib/rehype-gated.ts with components refused. |
| 22 | + |
| 23 | +type GatedPageData = { title: string; description: string | null; html: string }; |
| 24 | + |
| 25 | +/** The gated response at `path` for this reader: undefined until it answers, null if it found nothing. */ |
| 26 | +function useGated<T>(path: string): T | null | undefined { |
| 27 | + const reader = useReader(); |
| 28 | + const flags = reader.kind === "signed-in" ? reader.docsFlags.join() : ""; |
| 29 | + const key = `${flags} ${path}`; |
| 30 | + const [got, setGot] = useState<{ key: string; data: T | null }>(); |
| 31 | + useEffect(() => { |
| 32 | + if (!flags) return; |
| 33 | + let live = true; |
| 34 | + fetchGated(path) |
| 35 | + .then((res) => (res?.ok ? (res.json() as Promise<T>) : null)) |
| 36 | + .catch(() => null) |
| 37 | + .then((data) => live && setGot({ key, data })); |
| 38 | + return () => { |
| 39 | + live = false; |
| 40 | + }; |
| 41 | + }, [path, flags, key]); |
| 42 | + // Keyed, so another URL or reader never shows the last answer while its own loads. |
| 43 | + return got?.key === key ? got.data : undefined; |
| 44 | +} |
| 45 | + |
| 46 | +const Html = ({ html }: { html: string }) => <div dangerouslySetInnerHTML={{ __html: html }} />; |
| 47 | + |
| 48 | +// Gated MDX can't use components, so the site marks it as a private beta here, in the callout |
| 49 | +// "tip" tone (app/global.css), and authors never write the note themselves. |
| 50 | +const tone = { |
| 51 | + "--tone": "var(--callout-tip)", |
| 52 | + "--tint": "var(--callout-tip-tint)", |
| 53 | +} as CSSProperties; |
| 54 | + |
| 55 | +function PrivateBetaBadge() { |
| 56 | + return ( |
| 57 | + <div |
| 58 | + style={tone} |
| 59 | + className="not-prose flex flex-wrap items-center gap-x-2 gap-y-1 text-[0.8125rem] leading-snug" |
| 60 | + > |
| 61 | + {/* Pure --tone text is under 4.5:1 on the chip in dark mode; 30% foreground lifts it past 5:1 in both themes. */} |
| 62 | + <span className="rounded bg-[color-mix(in_srgb,var(--tone)_14%,transparent)] px-1.5 py-px text-[0.75rem] font-medium text-[color-mix(in_srgb,var(--tone)_70%,var(--color-fd-foreground))]"> |
| 63 | + Private beta |
| 64 | + </span> |
| 65 | + <span className="text-fd-muted-foreground"> |
| 66 | + Visible to your organization while this feature is in private beta |
| 67 | + </span> |
| 68 | + </div> |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +/** `<Gated id="..." />` in a page: the section's content for readers whose flags open it. */ |
| 73 | +export function Gated({ id }: { id: string }) { |
| 74 | + const section = useGated<{ html: string }>(`section/${encodeURIComponent(id)}`); |
| 75 | + if (!section) return null; |
| 76 | + return ( |
| 77 | + <section |
| 78 | + aria-label="Private beta" |
| 79 | + style={tone} |
| 80 | + className="my-6 overflow-hidden rounded-lg border border-[color-mix(in_srgb,var(--tone)_28%,transparent)]" |
| 81 | + > |
| 82 | + <div className="border-b border-[color-mix(in_srgb,var(--tone)_20%,transparent)] bg-(--tint) px-4 py-2"> |
| 83 | + <PrivateBetaBadge /> |
| 84 | + </div> |
| 85 | + <div className="px-4 [&>div>:first-child]:mt-4 [&>div>:last-child]:mb-4"> |
| 86 | + <Html html={section.html} /> |
| 87 | + </div> |
| 88 | + </section> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +/** The docs layout, with the gated pages the reader's flags open placed in its sidebar tree and section tabs. */ |
| 93 | +export function GatedDocsLayout(props: ComponentProps<typeof DocsLayout>) { |
| 94 | + const items = useGated<GatedNavItem[]>("nav"); |
| 95 | + const tree = useMemo(() => withGatedPages(props.tree, items ?? []), [props.tree, items]); |
| 96 | + const tabs = useMemo( |
| 97 | + () => (Array.isArray(props.tabs) ? withGatedTabs(props.tabs, items ?? []) : props.tabs), |
| 98 | + [props.tabs, items] |
| 99 | + ); |
| 100 | + return <DocsLayout {...props} tree={tree} tabs={tabs} />; |
| 101 | +} |
| 102 | + |
| 103 | +const noSubscribe = () => () => {}; |
| 104 | + |
| 105 | +function useGatedPage() { |
| 106 | + const reader = useReader(); |
| 107 | + const path = usePathname().replace(/^\/embed(?=\/)/, ""); |
| 108 | + const page = useGated<GatedPageData>(`page${path}`); |
| 109 | + const hint = useSyncExternalStore( |
| 110 | + noSubscribe, |
| 111 | + () => hasSignedInHint(document.cookie), |
| 112 | + () => null |
| 113 | + ); |
| 114 | + const view = gatedPageView(reader, hint, page); |
| 115 | + useEffect(() => { |
| 116 | + if (page) document.title = page.title; |
| 117 | + }, [page]); |
| 118 | + // "page" always has one; "wait" never does. |
| 119 | + return { view, page: page ?? null, path }; |
| 120 | +} |
| 121 | + |
| 122 | +/** A neutral page while it isn't known yet whether this URL is a gated page or a 404. */ |
| 123 | +function Skeleton() { |
| 124 | + return ( |
| 125 | + <div role="status" aria-label="Loading" className="flex animate-pulse flex-col gap-6"> |
| 126 | + <div className="flex flex-col gap-3 border-b pb-6"> |
| 127 | + <div className="h-8 w-1/2 rounded-md bg-fd-muted" /> |
| 128 | + <div className="h-5 w-3/4 rounded-md bg-fd-muted" /> |
| 129 | + </div> |
| 130 | + <div className="flex flex-col gap-3"> |
| 131 | + {["w-full", "w-11/12", "w-full", "w-2/3"].map((w, i) => ( |
| 132 | + <div key={i} className={`h-4 rounded bg-fd-muted ${w}`} /> |
| 133 | + ))} |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +/** The docs 404: the gated page at this URL when the reader's flags open one, else `children`. */ |
| 140 | +export function GatedPageOr({ children }: { children: ReactNode }) { |
| 141 | + const { view, page } = useGatedPage(); |
| 142 | + if (view === "404") return children; |
| 143 | + return ( |
| 144 | + <DocsPage toc={[]}> |
| 145 | + {page ? ( |
| 146 | + <> |
| 147 | + <header className="flex flex-col gap-2 border-b pb-6"> |
| 148 | + <DocsTitle>{page.title}</DocsTitle> |
| 149 | + {page.description && ( |
| 150 | + <DocsDescription className="mb-0">{page.description}</DocsDescription> |
| 151 | + )} |
| 152 | + <PrivateBetaBadge /> |
| 153 | + </header> |
| 154 | + <DocsBody> |
| 155 | + <Html html={page.html} /> |
| 156 | + </DocsBody> |
| 157 | + </> |
| 158 | + ) : ( |
| 159 | + <Skeleton /> |
| 160 | + )} |
| 161 | + </DocsPage> |
| 162 | + ); |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * The docs panel's 404: as GatedPageOr, in the panel's frame. The frame tells the dashboard the |
| 167 | + * page's title, so it says nothing until the browser knows (never "Not found" for a gated page). |
| 168 | + */ |
| 169 | +export function EmbedGatedPageOr({ title, children }: { title: string; children: ReactNode }) { |
| 170 | + const { view, page, path } = useGatedPage(); |
| 171 | + const shown = view === "wait" ? "" : (page?.title ?? title); |
| 172 | + return ( |
| 173 | + <EmbedFrame title={shown} path={path}> |
| 174 | + {view === "404" ? ( |
| 175 | + children |
| 176 | + ) : ( |
| 177 | + <article className="mx-auto flex w-full max-w-[860px] flex-col gap-6 px-5 py-5"> |
| 178 | + {page ? ( |
| 179 | + <> |
| 180 | + <header className="flex flex-col gap-2 border-b pb-5"> |
| 181 | + <DocsTitle className="text-2xl">{page.title}</DocsTitle> |
| 182 | + {page.description && ( |
| 183 | + <DocsDescription className="mb-0 text-base">{page.description}</DocsDescription> |
| 184 | + )} |
| 185 | + <PrivateBetaBadge /> |
| 186 | + </header> |
| 187 | + <DocsBody> |
| 188 | + <Html html={page.html} /> |
| 189 | + </DocsBody> |
| 190 | + </> |
| 191 | + ) : ( |
| 192 | + <Skeleton /> |
| 193 | + )} |
| 194 | + </article> |
| 195 | + )} |
| 196 | + </EmbedFrame> |
| 197 | + ); |
| 198 | +} |
0 commit comments