import { useMemo } from "react"; import { ArrowDown, ArrowUp, ArrowUpDown } from "lucide-react"; import { Input } from "@/shared/ui/input"; import { Button } from "@/shared/ui/button"; import { FloatingWindow } from "@/shared/ui/floating-window"; import { cn } from "@/shared/ui/cn"; import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/shared/ui/table"; import { useAnswerNodeState } from "./answer-view-state-provider"; import type { AnswerTableState } from "./answer-view-state"; export function InteractiveDataTable({ title, columns, rows, state: controlledState, onStateChange, expanded = false }: { title: string; columns: string[]; rows: string[][]; state?: Omit; onStateChange?: (patch: Partial>) => void; expanded?: boolean; }) { const [localState, patchLocal] = useAnswerNodeState("Table", { title, columns, rows }); const state = controlledState ?? localState; const patch = onStateChange ?? patchLocal; const { query, sort } = state; const filtered = useMemo(() => { const items = rows.map((cells, id) => ({ cells, id })) .filter(({ cells }) => cells.join(" ").toLocaleLowerCase().includes(query.trim().toLocaleLowerCase())); if (sort) items.sort((a, b) => { const aValue = a.cells[sort.column], bValue = b.cells[sort.column]; const compared = /^[-+]?\d+(\.\d+)?%?$/.test(aValue) && /^[-+]?\d+(\.\d+)?%?$/.test(bValue) ? Number.parseFloat(aValue) - Number.parseFloat(bValue) : aValue.localeCompare(bValue, "zh-CN", { numeric: true }); return sort.descending ? -compared : compared; }); return items; }, [rows, sort, query]); return
{ patch({ query: event.currentTarget.value }); }} /> {filtered.length} / {rows.length} 行 {!expanded ? {() => } : null}
{title} {columns.map((column, index) => )} {filtered.map(({ cells, id }) => {cells.map((cell, index) => {cell})} )} {!filtered.length ? {rows.length ? "当前筛选没有匹配的数据。" : "没有已加载的数据。"} : null}
; }