Files
supabase/apps/studio/components/layouts/AppLayout/useStatusBanner.ts
Charis ab383421c5 fix(studio): show sign-in banner based on Dashboard component [FE-3443] (#51283)
## Summary

* Makes sign-in banner visible on unauthenticated pages
  * Show only when one of the affected components is Dashboard
  * Works only when new status page (under feature flag) is being used
* Regression fix: Auth pages without the status banner (sign-up,
sign-in-mfa, sign-in-recovery-code, sign-in-sso, sign-in-partner,
forgot-password, forgot-password-mfa, reset-password) no longer reserve
dead space at the top for a banner they don't render

## Test plan

- [X] TypeScript: `tsc --noEmit` passes clean, no type errors
- [X] Unit tests: All new/existing tests pass in
`apps/studio/lib/status-page/status-page.utils.test.ts` and
`apps/studio/components/layouts/AppLayout/StatusBanner.utils.test.ts`
(697 files / 7833 tests, no regressions)
- [X] Manual verification: Sign-in page still renders the banner
correctly when an incident affecting Dashboard is active; no layout
regression

**Related:**
[FE-3443](https://linear.app/supabase/issue/FE-3443/only-show-sign-in-incident-banner-when-relevant)
2026-10-06 15:56:48 -04:00

67 lines
2.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { LOCAL_STORAGE_KEYS } from 'common'
import {
getKeysToDismiss,
pruneDismissedKeys,
selectBanner,
type BannerSelection,
} from './StatusBanner.utils'
import { DEFAULT_STATUS_PAGE_URL } from '@/components/interfaces/Support/SupportStatus.utils'
import { statusPageQueryOptions } from '@/data/platform/status-page-query'
import { useEmergencyIncidentOverride } from '@/hooks/misc/useEmergencyIncidentOverride'
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { useUserProjectRegions } from '@/hooks/misc/useUserProjectRegions'
import { normalizeStatusPage } from '@/lib/status-page/status-page.utils'
export type StatusBannerState =
| { type: 'override' }
| { type: 'hidden' }
| { type: 'shown'; selection: BannerSelection; pageUrl: string; onDismiss: () => void }
const HIDDEN: StatusBannerState = { type: 'hidden' }
export function useStatusBanner({
signedOut = false,
}: { signedOut?: boolean } = {}): StatusBannerState {
const isEmergencyOverride = useEmergencyIncidentOverride()
const statusPageOptions = statusPageQueryOptions()
const { data } = useQuery({
...statusPageOptions,
select: normalizeStatusPage,
enabled: statusPageOptions.enabled && !isEmergencyOverride,
})
const items = data?.items ?? []
const userProjectRegions = useUserProjectRegions({
enabled: !signedOut && !isEmergencyOverride && items.length > 0,
})
const [dismissedKeys, setDismissedKeys, { isSuccess: isDismissedKeysLoaded }] =
useLocalStorageQuery<Array<string>>(LOCAL_STORAGE_KEYS.STATUS_BANNER_DISMISSED_KEYS, [])
if (isEmergencyOverride) return { type: 'override' }
if (data === undefined || !isDismissedKeysLoaded) return HIDDEN
if (!signedOut && userProjectRegions.status === 'loading') return HIDDEN
const selection = selectBanner({
items,
user: signedOut || userProjectRegions.status !== 'resolved' ? null : userProjectRegions.context,
dismissedKeys: new Set(dismissedKeys),
nowMs: Date.now(),
})
if (selection === null) return HIDDEN
const onDismiss = () => {
setDismissedKeys((prev) =>
pruneDismissedKeys({ prev, allItems: items, add: getKeysToDismiss(selection) })
)
}
return { type: 'shown', selection, pageUrl: DEFAULT_STATUS_PAGE_URL, onDismiss }
}