import type { ReactNode } from "react"; import { Badge, Text } from "@mantine/core"; import type { ColumnFormat } from "@/pages/ruleEngine/config/resources"; const extractLabel = (value: unknown): string | null => { if (!value || typeof value !== "object") return null; const obj = value as Record; return ( (typeof obj.label === "string" ? obj.label : null) || (typeof obj.cargoTypeName === "string" ? obj.cargoTypeName : null) || (typeof obj.code === "string" ? obj.code : null) || (typeof obj.name === "string" ? obj.name : null) || (typeof obj.actionLabel === "string" ? obj.actionLabel : null) || null ); }; export const formatCell = ( value: unknown, format?: ColumnFormat, // The row the cell came from — currency amounts read their code off it so a // last-mile rate priced in birr does not render as USD. row?: Record, ): ReactNode => { if (value === null || value === undefined || value === "") { return ; } // Handle stringified objects (e.g., "[object Object]") if (typeof value === "string" && value.trim() === "[object Object]") { return ; } if (format === "boolean") { if (typeof value === "string") { const boolVal = value.toLowerCase() === "true" || value === "1"; return {boolVal ? "✓ Yes" : "✗ No"}; } return {value ? "✓ Yes" : "✗ No"}; } if (format === "activeBadge") { const active = Boolean(value); return ( {active ? "Active" : "Inactive"} ); } if (format === "rateStatus") { const status = String(value); const color = status === "LIVE" ? "edr-green" : status === "DRAFT" ? "yellow" : status === "PENDING_APPROVAL" ? "orange" : "blue"; return ( {status} ); } if (format === "validityBadge") { const status = String(value); const label = status === "VALID" ? "Valid" : status === "EXPIRED" ? "Expired" : "Not started"; const color = status === "VALID" ? "edr-green" : status === "EXPIRED" ? "red" : "yellow"; return ( {label} ); } if (format === "code") { return ( {String(value).toUpperCase()} ); } if (format === "text") { return {String(value)}; } if (format === "number") { const num = Number(value); return {Number.isNaN(num) ? String(value) : num.toLocaleString()}; } if (format === "currency") { const num = Number(value); const code = typeof row?.currency === "string" ? row.currency : "USD"; return ( {Number.isNaN(num) ? String(value) : `${code} ${num.toLocaleString()}`} ); } if (format === "date") { const d = new Date(String(value)); if (Number.isNaN(d.getTime())) return {String(value)}; return {d.toLocaleDateString()}; } if (Array.isArray(value)) { return ( {value.length > 0 ? value.join(", ") : "—"} ); } if (format === "entityLabel" && value && typeof value === "object") { const label = extractLabel(value); if (label) { return {label}; } return ; } if (format === "rateLabel") { if (!value || typeof value !== "object") { return value ? ( {String(value)} ) : ( ); } const rate = value as { rateType?: string; currency?: string; rateValue?: number; rateUnit?: string; }; const parts = [ rate.rateType?.replace(/_/g, " "), rate.currency, rate.rateValue != null ? String(rate.rateValue) : "", rate.rateUnit?.replace(/_/g, " "), ].filter(Boolean); return parts.length > 0 ? ( {parts.join(" · ")} ) : ( ); } if (typeof value === "object") { const label = extractLabel(value); if (label) return {label}; return ; } return {String(value)}; };