"use client" import { useState } from "react" import { Pencil, Trash2 } from "lucide-react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { useMailWebhooks, useCreateMailWebhook, useUpdateMailWebhook, useDeleteMailWebhook, } from "@/lib/api/hooks/use-mail-automation-queries" import { useAuthReady } from "@/lib/api/use-auth-ready" import { SettingsSyncBanner } from "@/components/gmail/settings/settings-sync-banner" import { WebhookTemplateVariablesPanel } from "@/components/gmail/settings/automation/webhook-template-variables-panel" import { AutomationTabMasonry } from "@/components/gmail/settings/automation/automation-tab-masonry" import { WebhookEventScopeEditor } from "@/components/gmail/settings/automation/webhook-event-scope-editor" import { AutomationDomainFilterTab, AutomationDomainMark, AutomationDomainMarks, } from "@/components/gmail/settings/automation/automation-domain-mark" import { WEBHOOK_DEFAULT_TEMPLATES } from "@/lib/mail-automation/webhook-template-variables" import { AUTOMATION_DOMAIN_LABELS, type AutomationDomain, } from "@/lib/mail-automation/domains" import { createDefaultWebhookForm, defaultEventTypesForDomain, eventDomainsFromTypes, type WebhookFormState, } from "@/lib/mail-automation/webhook-config" import { TRIGGER_LABELS } from "@/lib/mail-automation/domains" import type { ApiWebhook } from "@/lib/api/types" import type { TriggerType } from "@/lib/mail-automation/types" const CREATE_DOMAINS: AutomationDomain[] = ["mail", "drive", "contacts"] function webhookToForm(hook: ApiWebhook): WebhookFormState { return { name: hook.name, url: hook.url, template: hook.body_template ?? WEBHOOK_DEFAULT_TEMPLATES.mail, eventTypes: (hook.event_types?.length ? hook.event_types : ["message_received"]) as TriggerType[], mailScope: hook.mail_scope ?? { all_accounts: true, account_ids: [] }, driveScope: hook.drive_scope ?? { all_folders: true, folder_paths: [] }, contactsScope: hook.contacts_scope ?? { all_books: true, book_ids: [] }, } } function summarizeWebhook(hook: ApiWebhook): string { const types = hook.event_types ?? [] if (types.length === 0) return "Aucun événement" const labels = types.slice(0, 3).map((t) => TRIGGER_LABELS[t as TriggerType] ?? t) const extra = types.length > 3 ? ` +${types.length - 3}` : "" return labels.join(", ") + extra } function webhookDomains(hook: ApiWebhook): AutomationDomain[] { const types = (hook.event_types ?? []) as TriggerType[] return eventDomainsFromTypes(types.length > 0 ? types : ["message_received"]) } export function WebhooksPanel() { const { ready, authenticated } = useAuthReady() const { data: webhooks = [], isFetching, isError, refetch, isPending } = useMailWebhooks() const createWebhook = useCreateMailWebhook() const updateWebhook = useUpdateMailWebhook() const deleteWebhook = useDeleteMailWebhook() const [editorOpen, setEditorOpen] = useState(false) const [editingId, setEditingId] = useState(null) const [presetDomain, setPresetDomain] = useState("mail") const [form, setForm] = useState(() => createDefaultWebhookForm("mail")) const showInitialLoad = ready && authenticated && isPending && webhooks.length === 0 const editorTitle = editingId ? "Modifier le webhook" : "Nouveau webhook" function openCreate(domain: AutomationDomain) { setEditingId(null) setPresetDomain(domain) setForm({ ...createDefaultWebhookForm(domain), template: WEBHOOK_DEFAULT_TEMPLATES[domain], }) setEditorOpen(true) } function openEdit(hook: ApiWebhook) { setEditingId(hook.id) setForm(webhookToForm(hook)) const domains = webhookDomains(hook) setPresetDomain(domains[0] ?? "mail") setEditorOpen(true) } async function handleSave() { const payload = { name: form.name.trim(), url: form.url.trim(), method: "POST" as const, body_template: form.template, event_types: form.eventTypes, mail_scope: form.mailScope, drive_scope: form.driveScope, contacts_scope: form.contactsScope, } if (editingId) { await updateWebhook.mutateAsync({ webhookId: editingId, ...payload }) } else { await createWebhook.mutateAsync(payload) } setEditorOpen(false) } const canSave = form.name.trim().length > 0 && form.url.trim().length > 0 && form.eventTypes.length > 0 && !createWebhook.isPending && !updateWebhook.isPending return (
refetch()} />

Webhooks

{CREATE_DOMAINS.map((domain) => ( ))}
{showInitialLoad ? (

Chargement…

) : webhooks.length === 0 ? (

Aucun webhook configuré.

) : (
    {webhooks.map((hook) => { const domains = webhookDomains(hook) return (
  • {hook.name}

    {hook.url}

    {summarizeWebhook(hook)}

  • ) })}
)}
{editorTitle}
{!editingId ? (
{CREATE_DOMAINS.map((domain) => ( { setPresetDomain(domain) setForm((f) => ({ ...f, eventTypes: defaultEventTypesForDomain(domain), template: WEBHOOK_DEFAULT_TEMPLATES[domain], })) }} /> ))}
) : null}
setForm({ ...form, name: e.target.value })} />
setForm({ ...form, url: e.target.value })} />