import React, { useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; import { Dialog, Transition } from "@headlessui/react"; // constants import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; import type { IWorkspace } from "types"; import { USER_WORKSPACES } from "constants/fetch-keys"; // services import workspaceService from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // icons // ui import { Button, Input } from "components/ui"; // types type Props = { isOpen: boolean; data: IWorkspace | null; onClose: () => void; }; const ConfirmWorkspaceDeletion: React.FC = ({ isOpen, data, onClose }) => { const cancelButtonRef = useRef(null); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [confirmProjectName, setConfirmProjectName] = useState(""); const [confirmDeleteMyProject, setConfirmDeleteMyProject] = useState(false); const [selectedWorkspace, setSelectedWorkspace] = useState(null); const router = useRouter(); const { setToastAlert } = useToast(); useEffect(() => { if (data) setSelectedWorkspace(data); else { const timer = setTimeout(() => { setSelectedWorkspace(null); clearTimeout(timer); }, 350); } }, [data]); const canDelete = confirmProjectName === data?.name && confirmDeleteMyProject; const handleClose = () => { onClose(); setIsDeleteLoading(false); }; const handleDeletion = async () => { setIsDeleteLoading(true); if (!data || !canDelete) return; await workspaceService .deleteWorkspace(data.slug) .then(() => { handleClose(); router.push("/"); mutate(USER_WORKSPACES, (prevData) => prevData?.filter((workspace) => workspace.id !== data.id) ); setToastAlert({ type: "success", message: "Workspace deleted successfully", title: "Success", }); }) .catch((error) => { console.log(error); setIsDeleteLoading(false); }); }; 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{" "} {selectedWorkspace?.name} to continue:

{ setConfirmProjectName(e.target.value); }} name="workspaceName" />

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

{ if (e.target.value === "delete my workspace") { setConfirmDeleteMyProject(true); } else { setConfirmDeleteMyProject(false); } }} name="typeDelete" />
); }; export default ConfirmWorkspaceDeletion;