"use client"; import { RiDownloadLine, RiUploadCloud2Line } from "@remixicon/react"; import { useRef, useState } from "react"; import { parseOfx } from "@/shared/lib/import/ofx-parser"; import type { ImportStatement } from "@/shared/lib/import/types"; import { generateXlsTemplate, parseXls } from "@/shared/lib/import/xls-parser"; interface UploadZoneProps { onParsed: (statement: ImportStatement) => void; } export function UploadZone({ onParsed }: UploadZoneProps) { const [error, setError] = useState(null); const [dragging, setDragging] = useState(false); const inputRef = useRef(null); const handleFile = (file: File) => { setError(null); const isOfx = /\.(ofx|qfx)$/i.test(file.name); const isXls = /\.(xlsx|xls)$/i.test(file.name); if (!isOfx && !isXls) { setError("Formato não suportado. Use .ofx, .qfx, .xlsx ou .xls."); return; } if (isOfx) { const reader = new FileReader(); reader.onload = (e) => { try { const content = e.target?.result as string; const statement = parseOfx(content); if (statement.transactions.length === 0) { setError("Nenhuma transação encontrada no arquivo."); return; } onParsed(statement); } catch { setError( "Não foi possível ler o arquivo. Verifique se é um OFX válido.", ); } }; reader.readAsText(file, "windows-1252"); } else { const reader = new FileReader(); reader.onload = (e) => { try { const buffer = e.target?.result as ArrayBuffer; const statement = parseXls(buffer); onParsed(statement); } catch (err) { setError( err instanceof Error ? err.message : "Não foi possível ler a planilha.", ); } }; reader.readAsArrayBuffer(file); } }; const handleDownloadTemplate = () => { const bytes = generateXlsTemplate(); const blob = new Blob([bytes], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "modelo-lancamentos.xlsx"; a.click(); URL.revokeObjectURL(url); }; return (
{ const file = e.target.files?.[0]; if (file) handleFile(file); e.target.value = ""; }} />
{error ?

{error}

: }
); }