"use client"; import { useRef, useState } from "react"; import { AnimatePresence, motion } from "motion/react"; import { Check, FileSpreadsheet, FileText, Minus, Trash2 } from "lucide-react"; import { ConfirmMorph } from "./confirm-morph"; import { cn } from "@/lib/utils"; export const deleteFiles = async (ids: string[]) => { await new Promise((resolve) => setTimeout(resolve, 900)); return ids; }; export const restoreFiles = async (ids: string[]) => { await new Promise((resolve) => setTimeout(resolve, 700)); return ids; }; export interface DeleteSelectionProps { ids: string[]; onConfirm?: (ids: string[]) => void | Promise; onUndo?: (ids: string[]) => void | Promise; disabled?: boolean; } export function DeleteSelection({ ids, onConfirm, onUndo, disabled, }: DeleteSelectionProps) { const count = ids.length; const promptText = `Delete ${count} ${count === 1 ? "file" : "files"}?`; return ( } prompt={promptText} tone="danger" disabled={disabled || count === 0} onConfirm={() => (onConfirm ? onConfirm(ids) : deleteFiles(ids))} onUndo={() => (onUndo ? onUndo(ids) : restoreFiles(ids))} /> ); } interface FileItem { id: string; name: string; author: string; date: string; size: string; type: "image" | "pdf" | "sheet"; thumbnail?: string; } const INITIAL_FILES: FileItem[] = [ { id: "1", name: "pookie virat 01.jpg", author: "Pookie Virat", date: "Sep 21", size: "4.2 MB", type: "image", thumbnail: "https://i.pinimg.com/736x/1d/49/b5/1d49b5da4ebfc59d5d7d4f1c288ecf17.jpg", }, { id: "2", name: "pookie virat 02.jpg", author: "Pookie Virat", date: "Sep 20", size: "3.8 MB", type: "image", thumbnail: "https://i.pinimg.com/1200x/63/01/21/630121fe689e8d2c43b0b10180d31f84.jpg", }, { id: "3", name: "pookie virat 03.jpg", author: "Pookie Virat", date: "Sep 19", size: "5.1 MB", type: "image", thumbnail: "https://i.pinimg.com/736x/54/cc/b5/54ccb5138e6452e45dcec18aee7b9f39.jpg", }, { id: "4", name: "pookie virat 04.jpg", author: "Pookie Virat", date: "Sep 18", size: "2.9 MB", type: "image", thumbnail: "https://i.pinimg.com/736x/25/4b/0b/254b0b03a39fc3736053c28263fa5053.jpg", }, { id: "5", name: "pookie virat 05.jpg", author: "Pookie Virat", date: "Sep 17", size: "4.6 MB", type: "image", thumbnail: "https://i.pinimg.com/736x/78/68/5e/78685eb464deaa7a7cc9cad4b12e9ef9.jpg", }, ]; export function DeleteSelectionShowcase() { const [files, setFiles] = useState(INITIAL_FILES); const [selectedIds, setSelectedIds] = useState(["2", "3"]); const [outcome, setOutcome] = useState<"succeeds" | "fails">("succeeds"); const [lastDeleted, setLastDeleted] = useState<{ items: FileItem[]; ids: string[]; } | null>(null); const highlightRef = useRef(null); const itemRefs = useRef<(HTMLDivElement | null)[]>([]); const handleMouseEnter = (index: number) => { const el = itemRefs.current[index]; const hl = highlightRef.current; if (!el || !hl) return; hl.style.transform = `translate3d(${el.offsetLeft}px, ${el.offsetTop}px, 0)`; hl.style.width = `${el.offsetWidth}px`; hl.style.height = `${el.offsetHeight}px`; hl.style.opacity = "1"; }; const handleMouseLeave = () => { const hl = highlightRef.current; if (!hl) return; hl.style.opacity = "0"; }; const toggleSelect = (id: string) => { setSelectedIds((prev) => prev.includes(id) ? prev.filter((item) => item !== id) : [...prev, id], ); }; const toggleAll = () => { if (selectedIds.length > 0) { setSelectedIds([]); } else { setSelectedIds(files.map((f) => f.id)); } }; const handleConfirm = async (ids: string[]) => { await new Promise((resolve) => setTimeout(resolve, 900)); if (outcome === "fails") { throw new Error("Failed to delete files"); } const removed = files.filter((f) => ids.includes(f.id)); setLastDeleted({ items: removed, ids }); setFiles((prev) => prev.filter((f) => !ids.includes(f.id))); setSelectedIds([]); handleMouseLeave(); }; const handleUndo = async () => { await new Promise((resolve) => setTimeout(resolve, 700)); if (lastDeleted) { setFiles((prev) => { const merged = [...prev]; for (const item of lastDeleted.items) { if (!merged.some((m) => m.id === item.id)) { merged.push(item); } } return merged.sort( (a, b) => INITIAL_FILES.findIndex((f) => f.id === a.id) - INITIAL_FILES.findIndex((f) => f.id === b.id), ); }); setSelectedIds(lastDeleted.ids); setLastDeleted(null); } }; const isAllSelected = files.length > 0 && selectedIds.length === files.length; const isPartiallySelected = selectedIds.length > 0 && selectedIds.length < files.length; return (
{files.length > 0 && ( {isAllSelected ? ( ) : isPartiallySelected ? ( ) : null} )}
pookie virat {files.length === 0 ? "No files remaining" : `${selectedIds.length} of ${files.length} selected`}
{files.length > 0 && (
); } export default DeleteSelectionShowcase;