From e2975bb9ff66ece04c869b5976f6a45c1a9e3777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20F=C3=ADsica?= Date: Thu, 12 Jun 2025 17:10:35 +0200 Subject: [PATCH] Fix portection packets for non-number values --- .../ProtectionView/ProtectionView.tsx | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/ethernet-view/src/components/MessagesContainer/Messages/MessageView/ProtectionMessageView/ProtectionView/ProtectionView.tsx b/ethernet-view/src/components/MessagesContainer/Messages/MessageView/ProtectionMessageView/ProtectionView/ProtectionView.tsx index af0d6e345..7a98e98d5 100644 --- a/ethernet-view/src/components/MessagesContainer/Messages/MessageView/ProtectionMessageView/ProtectionView/ProtectionView.tsx +++ b/ethernet-view/src/components/MessagesContainer/Messages/MessageView/ProtectionMessageView/ProtectionView/ProtectionView.tsx @@ -7,6 +7,10 @@ type Props = { const DECIMALS = 2; +function safeFixed(val: any, decimals = DECIMALS) { + return typeof val === "number" ? val.toFixed(decimals) : String(val); +} + export const ProtectionView = ({ protection }: Props) => { const ProtectionText = getProtectionText(protection); @@ -15,62 +19,60 @@ export const ProtectionView = ({ protection }: Props) => { function getProtectionText(protection: ProtectionMessage) { switch (protection.payload.kind) { - case 'OUT_OF_BOUNDS': + case "OUT_OF_BOUNDS": return ( <> - {' '} - Want: [{protection.payload.data.bounds[0].toFixed( - DECIMALS - )}, {protection.payload.data.bounds[1].toFixed(DECIMALS)}] - {' '} - Got: {protection.payload.data.value.toFixed(DECIMALS)} + {" "} + Want: [ + {safeFixed(protection.payload.data.bounds?.[0])}, {safeFixed(protection.payload.data.bounds?.[1])}] + {" "} + Got: {safeFixed(protection.payload.data.value)} ); - case 'UPPER_BOUND': + case "UPPER_BOUND": return ( <> - Want: [{protection.name}] {'<'} {protection.payload.data.bound.toFixed(DECIMALS)} - {' '} - Got: {protection.payload.data.value.toFixed(DECIMALS)} + Want: [{protection.name}] {"<"} {safeFixed(protection.payload.data.bound)} + {" "} + Got: {safeFixed(protection.payload.data.value)} ); - case 'LOWER_BOUND': + case "LOWER_BOUND": return ( <> - Want: [{protection.name}] {'>'} {protection.payload.data.bound.toFixed(DECIMALS)} - {' '} - Got: {protection.payload.data.value.toFixed(DECIMALS)} + Want: [{protection.name}] {">"} {safeFixed(protection.payload.data.bound)} + {" "} + Got: {safeFixed(protection.payload.data.value)} ); - case 'EQUALS': + case "EQUALS": return ( <> - Mustn't be {protection.payload.data.value.toFixed(DECIMALS)} + Mustn't be {safeFixed(protection.payload.data.value)} ); - case 'NOT_EQUALS': + case "NOT_EQUALS": return ( <> - Must be {protection.payload.data.want.toFixed(DECIMALS)} but is{' '} - {protection.payload.data.value.toFixed(DECIMALS)} + Must be {safeFixed(protection.payload.data.want)} but is{" "} + {safeFixed(protection.payload.data.value)} ); - case 'TIME_ACCUMULATION': + case "TIME_ACCUMULATION": return ( - Value was {protection.payload.data.value.toFixed(DECIMALS)} for{' '} - {protection.payload.data.timelimit.toFixed(DECIMALS)} seconds + Value was {safeFixed(protection.payload.data.value)} for{" "} + {safeFixed(protection.payload.data.timelimit)} seconds ); - case 'ERROR_HANDLER': - case 'WARNING': + case "ERROR_HANDLER": return {protection.payload.data}; } }