import { Code } from 'lucide-react'
import Link from 'next/link'
import type { DragEvent, ReactNode } from 'react'
import { Button, cn } from 'ui'
import { Admonition } from 'ui-patterns/admonition'
import { CodeBlock, type CodeBlockLang } from 'ui-patterns/CodeBlock'
import { ReportBlockContainer } from '@/components/interfaces/Reports/ReportBlock/ReportBlockContainer'
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
/** Hide the header deploy button (used when an external confirm footer is shown) */
hideDeployButton?: boolean
/** Disable interactive actions */
disabled?: boolean
/** Whether a deploy action is currently running */
isDeploying?: boolean
/** Whether a deploy action has completed */
isDeployed?: boolean
/** Optional message to show when deployment fails */
errorText?: string
/** URL to the deployed function */
functionUrl?: string
/** Link to the function details page */
deploymentDetailsUrl?: string
/** CLI command to download the function */
downloadCommand?: string
/** Show warning UI when replacing an existing function */
showReplaceWarning?: boolean
/** Cancel handler when replacing an existing function */
onCancelReplace?: () => void
/** Confirm handler when replacing an existing function */
onConfirmReplace?: () => void
/** Handler for triggering a deploy */
onDeploy?: () => void
}
export const EdgeFunctionBlock = ({
label,
code,
functionName,
actions,
tooltip,
hideDeployButton = false,
disabled = false,
isDeploying = false,
isDeployed = false,
errorText,
functionUrl,
deploymentDetailsUrl,
downloadCommand,
showReplaceWarning = false,
onCancelReplace,
onConfirmReplace,
onDeploy,
draggable = false,
onDragStart,
}: EdgeFunctionBlockProps) => {
const resolvedFunctionUrl = functionUrl ?? 'Function URL will be available after deployment'
const resolvedDownloadCommand = downloadCommand ?? `supabase functions download ${functionName}`
const hasStatusMessage = isDeploying || isDeployed || !!errorText
return (
An edge function with the name "{functionName}" already exists.
Deploying will replace the existing function. Are you sure you want to proceed?
Deploying function...
) : errorText ? ({errorText}
) : ( <>The{' '} {deploymentDetailsUrl ? ( new function ) : ( new function )}{' '} is now live at:
To download and work on this function locally, use the CLI command: