Files
supabase/apps/studio/hooks/ui/useFlag.ts
Alaister YoungandAlaister Young bbb0af65cb [FE-3106] (part 14) – refactor(studio): drop the dual-runtime shims and aliases (#53223)
Drops the remaining Next-era shims and aliases that only existed for the
dual runtime (FE-3106). Part 14 of the stack.

**Depends on:** #53222

**Changed:**
- ~45 files – import Sentry from `@sentry/react` directly instead of the
aliased `@sentry/nextjs`
- `hooks/ui/usePreventNavigationOnUnsavedChanges.ts` – is now the
TanStack `useBlocker` implementation (previously swapped in by a Vite
alias)
- `components/interfaces/App/RouteValidationWrapper.tsx` – `next/router`
→ `useNavigate` + the leaf route's `fullPath` (`/project/$ref/sql/$id`)
for the exempt-route and last-visited checks
- `hooks/misc/withAuth.tsx` – no `getLayout` forwarding
- `TemplateEditor.test.tsx` – renders inside a router for the
unsaved-changes blocker

**Removed:**
- `compat/sentry-nextjs.ts` and its Vite alias, the
`usePreventNavigationOnUnsavedChanges` alias and Next hook,
`types/next.ts` (`NextPageWithLayout`), the `@sentry/nextjs` dependency

The `compat/next/*` shims for `link`, `router`, `head`, `image`,
`navigation`, `dynamic` and `script` stay: about 300 components outside
`routes/**` still import them.

Addresses
[FE-3106](https://linear.app/supabase/issue/FE-3106/move-page-bodies-into-route-files-and-delete-pages).

## To test

- Trigger an error (e.g. a failing query) – it reaches Sentry as before
- Edit an email template or edge function and navigate away – the
unsaved-changes prompt appears
- Open an org you don't belong to – toast and redirect home; open a SQL
snippet, leave and come back – last snippet is restored

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-17 17:49:20 +09:00

73 lines
2.6 KiB
TypeScript

import * as Sentry from '@sentry/react'
import { IS_PLATFORM, useFeatureFlags } from 'common'
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { trackFeatureFlag } from '@/lib/posthog'
const isObjectEmpty = (obj: Object) => {
return Object.keys(obj).length === 0
}
/**
* Hook to retrieve a PostHog feature flag value.
*
* @returns `undefined | false | T` where:
* - `undefined` = PostHog store is still loading OR flag doesn't exist (treat as "don't show")
* - `false` = Flag is explicitly set to false (typically means "disabled" or "control" for experiments)
* - `T` = The actual flag value (string, boolean, or custom type like variant names)
*
* @example Experiment usage convention:
* ```typescript
* const variant = usePHFlag<ExperimentVariant | false>('experimentName')
*
* // undefined = loading/doesn't exist, don't render anything yet
* if (variant === undefined) return null
*
* // false = explicitly disabled, show control
* if (variant === false) return <Control />
*
* // Otherwise, variant has a value, show experiment
* return <Experiment variant={variant} />
* ```
*
* @todo TODO(Alaister): move this to packages/common/feature-flags.tsx and rename to useFlag
* @todo TODO(sean): Refactor to have explicit loading/disabled/value states
* See https://linear.app/supabase/issue/GROWTH-539
*/
export function usePHFlag<T = string | boolean>(name: string) {
const flagStore = useFeatureFlags()
// [Joshen] Prepend PH flags with "PH" in local storage for easier identification of PH flags
const [trackedValue, setTrackedValue] = useLocalStorageQuery(`ph_${name}`, '')
const store = flagStore.posthog
const flagValue = store[name]
if (!IS_PLATFORM) return false
// Flag store has not been initialized
if (isObjectEmpty(store)) return undefined
if (!isObjectEmpty(store) && flagValue === undefined) {
console.error(`Flag key "${name}" does not exist in PostHog flag store`)
return undefined
}
if (trackedValue !== flagValue) {
try {
// [Joshen] Only fire the track endpoint once across sessions unless the flag value changes
// Note: This cannot guarantee excess calls in the event for e.g user clears local storage or uses incognito
// trackFeatureFlag checks for telemetry consent before actually firing the request too
trackFeatureFlag({ feature_flag_name: name, feature_flag_value: flagValue })
setTrackedValue(flagValue as string)
} catch (error: any) {
Sentry.withScope((scope) => {
scope.setTag('type', 'phTrackFailure')
Sentry.captureException(error)
})
console.error(error.message)
}
}
return flagValue as T
}