/* eslint-disable @typescript-eslint/no-unused-vars */ "use client" import { useState, useEffect } from "react" import { useTranslations } from "next-intl" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Switch } from "@/components/ui/switch" import { useToast } from "@/components/ui/use-toast" import { Loader2, Send, ChevronDown, ChevronUp } from "lucide-react" import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip" export function WebhookConfig() { const t = useTranslations("profile.webhook") const tCommon = useTranslations("common.actions") const tMessages = useTranslations("emails.messages") const tApiKey = useTranslations("profile.apiKey") const [enabled, setEnabled] = useState(false) const [url, setUrl] = useState("") const [loading, setLoading] = useState(false) const [testing, setTesting] = useState(false) const [showDocs, setShowDocs] = useState(false) const [initialLoading, setInitialLoading] = useState(true) const { toast } = useToast() useEffect(() => { fetch("/api/webhook") .then(res => res.json() as Promise<{ enabled: boolean; url: string }>) .then(data => { setEnabled(data.enabled) setUrl(data.url) }) .catch(console.error) .finally(() => setInitialLoading(false)) }, []) if (initialLoading) { return (

{tMessages("loading")}

) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!url) return setLoading(true) try { const res = await fetch("/api/webhook", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ url, enabled }) }) if (!res.ok) throw new Error(t("saveFailed")) toast({ title: t("saveSuccess"), description: t("saveSuccess") }) } catch (_error) { toast({ title: t("saveFailed"), description: t("saveFailed"), variant: "destructive" }) } finally { setLoading(false) } } const handleTest = async () => { if (!url) return setTesting(true) try { const res = await fetch("/api/webhook/test", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ url }) }) if (!res.ok) throw new Error(t("testFailed")) toast({ title: t("testSuccess"), description: t("testSuccess") }) } catch (_error) { toast({ title: t("testFailed"), description: t("testFailed"), variant: "destructive" }) } finally { setTesting(false) } } return (
{t("description")}
{enabled && (
setUrl(e.target.value)} type="url" required />

{t("test")}

{t("description2")}

{showDocs && (

{t("docs.intro")}

                  Content-Type: application/json{'\n'}
                  X-Webhook-Event: new_message
                

{t("docs.exampleBody")}

                  {`{
  "emailId": "email-uuid",
  "messageId": "message-uuid",
  "fromAddress": "sender@example.com",
  "subject": "${t("docs.subject")}",
  "content": "${t("docs.content")}",
  "html": "${t("docs.html")}",
  "receivedAt": "2024-01-01T12:00:00.000Z",
  "toAddress": "your-email@${window.location.host}"
}`}
                
)}
)}
) }