"use client" import * as React from "react" import { type Row } from "@tanstack/react-table" import { Loader, Trash, AlertCircle } from "lucide-react" import { toast } from "sonner" import { useMediaQuery } from "@/hooks/use-media-query" import { Button } from "@/components/ui/button" import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@/components/ui/drawer" import { Alert, AlertDescription } from "@/components/ui/alert" import { DocumentStagesView } from "@/db/schema/vendorDocu" import { removeDocuments } from "../service" interface DeleteDocumentsDialogProps extends React.ComponentPropsWithoutRef { documents: Row["original"][] showTrigger?: boolean onSuccess?: () => void } export function DeleteDocumentsDialog({ documents, showTrigger = true, onSuccess, ...props }: DeleteDocumentsDialogProps) { const [isDeletePending, startDeleteTransition] = React.useTransition() const isDesktop = useMediaQuery("(min-width: 640px)") // "pending" 상태인 문서만 필터링 const pendingDocuments = documents.filter(doc => doc.status === "pending") const nonPendingDocuments = documents.filter(doc => doc.status !== "pending") const hasMixedStatus = pendingDocuments.length > 0 && nonPendingDocuments.length > 0 const hasNoPendingDocuments = pendingDocuments.length === 0 function onDelete() { // 삭제할 문서가 없으면 경고 if (pendingDocuments.length === 0) { toast.error("No pending documents to delete") props.onOpenChange?.(false) return } startDeleteTransition(async () => { // "pending" 상태인 문서 ID만 전달 const { success, error } = await removeDocuments({ ids: pendingDocuments.map((document) => document.documentId) }) if (!success) { toast.error(error || "Failed to delete documents") return } props.onOpenChange?.(false) // 적절한 성공 메시지 표시 if (hasMixedStatus) { toast.success(`${pendingDocuments.length} pending document(s) deleted successfully. ${nonPendingDocuments.length} non-pending document(s) were not affected.`) } else { toast.success(`${pendingDocuments.length} document(s) deleted successfully`) } onSuccess?.() }) } // 선택된 문서 상태에 대한 알림 메시지 렌더링 const renderStatusAlert = () => { if (hasNoPendingDocuments) { return ( None of the selected documents are in "pending" status. Only pending documents can be deleted. ) } if (hasMixedStatus) { return ( Only the {pendingDocuments.length} document(s) with "pending" status will be deleted. {nonPendingDocuments.length} document(s) cannot be deleted because they are not in pending status. ) } return null } if (isDesktop) { return ( {showTrigger ? ( ) : null} Are you absolutely sure? This action cannot be undone. Only documents with "pending" status can be deleted. {renderStatusAlert()}
{pendingDocuments.length > 0 && (

{pendingDocuments.length} pending document(s) will be deleted:

)} {pendingDocuments.length > 0 && (
    {pendingDocuments.map(doc => (
  • {doc.docNumber} - {doc.title}
  • ))}
)}
) } return ( {showTrigger ? ( ) : null} Are you absolutely sure? This action cannot be undone. Only documents with "pending" status can be deleted. {renderStatusAlert()}
{pendingDocuments.length > 0 && (

{pendingDocuments.length} pending document(s) will be deleted:

)} {pendingDocuments.length > 0 && (
    {pendingDocuments.map(doc => (
  • {doc.docNumber} - {doc.title}
  • ))}
)}
) }