"use client"; import { RiArrowLeftSLine, RiArrowRightSLine, RiCloseLine, RiDownloadLine, RiExternalLinkLine, } from "@remixicon/react"; import { useEffect, useState } from "react"; import type { AttachmentForPeriod } from "@/features/attachments/queries"; import { Button } from "@/shared/components/ui/button"; import { Dialog, DialogClose, DialogContent, DialogHeader, DialogTitle, } from "@/shared/components/ui/dialog"; interface AttachmentPreviewProps { attachments: AttachmentForPeriod[]; selectedIndex: number; onClose: () => void; } export function AttachmentPreview({ attachments, selectedIndex, onClose, }: AttachmentPreviewProps) { const [currentIndex, setCurrentIndex] = useState(selectedIndex); const [previewUrl, setPreviewUrl] = useState(null); const open = selectedIndex >= 0; useEffect(() => { if (selectedIndex >= 0) setCurrentIndex(selectedIndex); }, [selectedIndex]); useEffect(() => { if (!open) return; function handleKey(e: KeyboardEvent) { if (e.key === "ArrowLeft") setCurrentIndex((i) => Math.max(0, i - 1)); if (e.key === "ArrowRight") setCurrentIndex((i) => Math.min(attachments.length - 1, i + 1)); } window.addEventListener("keydown", handleKey); return () => window.removeEventListener("keydown", handleKey); }, [open, attachments.length]); const attachment = attachments[currentIndex]; const attachmentId = attachment?.attachmentId; // Busca URL fresca a cada troca de anexo useEffect(() => { if (!attachmentId) return; setPreviewUrl(null); fetch(`/api/attachments/${attachmentId}/presign`) .then((r) => r.json()) .then((data: { url: string }) => setPreviewUrl(data.url)) .catch(() => {}); }, [attachmentId]); if (!attachment) return null; const isPdf = attachment.mimeType === "application/pdf"; const isImage = attachment.mimeType.startsWith("image/"); const hasPrev = currentIndex > 0; const hasNext = currentIndex < attachments.length - 1; return ( { if (!o) onClose(); }} >
{attachment.transactionName}

{attachment.fileName}

{attachments.length > 1 && ( <> {currentIndex + 1} / {attachments.length} )}
{!previewUrl && (
)} {isPdf && previewUrl && (