Files
Alaister YoungandAlaister Young 680633ee4f [FE-5108] (part 5) refactor(studio): migrate remaining interfaces off next/link (#54308)
Part 5 of a 6-PR stack migrating all `next/link` imports in
`apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known
external URLs) and removing `compat/next/link.tsx`.

| Part | PR | Scope |
| --- | --- | --- |
| 1 | #54304 | `data/prefetchers/PrefetchableLink.tsx` and
`components/ui/**` |
| 2 | #54305 | `components/layouts/**` and top-level shell components |
| 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor,
BranchManagement, Branching, Functions, Realtime |
| 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues,
Linter, Reports, QueryPerformance, QuerySources, ConfigDrift,
DiskManagement, Compute |
| **5** | **#54308 ← this PR** | **Remaining `components/interfaces/**`
(Org, Billing, Auth, Support, Home, SignIn, ConnectSheet, etc.)** |
| 6 | #54309 | Remove `compat/next/link.tsx` shim, Vite alias, and
update basePath tests/docs |

Migrates the remaining `components/interfaces/**` directories off
`next/link`, leaving zero `next/link` component imports in
`apps/studio`.

**Changed:**

- Replaced `next/link` across all 73 remaining interface files
(`Organization`, `OrganizationInvite`, `Billing`, `Auth`, `Support`,
`ProjectHome`, `Home`, `Project`, `ProjectCreation`, `ProjectAPIDocs`,
`ApiAuthorization`, `App`, `ConnectSheet`, `SignIn`, `APIKeys`,
`Account`, `CliLogin`, `Docs`, `JwtSecrets`, `Platform`)
- Converted the sole remaining `UrlObject` link (`<Link href={{
pathname: '/sign-in-sso', query }}>`) in `SignInSSOForm.tsx` to `<Link
to="/sign-in-sso" search={query}>`
- Migrated `SupportLink.tsx` onto `DynamicLink`
(`createSupportFormUrl(...)`)
- Standardized external `target="_blank"` anchors with `rel="noreferrer
noopener"`, switched `OverviewMetrics.tsx` observability link to typed
`<Link to="/project/$ref/observability/auth">`, and adopted `InlineLink`
/ `InlineLinkClassName` on inline paragraph links per review feedback

Addresses
[FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link)

## To test

- [ ] Check Organization (billing, usage, team, documents, AWS
Marketplace), Auth (providers, templates, rate limits, users, OAuth
apps), Support form/links, Project Home, Connect sheet, and Sign-In/SSO
links
- [ ] Verify `SignInWithSSOButton` preserves query params when
navigating to `/sign-in-sso`
- [ ] Verify `SupportLink` pre-populates support form query parameters
cleanly

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 17:33:03 +08:00

279 lines
11 KiB
TypeScript

import { Link } from '@tanstack/react-router'
import { ChevronRightIcon } from 'lucide-react'
import { useMemo } from 'react'
import { cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import { billingMetricUnit, formatUsage } from '../helpers'
import { Metric, USAGE_APPROACHING_THRESHOLD } from './BillingBreakdown.constants'
import { UsageProjectName } from '@/components/interfaces/Organization/Usage/UsageProjectName'
import { InlineLink } from '@/components/ui/InlineLink'
import { UpgradePlanButton } from '@/components/ui/UpgradePlanButton'
import { PricingMetric } from '@/data/analytics/org-daily-stats-query'
import type { OrgSubscription } from '@/data/subscriptions/types'
import type { OrgUsageResponse } from '@/data/usage/org-usage-query'
import { formatCurrency } from '@/lib/helpers'
export interface BillingMetricProps {
idx: number
slug?: string
metric: Metric
usage: OrgUsageResponse
subscription: OrgSubscription
relativeToSubscription: boolean
projectRef?: string | null
}
export const BillingMetric = ({
slug,
metric,
usage,
subscription,
relativeToSubscription,
projectRef,
}: BillingMetricProps) => {
const usageMeta = usage.usages.find((x) => x.metric === metric.key)
const usageLabel = useMemo(() => {
if (!usageMeta) return ''
if (relativeToSubscription && usageMeta.available_in_plan === false) {
return 'Unavailable in plan'
} else if (
(usageMeta.cost && usageMeta.cost > 0) ||
!relativeToSubscription ||
usageMeta.unlimited ||
usageMeta.pricing_free_units === 0
) {
return metric.units === 'bytes' || metric.units === 'gigabytes'
? `${usageMeta.usage.toLocaleString() ?? 0} GB`
: usageMeta.usage.toLocaleString() + (metric.unitName ? ` ${metric.unitName}` : '')
} else {
return metric.units === 'bytes' || metric.units === 'gigabytes'
? `${usageMeta.usage.toLocaleString() ?? 0} / ${usageMeta.pricing_free_units?.toLocaleString() ?? 0} GB`
: `${usageMeta.usage.toLocaleString()} / ${usageMeta.pricing_free_units?.toLocaleString()}` +
(metric.unitName ? ` ${metric.unitName}` : '')
}
}, [usageMeta, relativeToSubscription, metric])
const isLogMetricOnNonPlatformPlan =
(metric.key === PricingMetric.LOG_INGESTION || metric.key === PricingMetric.LOG_QUERYING) &&
subscription?.plan.id !== 'platform'
const sortedProjectAllocations = useMemo(() => {
if (!usageMeta || !usageMeta.project_allocations) return []
return usageMeta.project_allocations.sort((a, b) => b.usage - a.usage)
}, [usageMeta])
if (!usageMeta) return null
const usageRatio =
usageMeta.usage === 0 ? 0 : usageMeta.usage / (usageMeta.pricing_free_units ?? 0)
const isUsageBillingEnabled = subscription?.usage_billing_enabled === true
const hasLimit = !!usageMeta.unlimited === false
const isApproachingLimit = hasLimit && usageRatio >= USAGE_APPROACHING_THRESHOLD
const isExceededLimit = relativeToSubscription && hasLimit && usageRatio >= 1
const unit = billingMetricUnit(usageMeta.metric as PricingMetric)
const percentageLabel =
usageMeta.usage === 0 || usageMeta.pricing_free_units === 0
? ''
: usageRatio < 0.01
? '(<1%)'
: `(${(+(usageRatio * 100).toFixed(0)).toLocaleString()}%)`
return (
<Tooltip delayDuration={100}>
<TooltipTrigger asChild>
<div className={cn('flex items-center justify-between')}>
{metric.anchor ? (
<Link
to="/org/$slug/usage"
params={{ slug: slug ?? '_' }}
hash={metric.anchor}
className="block w-full group"
>
<div className="group flex items-center gap-1">
<p className="text-sm text-foreground-light group-hover:text-foreground transition cursor-pointer items-center">
<span>{metric.name}</span>
</p>
{usageMeta.available_in_plan && (
<span className="text-foreground-muted transition inline-block group-hover:transform group-hover:translate-x-0.5">
<ChevronRightIcon strokeWidth={1.5} size={16} className="transition" />
</span>
)}
</div>
<span className="text-sm">{usageLabel}</span>&nbsp;
{relativeToSubscription && usageMeta.cost && usageMeta.cost > 0 ? (
<span
className={cn('text-sm', isLogMetricOnNonPlatformPlan && 'line-through')}
translate="no"
>
({formatCurrency(usageMeta.cost)})
</span>
) : usageMeta.available_in_plan &&
usageMeta.pricing_free_units !== 0 &&
!usageMeta.unlimited &&
relativeToSubscription ? (
<span className="text-sm">{percentageLabel}</span>
) : null}
</Link>
) : (
<div className="block w-full">
<p className="text-sm text-foreground-light flex space-x-1">{metric.name}</p>
<span className="text-sm">{usageLabel}</span>&nbsp;
{relativeToSubscription && usageMeta.cost && usageMeta.cost > 0 ? (
<span className="text-sm" translate="no">
({formatCurrency(usageMeta.cost)})
</span>
) : usageMeta.available_in_plan &&
usageMeta.pricing_free_units !== 0 &&
!usageMeta.unlimited &&
relativeToSubscription ? (
<span className="text-sm">{percentageLabel}</span>
) : null}
</div>
)}
{usageMeta.available_in_plan ? (
<div>
{relativeToSubscription &&
!usageMeta.unlimited &&
usageMeta.pricing_free_units !== 0 ? (
<svg className="h-8 w-8 -rotate-90 transform">
<circle
cx={15}
cy={15}
r={12}
fill="transparent"
stroke="currentColor"
strokeWidth={4}
className="text-background-surface-300"
/>
<circle
cx={15}
cy={15}
r={12}
fill="transparent"
stroke="currentColor"
strokeDasharray={75.398}
strokeDashoffset={`calc(75.39822 - ${
usageRatio < 1 ? usageRatio * 100 : 100
} / 100 * 75.39822)`}
strokeWidth={4}
className={
isUsageBillingEnabled
? 'text-foreground-lighter'
: isExceededLimit
? 'text-red-900'
: isApproachingLimit
? 'text-yellow-1000'
: 'text-foreground-lighter'
}
/>
</svg>
) : null}
</div>
) : (
<div>
<UpgradePlanButton
source={`billingBreakdownUsage${metric.anchor}`}
featureProposition={`to use ${metric.name}`}
>
Upgrade
</UpgradePlanButton>
</div>
)}
</div>
</TooltipTrigger>
{usageMeta.available_in_plan && (
<TooltipContent side="bottom" align="start" className="max-w-[400px]" alignOffset={-15}>
<div className="text-xs flex flex-col gap-y-2">
<p className="font-medium" translate="no">
{usageMeta.unit_price_desc}
</p>
{metric.tip && (
<p className="text-foreground-light">
{metric.tip}{' '}
{metric.docLink && (
<InlineLink href={metric.docLink.url}>{metric.docLink.title}</InlineLink>
)}
</p>
)}
{metric.key === PricingMetric.LOG_QUERYING && isLogMetricOnNonPlatformPlan ? (
<p className="text-foreground-light">
We won't enforce this metric until known mis-reporting issues are resolved.{' '}
<InlineLink href="https://github.com/orgs/supabase/discussions/50925">
Share feedback on GitHub.
</InlineLink>
</p>
) : (
<>
{isLogMetricOnNonPlatformPlan && (
<p className="text-foreground-light">
Billing and enforcement will only start after the grace period ends in early
2027.
</p>
)}
{subscription.usage_billing_enabled === false &&
relativeToSubscription &&
(isApproachingLimit || isExceededLimit) && (
<p className="text-foreground-light">
{isLogMetricOnNonPlatformPlan
? 'Enforcement will only start after the grace period ends in early 2027. Reduce your usage, upgrade to a usage-based plan or disable the spend cap to avoid restrictions.'
: 'Exceeding your plans included usage will lead to restrictions to your project. Upgrade to a usage-based plan or disable the spend cap to avoid restrictions.'}
</p>
)}
</>
)}
{sortedProjectAllocations && sortedProjectAllocations.length > 0 && (
<table className="list-disc w-full">
<thead>
<tr>
<th className="text-left">Project</th>
<th className="text-right">Usage</th>
</tr>
</thead>
<tbody className="[&>tr:last-child>td]:pb-1">
{sortedProjectAllocations.map((allocation) => (
<tr key={`${usageMeta.metric}_${allocation.ref}`}>
<td className="text-foreground-light">
<UsageProjectName
name={allocation.name}
projectRef={allocation.ref}
selectedProjectRef={projectRef}
/>
</td>
<td className="text-foreground-light text-right">
{formatUsage(usageMeta.metric as PricingMetric, allocation)}
</td>
</tr>
))}
</tbody>
<tfoot>
<tr>
<td className="py-1 border-t text-left">
Total{unit && <span> ({unit})</span>}
</td>
<td className="py-1 border-t text-right">
{formatUsage(usageMeta.metric as PricingMetric, {
usage: usageMeta.usage_original,
})}{' '}
</td>
</tr>
</tfoot>
</table>
)}
</div>
</TooltipContent>
)}
</Tooltip>
)
}