2024-12-20 17:17:51 +03:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2023-12-16 02:24:50 +03:00
|
|
|
import CameraImage from "./CameraImage";
|
|
|
|
|
|
|
|
|
|
type AutoUpdatingCameraImageProps = {
|
|
|
|
|
camera: string;
|
2024-02-29 01:23:56 +03:00
|
|
|
searchParams?: URLSearchParams;
|
2023-12-16 02:24:50 +03:00
|
|
|
showFps?: boolean;
|
|
|
|
|
className?: string;
|
2024-04-19 14:34:07 +03:00
|
|
|
cameraClasses?: string;
|
2024-02-10 15:30:53 +03:00
|
|
|
reloadInterval?: number;
|
2024-12-20 17:17:51 +03:00
|
|
|
periodicCache?: boolean;
|
2023-12-16 02:24:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const MIN_LOAD_TIMEOUT_MS = 200;
|
|
|
|
|
|
|
|
|
|
export default function AutoUpdatingCameraImage({
|
|
|
|
|
camera,
|
2024-02-29 01:23:56 +03:00
|
|
|
searchParams = undefined,
|
2023-12-16 02:24:50 +03:00
|
|
|
showFps = true,
|
|
|
|
|
className,
|
2024-04-19 14:34:07 +03:00
|
|
|
cameraClasses,
|
2024-02-10 15:30:53 +03:00
|
|
|
reloadInterval = MIN_LOAD_TIMEOUT_MS,
|
2024-12-20 17:17:51 +03:00
|
|
|
periodicCache = false,
|
2023-12-16 02:24:50 +03:00
|
|
|
}: AutoUpdatingCameraImageProps) {
|
|
|
|
|
const [key, setKey] = useState(Date.now());
|
|
|
|
|
const [fps, setFps] = useState<string>("0");
|
2024-07-08 16:14:10 +03:00
|
|
|
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
2024-02-13 04:28:36 +03:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (reloadInterval == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setKey(Date.now());
|
|
|
|
|
|
|
|
|
|
return () => {
|
2024-07-08 16:14:10 +03:00
|
|
|
if (timeoutRef.current) {
|
|
|
|
|
clearTimeout(timeoutRef.current);
|
|
|
|
|
timeoutRef.current = null;
|
2024-02-13 04:28:36 +03:00
|
|
|
}
|
|
|
|
|
};
|
2024-02-29 01:23:56 +03:00
|
|
|
// we know that these deps are correct
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-02-13 04:28:36 +03:00
|
|
|
}, [reloadInterval]);
|
2023-12-16 02:24:50 +03:00
|
|
|
|
|
|
|
|
const handleLoad = useCallback(() => {
|
2024-12-20 17:17:51 +03:00
|
|
|
setIsCached(true);
|
|
|
|
|
|
2024-02-13 04:28:36 +03:00
|
|
|
if (reloadInterval == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 16:14:10 +03:00
|
|
|
if (timeoutRef.current) {
|
|
|
|
|
clearTimeout(timeoutRef.current);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 02:24:50 +03:00
|
|
|
const loadTime = Date.now() - key;
|
|
|
|
|
|
|
|
|
|
if (showFps) {
|
2024-02-10 15:30:53 +03:00
|
|
|
setFps((1000 / Math.max(loadTime, reloadInterval)).toFixed(1));
|
2023-12-16 02:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
2024-07-08 16:14:10 +03:00
|
|
|
timeoutRef.current = setTimeout(
|
|
|
|
|
() => {
|
|
|
|
|
setKey(Date.now());
|
|
|
|
|
},
|
|
|
|
|
loadTime > reloadInterval ? 1 : reloadInterval,
|
2023-12-16 02:24:50 +03:00
|
|
|
);
|
2024-02-29 01:23:56 +03:00
|
|
|
// we know that these deps are correct
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-12-16 02:24:50 +03:00
|
|
|
}, [key, setFps]);
|
|
|
|
|
|
2024-12-20 17:17:51 +03:00
|
|
|
// periodic cache to reduce loading indicator
|
|
|
|
|
|
|
|
|
|
const [isCached, setIsCached] = useState(false);
|
|
|
|
|
|
|
|
|
|
const cacheKey = useMemo(() => {
|
|
|
|
|
let baseParam = "";
|
|
|
|
|
|
|
|
|
|
if (periodicCache && !isCached) {
|
2025-03-14 16:48:16 +03:00
|
|
|
const date = new Date(key);
|
|
|
|
|
date.setMinutes(date.getMinutes() - (date.getMinutes() % 10), 0, 0);
|
|
|
|
|
|
|
|
|
|
baseParam = `store=1&cache=${date.getTime() / 1000}`;
|
2024-12-20 17:17:51 +03:00
|
|
|
} else {
|
|
|
|
|
baseParam = `cache=${key}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${baseParam}${searchParams ? `&${searchParams}` : ""}`;
|
|
|
|
|
}, [isCached, periodicCache, key, searchParams]);
|
|
|
|
|
|
2023-12-16 02:24:50 +03:00
|
|
|
return (
|
|
|
|
|
<div className={className}>
|
|
|
|
|
<CameraImage
|
|
|
|
|
camera={camera}
|
|
|
|
|
onload={handleLoad}
|
2024-12-20 17:17:51 +03:00
|
|
|
searchParams={cacheKey}
|
2024-04-19 14:34:07 +03:00
|
|
|
className={cameraClasses}
|
2023-12-16 02:24:50 +03:00
|
|
|
/>
|
|
|
|
|
{showFps ? <span className="text-xs">Displaying at {fps}fps</span> : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|