import dayjs from 'dayjs' import { useRouter } from 'next/router' import { FC, useState, useEffect } from 'react' import * as Tooltip from '@radix-ui/react-tooltip' import { PermissionAction } from '@supabase/shared-types/out/constants' import { Alert, IconGlobe, IconTerminal, IconMinimize2, IconMaximize2, IconCheck, IconClipboard, Button, Modal, } from 'ui' import { useStore, useParams, checkPermissions } from 'hooks' import Panel from 'components/ui/Panel' import CommandRender from './CommandRender' import { useProjectApiQuery } from 'data/config/project-api-query' import { useEdgeFunctionDeleteMutation } from 'data/edge-functions/edge-functions-delete-mutation' interface Props {} // [Joshen] Next - additional configs: Verify jwt + import maps const EdgeFunctionDetails: FC = () => { const router = useRouter() const { functions, ui } = useStore() const { ref: projectRef, id } = useParams() const [isCopied, setIsCopied] = useState(false) const [showDeleteModal, setShowDeleteModal] = useState(false) const [showInstructions, setShowInstructions] = useState(false) const [selectedFunction, setSelectedFunction] = useState(null) const canDeleteEdgeFunction = checkPermissions(PermissionAction.FUNCTIONS_WRITE, '*') const { mutateAsync: deleteEdgeFunction, isLoading: isDeleting } = useEdgeFunctionDeleteMutation() useEffect(() => { setSelectedFunction(functions.byId(id)) }, [functions.isLoaded, ui.selectedProject]) const { data: settings } = useProjectApiQuery({ projectRef }) // Get the API service const apiService = settings?.autoApiService const anonKey = apiService?.service_api_keys.find((x) => x.name === 'anon key') ? apiService.defaultApiKey : '[YOUR ANON KEY]' const endpoint = apiService?.app_config.endpoint ?? '' const endpointSections = endpoint.split('.') const functionsEndpoint = [ ...endpointSections.slice(0, 1), 'functions', ...endpointSections.slice(1), ].join('.') const functionUrl = `${apiService?.protocol}://${functionsEndpoint}/${selectedFunction?.slug}` const onConfirmDelete = async () => { if (!projectRef) return console.error('Project ref is required') if (selectedFunction === undefined) return console.error('No edge function selected') try { await deleteEdgeFunction({ projectRef, slug: selectedFunction.slug }) ui.setNotification({ category: 'success', message: `Successfully deleted "${selectedFunction.name}"`, }) router.push(`/project/${projectRef}/functions`) } catch (error: any) { ui.setNotification({ category: 'error', message: `Failed to delete function: ${error.message}`, }) } } const managementCommands: any = [ { command: `supabase functions deploy ${selectedFunction?.slug}`, description: 'This will overwrite the deployed function with your new function', jsx: () => { return ( <> supabase functions deploy{' '} {selectedFunction?.slug} ) }, comment: 'Deploy a new version', }, { command: `supabase functions delete ${selectedFunction?.slug}`, description: 'This will remove the function and all the logs associated with it', jsx: () => { return ( <> supabase functions delete{' '} {selectedFunction?.slug} ) }, comment: 'Delete the function', }, ] const secretCommands: any = [ { command: `supabase secrets list`, description: 'This will list all the secrets for your project', jsx: () => { return ( <> supabase secrets list ) }, comment: 'View all secrets', }, { command: `supabase secrets set NAME1=VALUE1 NAME2=VALUE2`, description: 'This will set secrets for your project', jsx: () => { return ( <> supabase secrets set NAME1=VALUE1 NAME2=VALUE2 ) }, comment: 'Set secrets for your project', }, { command: `supabase secrets unset NAME1 NAME2 `, description: 'This will delete secrets for your project', jsx: () => { return ( <> supabase secrets unset NAME1 NAME2 ) }, comment: 'Unset secrets for your project', }, ] const invokeCommands: any = [ { command: `curl -L -X POST '${functionUrl}' -H 'Authorization: Bearer ${ anonKey ?? '[YOUR ANON KEY]' }' --data '{"name":"Functions"}'`, description: 'Invokes the hello function', jsx: () => { return ( <> curl -L -X POST 'https://{functionsEndpoint}/ {selectedFunction?.slug}' -H 'Authorization: Bearer [YOUR ANON KEY]'{' '} {`--data '{"name":"Functions"}'`} ) }, comment: 'Invoke your function', }, ] return ( <>

Function Name

{selectedFunction?.name}

Endpoint URL

{functionUrl}

Created At

{selectedFunction?.created_at && dayjs(selectedFunction.created_at).format('dddd, MMMM D, YYYY h:mm A')}

Last Updated At

{selectedFunction?.updated_at && dayjs(selectedFunction.updated_at).format('dddd, MMMM D, YYYY h:mm A')}

Deployments
{selectedFunction?.version}
Regions
Earth
All functions are deployed globally

Command line access

setShowInstructions(!showInstructions)}> {showInstructions ? ( ) : ( )}
Deployment management
Invoke
Secrets management
Delete Edge Function

}>

Make sure you have made a backup if you want to restore your edge function

{!canDeleteEdgeFunction && (
You need additional permissions to delete an edge function
)}
Confirm to delete {selectedFunction?.name}} visible={showDeleteModal} loading={isDeleting} onCancel={() => setShowDeleteModal(false)} onConfirm={onConfirmDelete} >
Ensure that you have made a backup if you want to restore your edge function
) } export default EdgeFunctionDetails