Files
Alaister YoungandAlaister Young e0635f2f38 [FE-5108] (part 3) refactor(studio): migrate database, editors & storage off next/link (#54306)
Part 3 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 ← this PR** | **Database, TableGridEditor, Storage,
SQLEditor, BranchManagement, Branching, Functions, Realtime** |
| 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues,
Linter, Reports, QueryPerformance, QuerySources, ConfigDrift,
DiskManagement, Compute |
| 5 | #54308 | 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
`components/interfaces/{Database,TableGridEditor,Storage,SQLEditor,BranchManagement,Branching,Functions,Realtime}/`
off `next/link`.

**Changed:**

- Replaced `next/link` across all 60 importing files in Database,
TableGridEditor, Storage, SQLEditor, BranchManagement, Branching,
Functions, and Realtime interfaces
- Updated legacy permanent-redirect hrefs (`/reports` →
`/observability`, `/database/schema` → `/database/schemas`) to their
canonical TanStack routes
- Dropped legacy `passHref` in `GridHeaderActions.tsx`, removed
duplicate `RouterLink` alias in `ChooseFunctionForm.tsx`, and removed
`vi.mock('next/link', ...)` in `ReplicationPipelineLayout.test.tsx`

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

## To test

- [ ] Check Table Editor, SQL Editor, Database (tables, policies,
functions, triggers, replication, backups), Storage
(files/analytics/vector buckets), Edge Functions, Branching, and
Realtime links
- [ ] Verify SQL Editor and Table Editor links with query parameters
(`schema`, `content`, `search`) do not double-encode values

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 16:43:56 +08:00

269 lines
9.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { Link, useNavigate } from '@tanstack/react-router'
import { LOCAL_STORAGE_KEYS, useParams } from 'common'
import dayjs from 'dayjs'
import { GitBranchIcon, GitMergeIcon, MoreVerticalIcon, ShieldIcon } from 'lucide-react'
import { useMemo } from 'react'
import { toast } from 'sonner'
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from 'ui'
import { TimestampInfo } from 'ui-patterns/TimestampInfo'
import { useIsPgDeltaDiffEnabled } from '../App/FeaturePreview/FeaturePreviewContext'
import { ReviewWithAI } from '../BranchManagement/ReviewWithAI'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { FeaturePreviewBadge } from '@/components/ui/FeaturePreviewBadge'
import { useBranchUpdateMutation } from '@/data/branches/branch-update-mutation'
import { branchesQueryOptions } from '@/data/branches/branches-query'
import { useProjectGitHubConnectionQuery } from '@/data/integrations/github-connections-query'
import { useProjectDetailQuery } from '@/data/projects/project-detail-query'
import { useBranchGrant } from '@/hooks/branches/useBranchGrant'
import { useBranchMergeDiff } from '@/hooks/branches/useBranchMergeDiff'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useGrant } from '@/lib/grants'
import { navigateToUrl } from '@/lib/navigation'
import { useTrack } from '@/lib/telemetry/track'
export const MergeTitle = () => {
const { ref } = useParams()
const { data: project } = useSelectedProjectQuery()
const pgDeltaDiffEnabled = useIsPgDeltaDiffEnabled()
const parentProjectRef = project?.parent_project_ref
const branchesGrant = useGrant('GET /v1/projects/{ref}/branches', {
projectRef: parentProjectRef,
})
const { data: branches } = useQuery(
branchesQueryOptions(
{ projectRef: parentProjectRef, grant: branchesGrant.grant },
{ refetchOnMount: 'always', refetchOnWindowFocus: true, staleTime: 0 }
)
)
const currentBranch = branches?.find((branch) => branch.project_ref === ref)
const mainBranch = branches?.find((branch) => branch.is_default)
return (
<div className="flex items-center gap-x-4">
<div className="flex items-center gap-x-2">
<span>Merge</span>
<code className="flex items-center gap-x-1.5 px-2 py-1 border border-border">
<GitBranchIcon strokeWidth={1.5} size={14} className="text-foreground-lighter" />
{currentBranch?.name}
</code>
<span>into</span>
<Link
to="/project/$ref"
params={{ ref: mainBranch?.project_ref ?? '_' }}
className="font-mono inline-flex gap-4"
>
<code className="flex items-center font-mono gap-x-1.5 px-2 py-1 border border-border">
<ShieldIcon strokeWidth={1.5} size={14} className="text-warning" />
{mainBranch?.name || <span>main</span>}
</code>
</Link>
</div>
{pgDeltaDiffEnabled && (
<FeaturePreviewBadge featureKey={LOCAL_STORAGE_KEYS.UI_PREVIEW_PG_DELTA_DIFF} />
)}
</div>
)
}
export const MergeSubtitle = () => {
const { ref } = useParams()
const { data: project } = useSelectedProjectQuery()
const parentProjectRef = project?.parent_project_ref
const branchesGrant = useGrant('GET /v1/projects/{ref}/branches', {
projectRef: parentProjectRef,
})
const { data: branches } = useQuery(
branchesQueryOptions(
{ projectRef: parentProjectRef, grant: branchesGrant.grant },
{ refetchOnMount: 'always', refetchOnWindowFocus: true, staleTime: 0 }
)
)
const currentBranch = branches?.find((branch) => branch.project_ref === ref)
const subtitle = useMemo(() => {
if (!currentBranch?.created_at) return 'Branch information unavailable'
if (!currentBranch?.review_requested_at) {
return 'Not ready for review'
}
const reviewRequestedTime = dayjs(currentBranch.review_requested_at).fromNow()
return (
<>
Request opened{' '}
<TimestampInfo
className="text-sm"
utcTimestamp={currentBranch.review_requested_at}
label={reviewRequestedTime}
/>
</>
)
}, [currentBranch?.created_at, currentBranch?.review_requested_at])
return <p className="text-foreground-lighter text-sm">{subtitle}</p>
}
export const MergeActions = ({
isWorkflowRunning,
isSubmitting,
canMerge,
onSelectMerge,
}: {
isWorkflowRunning: boolean
isSubmitting: boolean
canMerge: boolean
onSelectMerge: () => void
}) => {
const navigate = useNavigate()
const { ref } = useParams()
const { data: project } = useSelectedProjectQuery()
const track = useTrack()
const { mutate: updateBranch, isPending: isUpdating } = useBranchUpdateMutation({
onError: (error) => {
toast.error(`Failed to update branch: ${error.message}`)
},
})
const parentProjectRef = project?.parent_project_ref
const { data: parentProject } = useProjectDetailQuery({
ref: parentProjectRef,
})
const { data: ghConnection } = useProjectGitHubConnectionQuery({
ref: parentProjectRef,
})
const branchesGrant = useGrant('GET /v1/projects/{ref}/branches', {
projectRef: parentProjectRef,
})
const { data: branches } = useQuery(
branchesQueryOptions(
{ projectRef: parentProjectRef, grant: branchesGrant.grant },
{ refetchOnMount: 'always', refetchOnWindowFocus: true, staleTime: 0 }
)
)
const currentBranch = branches?.find((branch) => branch.project_ref === ref)
const mainBranch = branches?.find((branch) => branch.is_default)
const updateBranchGrant = useBranchGrant('PATCH /v1/branches/{branch_id_or_ref}', {
projectRef: parentProjectRef,
isDefault: currentBranch?.is_default,
})
const {
diffContent,
isBranchOutOfDateOverall,
isLoading: isCombinedDiffLoading,
hasChanges: combinedHasChanges,
} = useBranchMergeDiff({
currentBranchRef: ref,
isCurrentBranchDefault: currentBranch?.is_default,
parentProjectRef,
currentBranchConnectionString: project?.connectionString || undefined,
parentBranchConnectionString: parentProject?.connectionString || undefined,
currentBranchCreatedAt: currentBranch?.created_at,
})
const isMergeDisabled =
!canMerge ||
!combinedHasChanges ||
isCombinedDiffLoading ||
isBranchOutOfDateOverall ||
isWorkflowRunning ||
(!!ghConnection && Boolean(mainBranch?.git_branch))
return (
<div className="flex items-end gap-2">
<ReviewWithAI
currentBranch={currentBranch}
mainBranch={mainBranch}
parentProjectRef={parentProjectRef}
diffContent={diffContent}
disabled={!currentBranch || !mainBranch || isCombinedDiffLoading}
/>
{isMergeDisabled ? (
<ButtonTooltip
tooltip={{
content: {
side: 'bottom',
text: !canMerge
? 'You need additional permissions to merge branches'
: !combinedHasChanges
? 'No changes to merge'
: isWorkflowRunning
? 'Workflow is currently running'
: !!ghConnection && Boolean(mainBranch?.git_branch)
? 'Deploy to production from GitHub is enabled'
: 'Unable to merge at this time',
},
}}
variant="primary"
loading={isSubmitting}
disabled={isMergeDisabled}
onClick={onSelectMerge}
icon={<GitMergeIcon size={16} strokeWidth={1.5} className="text-primary" />}
>
Merge branch
</ButtonTooltip>
) : (
<Button
variant="primary"
loading={isSubmitting}
onClick={onSelectMerge}
icon={<GitMergeIcon size={16} strokeWidth={1.5} className="text-primary" />}
>
Merge branch
</Button>
)}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button loading={isUpdating} className="px-1.5" icon={<MoreVerticalIcon />} />
</DropdownMenuTrigger>
<DropdownMenuContent side="bottom" align="end" className="w-52">
<DropdownMenuItem
className="gap-x-2"
disabled={updateBranchGrant.status !== 'granted'}
onClick={() => {
if (!ref || !parentProjectRef || updateBranchGrant.status !== 'granted') return
updateBranch(
{
grant: updateBranchGrant.grant,
branchRef: ref,
projectRef: parentProjectRef,
requestReview: false,
},
{
onSuccess: () => {
toast.success('Successfully closed merge request')
void navigateToUrl(navigate, `/project/${project?.ref}/branches?tab=prs`)
track('branch_close_merge_request_button_clicked', undefined, {
project: parentProjectRef,
})
},
}
)
}}
>
Close this merge request
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
)
}