Files
anthony lio 6bd006dc08 feat(docs): layout toggle (#53688)
## Problem

- docs layout v2 (variant `option-1` of the `docs-ia-mvp-v1` flag) is
about to roll out and a reader in the rollout has no way back to the
classic layout

## Solution

- adds a toggle button bottom of the guide right column so user in the
rollout can switch back
- adds a dialog that asks for an optional reason when a reader switches
back to classic, and stores the reason in `feedback_comments` with
`metadata.source = 'docs_layout_switch'` so we relying on existing
- adds the `docs_layout_updated` event and stops new experiment
exposures for readers who picked a layout

| toggle | survey |
| -------|------|
| <img width="654" height="312" alt="image"
src="https://github.com/user-attachments/assets/69bcd4b8-7dd2-4fd3-9045-e3246625ccf9"
/> | <img width="654" height="312" alt="image"
src="https://github.com/user-attachments/assets/4b18eb76-b9f1-4d96-be25-14a960b11475"
/> |

<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions

1. open
[/docs/guides/database/prisma?docs-ia-mvp-v1=option-1](https://docs-git-antlio-feat-docs-layout-switch-v2-supabase.vercel.app/docs/guides/database/prisma?docs-ia-mvp-v1=option-1)
2. click "Switch to classic layout" in the notice and make sure that a
dialog asks "What made you switch back?"

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-12-01 11:43:40 +02:00

104 lines
4.4 KiB
TypeScript

'use client'
import { posthogClient, useConsentState, useFeatureFlags, useSearchParamsShallow } from 'common'
import { LOCAL_STORAGE_KEYS } from 'common/constants/local-storage'
import { safeLocalStorage, safeSessionStorage } from 'common/safe-storage'
import { useEffect, useSyncExternalStore } from 'react'
import {
IA_NAV_FLAG,
IA_NAV_FLAG_OFF,
IA_NAV_OVERRIDE_KEY,
isIaNavVariant as isVariant,
type IaNavVariant,
} from './constants'
import { useIaNavRouteVariant } from './IaNavRouteProvider'
import { subscribeToLayoutChoice, useLayoutChoice } from './layoutChoice'
const subscribeToNothing = () => () => {}
// the session override is the variant a url forced earlier in this browser session
const readSessionOverride = () => {
const stored = safeSessionStorage.getItem(IA_NAV_OVERRIDE_KEY)
return isVariant(stored) ? stored : null
}
// the url override wins, so a forced variant shows at hydration like a cached one
const readKnownFlag = () => {
const override = new URLSearchParams(window.location.search).get(IA_NAV_FLAG)
if (isVariant(override)) return override
return readSessionOverride() ?? safeLocalStorage.getItem(LOCAL_STORAGE_KEYS.IA_NAV_FLAG)
}
/**
* Reads the `docs-ia-mvp-v1` PostHog experiment flag. A layout the user picked wins over the flag, unless the flag is off.
* While the flags load, or when the posthog request fails, it uses the value from the last visit.
* If neither is known, it returns `'control'`, the current navigation.
*/
export function useIaNavVariant(): IaNavVariant {
const { posthog, hasLoaded } = useFeatureFlags()
const { hasAccepted } = useConsentState()
const searchParams = useSearchParamsShallow()
const routeVariant = useIaNavRouteVariant()
// a layout pick clears the session override, so the override reads again on each pick
const sessionOverride = useSyncExternalStore(
subscribeToLayoutChoice,
readSessionOverride,
() => null
)
const layoutChoice = useLayoutChoice()
const urlOverride = searchParams.get(IA_NAV_FLAG)
// the value known before the flags load. it is null on the server and on a first visit
const knownFlag = useSyncExternalStore(subscribeToNothing, readKnownFlag, () => null)
// exception: the url is outside React state and links drop it, so the override must outlive the page
useEffect(() => {
const urlOverride = new URLSearchParams(window.location.search).get(IA_NAV_FLAG)
if (isVariant(urlOverride)) safeSessionStorage.setItem(IA_NAV_OVERRIDE_KEY, urlOverride)
}, [searchParams])
// a failed posthog request loads no flags at all, so the last assignment stays
const hasPosthogFlags = hasLoaded && Object.keys(posthog).length > 0
const flagValue = hasPosthogFlags ? posthog[IA_NAV_FLAG] : knownFlag
// `false` means the visitor is outside the rollout, not assigned to control.
const isAssigned = isVariant(flagValue)
const flagVariant: IaNavVariant = flagValue === 'option-1' ? 'option-1' : 'control'
// a disabled flag ignores the picked layout, so the flag stays the kill switch
const isFlagOff = hasPosthogFlags ? !(IA_NAV_FLAG in posthog) : knownFlag === IA_NAV_FLAG_OFF
const override =
routeVariant ??
(isVariant(urlOverride) ? urlOverride : sessionOverride) ??
(isFlagOff ? null : layoutChoice)
// exception: the flags arrive through context, with no event to hook
useEffect(() => {
if (!hasPosthogFlags) return
// a missing key means the flag is gone, so the cache drops the old assignment
safeLocalStorage.setItem(
LOCAL_STORAGE_KEYS.IA_NAV_FLAG,
IA_NAV_FLAG in posthog ? String(posthog[IA_NAV_FLAG]) : IA_NAV_FLAG_OFF
)
}, [hasPosthogFlags, posthog])
useEffect(() => {
// the cached flag can be stale, so only the loaded flag records an exposure
// a layout the user picked is not the flag assignment, so it records no exposure
if (!hasPosthogFlags || !isAssigned || routeVariant || layoutChoice) return
// useSearchParamsShallow is empty until its own effect runs, so read the URL directly.
const urlOverride = new URLSearchParams(window.location.search).get(IA_NAV_FLAG)
if (isVariant(urlOverride) || sessionOverride) return
posthogClient.captureExperimentExposure(IA_NAV_FLAG, { variant: flagVariant }, hasAccepted)
}, [
hasPosthogFlags,
isAssigned,
routeVariant,
layoutChoice,
sessionOverride,
flagVariant,
hasAccepted,
])
return isVariant(override) ? override : flagVariant
}