Files
supabase/apps/studio/components/interfaces/App/CommandMenu/FeaturePreviews.utils.ts
T
Monica Khoury 7454d5140d test: cover Cmd+K feature preview toggle routing logic
Extract the enable/disable-and-route decision out of
useFeaturePreviewCommands into a pure resolveFeaturePreviewToggle
util, and unit test every permutation: disabling, enabling with a
route, enabling outside a project-scoped page, missing ref, and
previews without a route.
2026-09-21 17:52:20 +03:00

39 lines
1.2 KiB
TypeScript

import { type FeaturePreview } from '@/components/interfaces/App/FeaturePreview/useFeaturePreviews'
export type FeaturePreviewToggleOutcome =
| { type: 'disabled' }
| { type: 'enabled'; route?: string }
/**
* Decides what should happen when a feature preview is toggled from the
* Cmd+K "Feature previews" page: whether to route the user to where they can
* try the feature out, or just flip the flag in place.
*
* `ref` alone isn't enough to decide this — it can retain a stale project ref
* after client-side navigating away from `/project/[ref]/...` to a route
* without that segment (e.g. the org view) — so routing is also gated on the
* current pathname actually being project-scoped.
*/
export function resolveFeaturePreviewToggle({
preview,
isEnabling,
pathname,
ref,
}: {
preview: Pick<FeaturePreview, 'getRoute'>
isEnabling: boolean
pathname: string
ref: string | undefined
}): FeaturePreviewToggleOutcome {
if (!isEnabling) return { type: 'disabled' }
const isProjectScopedRoute = pathname.startsWith('/project/')
const route = isProjectScopedRoute ? preview.getRoute?.(ref) : undefined
if (route !== undefined && ref !== undefined) {
return { type: 'enabled', route }
}
return { type: 'enabled' }
}