Skip to content

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

4.0 KB
Raw Blame
"use client";

import { passwordManagerIgnoreProps } from "@rallly/ui";
import { Button } from "@rallly/ui/button";
import type { DialogProps } from "@rallly/ui/dialog";
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} 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 { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { Trans, useTranslation } from "@/i18n/client";
import { trpc } from "@/trpc/client";

interface LeaveSpaceDialogProps extends DialogProps {
  spaceName: string;
  spaceId: string;
}

const formName = "leave-space-form";

function LeaveSpaceDialog({
  spaceName,
  spaceId,
  children,
  ...rest
}: LeaveSpaceDialogProps) {
  const form = useForm<{ spaceName: string }>({
    defaultValues: {
      spaceName: "",
    },
  });

  const leaveSpace = trpc.spaces.leave.useMutation({
    onSuccess: () => {
      toast.success(
        t("leftSpaceSuccess", {
          defaultValue: "You have left the space",
        }),
      );
      router.push("/");
    },
  });

  const router = useRouter();
  const { t } = useTranslation();

  return (
    <Form {...form}>
      <Dialog {...rest}>
        {children}

        <DialogContent>
          <DialogHeader>
            <DialogTitle>
              <Trans i18nKey="leaveSpaceDialogTitle" defaults="Leave space" />
            </DialogTitle>
            <DialogDescription>
              <Trans
                i18nKey="leaveSpaceDialogDescription"
                defaults="Are you sure you want to leave this space?"
              />
            </DialogDescription>
          </DialogHeader>
          <form
            id={formName}
            onSubmit={form.handleSubmit(() => {
              leaveSpace.mutate();
            })}
          >
            <FormField
              control={form.control}
              name="spaceName"
              rules={{
                validate: (value) => {
                  if (value !== spaceName) {
                    return t("spaceNameMismatch", {
                      defaultValue: "Space name does not match",
                    });
                  }
                  return true;
                },
              }}
              render={({ field }) => (
                <FormItem>
                  <FormLabel>
                    <Trans
                      i18nKey="leaveSpaceInstruction"
                      defaults="Please type the space name to confirm:"
                    />
                  </FormLabel>
                  <FormControl>
                    <Input
                      autoComplete="off"
                      {...passwordManagerIgnoreProps}
                      placeholder={spaceName}
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
          </form>
          <DialogFooter>
            <DialogClose render={<Button />}>
              <Trans i18nKey="cancel" defaults="Cancel" />
            </DialogClose>
            <Button
              form={formName}
              type="submit"
              loading={leaveSpace.isPending}
              variant="destructive"
            >
              <Trans i18nKey="leaveSpace" defaults="Leave space" />
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </Form>
  );
}

interface LeaveSpaceButtonProps {
  spaceName: string;
  spaceId: string;
}

export function LeaveSpaceButton({
  spaceName,
  spaceId,
}: LeaveSpaceButtonProps) {
  return (
    <LeaveSpaceDialog spaceName={spaceName} spaceId={spaceId}>
      <DialogTrigger render={<Button className="text-destructive" />}>
        <Trans i18nKey="leaveSpace" defaults="Leave space" />
      </DialogTrigger>
    </LeaveSpaceDialog>
  );
}