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 { closable?: boolean removeBorder?: boolean } export const TerminalInstructions = forwardRef< ElementRef, 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 ( <> supabase 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 ( <> supabase 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 ( <> curl -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 ( setShowInstructions(!showInstructions)} {...props} >

Create your first Edge Function via the CLI

{closable && (
setShowInstructions(!showInstructions)}> {showInstructions ? ( ) : ( )}
)}
{isFunctionsEndpointLoading && }
{tokens && tokens.length === 0 ? (

You may need to create an access token

You can create a secure access token in your account section

) : (

Need help?

Read the documentation, or browse some sample code.

)}
) }) TerminalInstructions.displayName = 'TerminalInstructions'