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 { Webhook } from '@prisma/client'; import { WebhookTriggerEvents } from '@prisma/client'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import { toFriendlyWebhookEventName } from '@documenso/lib/universal/webhook/to-friendly-webhook-event-name'; import { trpc } from '@documenso/trpc/react'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@documenso/ui/primitives/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@documenso/ui/primitives/select'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { useCurrentTeam } from '~/providers/team'; export type WebhookTestDialogProps = { webhook: Pick; children: React.ReactNode; }; const ZTestWebhookFormSchema = z.object({ event: z.nativeEnum(WebhookTriggerEvents), }); type TTestWebhookFormSchema = z.infer; export const WebhookTestDialog = ({ webhook, children }: WebhookTestDialogProps) => { const { _ } = useLingui(); const { toast } = useToast(); const team = useCurrentTeam(); const [open, setOpen] = useState(false); const { mutateAsync: testWebhook } = trpc.webhook.testWebhook.useMutation(); const form = useForm({ resolver: zodResolver(ZTestWebhookFormSchema), defaultValues: { event: webhook.eventTriggers[0], }, }); const onSubmit = async ({ event }: TTestWebhookFormSchema) => { try { await testWebhook({ id: webhook.id, event, teamId: team.id, }); toast({ title: _(msg`Test webhook sent`), description: _(msg`The test webhook has been successfully sent to your endpoint.`), duration: 5000, }); setOpen(false); } catch (error) { toast({ title: _(msg`Test webhook failed`), description: _( msg`We encountered an error while sending the test webhook. Please check your endpoint and try again.`, ), variant: 'destructive', duration: 5000, }); } }; return ( !form.formState.isSubmitting && setOpen(value)}> {children} Test Webhook Send a test webhook with sample data to verify your integration is working correctly.
( Event Type )} />

Webhook URL

{webhook.webhookUrl}

); };