import { downloadToCSV, downloadToPNG } from '../actions' import type { ConfigProps } from '../loader/types' import type { TableDownloadConfig, TableWidgetConfig, TableColumn, Mode, SortDirection, } from './types' const DEFAULT_MODE: Mode = 'local' export const DEFAULT_PAGE = 0 export const DEFAULT_ROWS_PER_PAGE = 10 export const DEFAULT_ROWS_PER_PAGE_OPTIONS = [5, 10, 25, 50] export const DEFAULT_COLUMN_ID = null export const DEFAULT_DIRECTION: SortDirection = 'asc' /** * Creates download configuration for table widgets, supporting PNG (screenshot) and CSV (data) exports. CSV output uses column keys as headers and filters out `id` fields. * * @param props - Configuration with `refUI` reference for PNG export. * @returns Array of download items for use with the Download action. */ export function tableDownloadConfig({ refUI, }: ConfigProps): TableDownloadConfig { return [ { ...downloadToPNG, modifier: () => downloadToPNG.modifier(refUI), }, { ...downloadToCSV, modifier: async (data) => { if (!data?.length) { return downloadToCSV.modifier([]) } // Convert table data to CSV format // First row contains headers (column keys) const firstRow = data[0] if (!firstRow) { return downloadToCSV.modifier([]) } const headers = Object.keys(firstRow).filter((key) => key !== 'id') const rows = data.map((row) => headers.map((header) => { const value = row[header] if (value === null || value === undefined) return '' if (typeof value === 'object') return JSON.stringify(value) return value }), ) return downloadToCSV.modifier([headers, ...rows]) }, }, ] } /** * Returns the default configuration for table widgets with pagination and sorting, including column definitions and local data mode. * * @param columns - Array of column definitions for the table. * @returns Default table widget config. */ export function tableConfig(columns: TableColumn[] = []): TableWidgetConfig { return { columns, selectable: false, mode: DEFAULT_MODE, } }