Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

7.9 KB
Raw Blame
"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { mutationOptions } from "@next-safe-action/adapter-tanstack-query";
import { passwordManagerIgnoreProps } from "@rallly/ui";
import { Alert, AlertDescription } from "@rallly/ui/alert";
import { Button } from "@rallly/ui/button";
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  useDialog,
} from "@rallly/ui/dialog";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@rallly/ui/form";
import { Input } from "@rallly/ui/input";
import { toast } from "@rallly/ui/sonner";
import { useMutation } from "@tanstack/react-query";
import { AlertTriangleIcon, CheckIcon, CopyIcon, PlusIcon } from "lucide-react";
import React from "react";
import { useForm } from "react-hook-form";
import { useCopyToClipboard } from "react-use";
import { Trans, useTranslation } from "@/i18n/client";
import { createWebhookAction } from "../actions";
import { createWebhookInputSchema } from "../schema";
import { WebhookEventCombobox } from "./webhook-event-combobox";

export function CreateWebhookButton() {
  const { t } = useTranslation();
  const dialog = useDialog();
  const [createdSecret, setCreatedSecret] = React.useState<string | null>(null);
  const createWebhook = useMutation(mutationOptions(createWebhookAction));
  const [, copy] = useCopyToClipboard();
  const [didCopy, setDidCopy] = React.useState(false);

  const form = useForm({
    resolver: zodResolver(createWebhookInputSchema),
    defaultValues: {
      url: "",
      events: [],
    },
  });

  const handleClose = () => {
    dialog.dismiss();
    setCreatedSecret(null);
    form.reset();
  };

  const handleCopy = () => {
    if (createdSecret) {
      copy(createdSecret);
      setDidCopy(true);
      setTimeout(() => setDidCopy(false), 2000);
      toast.success(
        t("copiedToClipboard", {
          defaultValue: "Copied to clipboard",
        }),
      );
    }
  };

  return (
    <>
      <Button onClick={() => dialog.trigger()}>
        <PlusIcon data-icon="inline-start" />
        <Trans i18nKey="addWebhook" defaults="Add webhook" />
      </Button>
      <Dialog {...dialog.dialogProps} onOpenChange={handleClose}>
        <DialogContent>
          {createdSecret ? (
            <>
              <DialogHeader>
                <DialogTitle>
                  <Trans i18nKey="webhookAdded" defaults="Webhook added" />
                </DialogTitle>
                <DialogDescription>
                  <Trans
                    i18nKey="webhookAddedDescription"
                    defaults="Copy your signing secret now. You won't be able to see it again."
                  />
                </DialogDescription>
              </DialogHeader>
              <div className="space-y-4">
                <Alert variant="warning">
                  <AlertTriangleIcon />
                  <AlertDescription>
                    <Trans
                      i18nKey="webhookSecretSecurityWarning"
                      defaults="Store this secret securely. Use it to verify that deliveries came from Rallly."
                    />
                  </AlertDescription>
                </Alert>
                <div className="space-y-2">
                  <label
                    htmlFor="created-webhook-secret"
                    className="font-medium text-sm"
                  >
                    <Trans i18nKey="signingSecret" defaults="Signing secret" />
                  </label>
                  <div className="flex gap-2">
                    <Input
                      id="created-webhook-secret"
                      value={createdSecret}
                      readOnly
                      className="font-mono text-sm"
                      {...passwordManagerIgnoreProps}
                    />
                    <Button
                      aria-label={t("copy", { defaultValue: "Copy" })}
                      variant="ghost"
                      size="icon"
                      onClick={handleCopy}
                      type="button"
                    >
                      {didCopy ? <CheckIcon /> : <CopyIcon />}
                    </Button>
                  </div>
                </div>
              </div>
              <DialogFooter>
                <Button onClick={handleClose}>
                  <Trans i18nKey="done" defaults="Done" />
                </Button>
              </DialogFooter>
            </>
          ) : (
            <>
              <DialogHeader>
                <DialogTitle>
                  <Trans i18nKey="addWebhook" defaults="Add webhook" />
                </DialogTitle>
              </DialogHeader>
              <Form {...form}>
                <form
                  onSubmit={form.handleSubmit(async (values) => {
                    try {
                      const data = await createWebhook.mutateAsync(values);
                      if (data.ok) {
                        setCreatedSecret(data.secret);
                        form.reset();
                      } else if (data.reason === "max_webhooks_exceeded") {
                        toast.error(
                          t("webhookLimitReached", {
                            defaultValue:
                              "You've reached the maximum number of webhooks. Delete one before adding another.",
                          }),
                        );
                      }
                    } catch {
                      // The mutation cache toasts the error
                    }
                  })}
                >
                  <div className="space-y-4">
                    <FormField
                      control={form.control}
                      name="url"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>
                            <Trans i18nKey="webhookUrl" defaults="URL" />
                          </FormLabel>
                          <FormControl>
                            <Input
                              {...field}
                              type="url"
                              inputMode="url"
                              placeholder="https://"
                              autoFocus
                              {...passwordManagerIgnoreProps}
                            />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                    <FormField
                      control={form.control}
                      name="events"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>
                            <Trans i18nKey="events" defaults="Events" />
                          </FormLabel>
                          <FormControl>
                            <WebhookEventCombobox
                              value={field.value}
                              onValueChange={field.onChange}
                              onBlur={field.onBlur}
                            />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>
                  <DialogFooter>
                    <DialogClose render={<Button />}>
                      <Trans i18nKey="cancel" defaults="Cancel" />
                    </DialogClose>
                    <Button
                      variant="primary"
                      type="submit"
                      disabled={createWebhook.isPending}
                      loading={createWebhook.isPending}
                    >
                      <Trans i18nKey="addWebhook" defaults="Add webhook" />
                    </Button>
                  </DialogFooter>
                </form>
              </Form>
            </>
          )}
        </DialogContent>
      </Dialog>
    </>
  );
}