Files
supabase/apps/ui-library/components/framework-selector.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

84 lines
2.6 KiB
TypeScript

'use client'
import { usePathname, useRouter } from 'next/navigation'
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from 'ui'
import { componentPages, frameworkTitles, oauthBlocks } from '@/config/docs'
import { useFrameworkPreference } from '@/context/framework-preference-context'
const frameworks = Object.keys(frameworkTitles)
export function FrameworkSelector() {
const pathname = usePathname()
// Extract framework and docTitle from pathname
// Example: /ui/docs/nextjs/password-based-auth
const pathParts = pathname.split('/')
const docTitle = pathParts[pathParts.length - 1]
const framework = pathParts[pathParts.length - 2]
const router = useRouter()
const { setFramework: setPreferredFramework } = useFrameworkPreference()
const selectedFramework = frameworks.includes(framework) ? framework : 'nextjs'
if (!framework) {
return null
}
// Get the supported frameworks for this component
const componentItem = [...componentPages.items, ...oauthBlocks.items].find(
(item) => item.href?.split('/').pop() === docTitle.toLowerCase()
)
// Use the supportedFrameworks property if it exists, otherwise default to all frameworks
const supportedFrameworks = componentItem?.supportedFrameworks
if (!supportedFrameworks) {
return null
}
// Don't show selector if component only supports one framework
if (supportedFrameworks && supportedFrameworks.length <= 1) {
return null
}
const onSelect = (value: string) => {
// Get current path parts
const currentPathParts = [...pathParts]
// Replace the framework part (second to last) with the new framework
currentPathParts[currentPathParts.length - 2] = value
// Build the new path
const newPath = currentPathParts.join('/')
router.push(newPath)
// Also update the context/localStorage for future use
setPreferredFramework(value as keyof typeof frameworkTitles)
}
const options =
supportedFrameworks?.map((f) => ({
label: frameworkTitles[f],
value: f,
})) || []
return (
<Select value={selectedFramework} onValueChange={onSelect}>
<SelectTrigger
aria-label="Framework"
className="h-7 w-auto min-w-28 gap-2 rounded-md bg-surface-75 px-2 text-xs"
>
<SelectValue>{frameworkTitles[selectedFramework]}</SelectValue>
</SelectTrigger>
<SelectContent>
<SelectGroup>
{options.map((f) => (
<SelectItem key={f.value} value={f.value}>
{f.label}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
)
}