"use client"; import { RiFileLine, RiFilePdf2Line, RiImageLine } from "@remixicon/react"; import Image from "next/image"; import { useEffect, useRef, useState } from "react"; import type { AttachmentForPeriod } from "@/features/attachments/queries"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/shared/components/ui/tooltip"; import { cn } from "@/shared/utils"; import { formatCurrency } from "@/shared/utils/currency"; import { formatDate } from "@/shared/utils/date"; import { formatBytes } from "@/shared/utils/number"; interface PdfCanvasProps { url: string; } function PdfCanvas({ url }: PdfCanvasProps) { const canvasRef = useRef(null); const [locked, setLocked] = useState(false); useEffect(() => { let cancelled = false; setLocked(false); async function render() { const pdfjsLib = await import("pdfjs-dist"); pdfjsLib.GlobalWorkerOptions.workerSrc = "/pdf.worker.min.mjs"; let pdf: Awaited["promise"]>; try { pdf = await pdfjsLib.getDocument(url).promise; } catch (err) { if ((err as { name?: string }).name === "PasswordException") { if (!cancelled) setLocked(true); } return; } const page = await pdf.getPage(1); const canvas = canvasRef.current; if (!canvas || cancelled) return; const containerWidth = canvas.parentElement?.offsetWidth ?? 200; const viewport = page.getViewport({ scale: 1 }); const scale = containerWidth / viewport.width; const scaled = page.getViewport({ scale }); canvas.width = scaled.width; canvas.height = scaled.height; const ctx = canvas.getContext("2d"); if (!ctx) return; await page.render({ canvasContext: ctx, canvas, viewport: scaled }) .promise; } render().catch(() => {}); return () => { cancelled = true; }; }, [url]); if (locked) { return (
PDF Protegido
); } return ( ); } interface AttachmentGridItemProps { attachment: AttachmentForPeriod; url?: string; onClick: () => void; onDetails: () => void; isLoadingDetails?: boolean; } export function AttachmentGridItem({ attachment, url, onClick, onDetails, isLoadingDetails = false, }: AttachmentGridItemProps) { const isPdf = attachment.mimeType === "application/pdf"; const isImage = attachment.mimeType.startsWith("image/"); const amount = Number.parseFloat(attachment.transactionAmount); return (
{/* Thumbnail */} {/* Informações */}
{/* Nome do arquivo + tipo */}
{isPdf && } {isImage && } {!isPdf && !isImage && }

{attachment.fileName}

{attachment.fileName}
{/* Data */} {formatDate(attachment.purchaseDate)} {/* Transação e Valor */}

{attachment.transactionName}

{attachment.transactionName}
{formatCurrency(amount)}
{/* Footer: Tamanho + Botão Detalhes */}
{formatBytes(attachment.fileSize)}
); }