import type { TableRow, TableWidgetData } from './types' /** * Sanitizes a table row by ensuring all values are serializable. * Converts ReactNode and function values to undefined. * * @param row - The table row to sanitize * @returns A sanitized table row */ export function sanitizeTableRow(row: TableRow): TableRow { const sanitized: TableRow = { id: row.id } for (const [key, value] of Object.entries(row)) { if (key === 'id') continue // Keep primitive values if ( typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null ) { sanitized[key] = value } // Serialize arrays and plain objects else if (Array.isArray(value) || typeof value === 'object') { try { // Attempt to serialize - if it fails, the value contains non-serializable content JSON.stringify(value) sanitized[key] = value } catch { sanitized[key] = undefined } } // Skip functions and other non-serializable values else { sanitized[key] = undefined } } return sanitized } /** * Sanitizes table widget data by ensuring all rows are serializable. * * @param data - The table data to sanitize * @returns Sanitized table data */ export function sanitizeTableData( data: TableWidgetData | undefined, ): TableWidgetData | undefined { return data?.map(sanitizeTableRow) }