import { DragEvent, ReactNode, useState } from 'react' import { toast } from 'sonner' import { useParams } from 'common' import { useEdgeFunctionDeployMutation } from 'data/edge-functions/edge-functions-deploy-mutation' import { ReportBlockContainer } from 'components/interfaces/Reports/ReportBlock/ReportBlockContainer' import { Button, CodeBlock, cn, CodeBlockLang } from 'ui' import { Code } from 'lucide-react' interface EdgeFunctionBlockProps { /** Title of the EdgeFunctionBlock */ label: string /** Function code to display */ code: string /** Function name/slug */ functionName: string /** Any other actions specific to the parent to be rendered in the header */ actions?: ReactNode /** Toggle visiblity of code on render */ showCode?: boolean /** Whether function block is draggable */ draggable?: boolean /** Tooltip when hovering over the header of the block */ tooltip?: ReactNode /** Optional callback on drag start */ onDragStart?: (e: DragEvent) => void } export const EdgeFunctionBlock = ({ label, code, functionName, actions, showCode: _showCode = false, tooltip, }: EdgeFunctionBlockProps) => { const { ref } = useParams() const [isDeployed, setIsDeployed] = useState(false) const { mutateAsync: deployFunction, isLoading: isDeploying } = useEdgeFunctionDeployMutation({ onSuccess: () => { setIsDeployed(true) toast.success('Successfully deployed edge function') }, }) const handleDeploy = async () => { if (!code || isDeploying || !ref) return try { const deployResult = await deployFunction({ projectRef: ref, metadata: { entrypoint_path: 'index.ts', name: functionName, verify_jwt: false, }, files: [{ name: 'index.ts', content: code }], }) } catch (error) { toast.error( `Failed to deploy function: ${error instanceof Error ? error.message : 'Unknown error'}` ) } } const functionUrl = ref ? `https://${ref}.supabase.co/functions/v1/${functionName}` : '' return ( } label={label} actions={ <> {actions} } >
code]:m-0 [&>code>span]:text-foreground' )} />
{(isDeploying || isDeployed) && (
{isDeploying ? (

Deploying function...

) : ( <>

Function deployed successfully! You can now invoke it at:

To download and work on this function locally, use the CLI command:

)}
)}
) }