Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add delete domain modal
Marc Seitze947ef005baa
diff --git a/components/domains/delete-domain-modal.tsx b/components/domains/delete-domain-modal.tsxnew file mode 100644index 00000000..f38a2450--- /dev/null+++ b/components/domains/delete-domain-modal.tsx@@ -0,0 +1,170 @@+import {+ Dispatch,+ SetStateAction,+ useCallback,+ useMemo,+ useState,+} from "react";++import { useTeam } from "@/context/team-context";+import { toast } from "sonner";++import { Button } from "@/components/ui/button";+import { DialogDescription, DialogTitle } from "@/components/ui/dialog";+import { Input } from "@/components/ui/input";+import { Modal } from "@/components/ui/modal";++import { useAnalytics } from "@/lib/analytics";++function DeleteDomainModal({+ showDeleteDomainModal,+ setShowDeleteDomainModal,+ domain,+ onDelete,+}: {+ showDeleteDomainModal: boolean;+ setShowDeleteDomainModal: Dispatch<SetStateAction<boolean>>;+ domain: string;+ onDelete: (deletedDomain: string) => void;+}) {+ const teamInfo = useTeam();+ const analytics = useAnalytics();++ const [deleting, setDeleting] = useState(false);+ const [isValid, setIsValid] = useState(false);++ const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {+ const { value } = e.target;+ // Check if the input matches the pattern+ if (value === "permanently delete") {+ setIsValid(true);+ } else {+ setIsValid(false);+ }+ };++ async function deleteDomain() {+ return new Promise(async (resolve, reject) => {+ setDeleting(true);++ try {+ const response = await fetch(+ `/api/teams/${teamInfo?.currentTeam?.id}/domains/${domain}`,+ {+ method: "DELETE",+ },+ );++ if (!response.ok) {+ throw new Error(`HTTP error! status: ${response.status}`);+ }++ analytics.capture("Domain Deleted", {+ slug: domain,+ teamId: teamInfo?.currentTeam?.id,+ });++ // Update local data by filtering out the deleted domain+ onDelete(domain);+ setDeleting(false);+ resolve(null);+ } catch (error) {+ setDeleting(false);+ reject((error as Error).message);+ } finally {+ setShowDeleteDomainModal(false);+ }+ });+ }++ return (+ <Modal+ showModal={showDeleteDomainModal}+ setShowModal={setShowDeleteDomainModal}+ noBackdropBlur+ >+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-border bg-white px-4 py-4 pt-8 dark:border-gray-900 dark:bg-gray-900 sm:px-8">+ <DialogTitle className="text-2xl">Delete Domain</DialogTitle>+ <DialogDescription>+ This will permanently delete your domain. Links using this domain will+ be reset to <span className="font-medium">papermark.io</span> links.+ This action cannot be undone.+ <div className="mt-3 text-sm font-medium text-foreground">+ {domain}{" "}+ <span className="text-muted-foreground">→ papermark.io</span>+ </div>+ </DialogDescription>+ </div>++ <form+ onSubmit={async (e) => {+ e.preventDefault();+ toast.promise(deleteDomain(), {+ loading: "Deleting domain...",+ success: "Domain deleted successfully!",+ error: (err) => err,+ });+ }}+ className="flex flex-col space-y-6 bg-muted px-4 py-8 text-left dark:bg-gray-900 sm:px-8"+ >+ <div>+ <label+ htmlFor="verification"+ className="block text-sm text-muted-foreground"+ >+ To confirm deletion, type{" "}+ <span className="font-semibold text-foreground">+ permanently delete+ </span>{" "}+ below+ </label>+ <div className="relative mt-1 rounded-md shadow-sm">+ <Input+ type="text"+ name="verification"+ id="verification"+ pattern="permanently delete"+ required+ autoComplete="off"+ className="bg-white dark:border-gray-500 dark:bg-gray-800 focus:dark:bg-transparent"+ onInput={handleInputChange}+ />+ </div>+ </div>++ <Button variant="destructive" loading={deleting} disabled={!isValid}>+ Confirm delete domain+ </Button>+ </form>+ </Modal>+ );+}++export function useDeleteDomainModal({+ domain,+ onDelete,+}: {+ domain: string;+ onDelete: (deletedDomain: string) => void;+}) {+ const [showDeleteDomainModal, setShowDeleteDomainModal] = useState(false);++ const DeleteDomainModalCallback = useCallback(() => {+ return (+ <DeleteDomainModal+ showDeleteDomainModal={showDeleteDomainModal}+ setShowDeleteDomainModal={setShowDeleteDomainModal}+ domain={domain}+ onDelete={onDelete}+ />+ );+ }, [showDeleteDomainModal, setShowDeleteDomainModal, domain, onDelete]);++ return useMemo(+ () => ({+ setShowDeleteDomainModal,+ DeleteDomainModal: DeleteDomainModalCallback,+ }),+ [setShowDeleteDomainModal, DeleteDomainModalCallback],+ );+}diff --git a/components/domains/domain-card.tsx b/components/domains/domain-card.tsxindex 905e6b9c..c0673a80 100644--- a/components/domains/domain-card.tsx+++ b/components/domains/domain-card.tsx@@ -1,5 +1,3 @@-import Link from "next/link";-import { useRef, useState } from "react";import { useTeam } from "@/context/team-context";@@ -16,18 +14,19 @@ import {} from "lucide-react";import { mutate } from "swr";-import { useAnalytics } from "@/lib/analytics";-import { cn } from "@/lib/utils";--import { Button } from "../ui/button";+import { Button } from "@/components/ui/button";import {DropdownMenu,DropdownMenuContent,DropdownMenuItem,DropdownMenuLabel,DropdownMenuTrigger,-} from "../ui/dropdown-menu";-import { StatusBadge } from "../ui/status-badge";+} from "@/components/ui/dropdown-menu";+import { StatusBadge } from "@/components/ui/status-badge";++import { cn } from "@/lib/utils";++import { useDeleteDomainModal } from "./delete-domain-modal";import DomainConfiguration from "./domain-configuration";import { useDomainStatus } from "./use-domain-status";@@ -40,7 +39,6 @@ export default function DomainCard({isDefault: boolean;onDelete: (deletedDomain: string) => void;}) {- const [deleting, setDeleting] = useState<boolean>(false);const [showDetails, setShowDetails] = useState(false);const [groupHover, setGroupHover] = useState(false);const [menuOpen, setMenuOpen] = useState(false);@@ -57,46 +55,27 @@ export default function DomainCard({domain,});const teamInfo = useTeam();- const analytics = useAnalytics();+ const teamId = teamInfo?.currentTeam?.id;const isInvalid =status && !["Valid Configuration", "Pending Verification"].includes(status);- const handleDelete = async () => {- setDeleting(true);- const response = await fetch(- `/api/teams/${teamInfo?.currentTeam?.id}/domains/${domain}`,- {- method: "DELETE",- },- );-- if (!response.ok) {- throw new Error(`HTTP error! status: ${response.status}`);- }-- analytics.capture("Domain Deleted", {- slug: domain,- teamId: teamInfo?.currentTeam?.id,- });-- // Update local data by filtering out the deleted domain- onDelete(domain);- setDeleting(false);- };+ const { setShowDeleteDomainModal, DeleteDomainModal } = useDeleteDomainModal({+ domain,+ onDelete,+ });const handleMakeDefault = async () => {- const response = await fetch(- `/api/teams/${teamInfo?.currentTeam?.id}/domains/${domain}`,- { method: "PATCH" },- );+ const response = await fetch(`/api/teams/${teamId}/domains/${domain}`, {+ method: "PATCH",+ });if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}// Update domains by refetching- mutate(`/api/teams/${teamInfo?.currentTeam?.id}/domains`);+ mutate(`/api/teams/${teamId}/domains`);};return (@@ -229,7 +208,7 @@ export default function DomainCard({</DropdownMenuItem><DropdownMenuItemclassName="text-destructive transition-colors duration-200 focus:bg-destructive focus:text-destructive-foreground"- onClick={handleDelete}+ onClick={() => setShowDeleteDomainModal(true)}><TrashIcon className="mr-2 h-4 w-4" />Delete domain@@ -263,6 +242,7 @@ export default function DomainCard({)}</motion.div></div>+ <DeleteDomainModal /></>);}