"use client"; import { useState } from "react"; import { EventChecklist } from "../shared/EventChecklist"; import { PayloadPreview } from "../shared/PayloadPreview"; interface Step3Props { webhookId?: string; events: string[]; enabled: boolean; description: string; onChangeEvents: (events: string[]) => void; onChangeEnabled: (enabled: boolean) => void; onChangeDescription: (desc: string) => void; t: (key: string, opts?: Record) => string; } interface TestResult { delivered: boolean; status: number; latencyMs: number; payloadSent: Record | null; responseBody: string; error?: string | null; } export function Step3EventsAndTest({ webhookId, events, enabled, description, onChangeEvents, onChangeEnabled, onChangeDescription, t, }: Step3Props) { const [testState, setTestState] = useState<"idle" | "sending" | "ok" | "fail">("idle"); const [testResult, setTestResult] = useState(null); const sendTest = async () => { if (!webhookId) return; setTestState("sending"); setTestResult(null); try { const res = await fetch(`/api/webhooks/${webhookId}/test`, { method: "POST" }); const data: TestResult & { error?: string } = await res.json().catch(() => ({})); if (!res.ok || data.delivered === false) { throw new Error(data.error || t("testFailed")); } setTestResult(data); setTestState("ok"); } catch (err) { setTestState("fail"); setTestResult((prev) => ({ delivered: false, status: 0, latencyMs: 0, payloadSent: prev?.payloadSent ?? null, responseBody: prev?.responseBody ?? "", error: err instanceof Error ? err.message : t("testFailed"), })); } }; const parseResponseBody = (raw: string): Record | null => { try { return JSON.parse(raw) as Record; } catch { return raw ? { raw } : null; } }; return (
onChangeDescription(e.target.value)} placeholder={t("namePlaceholder")} className="mt-1 w-full rounded-lg border border-border bg-surface px-3 py-2 text-sm text-text-main focus:outline-none focus:ring-2 focus:ring-primary/40" />
{webhookId && (

{t("testWebhook")}

{testState === "ok" && testResult && (

✅ {testResult.status} · {testResult.latencyMs}ms · {t("testSuccess")}

{testResult.payloadSent && ( )} {testResult.responseBody && ( )}
)} {testState === "fail" && testResult && (

{testResult.error ?? t("testFailed")}

{testResult.payloadSent && ( )}
)}
)}
); }