import React from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import { WorkspaceService } from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // icons import { AlertTriangle } from "lucide-react"; // ui import { Button, Input } from "@plane/ui"; // types import type { IUser, IWorkspace } from "types"; // fetch-keys import { USER_WORKSPACES } from "constants/fetch-keys"; type Props = { isOpen: boolean; data: IWorkspace | null; onClose: () => void; user: IUser | undefined; }; const defaultValues = { workspaceName: "", confirmDelete: "", }; // services const workspaceService = new WorkspaceService(); export const DeleteWorkspaceModal: React.FC = ({ isOpen, data, onClose, user }) => { const router = useRouter(); const { setToastAlert } = useToast(); const { control, formState: { errors, isSubmitting }, handleSubmit, reset, watch, } = useForm({ defaultValues }); const canDelete = watch("workspaceName") === data?.name && watch("confirmDelete") === "delete my workspace"; const handleClose = () => { const timer = setTimeout(() => { reset(defaultValues); clearTimeout(timer); }, 350); onClose(); }; const onSubmit = async () => { if (!data || !canDelete) return; await workspaceService .deleteWorkspace(data.slug, user) .then(() => { handleClose(); router.push("/"); mutate(USER_WORKSPACES, (prevData) => prevData?.filter((workspace) => workspace.id !== data.id)); setToastAlert({ type: "success", title: "Success!", message: "Workspace deleted successfully.", }); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again later.", }) ); }; return (

Delete Workspace

Are you sure you want to delete workspace{" "} {data?.name}? All of the data related to the workspace will be permanently removed. This action cannot be undone.

Enter the workspace name {data?.name} to continue:

( )} />

To confirm, type delete my workspace{" "} below:

( )} />
); };