import { useEffect, useState } from "react"; import type { RoutineTrigger } from "@paperclipai/shared"; import { AlertCircle, Check, CheckCircle2, GitBranch, Globe, PauseCircle, Radio } from "lucide-react"; import { webhookAgentInstructions, webhookInstructionSigningMode } from "@/components/routine-triggers/webhook-instructions"; import { AgentInstructions, CopyField } from "@/components/routine-triggers/WebhookFields"; import { WebhookUrlWarning } from "@/components/routine-triggers/WebhookUrlWarning"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { cn } from "@/lib/utils"; import { WEBHOOK_STATUS_COPY, webhookStatusKey, webhookTitle, type WebhookStatusKey } from "./routine-summary"; export type WebhookSender = "custom" | "github"; export type RoutineWebhookDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; routineTitle: string; /** False while the routine is paused or has no default agent: real events are rejected. */ routineActive?: boolean; /** `null` while choosing what to connect; the saved webhook afterwards. */ trigger: RoutineTrigger | null; /** The key, available only right after the webhook is created or its key is replaced. */ secret?: string | null; busy?: boolean; error?: string | null; onCreate: (sender: WebhookSender) => void; onReplaceKey: () => void; onSetPaused: (paused: boolean) => void; onRemove: () => void; /** A finished webhook put back into test mode: events are checked but never start the routine. */ testing?: boolean; /** Omit to hide "Test webhook". */ onStartTest?: () => void; onFinishTest?: () => void; }; const SENDERS = [ { sender: "custom", label: "Another app or script", Icon: Globe }, { sender: "github", label: "GitHub", Icon: GitBranch }, ] as const; const STATUS_LOOK: Record = { setup: { icon: Radio, tone: "text-muted-foreground" }, setup_testing: { icon: Radio, tone: "text-muted-foreground" }, testing: { icon: Radio, tone: "text-muted-foreground" }, waiting: { icon: Radio, tone: "text-muted-foreground" }, receiving: { icon: CheckCircle2, tone: "text-(--status-task-done)" }, rejected: { icon: AlertCircle, tone: "text-destructive" }, test_rejected: { icon: AlertCircle, tone: "text-destructive" }, test_received: { icon: CheckCircle2, tone: "text-(--status-task-done)" }, ignored_while_paused: { icon: PauseCircle, tone: "text-muted-foreground" }, paused: { icon: PauseCircle, tone: "text-muted-foreground" }, }; export function RoutineWebhookDialog({ open, onOpenChange, routineTitle, routineActive = true, trigger, secret = null, busy = false, error = null, onCreate, onReplaceKey, onSetPaused, onRemove, testing = false, onStartTest, onFinishTest, }: RoutineWebhookDialogProps) { const [sender, setSender] = useState("custom"); useEffect(() => { if (open && !trigger) setSender("custom"); }, [open, trigger]); const github = trigger?.signingMode === "github_hmac"; const bearer = trigger?.signingMode === "bearer"; const inTest = testing && Boolean(trigger?.setupPending); const statusKey = trigger ? webhookStatusKey(trigger, { routineActive, testing: inTest }) : null; const status = statusKey ? { ...STATUS_LOOK[statusKey], ...WEBHOOK_STATUS_COPY[statusKey] } : null; const testReceived = statusKey === "test_received"; const StatusIcon = status?.icon; const statusDetail = status?.detail; const canTest = Boolean(onStartTest) && trigger != null && !trigger.setupPending && trigger.enabled; const url = trigger?.webhookUrl ?? ""; return ( {trigger ? webhookTitle(trigger) : "Add webhook"} {trigger ? `Starts “${routineTitle}” when ${github ? "GitHub" : "another app"} sends an event.` : "Start this routine when something happens in another app."}
{!trigger ? (
{SENDERS.map(({ sender: value, label, Icon }) => ( ))}
) : ( <> {status && StatusIcon ? (
) : null} {trigger.signingMode === "hmac_sha256" ? (

Sign the timestamp, a period, and the exact JSON body with HMAC-SHA256. Send X-Paperclip-Timestamp and X-Paperclip-Signature: sha256=<signature>.

) : null} {trigger.signingMode === "none" ? (

This webhook has no key. Its URL is the shared secret, so keep it private.

) : secret ? ( ) : (

The secret key is hidden.

)} {/* Instructions follow the signing mode; with no known mode (a restored webhook before its refresh) none are shown. */} {trigger.signingMode && (secret || trigger.signingMode === "none") ? ( ) : null} )} {error ?

{error}

: null}
); }