Files
supabase/apps/studio/components/interfaces/Database/Triggers/ChooseFunctionForm.tsx
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

179 lines
5.8 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { Link } from '@tanstack/react-router'
import { useParams } from 'common'
import { map as lodashMap, uniqBy } from 'lodash'
import { HelpCircleIcon, TerminalIcon } from 'lucide-react'
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, Button, SidePanel } from 'ui'
import { EmptyStatePresentational } from 'ui-patterns/EmptyStatePresentational'
import { CodeEditor } from '@/components/ui/CodeEditor/CodeEditor'
import InformationBox from '@/components/ui/InformationBox'
import {
databaseFunctionsQueryOptions,
type DatabaseFunction,
} from '@/data/database-functions/database-functions-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useGrant } from '@/lib/grants'
export interface ChooseFunctionFormProps {
visible: boolean
setVisible: (value: boolean) => void
onChange: (fn: DatabaseFunction) => void
}
export const ChooseFunctionForm = ({ visible, onChange, setVisible }: ChooseFunctionFormProps) => {
const { data: project } = useSelectedProjectQuery()
const readGrant = useGrant(
'POST /platform/pg-meta/{ref}/query',
{ projectRef: project?.ref },
{ variant: 'read' }
)
const { data = [] } = useQuery(
databaseFunctionsQueryOptions({
projectRef: project?.ref,
connectionString: project?.connectionString,
grant: readGrant.grant,
})
)
const triggerFunctions = data.filter((fn) => fn.return_type === 'trigger')
const hasPublicSchemaFunctions = triggerFunctions.length >= 1
const functionSchemas = lodashMap(uniqBy(triggerFunctions, 'schema'), 'schema')
const selectFunction = (id: number) => {
const fn = triggerFunctions.find((x) => x.id === id)
if (!!fn) onChange(fn)
setVisible(!visible)
}
return (
<SidePanel
size="large"
header="Pick a function"
visible={visible}
onCancel={() => setVisible(!visible)}
>
<div className="py-6">
{hasPublicSchemaFunctions ? (
<div className="space-y-6">
<NoticeBox />
{functionSchemas.map((schema: string) => (
<SchemaFunctionGroup
key={schema}
schema={schema}
functions={triggerFunctions.filter((x) => x.schema == schema)}
selectFunction={selectFunction}
/>
))}
</div>
) : (
<div className="px-6">
<EmptyStatePresentational
icon={TerminalIcon}
title="No trigger functions found"
description="You will need to create a trigger function before you can add it to your trigger."
>
<Button asChild variant="default">
<Link to="/project/$ref/database/functions" params={{ ref: project?.ref ?? '' }}>
Create a trigger function
</Link>
</Button>
</EmptyStatePresentational>
</div>
)}
</div>
</SidePanel>
)
}
const NoticeBox = () => {
const { ref } = useParams()
return (
<div className="px-6">
<InformationBox
icon={<HelpCircleIcon size="20" strokeWidth={1.5} />}
title="Only functions that return a trigger will be displayed below"
description={`You can make functions by using the Database Functions`}
button={
<Button asChild>
<Link to="/project/$ref/database/functions" params={{ ref: ref ?? '_' }}>
Go to Functions
</Link>
</Button>
}
/>
</div>
)
}
export interface SchemaFunctionGroupProps {
schema: string
functions: DatabaseFunction[]
selectFunction: (id: number) => void
}
const SchemaFunctionGroup = ({ schema, functions, selectFunction }: SchemaFunctionGroupProps) => {
return (
<div className="space-y-4">
<div className="sticky top-0 flex items-center space-x-1 px-6 backdrop-blur-sm backdrop-filter">
<h5 className="text-foreground-light">schema</h5>
<h5>{schema}</h5>
</div>
<div className="space-y-0 divide-y border-t border-b border-default">
{functions.map((x) => (
<Function
id={x.id}
key={x.id}
completeStatement={x.complete_statement}
name={x.name}
onClick={selectFunction}
/>
))}
</div>
</div>
)
}
export interface FunctionProps {
id: number
completeStatement: string
name: string
onClick: (id: number) => void
}
const Function = ({ id, completeStatement, name, onClick }: FunctionProps) => {
return (
<div className="cursor-pointer rounded-sm p-3 px-6 hover:bg-studio" onClick={() => onClick(id)}>
<Accordion type="single" collapsible>
<AccordionItem value="definition" className="border-none">
<div className="flex items-center justify-between space-x-3">
<div className="flex items-center space-x-3">
<div className="flex items-center justify-center rounded-sm bg-foreground p-1 text-background">
<TerminalIcon strokeWidth={2} size={14} />
</div>
<p className="mb-0 text-sm">{name}</p>
</div>
<AccordionTrigger
onClick={(e) => e.stopPropagation()}
className="py-0 text-xs font-normal text-foreground-light hover:no-underline"
>
View definition
</AccordionTrigger>
</div>
<AccordionContent className="[&>div]:pb-0" onClick={(e) => e.stopPropagation()}>
<div className="mt-4 h-64 border border-default">
<CodeEditor
isReadOnly
language="pgsql"
defaultValue={completeStatement}
options={{ contextmenu: false }}
/>
</div>
</AccordionContent>
</AccordionItem>
</Accordion>
</div>
)
}