import { useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { useLingui } from '@lingui/react/macro'; import { Trans } from '@lingui/react/macro'; import type { Webhook } from '@prisma/client'; import type * as DialogPrimitive from '@radix-ui/react-dialog'; import { useForm } from 'react-hook-form'; import type { z } from 'zod'; import { trpc } from '@documenso/trpc/react'; import { ZEditWebhookRequestSchema } from '@documenso/trpc/server/webhook-router/schema'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@documenso/ui/primitives/dialog'; 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 { WebhookMultiSelectCombobox } from '../general/webhook-multiselect-combobox'; const ZEditWebhookFormSchema = ZEditWebhookRequestSchema.omit({ id: true }); type TEditWebhookFormSchema = z.infer; export type WebhookEditDialogProps = { trigger?: React.ReactNode; webhook: Webhook; } & Omit; export const WebhookEditDialog = ({ trigger, webhook, ...props }: WebhookEditDialogProps) => { const { t } = useLingui(); const { toast } = useToast(); const [open, setOpen] = useState(false); 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: webhook.id, ...data, }); toast({ title: t`Webhook updated`, description: t`The webhook has been updated successfully.`, duration: 5000, }); } catch (err) { toast({ title: t`Failed to update webhook`, description: t`We encountered an error while updating the webhook. Please try again later.`, variant: 'destructive', }); } }; return ( !form.formState.isSubmitting && setOpen(value)} {...props} > e.stopPropagation()} asChild> {trigger} Edit webhook {webhook.id}
( 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. )} />
); };