/** * IDEA's Export Data dialog: extractor (format) and its options on the left, * a live preview on the right; copies to the clipboard or writes a file. */ import { useEffect, useMemo, useState } from "react"; import { call, commands } from "@/platform/ipc"; import { Checkbox, Dialog, IconButton, Input, Select, T } from "@/ui"; import { notifications } from "@/workbench/contributions"; import { DEFAULT_EXPORT_OPTIONS, EXPORT_FORMATS, exportData, type ExportData, type ExportFormat, type ExportOptions, } from "./export"; import { CodePreview } from "./CodePreview"; import { closeDialog } from "./store"; import styles from "./Database.module.css"; import { cellPreview } from "./cellPreview"; const PREVIEW_ROWS = 100; /** Formats with a tabular layout (the options apply). */ const TABULAR: ReadonlySet = new Set(["csv", "tsv", "markdown"]); function joinPath(dir: string, name: string): string { return `${dir.replace(/[\\/]+$/, "")}/${name}`; } export function ExportDialog({ data, source, initialFormat = "csv", }: { data: ExportData; source: string; initialFormat?: ExportFormat; }) { const [format, setFormat] = useState(initialFormat); const [options, setOptions] = useState(DEFAULT_EXPORT_OPTIONS); const ext = EXPORT_FORMATS.find((f) => f.id === format)!.extension; const [file, setFile] = useState(`${data.table}.${ext}`); const [dir, setDir] = useState(null); useEffect(() => { void import("@tauri-apps/api/path") .then((p) => p.homeDir()) .then((home) => { setDir(home); setFile((f) => (f.includes("/") || f.includes("\\") ? f : joinPath(home, f))); }) .catch(() => {}); }, []); const changeFormat = (f: ExportFormat) => { setFormat(f); // The file follows the format's extension. const next = EXPORT_FORMATS.find((x) => x.id === f)!.extension; setFile((path) => path.replace(/\.[^./\\]*$/, `.${next}`)); }; const tabular = TABULAR.has(format); const effective = tabular ? options : DEFAULT_EXPORT_OPTIONS; const preview = useMemo( () => exportData( format, { ...data, rows: data.rows.slice(0, PREVIEW_ROWS).map((row) => row.map((v) => (v === null ? null : cellPreview(v)))), }, effective, ), [format, data, effective], ); const text = () => exportData(format, data, effective); const set = (k: keyof ExportOptions) => (v: boolean) => setOptions((o) => ({ ...o, [k]: v })); const browse = async () => { try { const { save } = await import("@tauri-apps/plugin-dialog"); const f = EXPORT_FORMATS.find((x) => x.id === format)!; const path = await save({ defaultPath: file, filters: [{ name: f.label, extensions: [f.extension] }] }); if (path) setFile(path); } catch { // No dialog (tests): keep typing the path. } }; const toFile = async () => { const path = file.trim(); if (!path) return; const target = /^([a-zA-Z]:)?[\\/]/.test(path) || !dir ? path : joinPath(dir, path); try { await call(commands.dbExportFile(target, text())); notifications().notify({ level: "info", title: `${data.rows.length} rows exported`, body: target, key: "db-export", }); closeDialog(); } catch (e) { notifications().notify({ level: "error", title: "Cannot export data", body: e instanceof Error ? e.message : String(e), }); } }; return ( { void navigator.clipboard?.writeText(text()); closeDialog(); }, }, { label: "Export to File", role: "default", disabled: !file.trim(), onClick: () => void toFile() }, ]} >
Source: Extractor: setFile(e.target.value)} /> void browse()} />
Export preview: PREVIEW_ROWS ? `\n… ${data.rows.length - PREVIEW_ROWS} more rows` : "")} />
); }