"use client"; import { FormEvent, useEffect, useState } from "react"; import { api, apiForm } from "@/lib/api"; import { Modal, RegistryTable } from "@/components/registry-table"; type OperationFile = { id: string; name: string; type: string; sizeBytes: number; createdAt: string; }; function formatSize(bytes: number) { if (bytes < 1024) { return `${bytes} B`; } return `${(bytes / 1024).toFixed(1)} KB`; } export default function ArquivosPage() { const [rows, setRows] = useState([]); const [file, setFile] = useState(null); const [open, setOpen] = useState(false); const [error, setError] = useState(""); const [saving, setSaving] = useState(false); async function load() { setRows(await api("/api/files")); } useEffect(() => { load().catch((err: unknown) => setError(err instanceof Error ? err.message : "Erro")); }, []); function closeModal() { setOpen(false); setFile(null); setError(""); } async function onSubmit(event: FormEvent) { event.preventDefault(); if (!file) { setError("Selecione um arquivo."); return; } setError(""); setSaving(true); try { const body = new FormData(); body.append("file", file); await apiForm("/api/files", body); closeModal(); await load(); } catch (err) { setError(err instanceof Error ? err.message : "Não foi possível anexar."); } finally { setSaving(false); } } return (

Arquivos

Anexos da operação

{error && !open ?

{error}

: null} setOpen(true)}> Anexar } > {rows.map((row) => ( {row.name} {row.type} {formatSize(row.sizeBytes)} {new Date(row.createdAt).toLocaleString("pt-BR")} Baixar ))} {open ? (

Anexar arquivo

{error ?

{error}

: null} setFile(event.target.files?.[0] ?? null)} />
) : null}
); }