import { type CSSProperties } from 'react'; import { Activity, AlertTriangle, PlugZap } from 'lucide-react'; import type { DiagramNode } from '../../entities/diagram/types'; import { formatNumber } from '../../utils/format'; import { getNodeSize } from './geometry'; import { getLiveValue } from './model'; import type { LiveValue } from './types'; function getNodeTitle(node: DiagramNode, value?: LiveValue) { if (node.kind === 'tagWidget') { return node.label || value?.name || node.tagId; } return String(node.data?.title || node.data?.text || node.decorationType); } type ElectricalNodeProps = { liveValues: Map; node: DiagramNode; ownerEdgeId: string; }; export function ElectricalNode({ liveValues, node, ownerEdgeId }: ElectricalNodeProps) { const size = getNodeSize(node); const bindings = node.kind === 'decoration' ? node.bindings : undefined; const bindingEdgeId = bindings?.sseEdgeId || bindings?.edgeId || ownerEdgeId; const mainValue = node.kind === 'tagWidget' ? getLiveValue(liveValues, node.edgeId || ownerEdgeId, node.tagId) : getLiveValue(liveValues, bindingEdgeId, bindings?.stateTagId); const alarmValue = node.kind === 'decoration' ? getLiveValue(liveValues, bindingEdgeId, bindings?.alarmTagId) : undefined; const alarmActive = Boolean(alarmValue && alarmValue.value !== null && alarmValue.value !== 0); const stateActive = Boolean(mainValue && mainValue.value !== null && mainValue.value !== 0); const title = getNodeTitle(node, mainValue); const style: CSSProperties = { left: node.position.x, top: node.position.y, width: size.width, height: size.height, zIndex: node.zIndex ?? 1, transform: node.kind === 'decoration' && node.rotation ? `rotate(${node.rotation}deg)` : undefined, }; return (
{alarmActive ? : node.kind === 'tagWidget' ? : } {title}
{mainValue ? ( {formatNumber(mainValue.value)} {mainValue.unitOfMeasurement ? {mainValue.unitOfMeasurement} : null} ) : node.kind === 'decoration' ? ( {String(node.data?.text || node.data?.title || node.decorationType)} ) : ( - )}
{node.kind === 'tagWidget' ? {node.tagId} : bindings?.stateTagId ? {bindings.stateTagId} : {node.decorationType}} {alarmValue ? alarm {formatNumber(alarmValue.value)} : null}
); }