import { useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; 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 { ZCreateWebhookMutationSchema } from '@documenso/trpc/server/webhook-router/schema'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, 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 { useOptionalCurrentTeam } from '~/providers/team'; import { WebhookMultiSelectCombobox } from '../general/webhook-multiselect-combobox'; const ZCreateWebhookFormSchema = ZCreateWebhookMutationSchema.omit({ teamId: true }); type TCreateWebhookFormSchema = z.infer; export type WebhookCreateDialogProps = { trigger?: React.ReactNode; } & Omit; export const WebhookCreateDialog = ({ trigger, ...props }: WebhookCreateDialogProps) => { const { _ } = useLingui(); const { toast } = useToast(); const team = useOptionalCurrentTeam(); const [open, setOpen] = useState(false); const form = useForm({ resolver: zodResolver(ZCreateWebhookFormSchema), values: { webhookUrl: '', eventTriggers: [], secret: '', enabled: true, }, }); const { mutateAsync: createWebhook } = trpc.webhook.createWebhook.useMutation(); const onSubmit = async ({ enabled, eventTriggers, secret, webhookUrl, }: TCreateWebhookFormSchema) => { try { await createWebhook({ enabled, eventTriggers, secret, webhookUrl, teamId: team?.id, }); setOpen(false); toast({ title: _(msg`Webhook created`), description: _(msg`The webhook was successfully created.`), }); form.reset(); } catch (err) { toast({ title: _(msg`Error`), description: _(msg`An error occurred while creating the webhook. Please try again.`), variant: 'destructive', }); } }; return ( !form.formState.isSubmitting && setOpen(value)} {...props} > e.stopPropagation()} asChild> {trigger ?? ( )} Create webhook On this page, you can create a new webhook.
( 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 . )} />
); };