diff --git a/src/chart/DeviceHistory.tsx b/src/chart/DeviceHistory.tsx index ed8eb2f1..e0d50c33 100644 --- a/src/chart/DeviceHistory.tsx +++ b/src/chart/DeviceHistory.tsx @@ -64,11 +64,12 @@ const findLowerLimit = (v: Array): number => */ export const DeviceHistory = () => { const { devices } = useDevices() - const deviceId = useDetails() + const deviceId = useDetails() if (deviceId === undefined) return null const history = devices[deviceId]?.history + if (history === undefined) return null const charts: ChartInfo[] = [] diff --git a/src/wirepas/WirepasTopology.tsx b/src/wirepas/WirepasTopology.tsx index acd40991..9fe0b2f5 100644 --- a/src/wirepas/WirepasTopology.tsx +++ b/src/wirepas/WirepasTopology.tsx @@ -1,35 +1,45 @@ import type { Ref } from 'preact' import { ChartContainer, Button } from '../chart/DeviceHistory.js' import { isWirepasGateway, useDevices } from '../context/Devices.js' -import { parseWirepasNodes } from './parseWirepasNodes.js' -import { useRef } from 'preact/hooks' +import { parseWirepasNodes, type Connection } from './parseWirepasNodes.js' +import { useEffect, useRef, useState } from 'preact/hooks' import { WirepasTopologyDiagram } from './WirepasTopologyDiagram.js' import { X } from 'lucide-preact' import { useDetails, hideDetails } from '../hooks/useDetails.js' +import { isEqual } from 'lodash-es' export const WirepasTopology = () => { const deviceId = useDetails() const { devices, type } = useDevices() - const containerRef = useRef() + if (deviceId === undefined) return null const maybeDevice = devices[deviceId] if (maybeDevice === undefined) return null const gw = { ...maybeDevice, type: type(maybeDevice.id) } if (!isWirepasGateway(gw)) return null + const connections = parseWirepasNodes(gw.state.nodes) + if (connections.length === 0) return null - const parsedWirepasTopology = parseWirepasNodes(gw.state.nodes) + return +} - const [width, height] = [500, 500] +const Chart = ({ connections }: { connections: Connection[] }) => { + const [c, setC] = useState(connections) + useEffect(() => { + if (!isEqual(c, connections)) { + setC(connections) + } + }, []) + + const [width, height] = [500, 500] + const containerRef = useRef() return ( } style={{ height: `${height}px`, width: `${width}px` }} > - + diff --git a/src/wirepas/WirepasTopologyDiagram.tsx b/src/wirepas/WirepasTopologyDiagram.tsx index c712552e..387d75bf 100644 --- a/src/wirepas/WirepasTopologyDiagram.tsx +++ b/src/wirepas/WirepasTopologyDiagram.tsx @@ -15,6 +15,11 @@ export const WirepasTopologyDiagram = ({ size: { width: number; height: number } distance?: number }) => { + if (connections.length === 0) { + console.warn(`[WirepasTopology]`, `No connections.`) + return null + } + const targets = connections.map(({ to }) => to) const sources = connections.map(({ from }) => from)