Files
frigate/web/src/components/camera/AutoUpdatingCameraImage.tsx
T

104 lines
2.5 KiB
TypeScript
Raw Normal View History

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