"use client"; import { useState } from "react"; import { ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, SortingState, } from "@tanstack/react-table"; import { useQuery, useQueryClient } from "@tanstack/react-query"; interface FileData { id: string; filename: string; mimetype: string; size: number; createdAt: string; updatedAt: string; } const columns: ColumnDef[] = [ { accessorKey: "filename", header: "File Name", cell: ({ row }) => (
{row.getValue("filename")}
), }, { accessorKey: "mimetype", header: "Type", cell: ({ row }) => (
{row.getValue("mimetype")}
), }, { accessorKey: "size", header: "Size", cell: ({ row }) => { const bytes = row.getValue("size") as number; if (bytes === 0) return "0 Bytes"; const k = 1024; const sizes = ["Bytes", "KB", "MB", "GB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]; }, }, { accessorKey: "createdAt", header: "Created", cell: ({ row }) => { const date = new Date(row.getValue("createdAt")); return date.toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); }, }, { accessorKey: "updatedAt", header: "Updated", cell: ({ row }) => { const date = new Date(row.getValue("updatedAt")); return date.toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); }, }, ]; export function FilesTable() { const [sorting, setSorting] = useState([]); const queryClient = useQueryClient(); const { data, isLoading, isError, error, refetch } = useQuery({ queryKey: ["files"], queryFn: async () => { const response = await fetch("/api/files"); if (!response.ok) { throw new Error("Failed to fetch files"); } const data = await response.json(); return data.files as FileData[]; }, }); const table = useReactTable({ data: data || [], columns, state: { sorting, }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), }); const handleRefresh = () => { refetch(); }; if (isLoading) { return (

Files in Database

{[...Array(5)].map((_, i) => (
))}
); } if (isError) { return (
Error: {error?.message || "Failed to load files"}
); } if (!data || data.length === 0) { return (

No files found

Upload some files to get started.

); } return (

Files in Database

{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( ))} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( ))} ))}
{flexRender(header.column.columnDef.header, header.getContext())} {header.column.getIsSorted() && ( {header.column.getIsSorted() === "asc" ? "↑" : "↓"} )}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
{/* Pagination */}
Showing {table.getRowModel().rows.length} of {data.length} results
); }