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

100 lines
3.6 KiB
TypeScript

'use client'
import { IA_NAV_FLAG, isIaNavVariant, type IaNavVariant } from '~/features/IaNav/constants'
import { useDocsPathname, useIaNavRouteVariant } from '~/features/IaNav/IaNavRouteProvider'
import { setLayoutChoice } from '~/features/IaNav/layoutChoice'
import { useIaNavReady } from '~/features/IaNav/useIaNavReady'
import { useIaNavVariant } from '~/features/IaNav/useIaNavVariant'
import { BASE_PATH, IS_PLATFORM } from '~/lib/constants'
import { supabase } from '~/lib/supabase'
import { useSendTelemetryEvent } from '~/lib/telemetry'
import { useFeatureFlags } from 'common'
import { PanelsTopLeft } from 'lucide-react'
import { usePathname } from 'next/navigation'
import { useRef, useState } from 'react'
import { Button, cn } from 'ui'
import { LayoutSwitchFeedbackDialog } from './LayoutSwitchFeedbackDialog'
interface LayoutSwitchButtonProps {
className?: string
}
export const LayoutSwitchButton = ({ className }: LayoutSwitchButtonProps) => {
const variant = useIaNavVariant()
const isNavReady = useIaNavReady()
const sendTelemetryEvent = useSendTelemetryEvent()
const assignedVariant = useFeatureFlags().posthog[IA_NAV_FLAG]
const pathname = usePathname() ?? ''
const routeVariant = useIaNavRouteVariant()
const docsPathname = useDocsPathname()
const [isFeedbackOpen, setIsFeedbackOpen] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)
const switchLayout = (nextVariant: IaNavVariant) => {
setLayoutChoice(nextVariant)
sendTelemetryEvent({
action: 'docs_layout_updated',
properties: {
layoutVariant: nextVariant,
...(isIaNavVariant(assignedVariant) && { assignedVariant }),
},
})
// a forced url or route keeps its variant over the choice, so only a load of the public page switches
const url = new URL(window.location.href)
const isForcedUrl = url.searchParams.has(IA_NAV_FLAG)
if (!isForcedUrl && !routeVariant) return
url.searchParams.delete(IA_NAV_FLAG)
if (routeVariant) url.pathname = `${BASE_PATH}${docsPathname}`
window.location.replace(url)
}
// a switch back asks why first, so the rollout gets a reason for each opt-out
const handleClick = () =>
variant === 'option-1' ? setIsFeedbackOpen(true) : switchLayout('option-1')
const handleFeedbackCancel = () => setIsFeedbackOpen(false)
const handleFeedbackSubmit = async (comment: string) => {
if (comment && IS_PLATFORM) {
// the vote and title columns are for page feedback, so the source lives in metadata
const { error } = await supabase()
.from('feedback_comments')
.insert({
page: pathname,
comment,
metadata: { source: 'docs_layout_switch', layoutVariant: 'control' },
})
// the reason is optional, so a hung request must not block the switch
.abortSignal(AbortSignal.timeout(5000))
if (error) console.error(error)
}
setIsFeedbackOpen(false)
switchLayout('control')
}
// the server renders control, so the button waits for the known variant
if (!isNavReady) return null
return (
<>
<Button
ref={buttonRef}
size="small"
icon={<PanelsTopLeft className="size-4!" />}
className={cn('justify-start self-stretch pl-1.5', className)}
onClick={handleClick}
>
{variant === 'option-1' ? 'Switch to classic layout' : 'Switch to new layout'}
</Button>
<LayoutSwitchFeedbackDialog
isOpen={isFeedbackOpen}
returnFocusRef={buttonRef}
onCancel={handleFeedbackCancel}
onSubmit={handleFeedbackSubmit}
/>
</>
)
}