import React, { useState } from "react"; import { useNavigate } from "react-router-dom"; import { Box, Typography, Chip, Avatar, Dialog, DialogTitle, DialogContent, DialogActions, Button, Grid } from "@mui/material"; import type { FieldConfig } from "../../types"; import { applyDisplayFormat } from "./utils"; import { InlineRefField } from "./renderers/InlineRefField"; import { extractFields } from "../../transformers/field-config"; import { useAppContext } from "../../context/AppContext"; interface ListCellProps { field: FieldConfig; value: any; displayFormat?: string; basePath?: string; } export function ListCellRenderer({ field, value, displayFormat, basePath }: ListCellProps) { const navigate = useNavigate(); const { schemas } = useAppContext(); const [inlineItem, setInlineItem] = useState(null); if (value === null || value === undefined) { return ; } const handleFkClick = (e: React.MouseEvent, fkValue: any) => { e.stopPropagation(); if (!basePath || !field.fk || typeof fkValue !== "object") return; const id = fkValue?.id; if (id != null) { navigate(`${basePath}/${field.fk.resource}/${id}`); } }; if (field.refSchema && !field.fk && !field.isArray && typeof value === "object") { return ; } const renderInlineItemFields = (itemValue: any) => { const schema = field.refSchema ? schemas[field.refSchema] : undefined; let fields: FieldConfig[] = []; if (field.oneOfOptions && field.discriminatorProperty) { const opt = field.oneOfOptions.find((o) => o.value === itemValue?.[field.discriminatorProperty!]); if (opt) fields = opt.fields; } else if (schema && field.refSchema) { fields = extractFields(field.refSchema, schema, schemas); } return ( {fields.map((sf) => { const fv = itemValue?.[sf.name]; return ( {sf.label} {fv == null ? "—" : typeof fv === "object" ? (sf.inlineDisplayFormat ? applyDisplayFormat(fv, sf.inlineDisplayFormat) : JSON.stringify(fv)) : String(fv)} ); })} ); }; if (field.isArray && Array.isArray(value) && field.refSchema && !field.fk) { if (value.length === 0) { return ; } return ( {value.map((item: any, i: number) => { const label = typeof item === "object" ? applyDisplayFormat(item, displayFormat ?? "") : String(item); return ( { e.stopPropagation(); setInlineItem(item); }} sx={{ cursor: "pointer" }} /> ); })} setInlineItem(null)} maxWidth="sm" fullWidth> {field.label} {inlineItem && renderInlineItemFields(inlineItem)} ); } if (field.fk && typeof value === "object" && !field.isArray) { return ( handleFkClick(e, value)} sx={basePath ? { cursor: "pointer" } : undefined} /> ); } if (field.isArray && Array.isArray(value) && field.fk) { return ( {value.map((item: any, i: number) => { const label = typeof item === "object" ? applyDisplayFormat(item, displayFormat ?? "") : String(item); return ( handleFkClick(e, item)} sx={basePath ? { cursor: "pointer" } : undefined} /> ); })} ); } if (field.enumValues) { return ; } if (field.uiType === "image" && value) { return ; } if (field.type === "boolean") { return ; } if (typeof value === "object") { return {applyDisplayFormat(value, displayFormat ?? "")}; } return {String(value)}; }