-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathReportWatchdog.tsx
36 lines (29 loc) · 1.07 KB
/
ReportWatchdog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useAppDispatch } from "../../hooks";
import useInterval from "../../helpers/useInterval";
import { usePageVisibility } from "react-page-visibility";
import { useEffect, useState } from "react";
import { getWeather } from "../weather/weatherSlice";
import { useParams } from "react-router";
import { WindsAloftHour } from "../../models/WindsAloft";
interface ReportWatchdogProps {
hours: WindsAloftHour[];
}
export default function ReportWatchdog({ hours }: ReportWatchdogProps) {
const dispatch = useAppDispatch();
const visibility = usePageVisibility();
const [lastUpdated, setLastUpdated] = useState(Date.now());
const { location } = useParams<"location">();
const [lat, lon] = (location ?? "").split(",");
if (!lat || !lon) throw new Error("lat or lon not defined!");
useInterval(() => {
setLastUpdated(Date.now());
}, 5000);
useEffect(() => {
setLastUpdated(Date.now());
}, [visibility]);
useEffect(() => {
if (document.hidden) return;
dispatch(getWeather(+lat, +lon));
}, [lastUpdated, dispatch, hours, lat, lon]);
return <></>;
}