From 19134924bde623dc8dfcb547b3e63593863bc67d Mon Sep 17 00:00:00 2001 From: mblanke Date: Tue, 17 Feb 2026 08:45:30 -0500 Subject: [PATCH] fix: extract data.devices in UnifiWidget to prevent e.filter crash API returns {devices, health, totalClients} object, not an array. setDevices(data) was setting the whole object, causing .filter() to crash. --- src/components/UnifiWidget.tsx | 152 +++++++++++++++++---------------- 1 file changed, 77 insertions(+), 75 deletions(-) diff --git a/src/components/UnifiWidget.tsx b/src/components/UnifiWidget.tsx index 967345e..864e202 100644 --- a/src/components/UnifiWidget.tsx +++ b/src/components/UnifiWidget.tsx @@ -1,75 +1,77 @@ -"use client"; - -import { useEffect, useState } from "react"; -import { Wifi, WifiOff } from "lucide-react"; - -export default function UnifiWidget() { - const [devices, setDevices] = useState([]); - const [loading, setLoading] = useState(true); - const [error, setError] = useState(false); - - useEffect(() => { - fetchDevices(); - const interval = setInterval(fetchDevices, 30000); - return () => clearInterval(interval); - }, []); - - const fetchDevices = async () => { - try { - const response = await fetch("/api/unifi"); - if (response.ok) { - const data = await response.json(); - setDevices(data); - setError(false); - } else { - setError(true); - } - setLoading(false); - } catch (err) { - setError(true); - setLoading(false); - } - }; - - const onlineDevices = devices.filter((d) => d.state === 1).length; - const totalClients = devices.reduce((sum, d) => sum + (d.clients || 0), 0); - - return ( -
-
-

- - UniFi Network -

-
- - {loading ? ( -
-
-
- ) : error ? ( -
- -

- Configure UniFi credentials in .env -

-
- ) : ( -
-
-
-

Devices Online

-

- {onlineDevices}/{devices.length} -

-
-
-

Connected Clients

-

{totalClients}

-
-
-
- )} -
- ); -} +"use client"; + +import { useEffect, useState } from "react"; +import { Wifi, WifiOff } from "lucide-react"; + +export default function UnifiWidget() { + const [devices, setDevices] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(false); + + useEffect(() => { + fetchDevices(); + const interval = setInterval(fetchDevices, 30000); + return () => clearInterval(interval); + }, []); + + const fetchDevices = async () => { + try { + const response = await fetch("/api/unifi"); + if (response.ok) { + const data = await response.json(); + // API returns { devices, health, totalClients } — extract the array + const devArr = Array.isArray(data) ? data : Array.isArray(data?.devices) ? data.devices : []; + setDevices(devArr); + setError(false); + } else { + setError(true); + } + setLoading(false); + } catch (err) { + setError(true); + setLoading(false); + } + }; + + const onlineDevices = devices.filter((d) => d.state === 1).length; + const totalClients = devices.reduce((sum, d) => sum + (d.clients || 0), 0); + + return ( +
+
+

+ + UniFi Network +

+
+ + {loading ? ( +
+
+
+ ) : error ? ( +
+ +

+ Configure UniFi credentials in .env +

+
+ ) : ( +
+
+
+

Devices Online

+

+ {onlineDevices}/{devices.length} +

+
+
+

Connected Clients

+

{totalClients}

+
+
+
+ )} +
+ ); +}