Open History to see the last commit
1/* oxlint-disable react/no-array-index-key -- Table rows and columns have stable positions and may contain identical values. */
2import { parseDelimitedPreview } from "@t3tools/shared/delimitedPreview";
3import { useMemo } from "react";
4
5import { FileSurfaceNotice } from "./fileSurfaceChrome";
6
7export function DelimitedTablePreview(props: {
8 readonly name: string;
9 readonly text: string;
10 readonly delimiter: "," | "\t";
11}) {
12 const table = useMemo(
13 () => parseDelimitedPreview(props.text, props.delimiter),
14 [props.text, props.delimiter],
15 );
16 const [header, ...body] = table.rows;
17 return (
18 <div className="flex min-h-0 flex-1 flex-col">
19 {table.truncated ? (
20 <FileSurfaceNotice>
21 Table limited to the first 100 rows and 30 columns. Switch to source for the rest.
22 </FileSurfaceNotice>
23 ) : null}
24 <div className="min-h-0 flex-1 overflow-auto">
25 <table
26 className="min-w-full border-separate border-spacing-0 text-xs"
27 aria-label={props.name}
28 >
29 {header ? (
30 <thead className="sticky top-0 z-10">
31 <tr>
32 {header.map((cell, columnIndex) => (
33 <th
34 key={columnIndex}
35 scope="col"
36 className="max-w-80 border-b border-border bg-muted/60 px-3 py-1.5 text-left align-bottom font-medium whitespace-pre-wrap break-words backdrop-blur"
37 >
38 {cell}
39 </th>
40 ))}
41 </tr>
42 </thead>
43 ) : null}
44 <tbody>
45 {body.map((row, rowIndex) => (
46 <tr key={rowIndex} className="even:bg-muted/30">
47 {row.map((cell, columnIndex) => (
48 <td
49 key={columnIndex}
50 className="max-w-80 border-b border-border/60 px-3 py-1.5 align-top whitespace-pre-wrap break-words tabular-nums"
51 >
52 {cell}
53 </td>
54 ))}
55 </tr>
56 ))}
57 </tbody>
58 </table>
59 </div>
60 </div>
61 );
62}