import { useApiHost } from "@/api"; import { useEffect, useRef, useState } from "react"; import useSWR from "swr"; import ActivityIndicator from "../indicators/activity-indicator"; type CameraImageProps = { className?: string; camera: string; onload?: () => void; searchParams?: string; }; export default function CameraImage({ className, camera, onload, searchParams = "", }: CameraImageProps) { const { data: config } = useSWR("config"); const apiHost = useApiHost(); const [hasLoaded, setHasLoaded] = useState(false); const containerRef = useRef(null); const imgRef = useRef(null); const { name } = config ? config.cameras[camera] : ""; const enabled = config ? config.cameras[camera].enabled : "True"; useEffect(() => { if (!config || !imgRef.current) { return; } imgRef.current.src = `${apiHost}api/${name}/latest.jpg${ searchParams ? `?${searchParams}` : "" }`; }, [apiHost, name, imgRef, searchParams, config]); return (
{enabled ? ( { setHasLoaded(true); if (onload) { onload(); } }} /> ) : (
Camera is disabled in config, no stream or snapshot available!
)} {!hasLoaded && enabled ? (
) : null}
); }