Files
supabase/apps/docs/features/SearchV2/SearchV2Provider.tsx
Anthony Lio b28c6fa822 feat(docs): ai layout top bar + breadcrumbs (#52975)
## Problem

- docs top bar holds the global nav menus, so it repeats what the new
sidebar shows
- redesign needs a top bar with a breadcrumb for the current page and
room for the page actions

## Solution

- replaces the top bar with a logo cell, a breadcrumb, the page actions
and an account cell
- adds a sections menu under the logo, so a reader moves between docs,
reference and the knowledge base
- adds a breadcrumb slot for the current heading, which the right rail
fills in #PR5, and a reference version dropdown
- replaces the search field with an icon button, and shows ⌘K and ⌘I in
the search and ask ai tooltips
- adds a legacy copy of the current top bar, so the flag can show it

merge the stack together, because the new top bar shows for every reader
until #PR5 adds the flag switch

<!--
## 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/overview](https://docs-git-antlio-docs-v2-4-top-bar-supabase.vercel.app/docs/guides/database/overview)
2. make sure that the breadcrumb shows Database and the page
3. open
[/docs/reference/javascript/introduction](https://docs-git-antlio-docs-v2-4-top-bar-supabase.vercel.app/docs/reference/javascript/introduction),
and make sure that the breadcrumb has a version dropdown after
JavaScript

## 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-11-11 16:31:33 +02:00

159 lines
5.2 KiB
TypeScript

'use client'
import type {
DocsSearchV2AskAiClosedEvent,
DocsSearchV2AskAiOpenedEvent,
} from 'common/telemetry-constants'
import { SearchDialog, useSearchShortcut } from 'docs-search/components'
import { useRouter } from 'next/navigation'
import { createContext, use, useState, type ReactNode } from 'react'
import { flushSync } from 'react-dom'
import { ASK_AI_DIALOG_LABELS, AskAiPanel } from './AskAiPanel'
import { SearchV2Panel } from './SearchV2Dialog'
import { ASK_AI_SHORTCUT_KEY, SEARCH_SHORTCUT_KEY } from './constants'
import { useSearchV2Dialog, type SearchV2CloseReason } from './useSearchV2Dialog'
import { useSendTelemetryEvent } from '@/lib/telemetry'
type SearchTriggerType = Parameters<ReturnType<typeof useSearchV2Dialog>['openDialog']>[0]
type AskAiTriggerType = DocsSearchV2AskAiOpenedEvent['properties']['triggerType']
interface SearchV2ContextValue {
isSearchOpen: boolean
isAskAiOpen: boolean
showSearch: (triggerType: SearchTriggerType) => void
showAskAi: (options: { triggerType: AskAiTriggerType; question?: string }) => void
}
type DialogPanel = 'search' | 'ask-ai'
interface SearchV2ProviderProps {
children: ReactNode
}
const SearchV2Context = createContext<SearchV2ContextValue | null>(null)
/**
* Owns the one search dialog on the page. The top bar renders two times (desktop and mobile).
* One dialog per trigger let search and Ask AI open on top of each other.
*/
export function SearchV2Provider({ children }: SearchV2ProviderProps) {
const router = useRouter()
const sendTelemetryEvent = useSendTelemetryEvent()
const {
open: isSearchOpen,
openDialog: openSearch,
handleOpenChange: handleSearchOpenChange,
} = useSearchV2Dialog()
const [isAskAiOpen, setIsAskAiOpen] = useState(false)
const [askAiQuestion, setAskAiQuestion] = useState('')
// the panel stays set after close, so the closing dialog does not show the other panel
const [panel, setPanel] = useState<DialogPanel>('search')
const isOpen = isSearchOpen || isAskAiOpen
const showPanel = ({ next, update }: { next: DialogPanel; update: () => void }) => {
const canMorph =
isOpen &&
panel !== next &&
typeof document.startViewTransition === 'function' &&
!window.matchMedia('(prefers-reduced-motion: reduce)').matches
if (!canMorph) {
update()
return
}
// dialog-morph.css in docs-search reads this to pick the roll direction
const root = document.documentElement
root.dataset.searchDialogMorph = next
document
.startViewTransition(() => flushSync(update))
.finished.finally(() => delete root.dataset.searchDialogMorph)
}
const closeAskAi = (closeReason: DocsSearchV2AskAiClosedEvent['properties']['closeReason']) => {
if (!isAskAiOpen) return
setIsAskAiOpen(false)
sendTelemetryEvent({ action: 'docs_search_v2_ask_ai_closed', properties: { closeReason } })
}
const showSearch = (triggerType: SearchTriggerType) =>
showPanel({
next: 'search',
update: () => {
closeAskAi('switched_to_search')
setPanel('search')
openSearch(triggerType)
},
})
const showAskAi: SearchV2ContextValue['showAskAi'] = ({ triggerType, question = '' }) => {
if (!isAskAiOpen) {
sendTelemetryEvent({ action: 'docs_search_v2_ask_ai_opened', properties: { triggerType } })
}
if (question) {
sendTelemetryEvent({
action: 'docs_search_v2_ask_ai_question_submitted',
properties: {
questionLength: question.length,
questionSource: 'search',
},
})
}
showPanel({
next: 'ask-ai',
update: () => {
if (isSearchOpen) handleSearchOpenChange(false, 'switched_to_ask_ai')
setAskAiQuestion(question)
setPanel('ask-ai')
setIsAskAiOpen(true)
},
})
}
const closeDialog = (
closeReason: Extract<SearchV2CloseReason, 'dismissed' | 'result_navigated'>
) => {
closeAskAi('dismissed')
if (isSearchOpen) handleSearchOpenChange(false, closeReason)
}
const handleDialogOpenChange = (next: boolean) => {
if (!next) closeDialog('dismissed')
}
const handleResultNavigate = (path: string) => {
router.push(path)
closeDialog('result_navigated')
}
useSearchShortcut(() => showSearch('keyboard_shortcut'), SEARCH_SHORTCUT_KEY)
useSearchShortcut(() => showAskAi({ triggerType: 'keyboard_shortcut' }), ASK_AI_SHORTCUT_KEY)
return (
<SearchV2Context value={{ isSearchOpen, isAskAiOpen, showSearch, showAskAi }}>
{children}
<SearchDialog
open={isOpen}
onOpenChange={handleDialogOpenChange}
labels={panel === 'ask-ai' ? ASK_AI_DIALOG_LABELS : undefined}
>
{panel === 'ask-ai' ? (
<AskAiPanel initialQuestion={askAiQuestion} />
) : (
<SearchV2Panel
onResultNavigate={handleResultNavigate}
onResultLinkNavigate={() => closeDialog('result_navigated')}
onAskAi={showAskAi}
/>
)}
</SearchDialog>
</SearchV2Context>
)
}
export function useSearchV2(): SearchV2ContextValue {
const context = use(SearchV2Context)
if (!context) throw new Error('useSearchV2 must be used inside SearchV2Provider')
return context
}