Files
supabase/apps/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.constants.ts
Alaister YoungandAlaister Young c59821fde6 [FE-5109] (part 2) refactor(studio): migrate shared UI components off next/router (#54054)
Part 2 of a 6-PR stack migrating all `next/router` and
`next/compat/router` imports in `apps/studio` onto TanStack Router hooks
and removing the router shims.

| Part | PR | Scope |
| --- | --- | --- |
| 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`,
`data/prefetchers/**` |
| **2** | **#54054 ← this PR** | **`components/ui/**`** |
| 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` |
| 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer,
SQL Editor, Storage, Table Editor |
| 5 | #54057 | Remaining `components/interfaces/**` and
`QueryInsightsTable` router events |
| 6 | #54058 | Remove `next/router` & `next/compat/router` shims,
`_router-events.ts`, Vite aliases, and test mocks |

Migrates all 15 `apps/studio/components/ui/**` files off `next/router`.

**Changed:**

- Replaced `useRouter` across `AIAssistantPanel` (`AIAssistant`,
`ModelSelector`), `Charts/ComposedChartHandler`, `EditorPanel`,
`ErrorBoundary` (`ClientSideExceptionHandler`,
`GlobalErrorBoundaryState`, `InsertBeforeRemoveChildErrorHandler`),
`GlobalShortcuts/ShortcutsReferenceSheet`, `HelpPanel`
(`HelpOptionsList`, `HelpPanel`), `ProductMenu/ProductMenuShortcuts`,
`QueryBlock/EditQueryButton`, and `ResourceExhaustionWarningBanner` with
TanStack's `useNavigate`, `useLocation`, `useRoutePattern`, and
`useParams` from `common`
- Updated `AIAssistant.test.tsx`, `HelpOptionsList.test.tsx`, and
`ProductMenuShortcuts.test.tsx` to use TanStack Router instead of
`next/router` mocks

**Simplified:**

- `EditQueryButton` and `ShortcutsReferenceSheet` match against
`useRoutePattern()` instead of the real pathname, as `router.pathname`
did before. A ref or slug containing `sql` or `advisors` no longer
misfires.
- Removed the unused `restrictToRoutes` field from
`ResourceExhaustionWarningBanner` (no warning sets it)
- `AIAssistant` clears the handoff param with `navigate({ to: '.' })`,
dropping a pathname subscription and an `AnyRouter` cast
- `ProductMenuShortcuts` inlines its handler (`useShortcut` always calls
the latest callback)
- Moved the `AdvisorsNavigationShortcuts.test.tsx` fix into this PR so
it passes on its own
- Tests use the shared `createTestRouter` and `customRender` router

Addresses
[FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks)

## To test

- [ ] AI Assistant panel links, model selector navigation, and "Edit in
SQL Editor" (`EditQueryButton`) navigate cleanly
- [ ] Keyboard shortcuts in `ProductMenuShortcuts` and the shortcuts
reference sheet (`?`) match the active section and navigate as expected
- [ ] Error boundary recovery ("Reload page" / navigation actions) and
Help panel links still work
- [ ] Resource exhaustion warning banner matches project routes as
before

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-30 14:49:15 +08:00

249 lines
9.7 KiB
TypeScript

import { DOCS_URL } from '@/lib/constants'
interface ResourceWarningMessage {
bannerContent: {
warning: { title: string; description: string }
critical: { title?: string; description?: string }
}
cardContent: {
warning: { title: string; description: string }
critical: { title?: string; description?: string }
}
docsUrl?: string
resourceLabel?: string
metricsChartId?: string
buttonText?: string
aiPrompt?: string
metric: string | null
}
type ResourceWarningMessages = Record<string, ResourceWarningMessage>
export const DISK_SPACE_EXHAUSTION_DESCRIPTION = {
free: "Free Plan disks don't expand automatically. Free up space, or upgrade to the Pro Plan for a disk that expands as it fills up.",
paid: 'Your disk expands automatically as it fills up. The Infrastructure page shows its current size and any limits on resizing it.',
}
const RESOURCE_WARNING_MESSAGE_CONFIG = {
is_readonly_mode_enabled: {
bannerContent: {
warning: {
title:
'Your project is currently in read-only mode and is no longer accepting write requests',
description:
'You will need to manually override read-only mode and reduce the disk size to below 95%',
},
critical: {
title:
'Your project is currently in read-only mode and is no longer accepting write requests',
description:
'You will need to manually override read-only mode and reduce the disk size to below 95%',
},
},
cardContent: {
warning: {
title: 'Project is in read-only mode',
description: 'Database is no longer accepting write requests.',
},
critical: {
title: 'Project is in read-only mode',
description: 'Database is no longer accepting write requests.',
},
},
docsUrl: `${DOCS_URL}/guides/platform/database-size#disabling-read-only-mode`,
buttonText: 'Manage disk',
metric: 'read_only',
},
disk_io_exhaustion: {
bannerContent: {
warning: {
title: 'Your project is about to deplete its Disk IO Budget',
description:
'Once exhausted, disk throughput will return to its baseline of {baseline}. The budget refills whenever disk IO demand is at or below baseline. Upgrade your compute or use the AI Assistant to identify and optimize disk-intensive queries.',
},
critical: {
title:
'Your project has depleted its Disk IO Budget. Disk throughput is at its baseline of {baseline}',
description:
'Throughput stays at baseline until the budget refills. It refills whenever disk IO demand is at or below baseline. Upgrade your compute to sustain higher throughput, or use the AI Assistant to identify and optimize disk-intensive queries.',
},
},
cardContent: {
warning: {
title: 'Project is depleting its Disk IO Budget',
description: 'It may become unresponsive if fully exhausted',
},
critical: {
title: 'Project has depleted its Disk IO Budget',
description: 'It may become unresponsive',
},
},
docsUrl: `${DOCS_URL}/guides/troubleshooting/exhaust-disk-io`,
buttonText: 'Upgrade compute',
aiPrompt:
'My database is running out of Disk IO budget. Can you query pg_stat_statements to find the top queries by shared blocks read and written, identify which are causing the most disk I/O, and suggest specific optimizations to reduce disk usage?',
resourceLabel: 'Disk IO',
metricsChartId: 'disk-throughput',
metric: 'disk_io',
},
disk_space_exhaustion: {
bannerContent: {
warning: {
title:
'Your project is about to exhaust its available disk space, and may become unresponsive once fully exhausted',
description:
"The Infrastructure page shows your disk's current size and any limits on resizing it.",
},
critical: {
title: 'Your project has exhausted its available disk space, and may become unresponsive',
description:
"The Infrastructure page shows your disk's current size and any limits on resizing it.",
},
},
cardContent: {
warning: {
title: 'Project is exhausting its available disk space',
description: 'It may become unresponsive if fully exhausted',
},
critical: {
title: 'Project has exhausted its available disk space',
description: 'It may become unresponsive',
},
},
docsUrl: `${DOCS_URL}/guides/platform/database-size#disk-management`,
buttonText: undefined,
resourceLabel: 'Disk space',
metricsChartId: 'disk-size',
metric: 'disk_space',
},
cpu_exhaustion: {
bannerContent: {
warning: {
title: 'Your project is currently facing high CPU usage, and its performance is affected',
description:
'Upgrade your compute or use the AI Assistant to identify and optimize CPU-intensive queries.',
},
critical: {
title: "Your project's CPU usage is at 100% and its performance is affected",
description:
'Upgrade your compute or use the AI Assistant to identify and optimize CPU-intensive queries.',
},
},
cardContent: {
warning: {
title: 'Project has high CPU usage',
description: `Performance is affected`,
},
critical: {
title: 'Project CPU usage is at 100%',
description: `Performance is affected`,
},
},
docsUrl: `${DOCS_URL}/guides/troubleshooting/high-cpu-usage`,
buttonText: 'Upgrade compute',
aiPrompt:
'My database is experiencing high CPU usage. Can you query pg_stat_statements to find the top queries by total execution time and mean execution time, identify which are most CPU-intensive, and suggest specific optimizations such as missing indexes or query rewrites to reduce CPU load?',
resourceLabel: 'CPU',
metricsChartId: 'cpu-usage',
metric: 'cpu',
},
memory_and_swap_exhaustion: {
bannerContent: {
warning: {
title:
'Your project is currently facing high memory usage, and its performance is affected',
description:
'Upgrade your compute or use the AI Assistant to identify and optimize memory-intensive queries.',
},
critical: {
title: "Your project's memory usage is at 100%, and its performance is affected",
description:
'Upgrade your compute or use the AI Assistant to identify and optimize memory-intensive queries.',
},
},
cardContent: {
warning: {
title: 'Project has high memory usage',
description: `Performance is affected`,
},
critical: {
title: 'Project memory usage is at 100%',
description: `Performance is affected`,
},
},
docsUrl: `${DOCS_URL}/guides/troubleshooting/exhaust-ram`,
buttonText: 'Upgrade compute',
aiPrompt:
'My database is experiencing high memory and swap usage. Can you query pg_stat_statements to find the top queries by shared buffer hits and rows returned, identify which queries are putting the most pressure on memory, and suggest optimizations to reduce memory consumption?',
resourceLabel: 'Memory',
metricsChartId: 'ram-usage',
metric: 'ram',
},
auth_rate_limit_exhaustion: {
// [Joel] There is no critical warning as there is no notion of critical rate limits for auth at the moment
bannerContent: {
warning: {
title:
'Your project has exceeded email rate limits in the past 24 hours and may not reliably send auth related emails to users',
description:
'Set up a custom SMTP and adjust rate limits where necessary to ensure that emails are sent out reliably.',
},
critical: {
title: undefined,
description: undefined,
},
},
cardContent: {
warning: {
title: 'Your project has exceeded email rate limits',
description: `You will need to set up a custom SMTP provider and adjust rate limits where necessary`,
},
critical: {
title: undefined,
description: undefined,
},
},
docsUrl: `${DOCS_URL}/guides/platform/going-into-prod#auth-rate-limits`,
buttonText: 'Enable custom SMTP',
resourceLabel: 'Auth rate limits',
metric: 'auth_email_rate_limit',
},
multiple_resource_warnings: {
bannerContent: {
warning: {
title: 'Your project is exhausting {resources}, which is affecting its performance',
description:
'Upgrade your compute or use the AI Assistant to identify and optimize the most expensive queries.',
},
critical: {
title: 'Your project has exhausted {resources}, which is affecting its performance',
description:
'Upgrade your compute or use the AI Assistant to identify and optimize the most expensive queries.',
},
},
cardContent: {
warning: {
title: 'Project is exhausting multiple resources',
description: `Performance is affected.`,
},
critical: {
title: 'Project has exhausted multiple resources',
description: `Performance is affected.`,
},
},
docsUrl: undefined,
buttonText: 'Check usage',
aiPrompt:
'My database is exhausting multiple resources ({resources}). Can you query pg_stat_statements to identify the most expensive queries overall, and suggest which optimizations would have the biggest impact on reducing resource consumption?',
metric: null,
},
} satisfies ResourceWarningMessages
export type ResourceWarningMessageKey = keyof typeof RESOURCE_WARNING_MESSAGE_CONFIG
export const RESOURCE_WARNING_MESSAGES: ResourceWarningMessages = RESOURCE_WARNING_MESSAGE_CONFIG
export const isResourceWarningMessageKey = (
property: string
): property is ResourceWarningMessageKey => Object.hasOwn(RESOURCE_WARNING_MESSAGE_CONFIG, property)