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

280 lines
12 KiB
TypeScript

import { AlertTriangleIcon, BarChart2Icon } from 'lucide-react'
import { useMemo } from 'react'
import { Button, cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { SectionContent } from '../SectionContent'
import { CategoryAttribute } from '../Usage.constants'
import {
ChartTooltipValueFormatter,
ChartYFormatterCompactNumber,
useGetUpgradeUrl,
} from '../Usage.utils'
import UsageBarChart from '../UsageBarChart'
import { ChartMeta } from './UsageSection'
import { AlertError } from '@/components/ui/AlertError'
import { DynamicLink } from '@/components/ui/DynamicLink'
import Panel from '@/components/ui/Panel'
import { SparkBar } from '@/components/ui/SparkBar'
import type { OrgSubscription } from '@/data/subscriptions/types'
import type { OrgMetricsUsage, OrgUsageResponse } from '@/data/usage/org-usage-query'
import { USAGE_APPROACHING_THRESHOLD } from '@/lib/constants'
import type { StudioError } from '@/lib/errors/studio-errors'
export interface AttributeUsageProps {
slug: string
projectRef?: string | null
attribute: CategoryAttribute
usage?: OrgUsageResponse
usageMeta?: OrgMetricsUsage
chartMeta: ChartMeta
subscription?: OrgSubscription
error: StudioError | null
isLoading: boolean
isError: boolean
isSuccess: boolean
currentBillingCycleSelected: boolean
}
const AttributeUsage = ({
slug,
projectRef,
attribute,
usage,
usageMeta,
chartMeta,
subscription,
error,
isLoading,
isError,
isSuccess,
currentBillingCycleSelected,
}: AttributeUsageProps) => {
const upgradeUrl = useGetUpgradeUrl(slug ?? '', subscription, attribute.key)
const usageRatio = (usageMeta?.usage ?? 0) / (usageMeta?.pricing_free_units ?? 0)
const usageExcess = (usageMeta?.usage ?? 0) - (usageMeta?.pricing_free_units ?? 0)
const usageBasedBilling = subscription?.usage_billing_enabled
const exceededLimitStyle = !usageBasedBilling ? 'text-red-900' : 'text-amber-900'
const chartData = useMemo(() => chartMeta[attribute.key]?.data ?? [], [attribute.key, chartMeta])
const showUsageWarning =
projectRef === undefined &&
currentBillingCycleSelected &&
usageBasedBilling === false &&
usageRatio >= USAGE_APPROACHING_THRESHOLD
const notAllValuesZero = useMemo(() => {
return (
attribute.attributes
?.map((attr) => {
return chartData.some((dataPoint) => Number(dataPoint[attr.key]) !== 0)
})
.some((x) => !!x) ?? false
)
}, [attribute.attributes, chartData])
return (
<div id={attribute.anchor} className="scroll-my-12">
<SectionContent section={attribute}>
{isLoading && (
<div className="space-y-2">
<ShimmeringLoader />
<ShimmeringLoader className="w-3/4" />
<ShimmeringLoader className="w-1/2" />
</div>
)}
{isError && <AlertError subject="Failed to retrieve usage data" error={error} />}
{isSuccess && (
<>
{!usageMeta || usageMeta?.available_in_plan ? (
<>
{!projectRef && (
<div className="space-y-2">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-4">
<p className="text-sm">{attribute.name} usage</p>
{showUsageWarning && (
<Tooltip>
<TooltipTrigger asChild>
{usageRatio >= 1 ? (
<div className="flex items-center space-x-2 min-w-[115px] cursor-help">
<AlertTriangleIcon
size={14}
strokeWidth={2}
className={exceededLimitStyle}
/>
<p className={`text-sm ${exceededLimitStyle}`}>Exceeded limit</p>
</div>
) : (
usageRatio >= USAGE_APPROACHING_THRESHOLD && (
<div className="flex items-center space-x-2 min-w-[115px] cursor-help">
<AlertTriangleIcon
size={14}
strokeWidth={2}
className="text-amber-900"
/>
<p className="text-sm text-amber-900">Approaching limit</p>
</div>
)
)}
</TooltipTrigger>
<TooltipContent side="bottom">
<p>
Exceeding your plans included usage will lead to restrictions to
your project.
</p>
<p>
Upgrade to a usage-based plan or disable the spend cap to avoid
restrictions.
</p>
</TooltipContent>
</Tooltip>
)}
</div>
</div>
{currentBillingCycleSelected &&
usageMeta &&
usageMeta.capped &&
!usageMeta.unlimited && (
<SparkBar
type="horizontal"
barClass={cn(
usageRatio >= 1
? usageBasedBilling
? 'bg-foreground-light'
: 'bg-red-900'
: usageBasedBilling === false &&
usageRatio >= USAGE_APPROACHING_THRESHOLD
? 'bg-amber-900'
: 'bg-foreground-light'
)}
bgClass="bg-surface-300"
value={usageMeta?.usage ?? 0}
max={usageMeta?.pricing_free_units || 1}
/>
)}
<div>
{usageMeta && usageMeta.pricing_free_units !== 0 && (
<div className="flex items-center justify-between border-b py-1">
<p className="text-xs text-foreground-light">
Included in {subscription?.plan?.name} Plan
</p>
{usageMeta.unlimited ? (
<p className="text-xs">Unlimited</p>
) : (
<p className="text-xs">
{attribute.unit === 'bytes' || attribute.unit === 'gigabytes'
? `${usageMeta.pricing_free_units?.toLocaleString() ?? 0} GB`
: (usageMeta.pricing_free_units ?? 0).toLocaleString()}
</p>
)}
</div>
)}
{currentBillingCycleSelected && usageMeta && (
<div className="flex items-center justify-between py-1">
<p className="text-xs text-foreground-light">
{attribute.chartPrefix || <span>{'Used '}</span>} <span>in period</span>
</p>
<p className="text-xs">
{attribute.unit === 'bytes' || attribute.unit === 'gigabytes'
? `${(usageMeta?.usage ?? 0).toFixed(2)} GB`
: (usageMeta?.usage ?? 0).toLocaleString()}
</p>
</div>
)}
{currentBillingCycleSelected && (usageMeta?.pricing_free_units ?? 0) > 0 && (
<div className="flex items-center justify-between border-t py-1">
<p className="text-xs text-foreground-light">Overage in period</p>
<p className="text-xs">
{(usageMeta?.pricing_free_units ?? 0) === -1 || usageExcess < 0
? `0${attribute.unit === 'bytes' || attribute.unit === 'gigabytes' ? ' GB' : ''}`
: attribute.unit === 'bytes' || attribute.unit === 'gigabytes'
? `${usageExcess.toFixed(2)} GB`
: usageExcess.toLocaleString()}
</p>
</div>
)}
</div>
</div>
)}
{attribute.additionalInfo?.(usage)}
<div className="space-y-1">
<p className="text-sm">
{attribute.chartPrefix || ''} {attribute.name}{' '}
{attribute.chartSuffix || <span>per day</span>}
</p>
{attribute.chartDescription.split('\n').map((paragraph, idx) => (
<p key={`para-${idx}`} className="text-sm text-foreground-light">
{paragraph}
</p>
))}
</div>
{chartMeta[attribute.key].isLoading ? (
<div className="space-y-2">
<ShimmeringLoader />
<ShimmeringLoader className="w-3/4" />
<ShimmeringLoader className="w-1/2" />
</div>
) : chartData.length > 0 && notAllValuesZero ? (
<UsageBarChart
name={`${attribute.chartPrefix || ''} ${attribute.name}`}
unit={attribute.unit}
attributes={attribute.attributes}
data={chartData}
yLeftMargin={chartMeta[attribute.key].margin}
yFormatter={(value) => ChartYFormatterCompactNumber(value, attribute.unit)}
tooltipFormatter={(value) => ChartTooltipValueFormatter(value, attribute.unit)}
/>
) : (
<Panel>
<Panel.Content>
<div className="flex flex-col items-center justify-center">
<BarChart2Icon className="text-foreground-light mb-2" />
<p className="text-sm">No data in period</p>
<p className="text-sm text-foreground-light">
May take up to 24 hours to show
</p>
</div>
</Panel.Content>
</Panel>
)}
</>
) : (
<Panel>
<Panel.Content>
<div className="flex w-full items-center flex-col justify-center space-y-2 md:flex-row md:justify-between">
<div className="space-y-1">
<p className="text-sm">Not included in plan</p>
<div>
<p className="text-sm text-foreground-light">
You need to be on a higher plan in order to use this feature.
</p>
</div>
</div>
<Button variant="primary" asChild>
<DynamicLink href={upgradeUrl}>Upgrade plan</DynamicLink>
</Button>
</div>
</Panel.Content>
</Panel>
)}
</>
)}
</SectionContent>
</div>
)
}
export default AttributeUsage