import { WebhookTriggerEvents } from "@prisma/client"; import { useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { classNames } from "@calcom/lib"; import { WEBAPP_URL } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { inferQueryOutput } from "@calcom/trpc/react"; import Button from "@calcom/ui/v2/core/Button"; import Switch from "@calcom/ui/v2/core/Switch"; import Select from "@calcom/ui/v2/core/form/Select"; import { Form, Label, TextArea, TextField } from "@calcom/ui/v2/core/form/fields"; import customTemplate, { hasTemplateIntegration } from "../utils/integrationTemplate"; import WebhookTestDisclosure from "./WebhookTestDisclosure"; export type TWebhook = inferQueryOutput<"viewer.webhook.list">[number]; export type WebhookFormData = { id?: string; subscriberUrl: string; active: boolean; eventTriggers: WebhookTriggerEvents[]; secret: string | null; payloadTemplate: string | undefined | null; }; export type WebhookFormSubmitData = WebhookFormData & { changeSecret: boolean; newSecret: string; }; type WebhookTriggerEventOptions = { value: WebhookTriggerEvents; label: string }[]; const WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP_V2: Record = { core: [ { value: WebhookTriggerEvents.BOOKING_CANCELLED, label: "booking_cancelled" }, { value: WebhookTriggerEvents.BOOKING_CREATED, label: "booking_created" }, { value: WebhookTriggerEvents.BOOKING_RESCHEDULED, label: "booking_rescheduled" }, { value: WebhookTriggerEvents.MEETING_ENDED, label: "meeting_ended" }, ], routing_forms: [{ value: WebhookTriggerEvents.FORM_SUBMITTED, label: "form_submitted" }], }; const WebhookForm = (props: { webhook?: WebhookFormData; apps?: (keyof typeof WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP_V2)[]; onSubmit: (event: WebhookFormSubmitData) => void; }) => { const { t } = useLocale(); const triggerOptions: WebhookTriggerEventOptions = WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP_V2["core"]; if (props.apps) { for (const app of props.apps) { triggerOptions.push(...WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP_V2[app]); } } const translatedTriggerOptions = triggerOptions.map((option) => ({ ...option, label: t(option.label) })); const formMethods = useForm({ defaultValues: { subscriberUrl: props?.webhook?.subscriberUrl || "", active: props?.webhook?.active || false, eventTriggers: props?.webhook?.eventTriggers || [], secret: props?.webhook?.secret || "", payloadTemplate: props?.webhook?.payloadTemplate || undefined, }, }); const [useCustomTemplate, setUseCustomTemplate] = useState(false); const [newSecret, setNewSecret] = useState(""); const [changeSecret, setChangeSecret] = useState(false); const hasSecretKey = !!props?.webhook?.secret; // const currentSecret = props?.webhook?.secret; useEffect(() => { if (changeSecret) { formMethods.unregister("secret", { keepDefaultValue: false }); } }, [changeSecret]); return ( <>
props.onSubmit({ ...values, changeSecret, newSecret })}> ( <> { formMethods.setValue("subscriberUrl", e?.target.value); if (hasTemplateIntegration({ url: e.target.value })) { setUseCustomTemplate(true); formMethods.setValue("payloadTemplate", customTemplate({ url: e.target.value })); } }} /> )} /> (
{ formMethods.setValue("active", value); }} />
)} /> (