import { useState, ReactNode } from "react"; import { Plus } from "lucide-react"; import type { LayoutElement } from "../api/viewService"; import RecordViewTable from "./RecordViewTable"; import DetailViewPanel from "./DetailViewPanel"; import FormModal from "./FormModal"; interface Props { layout: LayoutElement[]; instanceId?: string; onRefresh?: () => void; onRowClick?: (instanceId: string) => void; } interface AnyNode { type?: string; style?: Record; config?: Record; children?: AnyNode[]; } function flatten(nodes: AnyNode[] | undefined): AnyNode[] { if (!nodes) return []; const out: AnyNode[] = []; for (const n of nodes) { if (n.config && (n.config.type || n.config.job_template)) out.push(n); if (n.children?.length) out.push(...flatten(n.children)); } return out; } export default function ScreenRenderer({ layout, instanceId, onRefresh, onRowClick }: Props) { const [formModal, setFormModal] = useState<{ activityId: string; instanceId?: string; title?: string } | null>(null); const buttons: ReactNode[] = []; const elements: { type: "rv" | "dv"; viewId: number | string; style?: Record }[] = []; for (const node of flatten(layout as AnyNode[])) { const cfg = node.config!; if (cfg.type === "button" || cfg.job_template) { const aid = cfg.params?.activity_uid_init || cfg.params?.activity_id_init || cfg.params?.activity_uid || cfg.params?.activity_id; if (!aid) continue; const label = (cfg.name || "").replace(/^\+\s*/, ""); buttons.push( ); } else if (cfg.type === "recordsview") { const id = cfg.view_uuid || cfg.view_id; elements.push({ type: "rv", viewId: id, style: node.style }); } else if (cfg.type === "detailsview") { const id = cfg.view_uuid || cfg.view_id; elements.push({ type: "dv", viewId: id, style: node.style }); } } return ( <>
{elements.map((el, i) => { if (el.type === "rv") { return (
0 ?
{buttons}
: undefined} />
); } if (el.type === "dv") { return
; } return null; })} {elements.length === 0 && buttons.length > 0 && (
{buttons}
)}
{formModal && ( setFormModal(null)} onSuccess={() => { setFormModal(null); onRefresh?.(); }} /> )} ); }