mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-27 14:31:17 -07:00
Chore: full react hooks eslint compliance (#7040)
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { useTranslation } from "next-i18next/pages";
|
||||
import dynamic from "next/dynamic";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useCallback } from "react";
|
||||
|
||||
import Block from "../components/block";
|
||||
import Container from "../components/container";
|
||||
|
||||
import useDataPoints from "./use-data-points";
|
||||
|
||||
import { parseVersionForUrl } from "utils/proxy/api-helpers";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
@@ -20,32 +22,29 @@ export default function Component({ service }) {
|
||||
const apiVersion = parseVersionForUrl(version, 3);
|
||||
const [, sensorName] = widget.metric.split(":");
|
||||
|
||||
const [dataPoints, setDataPoints] = useState(new Array(pointsLimit).fill({ value: 0 }, 0, pointsLimit));
|
||||
const [dataPoints, addDataPoint] = useDataPoints(pointsLimit, { value: 0 });
|
||||
|
||||
const { data, error } = useWidgetAPI(service.widget, `${apiVersion}/sensors`, {
|
||||
refreshInterval: Math.max(defaultInterval, refreshInterval),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (data && !data.error) {
|
||||
const sensorData = data.find((item) => item.label === sensorName);
|
||||
if (sensorData) {
|
||||
setDataPoints((prevDataPoints) => {
|
||||
const newDataPoints = [...prevDataPoints, { value: sensorData.value }];
|
||||
if (newDataPoints.length > pointsLimit) {
|
||||
newDataPoints.shift();
|
||||
}
|
||||
return newDataPoints;
|
||||
});
|
||||
} else {
|
||||
data.error = true;
|
||||
const handleData = useCallback(
|
||||
(newData) => {
|
||||
if (!newData?.error) {
|
||||
const sensorData = newData.find((item) => item.label === sensorName);
|
||||
if (sensorData) addDataPoint({ value: sensorData.value });
|
||||
}
|
||||
}
|
||||
}, [data, sensorName, pointsLimit]);
|
||||
},
|
||||
[addDataPoint, sensorName],
|
||||
);
|
||||
|
||||
if (error || (data && data.error)) {
|
||||
const finalError = error || data.error;
|
||||
return <Container error={finalError} widget={widget} />;
|
||||
const { data, error } = useWidgetAPI(
|
||||
service.widget,
|
||||
`${apiVersion}/sensors`,
|
||||
{
|
||||
refreshInterval: Math.max(defaultInterval, refreshInterval),
|
||||
},
|
||||
{ onSuccess: handleData },
|
||||
);
|
||||
|
||||
if (error || data?.error) {
|
||||
return <Container error={error || data.error} widget={widget} />;
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
|
||||
Reference in New Issue
Block a user