Files
Alaister YoungandAlaister Young 953e508a3e [FE-5110] refactor(studio): replace next/navigation with TanStack (#54041)
Moves the remaining `next/navigation` imports in Studio onto TanStack
Router hooks (and `useParams` from `common`), finishes migrating the
remaining `next/link` and `next/router` usages in the touched
components, and removes the `compat/next/navigation.ts` shim and its
Vite alias. Shared packages (`packages/**`) no longer import
`next/navigation`, so the alias can be dropped now.

**Removed:**

- `apps/studio/compat/next/navigation.ts` and the `'next/navigation'`
entry in `apps/studio/vite.config.ts`
- Global `next/navigation` mock in `apps/studio/tests/vitestSetup.ts`
and the obsolete `apps/studio/tests/lib/route-mock.test.tsx`

**Changed:**

- Replaced `usePathname` (12 files) and `useSearchParams` (3 files) with
`useLocation({ select })` from `@tanstack/react-router`
- Replaced `useRouter` in Storage bucket tables, SQL Editor commands,
and `SignInMfaForm` with `useNavigate` from `@tanstack/react-router`,
and updated `createNavigationHandler` in `lib/navigation.ts` to accept
either `NextRouter` or TanStack's `useNavigate`
- Replaced remaining `next/link` imports in touched components
(`SignInMfaForm`, `Restriction`, `DownloadLogsButton`,
`DownloadResultsButton`, `AuthEmailsLayout`, `AuthProvidersLayout`,
`JWTKeysLayout`, `StorageBucketsLayout`) with TanStack Router's `Link`
- Replaced `useParams` in `CreatePublishableAPIKeyDialog` with
`useParams` from `common` (matching `CreateSecretAPIKeyDialog`)
- Wrapped `CustomWrapper` (`tests/lib/custom-render.tsx`) in a default
TanStack `RouterContextProvider` (with optional `router` / `initialUrl`
overrides) and updated affected component tests to drop their
`next/navigation` mocks

Addresses
[FE-5110](https://linear.app/supabase/issue/FE-5110/replace-the-nextnavigation-shim-with-tanstack-router-hooks)

## To test

1. Check active tab highlighting and navigation across Auth Emails, Auth
Providers, JWT Keys, Storage Buckets, and Editor layouts
2. Verify clicking, Cmd/Ctrl-clicking, middle-clicking, and pressing
Enter on Storage bucket rows (Files, Analytics, Vector) navigates as
before
3. Run "Query a table..." and open SQL snippets from the Command Menu
and confirm `/project/$ref/sql/$id` opens with multi-line SQL preserved
in `?content=`
4. In the MFA sign-in flow, verify the recovery code link and
`forgot-password` completion preserve query params (`returnTo`,
`context=forgot-password`), and Cancel signs out and replaces history
with `/sign-in`
5. Confirm Table Editor sort/filter URL sync persists filters per table
without leaking across table switches

---------

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

263 lines
10 KiB
TypeScript

import { Link, useLocation } from '@tanstack/react-router'
import dayjs from 'dayjs'
import { ExternalLinkIcon } from 'lucide-react'
import { Alert, AlertDescription, AlertTitle, Button, CriticalIcon, WarningIcon } from 'ui'
import { PricingMetric } from '@/data/analytics/org-daily-stats-query'
import { VIOLATION_TYPE_LABELS } from '@/data/usage/constants'
import { useOrgUsageQuery } from '@/data/usage/org-usage-query'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import { DOCS_URL } from '@/lib/constants'
export const Restriction = () => {
const { data: org } = useSelectedOrganizationQuery()
const { data: usage, isSuccess: isSuccessOrgUsage } = useOrgUsageQuery({ orgSlug: org?.slug })
const pathname = useLocation({ select: (location) => location.pathname })
const isUsagePage = pathname.endsWith('/usage')
const hasExceededAnyLimits = Boolean(
usage?.usages.find(
(metric) =>
metric.metric !== PricingMetric.DISK_SIZE_GB_HOURS_GP3 &&
// Log metrics should not trigger the exceeded limits alert for now (soft rollout)
metric.metric !== PricingMetric.LOG_INGESTION &&
metric.metric !== PricingMetric.LOG_QUERYING &&
!metric.unlimited &&
metric.capped &&
metric.usage > (metric?.pricing_free_units ?? 0)
)
)
// don't show any alerts until everything has been fetched
if (!isSuccessOrgUsage || !org) {
return null
}
let shownAlert: 'exceededLimits' | 'gracePeriod' | 'gracePeriodOver' | 'restricted' | null = null
if (hasExceededAnyLimits && !org?.restriction_status) {
shownAlert = 'exceededLimits'
} else if (org?.restriction_status === 'grace_period') {
shownAlert = 'gracePeriod'
} else if (org?.restriction_status === 'grace_period_over') {
shownAlert = 'gracePeriodOver'
} else if (org?.restriction_status === 'restricted') {
shownAlert = 'restricted'
}
if (shownAlert === null || !org?.restriction_data) {
return null
}
const violationLabels =
Array.isArray(org.restriction_data['violations']) &&
org.restriction_data['violations'].length > 0
? `(${org.restriction_data['violations']
.map((violation: string) => VIOLATION_TYPE_LABELS[violation] || violation)
.join(', ')})`
: ''
return (
<>
{shownAlert === 'exceededLimits' && (
<Alert variant="destructive">
<CriticalIcon />
<AlertTitle>Your organization's usage has exceeded its included quota</AlertTitle>
<AlertDescription>
<p>
Your projects can become unresponsive or enter read-only mode.{' '}
{org.plan.id === 'free' ? (
<span>
Please upgrade to the Pro Plan to ensure that your projects remain available.
</span>
) : (
<span>Please disable spend cap to ensure that your projects remain available.</span>
)}
</p>
<div className="flex items-center gap-x-2 mt-3">
<Button key="upgrade-button" asChild>
<Link
to="/org/$slug/billing"
params={{ slug: org.slug }}
search={{ panel: org.plan.id === 'free' ? 'subscriptionPlan' : 'costControl' }}
>
{org.plan.id === 'free' ? 'Upgrade plan' : 'Change spend cap'}
</Link>
</Button>
{!isUsagePage && (
<Button key="view-usage-button" asChild>
<Link to="/org/$slug/usage" params={{ slug: org.slug }}>
View usage
</Link>
</Button>
)}
<Button asChild icon={<ExternalLinkIcon />}>
<a href={`${DOCS_URL}/guides/platform/cost-control#spend-cap`}>About spend cap</a>
</Button>
</div>
</AlertDescription>
</Alert>
)}
{shownAlert === 'gracePeriod' && (
<Alert variant="warning">
<WarningIcon />
<AlertTitle>Your grace period has started.</AlertTitle>
<AlertDescription>
<p className="leading-tight">
Your organization went over its quota in the previous billing cycle
{violationLabels && <span>{` ${violationLabels}`}</span>}
<span>
. You can continue with your projects until your grace period ends on
</span>{' '}
<span className="text-foreground">
{dayjs(org.restriction_data['grace_period_end']).format('DD MMM, YYYY')}
</span>
<span>
. After that, the Fair Use Policy will apply. If you plan to maintain this level of
usage,
</span>{' '}
{org.plan.id === 'free' ? (
<span>upgrade your plan</span>
) : (
<span>disable spend cap</span>
)}{' '}
<span>
to avoid any restrictions. If restrictions are applied, requests to your projects
will return a 402 status code.
</span>
</p>
<div className="flex items-center gap-x-2 mt-3">
<Button asChild key="upgrade-button">
<Link
to="/org/$slug/billing"
params={{ slug: org.slug }}
search={{
panel: org.plan.id === 'free' ? 'subscriptionPlan' : 'costControl',
source: 'fairUseGracePeriodStarted',
}}
>
{org.plan.id === 'free' ? 'Upgrade plan' : 'Disable spend cap'}
</Link>
</Button>
{!isUsagePage && (
<Button key="view-usage-button" asChild>
<Link to="/org/$slug/usage" params={{ slug: org.slug }}>
View usage
</Link>
</Button>
)}
<Button asChild icon={<ExternalLinkIcon />}>
<a href={`${DOCS_URL}/guides/platform/billing-faq#fair-use-policy`}>
About Fair Use Policy
</a>
</Button>
</div>
</AlertDescription>
</Alert>
)}
{shownAlert === 'gracePeriodOver' && (
<Alert variant="warning">
<WarningIcon />
<AlertTitle>Your grace period is over.</AlertTitle>
<AlertDescription>
<p>
Your grace period ended on{' '}
<span className="text-foreground">
{dayjs(org.restriction_data['grace_period_end']).format('DD MMM, YYYY')}
</span>
. Fair Use Policy applies now. If your organization is over its quota, your projects
can be restricted and requests will respond with a 402 status code.{' '}
{org.plan.id === 'free' ? (
<span>Upgrade your plan</span>
) : (
<span>Disable spend cap</span>
)}{' '}
<span>if you expect to exceed your plan's quota.</span>
</p>
<div className="flex items-center gap-x-2 mt-3">
<Button key="upgrade-button" asChild>
<Link
to="/org/$slug/billing"
params={{ slug: org.slug }}
search={{
panel: org.plan.id === 'free' ? 'subscriptionPlan' : 'costControl',
source: 'fairUseGracePeriodOver',
}}
>
{org.plan.id === 'free' ? 'Upgrade plan' : 'Disable spend cap'}
</Link>
</Button>
{!isUsagePage && (
<Button key="view-usage-button" asChild>
<Link to="/org/$slug/usage" params={{ slug: org.slug }}>
View usage
</Link>
</Button>
)}
<Button asChild icon={<ExternalLinkIcon />}>
<a href={`${DOCS_URL}/guides/platform/billing-faq#fair-use-policy`}>
About Fair Use Policy
</a>
</Button>
</div>
</AlertDescription>
</Alert>
)}
{shownAlert === 'restricted' && (
<Alert variant="destructive">
<CriticalIcon />
<AlertTitle>All services are restricted.</AlertTitle>
<AlertDescription>
<p>
Fair Use Policy applies and your service is restricted. Your projects are not able to
serve requests and will respond with a 402 status code. You have exceeded your plan's
quota{violationLabels && <span>{` ${violationLabels}`}</span>}
<span>.</span>{' '}
{org.plan.id === 'free' ? (
<span>Upgrade your plan</span>
) : (
<span>Disable spend cap</span>
)}{' '}
<span>
to lift restrictions immediately, or wait until the start of your next billing
period. Note that there may be a short delay after your billing period resets before
restrictions are fully lifted.
</span>
</p>
<div className="flex items-center gap-x-2 mt-3">
<Button key="upgrade-button" asChild>
<Link
to="/org/$slug/billing"
params={{ slug: org.slug }}
search={{
panel: org.plan.id === 'free' ? 'subscriptionPlan' : 'costControl',
source: 'fairUseRestricted',
}}
>
{org.plan.id === 'free' ? 'Upgrade plan' : 'Disable spend cap'}
</Link>
</Button>
{!isUsagePage && (
<Button key="view-usage-button" asChild>
<Link to="/org/$slug/usage" params={{ slug: org.slug }}>
View usage
</Link>
</Button>
)}
<Button asChild icon={<ExternalLinkIcon />}>
<a href={`${DOCS_URL}/guides/platform/billing-faq#fair-use-policy`}>
About Fair Use Policy
</a>
</Button>
</div>
</AlertDescription>
</Alert>
)}
</>
)
}