mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 4 of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | 2 | #54054 | `components/ui/**` | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | **4** | **#54056 ← this PR** | **Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor** | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | 6 | #54058 | Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks | Migrates the database, branching, compute, functions, explorer, SQL editor, storage, and table editor interfaces off `next/router` and `next/compat/router`. **Changed:** - Migrated the sole `next/compat/router` consumer (`components/interfaces/Database/Replication/Destinations.tsx`) to `useNavigate` - Replaced `useRouter` across `BranchManagement`, `Branching`, `Compute`, `Database` (`Backups`, `Functions`, `Replication`, `Schemas`, `Tables`, `Triggers`), `EdgeFunctions`, `Explorer`, `Functions`, `SQLEditor`, `Storage` (`AnalyticsBuckets`, `FilesBuckets`, `VectorBuckets`, `StorageMenuV2`, `Storage.utils`), and `TableGridEditor` with TanStack Router hooks (`useNavigate`, `useLocation`, `useRoutePattern`) and `useParams` from `common` - Updated unit and component tests across `Database/Replication`, `Explorer`, `Functions`, and `Storage/FilesBuckets/DeleteBucketModal.test.tsx` to use TanStack Router **Simplified:** - `useSnippetEditor` and `useRedirectLegacyReadReplicaDestination` read search values from `useParams` instead of casting `location.search` - `resolveSourceSwitch` returns `replace: boolean` instead of a `push`/`replace` method name - `ExplorerQueryTab` and `EdgeFunctionDetails` guard on a defined ref instead of navigating with `ref ?? ''` - `useFunctionsDetailShortcuts` and `StorageMenuV2` use a local `goTo` helper for their navigation shortcuts - Tests use the shared `createTestRouter` and `customRender` router, use `toMatchObject` for search assertions, and drop duplicate assertions Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] SQL Editor: creating, renaming, moving, opening templates/examples, and keyboard shortcuts navigate between snippets cleanly - [ ] Table Editor & Database: opening tables, schema table nodes, triggers, functions, backups/PITR, and replication destination flows navigate as expected - [ ] Storage: switching between Files, Analytics, and Vector buckets and deleting a bucket navigates cleanly - [ ] Edge Functions & Explorer: opening function details/logs, explorer chat/query tabs, and branch management actions navigate properly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
169 lines
6.6 KiB
TypeScript
169 lines
6.6 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { useNavigate } from '@tanstack/react-router'
|
|
import { useParams } from 'common'
|
|
import { ExternalLinkIcon, Maximize2Icon, Minimize2Icon, TerminalIcon } from 'lucide-react'
|
|
import { ComponentPropsWithoutRef, ElementRef, forwardRef, useState } from 'react'
|
|
import { Button, Collapsible, CollapsibleContent, CollapsibleTrigger } from 'ui'
|
|
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
|
|
|
|
import type { Commands } from './Functions.types'
|
|
import CommandRender from '@/components/interfaces/Functions/CommandRender'
|
|
import { ApiKeysAccessNote } from '@/components/ui/ApiKeysAccessNote'
|
|
import { DocsButton } from '@/components/ui/DocsButton'
|
|
import { useAccessTokensQuery } from '@/data/access-tokens/access-tokens-query'
|
|
import { apiKeysQueryOptions, getKeys } from '@/data/api-keys/api-keys-query'
|
|
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
|
|
import { DOCS_URL } from '@/lib/constants'
|
|
import { useGrant } from '@/lib/grants'
|
|
|
|
interface TerminalInstructionsProps extends ComponentPropsWithoutRef<typeof Collapsible> {
|
|
closable?: boolean
|
|
removeBorder?: boolean
|
|
}
|
|
|
|
export const TerminalInstructions = forwardRef<
|
|
ElementRef<typeof Collapsible>,
|
|
TerminalInstructionsProps
|
|
>(({ closable = false, removeBorder = false, ...props }, ref) => {
|
|
const navigate = useNavigate()
|
|
const { ref: projectRef } = useParams()
|
|
const [showInstructions, setShowInstructions] = useState(!closable)
|
|
|
|
const { data: tokens } = useAccessTokensQuery()
|
|
const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', { projectRef })
|
|
const { data: apiKeyData } = useQuery(
|
|
apiKeysQueryOptions(
|
|
{ projectRef, grant: apiKeysGrant.grant },
|
|
{
|
|
// A skipped query still returns cached data, so ignore it unless the grant is granted
|
|
select: (data) => getKeys(apiKeysGrant.status === 'granted' ? data : []),
|
|
}
|
|
)
|
|
)
|
|
const { anonKey, publishableKey } = apiKeyData ?? {}
|
|
|
|
const { data: endpoint, isPending: isEndpointPending } = useProjectApiUrl({ projectRef })
|
|
const functionsEndpoint = endpoint ? `${endpoint}/functions/v1` : undefined
|
|
const isFunctionsEndpointLoading = !functionsEndpoint && isEndpointPending
|
|
|
|
const apiKey = publishableKey?.api_key ?? anonKey?.api_key ?? '[YOUR ANON KEY]'
|
|
|
|
const commands: Commands[] = [
|
|
{
|
|
command: 'supabase functions new hello-world',
|
|
description: ' creates a function stub at ./functions/hello-world/index.ts',
|
|
jsx: () => {
|
|
return (
|
|
<>
|
|
<span className="text-brand-600">supabase</span> functions new hello-world
|
|
</>
|
|
)
|
|
},
|
|
comment: 'Create a function',
|
|
},
|
|
{
|
|
command: `supabase functions deploy hello-world --project-ref ${projectRef}`,
|
|
description: 'Deploys function at ./functions/hello-world/index.ts',
|
|
jsx: () => {
|
|
return (
|
|
<>
|
|
<span className="text-brand-600">supabase</span> functions deploy hello-world
|
|
--project-ref {projectRef}
|
|
</>
|
|
)
|
|
},
|
|
comment: 'Deploy your function',
|
|
},
|
|
]
|
|
if (functionsEndpoint) {
|
|
commands.push({
|
|
command: `curl -L -X POST '${functionsEndpoint}/hello-world' -H 'Authorization: Bearer ${apiKey}'${anonKey?.type === 'publishable' ? ` -H 'apikey: ${apiKey}'` : ''} --data '{"name":"Functions"}'`,
|
|
description: 'Invokes the hello-world function',
|
|
jsx: () => {
|
|
return (
|
|
<>
|
|
<span className="text-brand-600">curl</span> -L -X POST '{functionsEndpoint}
|
|
/hello-world' -H 'Authorization: Bearer [YOUR ANON KEY]'
|
|
{anonKey?.type === 'publishable' ? " -H 'apikey: [YOUR ANON KEY]' " : ''}
|
|
{`--data '{"name":"Functions"}'`}
|
|
</>
|
|
)
|
|
},
|
|
comment: 'Invoke your function',
|
|
})
|
|
}
|
|
|
|
return (
|
|
<Collapsible
|
|
ref={ref}
|
|
open={showInstructions}
|
|
className="w-full"
|
|
onOpenChange={() => setShowInstructions(!showInstructions)}
|
|
{...props}
|
|
>
|
|
<CollapsibleTrigger className="flex w-full justify-between" disabled={!closable}>
|
|
<div className="flex items-center gap-x-3">
|
|
<div className="flex items-center justify-center w-8 h-8 p-2 border rounded-sm bg-alternative">
|
|
<TerminalIcon strokeWidth={2} />
|
|
</div>
|
|
<h4>Create your first Edge Function via the CLI</h4>
|
|
</div>
|
|
{closable && (
|
|
<div className="cursor-pointer" onClick={() => setShowInstructions(!showInstructions)}>
|
|
{showInstructions ? (
|
|
<Minimize2Icon size={16} strokeWidth={1.5} />
|
|
) : (
|
|
<Maximize2Icon size={16} strokeWidth={1.5} />
|
|
)}
|
|
</div>
|
|
)}
|
|
</CollapsibleTrigger>
|
|
<CollapsibleContent className="w-full transition-all data-closed:animate-collapsible-up data-open:animate-collapsible-down">
|
|
<div className="my-4 space-y-4">
|
|
<CommandRender commands={commands} />
|
|
{isFunctionsEndpointLoading && <ShimmeringLoader className="w-3/4" />}
|
|
</div>
|
|
<ApiKeysAccessNote
|
|
grant={apiKeysGrant}
|
|
fallback="Replace the placeholder with your own key."
|
|
className="mb-4"
|
|
/>
|
|
{tokens && tokens.length === 0 ? (
|
|
<div className="py-4 space-y-3 border-t">
|
|
<div>
|
|
<p className="text-sm text-foreground">You may need to create an access token</p>
|
|
<p className="text-sm text-foreground-light">
|
|
You can create a secure access token in your account section
|
|
</p>
|
|
</div>
|
|
<Button onClick={() => void navigate({ to: '/account/tokens' })}>Access tokens</Button>
|
|
</div>
|
|
) : (
|
|
<div className="py-4 space-y-3 border-t">
|
|
<div>
|
|
<h3 className="text-base text-foreground">Need help?</h3>
|
|
<p className="text-sm text-foreground-light">
|
|
Read the documentation, or browse some sample code.
|
|
</p>
|
|
</div>
|
|
<div className="flex gap-2">
|
|
<DocsButton href={`${DOCS_URL}/guides/functions`} />
|
|
<Button asChild icon={<ExternalLinkIcon />}>
|
|
<a
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
href="https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions"
|
|
>
|
|
Examples
|
|
</a>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</CollapsibleContent>
|
|
</Collapsible>
|
|
)
|
|
})
|
|
|
|
TerminalInstructions.displayName = 'TerminalInstructions'
|