import { useMemo } from "react"; import { parseCsv } from "@/lib/csv-preview"; const MAX_VISIBLE_ROWS = 500; const MAX_VISIBLE_COLUMNS = 100; export function CsvPreview({ text, title }: { text: string; title: string }) { const parsed = useMemo(() => { try { return { rows: parseCsv(text), error: null }; } catch (error) { return { rows: [], error: (error as Error).message }; } }, [text]); if (parsed.error) return

{parsed.error}

; if (!parsed.rows.length) return

File is empty.

; const [header, ...rows] = parsed.rows; const columnCount = parsed.rows.reduce((count, row) => Math.max(count, row.length), 0); const columns = Array.from({ length: Math.min(columnCount, MAX_VISIBLE_COLUMNS) }, (_, index) => index); return (
CSV preview {rows.length.toLocaleString()} {rows.length === 1 ? "row" : "rows"} {columnCount.toLocaleString()} {columnCount === 1 ? "column" : "columns"} {(rows.length > MAX_VISIBLE_ROWS || columnCount > MAX_VISIBLE_COLUMNS) && Showing the first {Math.min(rows.length, MAX_VISIBLE_ROWS)} rows and {columns.length} columns. Download for the complete file.}
{columns.map((column) => )} {rows.slice(0, MAX_VISIBLE_ROWS).map((row, index) => {columns.map((column) => )} )}
{title} — first record used as column headers
Row#{header[column] || `Column ${column + 1}`}
{index + 1}{row[column] ?? ""}
); }