mirror of
https://github.com/supabase/supabase.git
synced 2026-07-06 08:54:22 +08:00
Replace docs command menu with Command Menu V2 from ui-patterns/CommandMenu. This introduces the prepackaged folder for the shared Command Menu, which contains commands that are shared across the sites (for example, docs search, theme switcher). Commands that are only applicable to a single site are now defined within that site's own app folder (put them wherever makes sense for your app folder organization, as long as you use the hooks within a global CommandProvider it will work...) Tested navigating around, using search and AI on laptop and mobile.
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
'use client'
|
|
|
|
import { useRouter as useLegacyRouter } from 'next/compat/router'
|
|
import { useRouter as useNextRouter } from 'next/navigation'
|
|
import { createContext, useContext, useEffect, useMemo, useRef, useTransition } from 'react'
|
|
|
|
type Handler = (...evts: any[]) => void
|
|
|
|
export const CrossCompatRouterContext = createContext<{ onPendingEnd: Set<Handler> } | undefined>(
|
|
undefined
|
|
)
|
|
|
|
function useCrossCompatRouterContext() {
|
|
const ctx = useContext(CrossCompatRouterContext)
|
|
if (!ctx) {
|
|
throw Error(
|
|
'useCrossCompatRouterContext must be used within a CrossCompatRouterContext.Provider'
|
|
)
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
function useHasChanged<T>(value: T, { onlyTo }: { onlyTo?: T } = {}) {
|
|
const prev = useRef(value)
|
|
|
|
let result = true
|
|
|
|
if (prev.current === value) result = false
|
|
if (onlyTo && value !== onlyTo) result = false
|
|
|
|
prev.current = value
|
|
|
|
return result
|
|
}
|
|
|
|
export function useCrossCompatRouter() {
|
|
const { onPendingEnd } = useCrossCompatRouterContext()
|
|
|
|
const legacyRouter = useLegacyRouter()
|
|
const newRouter = useNextRouter()
|
|
const isUsingLegacyRouting = !!legacyRouter
|
|
|
|
const [isPending, startTransition] = useTransition()
|
|
const hasPendingEnded = useHasChanged(isPending, { onlyTo: false })
|
|
if (!isUsingLegacyRouting && hasPendingEnded) {
|
|
onPendingEnd.forEach((fn) => fn())
|
|
}
|
|
|
|
const api = useMemo(
|
|
() => ({
|
|
push: (path: string) => {
|
|
if (isUsingLegacyRouting) {
|
|
legacyRouter.push(path)
|
|
} else {
|
|
startTransition(() => {
|
|
newRouter.push(path)
|
|
})
|
|
}
|
|
},
|
|
}),
|
|
[isUsingLegacyRouting, legacyRouter, newRouter]
|
|
)
|
|
|
|
return api
|
|
}
|