2023-12-16 00:24:50 +01:00
|
|
|
import { useApiHost } from "@/api";
|
2024-02-10 13:30:53 +01:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2023-12-16 00:24:50 +01:00
|
|
|
import useSWR from "swr";
|
2024-03-03 17:32:47 +01:00
|
|
|
import ActivityIndicator from "../indicators/activity-indicator";
|
2023-12-16 00:24:50 +01:00
|
|
|
|
|
|
|
type CameraImageProps = {
|
2024-02-10 13:30:53 +01:00
|
|
|
className?: string;
|
2023-12-16 00:24:50 +01:00
|
|
|
camera: string;
|
2024-02-10 13:30:53 +01:00
|
|
|
onload?: () => void;
|
2024-02-28 23:23:56 +01:00
|
|
|
searchParams?: string;
|
2023-12-16 00:24:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function CameraImage({
|
2024-02-10 13:30:53 +01:00
|
|
|
className,
|
2023-12-16 00:24:50 +01:00
|
|
|
camera,
|
|
|
|
onload,
|
|
|
|
searchParams = "",
|
|
|
|
}: CameraImageProps) {
|
|
|
|
const { data: config } = useSWR("config");
|
|
|
|
const apiHost = useApiHost();
|
|
|
|
const [hasLoaded, setHasLoaded] = useState(false);
|
|
|
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
2024-02-10 13:30:53 +01:00
|
|
|
const imgRef = useRef<HTMLImageElement | null>(null);
|
2023-12-16 00:24:50 +01:00
|
|
|
|
|
|
|
const { name } = config ? config.cameras[camera] : "";
|
|
|
|
const enabled = config ? config.cameras[camera].enabled : "True";
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-02-10 13:30:53 +01:00
|
|
|
if (!config || !imgRef.current) {
|
2023-12-16 00:24:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-02-10 13:30:53 +01:00
|
|
|
|
|
|
|
imgRef.current.src = `${apiHost}api/${name}/latest.jpg${
|
|
|
|
searchParams ? `?${searchParams}` : ""
|
2023-12-16 00:24:50 +01:00
|
|
|
}`;
|
2024-02-10 13:30:53 +01:00
|
|
|
}, [apiHost, name, imgRef, searchParams, config]);
|
2023-12-16 00:24:50 +01:00
|
|
|
|
|
|
|
return (
|
2023-12-16 15:40:00 +01:00
|
|
|
<div
|
2024-02-14 00:25:28 +01:00
|
|
|
className={`relative w-full h-full flex justify-center ${
|
|
|
|
className || ""
|
|
|
|
}`}
|
2023-12-16 15:40:00 +01:00
|
|
|
ref={containerRef}
|
|
|
|
>
|
2023-12-16 00:24:50 +01:00
|
|
|
{enabled ? (
|
2024-02-10 13:30:53 +01:00
|
|
|
<img
|
|
|
|
ref={imgRef}
|
|
|
|
className="object-contain rounded-2xl"
|
|
|
|
onLoad={() => {
|
|
|
|
setHasLoaded(true);
|
|
|
|
|
|
|
|
if (onload) {
|
|
|
|
onload();
|
|
|
|
}
|
|
|
|
}}
|
2023-12-16 00:24:50 +01:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="text-center pt-6">
|
|
|
|
Camera is disabled in config, no stream or snapshot available!
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{!hasLoaded && enabled ? (
|
2024-02-10 13:30:53 +01:00
|
|
|
<div className="absolute left-0 right-0 top-0 bottom-0 flex justify-center items-center">
|
2023-12-16 00:24:50 +01:00
|
|
|
<ActivityIndicator />
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|