import { useEffect, useState } from 'react'; import { api } from '../api/client'; interface LayoutElement { id: number; type: string; x: number; y: number; width: number | null; height: number | null; font_size: number; style: string; static_text: string | null; datapoint_id: number | null; } interface DisplayJson { display_id: number; layout: { id: number; width: number; height: number; }; elements: Array<{ type: string; x: number; y: number; width?: number; height?: number; font_size: number; style: string; static_text?: string; label?: string; value?: string; unit?: string; }>; } interface Display { id: number; name: string; layout_id: number | null; } interface EpaperPreviewProps { layoutId: number; elements: LayoutElement[]; layoutWidth?: number; layoutHeight?: number; } const SCALE = 0.5; export default function EpaperPreview({ layoutId, elements, layoutWidth = 800, layoutHeight = 480 }: EpaperPreviewProps) { const [displayJson, setDisplayJson] = useState(null); useEffect(() => { // Find a display using this layout and fetch its JSON api.get('/api/displays') .then((displays) => { const d = displays.find((disp) => disp.layout_id === layoutId); if (!d) return; return api.get(`/api/display/${d.id}`).then(setDisplayJson).catch(() => {}); }) .catch(() => {}); }, [layoutId]); const w = layoutWidth * SCALE; const h = layoutHeight * SCALE; const renderEl = (el: typeof elements[0], _idx?: number) => { const left = el.x * SCALE; const top = el.y * SCALE; const fs = Math.max(8, el.font_size * SCALE); if (el.type === 'line') { return (
); } if (el.type === 'rect') { return (
); } // label or value elements const text = el.static_text ?? (el.type === 'label' ? `[DP ${el.datapoint_id ?? '?'}]` : `[Wert]`); const isCard = el.style === 'card'; return (
{text}
); }; // Use live display JSON if available, else render from elements prop const liveEls = displayJson?.elements; return (

E-Paper Vorschau ({layoutWidth}x{layoutHeight}px){liveEls ? ' — Live-Daten' : ' — Design-Modus'}

{liveEls ? liveEls.map((el, idx) => { const left = el.x * SCALE; const top = el.y * SCALE; const fs = Math.max(8, el.font_size * SCALE); if (el.type === 'line') { return
; } if (el.type === 'rect') { return
; } const text = el.static_text ?? (el.type === 'value' ? `${el.value ?? '?'} ${el.unit ?? ''}` : (el.label ?? '')); const isCard = el.style === 'card'; return (
{text}
); }) : elements.map((el) => renderEl(el, el.id))}
); }