Files
supabase/apps/ui-library/components/library-overview.tsx
Alaister YoungandAlaister Young d6e4089b1a feat(common): add FrameworkProvider and remove next imports (#53905)
Introduces a lightweight `FrameworkProvider` React context in
`packages/common` (`Link`, `Image`, `pathname`, `asPath`, `isReady`,
`params`, `push`), wires it up in the consuming apps, and removes the
`next` dependency from `packages/common`.

**Added:**

- `FrameworkProvider`, `useFramework`, `FrameworkLink`, and
`FrameworkImage` in `packages/common/framework.tsx`, with plain HTML
defaults (`DefaultFrameworkLink` / `DefaultFrameworkImage`).
`useFramework().push` always exists and falls back to
`window.location.assign`
- App-level framework providers wiring `next/link`, `next/image`, and
router state in `www` (App + Pages routers), `docs`, `design-system`,
`learn`, and `ui-library`, and native TanStack Router bindings in
`studio` (`StudioFrameworkProvider`)
- `StudioFrameworkProvider` reuses the common defaults (its image only
applies the basePath) and reports `isReady` once the first route load
commits, so the first page view carries the final route after any
redirect
- Unit tests for `StudioFrameworkProvider` (`useParams`,
`FrameworkLink`, `FrameworkImage`, query/hash-only navigation,
`isReady`) and for `PageTelemetry` route-change sends and the
`TelemetryTagManager` consent gate

**Changed:**

- `common/hooks/useParams.ts`: read params from `useFramework()` instead
of `next/compat/router`
- `common/telemetry.tsx`: replace `next/script` in `TelemetryTagManager`
with `useEffect` script injection, and read route state from
`useFramework()` in `PageTelemetry`
- `common/MetaFavicons/app-router.ts`: drop type-only `Metadata` import
from `next`
- Moved `MetaFaviconsPagesRouter` to
`apps/www/components/MetaFaviconsPagesRouter.tsx` (its sole consumer)
- Studio: `toRoutePattern` and `applyBasePath` now live once in
`lib/internal-url.ts`, shared by the provider and the `compat/next`
shims
- `PageTelemetry` throws outside production when no `FrameworkProvider`
is mounted, since it would otherwise send only the landing pageview.
`useFramework()` itself still falls back to the defaults
- `ui-library`: renamed its code-sample framework context to
`FrameworkPreferenceProvider` / `useFrameworkPreference` so
`FrameworkProvider` only means the `common` one

**Removed:**

- `learn`'s unused copy of that context (`useCourse` had no callers)

- `next` from `packages/common/package.json` peerDependencies

App Router adapters set `asPath` to the pathname only, same as the
`usePathname`-based telemetry on master (reading search params at the
root would opt static pages out of static rendering).

## To test

- Run `packages/common` tests and typecheck, and `apps/studio`
`StudioFrameworkProvider` / `route-render` / `telemetry` tests
- Navigate between routes in Studio, Docs, and WWW and confirm page
telemetry fires once per navigation (and does not duplicate on hash-only
changes)
- Load Studio at `/` (which redirects) and confirm the first page event
has the final route
- Verify URL params (`useParams`) continue resolving route and search
params in Studio

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-25 20:13:22 +08:00

182 lines
7.4 KiB
TypeScript

'use client'
import { ArrowUpRight, Box } from 'lucide-react'
import Link from 'next/link'
import { useRouter, useSearchParams } from 'next/navigation'
import { Suspense, useLayoutEffect, useState } from 'react'
import { Button, cn } from 'ui'
import { CatalogPreview } from '@/components/catalog-preview'
import {
getLibraryBlockHref,
libraryBlocks,
libraryCategories,
type LibraryBlock,
} from '@/config/library'
import { useFrameworkPreference } from '@/context/framework-preference-context'
function LibraryCard({ block, framework }: { block: LibraryBlock; framework: string }) {
return (
<Link
href={getLibraryBlockHref(block, framework)}
className="group flex w-full min-w-0 flex-col gap-4 rounded-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-brand sm:w-[calc((100%-1.5rem)/2)] lg:w-[calc((100%-3rem)/3)] xl:w-[calc((100%-4.5rem)/4)]"
>
<div className="aspect-[4/3] overflow-hidden rounded-md border bg-surface-75 bg-[radial-gradient(var(--color-border)_1px,transparent_1px)] [background-size:20px_20px] transition-colors group-hover:border-stronger group-focus-visible:border-stronger">
<div className="h-full w-full grayscale transition-[filter] duration-200 group-hover:grayscale-0 group-focus-visible:grayscale-0 motion-reduce:transition-none">
<CatalogPreview kind={block.preview} />
</div>
</div>
<div className="flex flex-col gap-2 px-1 pb-2">
<div className="flex items-start justify-between gap-3">
<h3 className="min-w-0 text-sm font-medium tracking-tight">{block.title}</h3>
<span className="flex shrink-0 items-center gap-1 rounded border px-1.5 py-0.5 text-[10px] leading-3 text-foreground-light">
<Box aria-hidden="true" className="size-2.5" />
{block.category === 'Starter apps' ? 'Starter' : 'Block'}
</span>
</div>
<p className="max-w-sm text-sm leading-5 text-foreground-lighter">{block.description}</p>
{block.external && (
<span className="flex items-center gap-1 text-xs text-foreground-lighter">
View starter on GitHub
<ArrowUpRight aria-hidden="true" className="size-3" />
</span>
)}
</div>
</Link>
)
}
function CategoryFromUrl({ onChange }: { onChange: (category: string | null) => void }) {
const category = useSearchParams().get('category')
useLayoutEffect(() => onChange(category), [category, onChange])
return null
}
export function LibraryOverview() {
const router = useRouter()
const [selectedCategory, setSelectedCategory] = useState<string | null>(null)
const activeCategory =
libraryCategories.find((category) => category.name === selectedCategory)?.name ?? 'All'
const { framework } = useFrameworkPreference()
const filtered = libraryBlocks.filter(
(block) => activeCategory === 'All' || block.category === activeCategory
)
const sections = libraryCategories
.map((category) => ({
...category,
items: filtered.filter((block) => block.category === category.name),
}))
.filter((section) => section.items.length > 0)
function selectCategory(category: string) {
const params = new URLSearchParams(window.location.search)
if (category === 'All') params.delete('category')
else params.set('category', category)
router.replace(params.size ? `/?${params}` : '/', { scroll: false })
}
return (
<main>
<Suspense fallback={null}>
<CategoryFromUrl onChange={setSelectedCategory} />
</Suspense>
<section className="mx-auto flex max-w-7xl flex-col gap-6 px-4 pb-10 pt-16 md:px-8 md:pb-12 md:pt-24">
<h1 className="max-w-2xl text-balance font-heading text-4xl font-medium tracking-normal sm:text-5xl sm:leading-none">
Building blocks for your next backend.
</h1>
</section>
<section
className="mx-auto flex max-w-7xl flex-col gap-12 px-4 pb-16 md:px-8 md:pb-24"
aria-labelledby="catalog-heading"
>
<h2 id="catalog-heading" className="sr-only">
Browse blocks
</h2>
<div className="flex flex-col gap-3 border-b pb-12">
<span id="category-label" className="text-xs font-medium text-foreground-lighter">
Category
</span>
<div
role="group"
aria-labelledby="category-label"
className="grid w-fit max-w-full grid-cols-2 gap-x-4 gap-y-2 sm:grid-cols-3"
>
{['All', ...libraryCategories.map((category) => category.name)].map((category) => (
<Button
key={category}
variant="text"
aria-pressed={activeCategory === category}
onClick={() => selectCategory(category)}
className={cn(
'h-auto min-w-0 justify-start rounded-sm border-0 p-0 text-left text-sm leading-6 hover:bg-transparent hover:text-foreground',
activeCategory === category ? 'text-foreground' : 'text-foreground-light'
)}
>
{category}
</Button>
))}
</div>
</div>
<p className="sr-only" role="status">
{filtered.length} blocks found
</p>
{sections.length > 0 ? (
<div className="flex flex-col gap-12">
{sections.map((section) => (
<section
key={section.slug}
id={section.slug}
aria-labelledby={`${section.slug}-heading`}
className="flex flex-col gap-8"
>
<div className="flex flex-col gap-0">
<h2 id={`${section.slug}-heading`} className="text-lg text-foreground">
{section.name}
</h2>
<p className="text-lg text-foreground-muted">{section.description}</p>
</div>
<div className="flex flex-wrap gap-6">
{section.items.map((block) => (
<LibraryCard key={block.slug} block={block} framework={framework} />
))}
</div>
</section>
))}
</div>
) : (
<div className="flex min-h-72 flex-col items-center justify-center gap-2 text-center">
<p className="text-sm font-medium">No blocks found</p>
<p className="text-lg text-foreground-muted">Try another category.</p>
<Button
variant="default"
className="mt-3"
onClick={() => {
selectCategory('All')
}}
>
Clear filters
</Button>
</div>
)}
<div className="flex flex-col justify-between gap-5 border-t pt-8 sm:flex-row sm:items-center">
<p className="max-w-xl text-lg text-foreground-muted">
Components, blocks, and starter apps for Supabase. Add blocks to your project with a
single command.
</p>
<div className="flex flex-wrap items-center gap-3">
<Button asChild variant="default">
<Link href="/docs/getting-started/quickstart">Get started</Link>
</Button>
<Button asChild variant="text" iconRight={<ArrowUpRight className="size-3.5" />}>
<a href="https://supabase.com/docs/guides/ai-tools/ai-skills">Install skills</a>
</Button>
</div>
</div>
</section>
</main>
)
}