/** * A data grid with IDEA's result-view extras, shared by console results and * the table editor: the header's local filter (values of the loaded rows), * client-side sorting when the caller does not sort on the server, and the * Record / Value / Aggregates side panel following the selection. */ import { writeClipboardText } from "@/platform/clipboard"; import { useMemo, useRef, useState, type KeyboardEvent, type ReactNode, type RefObject } from "react"; import clsx from "clsx"; import { Button, Checkbox, Icon, IconButton, Input, Popup, SearchField, Select, Tabs, T, type MenuItem } from "@/ui"; import { EXPORT_FORMATS, exportData, type ExportData, type ExportFormat } from "./export"; import { Grid, type CopyFormat, type GridColumn, type GridProps, type GridRange } from "./Grid"; import type { Cell } from "./model"; import { aggregates, cellMatcher, distinctValues, findCells, formatNumber, rowMatches, toggleFilterValue, visibleRows, type LocalFilter, type LocalSort, type SearchOptions, } from "./resultView"; import { openDialog } from "./store"; import { hasLineBreak } from "./cellPreview"; import { initialJsonText, isJsonColumn, isJsonValue, jsonErrorOf, JSON_INLINE_LIMIT, type JsonError, } from "./jsonValue"; import { JsonValueEditor, JsonValueWindow, type JsonEditorApi } from "./JsonValueEditor"; import styles from "./Database.module.css"; import { tx, txPart } from "@/platform/i18n"; export type DataPanel = "record" | "value" | "aggregates"; /** The Export button's menu: the Export Data dialog (with the toolbar's extractor), then IDEA's quick Copy as… extractors. */ export function exportMenu(data: ExportData, source: string, format?: ExportFormat): MenuItem[] { return [ { label: "Export Data…", icon: "export", run: () => openDialog({ kind: "export", data, source, format }) }, { kind: "separator" }, ...EXPORT_FORMATS.map((f): MenuItem => ({ label: tx`Copy as ${txPart(f.label)}`, icon: "copy", run: () => void writeClipboardText(exportData(f.id, data)), })), ]; } /** IDEA's extractor drop-down at the right of a data toolbar ("CSV ▾"). */ export function ExtractorSelect({ value, onChange }: { value: ExportFormat; onChange: (f: ExportFormat) => void }) { return ( setQuery(e.target.value)} />
{list.map(({ value: v, count }) => (
props.onToggle(v, on)} label={v === null ? <null> : v} /> {count}
))} {!list.length && (
No matching values
)}
); } const AGGREGATE_ROWS: ReadonlyArray<[label: string, key: keyof ReturnType]> = [ ["Count", "count"], ["Distinct", "distinct"], ["Nulls", "nulls"], ["Sum", "sum"], ["Avg", "avg"], ["Median", "median"], ["Min", "min"], ["Max", "max"], ]; function SidePanel(props: { panel: DataPanel; onPanelChange: (p: DataPanel | null) => void; columns: readonly GridColumn[]; range: GridRange | null; value: (row: number, col: number) => Cell; truncated?: GridProps["truncated"]; loadFull?: GridProps["loadFull"]; editable: boolean; onEdit: (row: number, col: number, v: Cell) => void; }) { const { range, value, columns } = props; return ( ); } /** One field per column of the lead row; edits apply on Enter / leaving the field. */ function RecordView(props: { columns: readonly GridColumn[]; row: number; value: (row: number, col: number) => Cell; truncated?: GridProps["truncated"]; loadFull?: GridProps["loadFull"]; editable: boolean; onEdit: (row: number, col: number, v: Cell) => void; }) { return (
{props.columns.map((c, i) => ( void props.loadFull?.([{ row: props.row, col: i }])} editable={props.editable} onCommit={(v) => props.onEdit(props.row, i, v)} /> ))}
); } /** What a value that is shown cut short offers: the whole value is read on request (until then it is read-only). */ function CutNotice(props: { shown: number; total: number; onLoad: () => void }) { const { shown, total } = props; return (
{tx`Only the first ${shown} of ${total} characters are shown.`}
); } function RecordField(props: { column: GridColumn; cell: Cell; cut: number | null; onLoad: () => void; editable: boolean; onCommit: (v: Cell) => void; }) { const [text, setText] = useState(props.cell ?? ""); const commit = () => { if (text !== (props.cell ?? "") || (props.cell === null && text !== "")) props.onCommit(text); }; const c = props.column; const editable = props.editable && props.cut === null; return ( <>