'use client'; import { useRouter } from 'next/navigation'; import { zodResolver } from '@hookform/resolvers/zod'; import { Loader } from 'lucide-react'; import { useForm } from 'react-hook-form'; import type { z } from 'zod'; import { trpc } from '@documenso/trpc/react'; import { ZEditWebhookMutationSchema } from '@documenso/trpc/server/webhook-router/schema'; import { Button } from '@documenso/ui/primitives/button'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { Input } from '@documenso/ui/primitives/input'; import { PasswordInput } from '@documenso/ui/primitives/password-input'; import { Switch } from '@documenso/ui/primitives/switch'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { SettingsHeader } from '~/components/(dashboard)/settings/layout/header'; import { TriggerMultiSelectCombobox } from '~/components/(dashboard)/settings/webhooks/trigger-multiselect-combobox'; const ZEditWebhookFormSchema = ZEditWebhookMutationSchema.omit({ id: true }); type TEditWebhookFormSchema = z.infer; export type WebhookPageOptions = { params: { id: string; }; }; export default function WebhookPage({ params }: WebhookPageOptions) { const { toast } = useToast(); const router = useRouter(); const { data: webhook, isLoading } = trpc.webhook.getWebhookById.useQuery( { id: params.id, }, { enabled: !!params.id }, ); const { mutateAsync: updateWebhook } = trpc.webhook.editWebhook.useMutation(); const form = useForm({ resolver: zodResolver(ZEditWebhookFormSchema), values: { webhookUrl: webhook?.webhookUrl ?? '', eventTriggers: webhook?.eventTriggers ?? [], secret: webhook?.secret ?? '', enabled: webhook?.enabled ?? true, }, }); const onSubmit = async (data: TEditWebhookFormSchema) => { try { await updateWebhook({ id: params.id, ...data, }); toast({ title: 'Webhook updated', description: 'The webhook has been updated successfully.', duration: 5000, }); router.refresh(); } catch (err) { toast({ title: 'Failed to update webhook', description: 'We encountered an error while updating the webhook. Please try again later.', variant: 'destructive', }); } }; return (
{isLoading && (
)}
( Webhook URL The URL for Documenso to send webhook events to. )} /> ( Enabled
)} />
( Triggers { onChange(values); }} /> The events that will trigger a webhook to be sent to your URL. )} /> ( Secret A secret that will be sent to your URL so you can verify that the request has been sent by Documenso. )} />
); }