Files
supabase/apps/docs/features/IaNav/useIaNavVariant.ts
Miranda Limonczenkoandcoderabbitai[bot] 33c8ab1dbf feat(docs): keep a forced Option 1 for the browser session (#53381)
Part of DOCS-1474

## Problem

Someone dogfooding the new navigation opens `?docs-ia-mvp-v1=option-1`,
clicks a link, and sees today's navigation again.

The override lives only in the URL. Links do not carry the query string,
so the first click drops it. Everyone who uses the override link is
affected, including people outside the PostHog rollout.

## Solution

- **Save the override:** a forced variant is saved in `sessionStorage`
and read on later pages. Closing the tab clears it.
- **Read it before paint:** the head script reads the saved override, so
a full reload does not flash today's navigation.
- **Keep exposure clean:** a saved override records no PostHog exposure,
the same as a URL override.
- **Switch back:** `?docs-ia-mvp-v1=control` replaces the saved value.

**Priority:** the URL wins over the saved override. The saved override
wins over the PostHog value.

**Not changed:** the `/docs/ia/option-1/…` rewrite that the evals use,
and how PostHog assigns visitors.

**Tests:** `vitest run features/IaNav` passes, 7 files and 32 tests.
Three of the four new tests failed before the change. The fourth, a URL
`control` over a saved `option-1`, passed before and guards the
priority.

**Unverified:** typecheck, ESLint, and the steps below. `tsc` reports no
errors in `IaNav`, but this worktree has no generated GraphQL types.
ESLint did not run.

## Manual testing

1. Open the deploy preview at `/docs?docs-ia-mvp-v1=option-1`. The top
nav shows "Run and manage".
2. Click any guide link. The nav still shows "Run and manage".
3. Reload the page. The nav shows "Run and manage" with no flash of
today's navigation.
4. Open `/docs?docs-ia-mvp-v1=control`, then click any guide link. The
nav shows today's navigation and keeps it.

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2026-11-13 12:12:07 -08:00

79 lines
3.7 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_OVERRIDE_KEY,
isIaNavVariant as isVariant,
type IaNavVariant,
} from './constants'
import { useIaNavRouteVariant } from './IaNavRouteProvider'
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.
* 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()
const sessionOverride = useSyncExternalStore(subscribeToNothing, readSessionOverride, () => null)
const urlOverride = searchParams.get(IA_NAV_FLAG)
const override = routeVariant ?? (isVariant(urlOverride) ? urlOverride : sessionOverride)
// 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'
// 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, String(posthog[IA_NAV_FLAG] ?? false))
}, [hasPosthogFlags, posthog])
useEffect(() => {
// the cached flag can be stale, so only the loaded flag records an exposure
if (!hasPosthogFlags || !isAssigned || routeVariant) 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, sessionOverride, flagVariant, hasAccepted])
return isVariant(override) ? override : flagVariant
}