Files
sign/apps/remix/app/components/dialogs/webhook-create-dialog.tsx

248 lines
7.5 KiB
TypeScript
Raw Normal View History

2024-02-07 16:04:12 +02:00
import { useState } from 'react';
2024-02-09 16:07:33 +02:00
2024-02-07 16:04:12 +02:00
import { zodResolver } from '@hookform/resolvers/zod';
2025-01-02 15:33:37 +11:00
import { msg } from '@lingui/core/macro';
2024-08-27 20:34:39 +09:00
import { useLingui } from '@lingui/react';
2025-01-02 15:33:37 +11:00
import { Trans } from '@lingui/react/macro';
2024-02-07 16:04:12 +02:00
import type * as DialogPrimitive from '@radix-ui/react-dialog';
import { useForm } from 'react-hook-form';
2024-02-09 16:07:33 +02:00
import type { z } from 'zod';
2024-02-07 16:04:12 +02:00
2024-02-09 16:07:33 +02:00
import { trpc } from '@documenso/trpc/react';
2024-02-27 16:56:32 +11:00
import { ZCreateWebhookMutationSchema } from '@documenso/trpc/server/webhook-router/schema';
2024-02-07 16:04:12 +02:00
import { Button } from '@documenso/ui/primitives/button';
2024-02-09 16:07:33 +02:00
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@documenso/ui/primitives/dialog';
2024-02-07 16:04:12 +02:00
import {
Form,
FormControl,
2024-02-26 22:24:23 +11:00
FormDescription,
2024-02-07 16:04:12 +02:00
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@documenso/ui/primitives/form/form';
2024-02-09 16:07:33 +02:00
import { Input } from '@documenso/ui/primitives/input';
2024-02-16 11:44:03 +02:00
import { PasswordInput } from '@documenso/ui/primitives/password-input';
2024-02-09 16:07:33 +02:00
import { Switch } from '@documenso/ui/primitives/switch';
import { useToast } from '@documenso/ui/primitives/use-toast';
2024-02-07 16:04:12 +02:00
2024-02-27 16:56:32 +11:00
import { useOptionalCurrentTeam } from '~/providers/team';
2025-01-02 15:33:37 +11:00
import { WebhookMultiSelectCombobox } from '../general/webhook-multiselect-combobox';
2024-02-07 16:04:12 +02:00
2024-02-27 16:56:32 +11:00
const ZCreateWebhookFormSchema = ZCreateWebhookMutationSchema.omit({ teamId: true });
2024-02-09 16:07:33 +02:00
type TCreateWebhookFormSchema = z.infer<typeof ZCreateWebhookFormSchema>;
2024-02-07 16:04:12 +02:00
2025-01-02 15:33:37 +11:00
export type WebhookCreateDialogProps = {
2024-02-07 16:04:12 +02:00
trigger?: React.ReactNode;
} & Omit<DialogPrimitive.DialogProps, 'children'>;
2025-01-02 15:33:37 +11:00
export const WebhookCreateDialog = ({ trigger, ...props }: WebhookCreateDialogProps) => {
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
2024-02-09 16:07:33 +02:00
const { toast } = useToast();
2024-02-27 16:56:32 +11:00
const team = useOptionalCurrentTeam();
2024-02-07 16:04:12 +02:00
const [open, setOpen] = useState(false);
2024-02-09 16:07:33 +02:00
const form = useForm<TCreateWebhookFormSchema>({
resolver: zodResolver(ZCreateWebhookFormSchema),
2024-02-07 16:04:12 +02:00
values: {
webhookUrl: '',
eventTriggers: [],
secret: '',
enabled: true,
},
});
2024-02-09 16:07:33 +02:00
const { mutateAsync: createWebhook } = trpc.webhook.createWebhook.useMutation();
2024-02-27 16:56:32 +11:00
const onSubmit = async ({
enabled,
eventTriggers,
secret,
webhookUrl,
}: TCreateWebhookFormSchema) => {
2024-02-09 16:07:33 +02:00
try {
2024-02-27 16:56:32 +11:00
await createWebhook({
enabled,
eventTriggers,
secret,
webhookUrl,
teamId: team?.id,
});
2024-02-09 16:07:33 +02:00
setOpen(false);
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Webhook created`),
description: _(msg`The webhook was successfully created.`),
2024-02-09 16:07:33 +02:00
});
form.reset();
} catch (err) {
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Error`),
description: _(msg`An error occurred while creating the webhook. Please try again.`),
2024-02-09 16:07:33 +02:00
variant: 'destructive',
});
}
};
2024-02-07 16:04:12 +02:00
return (
<Dialog
open={open}
onOpenChange={(value) => !form.formState.isSubmitting && setOpen(value)}
{...props}
>
<DialogTrigger onClick={(e) => e.stopPropagation()} asChild>
2024-08-27 20:34:39 +09:00
{trigger ?? (
<Button className="flex-shrink-0">
<Trans>Create Webhook</Trans>
</Button>
)}
2024-02-07 16:04:12 +02:00
</DialogTrigger>
2024-02-26 22:24:23 +11:00
<DialogContent className="max-w-lg" position="center">
2024-02-07 16:04:12 +02:00
<DialogHeader>
2024-08-27 20:34:39 +09:00
<DialogTitle>
<Trans>Create webhook</Trans>
</DialogTitle>
<DialogDescription>
<Trans>On this page, you can create a new webhook.</Trans>
</DialogDescription>
2024-02-07 16:04:12 +02:00
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<fieldset
className="flex h-full flex-col space-y-4"
disabled={form.formState.isSubmitting}
>
2024-02-26 22:24:23 +11:00
<div className="flex flex-col-reverse gap-4 md:flex-row">
<FormField
control={form.control}
name="webhookUrl"
render={({ field }) => (
<FormItem className="flex-1">
2024-08-27 20:34:39 +09:00
<FormLabel required>
<Trans>Webhook URL</Trans>
</FormLabel>
2024-02-26 22:24:23 +11:00
<FormControl>
<Input className="bg-background" {...field} />
</FormControl>
<FormDescription>
2024-08-27 20:34:39 +09:00
<Trans>The URL for Documenso to send webhook events to.</Trans>
2024-02-26 22:24:23 +11:00
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="enabled"
render={({ field }) => (
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Enabled</Trans>
</FormLabel>
2024-02-26 22:24:23 +11:00
<div>
<FormControl>
<Switch
className="bg-background"
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</div>
<FormMessage />
</FormItem>
)}
/>
</div>
2024-02-07 16:04:12 +02:00
<FormField
control={form.control}
name="eventTriggers"
render={({ field: { onChange, value } }) => (
<FormItem className="flex flex-col gap-2">
2024-08-27 20:34:39 +09:00
<FormLabel required>
<Trans>Triggers</Trans>
</FormLabel>
2024-02-07 16:04:12 +02:00
<FormControl>
2025-01-02 15:33:37 +11:00
<WebhookMultiSelectCombobox
2024-02-07 16:04:12 +02:00
listValues={value}
onChange={(values: string[]) => {
2024-02-09 16:07:33 +02:00
onChange(values);
2024-02-07 16:04:12 +02:00
}}
/>
</FormControl>
2024-02-26 22:24:23 +11:00
<FormDescription>
2024-08-27 20:34:39 +09:00
<Trans>The events that will trigger a webhook to be sent to your URL.</Trans>
2024-02-26 22:24:23 +11:00
</FormDescription>
2024-02-07 16:04:12 +02:00
<FormMessage />
</FormItem>
)}
/>
2024-02-09 16:07:33 +02:00
<FormField
2024-02-07 16:04:12 +02:00
control={form.control}
name="secret"
render={({ field }) => (
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Secret</Trans>
</FormLabel>
2024-02-07 16:04:12 +02:00
<FormControl>
2024-02-16 11:44:03 +02:00
<PasswordInput
className="bg-background"
{...field}
value={field.value ?? ''}
/>
2024-02-07 16:04:12 +02:00
</FormControl>
2024-02-26 22:24:23 +11:00
<FormDescription>
2024-08-27 20:34:39 +09:00
<Trans>
A secret that will be sent to your URL so you can verify that the request
has been sent by Documenso
</Trans>
.
2024-02-26 22:24:23 +11:00
</FormDescription>
2024-02-07 16:04:12 +02:00
<FormMessage />
</FormItem>
)}
/>
<DialogFooter>
2025-02-24 21:47:06 +11:00
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
<Trans>Cancel</Trans>
</Button>
<Button type="submit" loading={form.formState.isSubmitting}>
<Trans>Create</Trans>
</Button>
2024-02-07 16:04:12 +02:00
</DialogFooter>
</fieldset>
</form>
</Form>
</DialogContent>
</Dialog>
);
2024-02-06 16:00:28 +02:00
};