import { useRouter } from 'next/router' import { Button, IconTerminal, IconMaximize2, IconMinimize2, IconBookOpen, IconCode } from 'ui' import { useProjectSettings } from 'hooks' import { useAccessTokens } from 'hooks/queries/useAccessTokens' import { Commands } from './Functions.types' import CommandRender from 'components/interfaces/Functions/CommandRender' import { FC, useState } from 'react' import { useStore } from 'hooks' import Link from 'next/link' interface Props { closable?: boolean } const TerminalInstructions: FC = ({ closable = false }) => { const router = useRouter() const { ref } = router.query const { ui } = useStore() const { tokens } = useAccessTokens() const { services } = useProjectSettings(ref as string | undefined) const [showInstructions, setShowInstructions] = useState(!closable) // Get the API service const API_SERVICE_ID = 1 const apiService = (services ?? []).find((x: any) => x.app.id == API_SERVICE_ID) const apiKeys = apiService?.service_api_keys ?? [] const anonKey = apiKeys.find((x: any) => x.name === 'anon key')?.api_key const endpoint = apiService?.app_config.endpoint ?? '' const endpointSections = endpoint.split('.') const functionsEndpoint = [ ...endpointSections.slice(0, 1), 'functions', ...endpointSections.slice(1), ].join('.') // get the .co or .net TLD from the restUrl const restUrl = ui.selectedProject?.restUrl const restUrlTld = new URL(restUrl as string).hostname.split('.').pop() 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 ${ref}`, description: 'Deploys function at ./functions/hello-world/index.ts', jsx: () => { return ( <> supabase functions deploy hello-world --project-ref {ref} ) }, comment: 'Deploy your function', }, { command: `curl -L -X POST 'https://${ref}.functions.supabase.${restUrlTld}/hello-world' -H 'Authorization: Bearer ${ anonKey ?? '[YOUR ANON KEY]' }' --data '{"name":"Functions"}'`, description: 'Invokes the hello-world function', jsx: () => { return ( <> curl -L -X POST 'https://{functionsEndpoint} /hello-world' -H 'Authorization: Bearer [YOUR ANON KEY]'{' '} {`--data '{"name":"Functions"}'`} ) }, comment: 'Invoke your function', }, ] return (

Terminal instructions

{closable && (
setShowInstructions(!showInstructions)}> {showInstructions ? ( ) : ( )}
)}
{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.

)}
) } export default TerminalInstructions