2024-02-13 02:28:36 +01:00
|
|
|
import { useFrigateEvents } from "@/api/ws";
|
2024-02-11 14:23:45 +01:00
|
|
|
import { AnimatedEventThumbnail } from "@/components/image/AnimatedEventThumbnail";
|
2023-12-16 00:24:50 +01:00
|
|
|
import LivePlayer from "@/components/player/LivePlayer";
|
2024-02-10 13:30:53 +01:00
|
|
|
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
|
|
|
import { TooltipProvider } from "@/components/ui/tooltip";
|
|
|
|
import { Event as FrigateEvent } from "@/types/event";
|
2023-12-16 00:24:50 +01:00
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
2024-02-13 02:28:36 +01:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
2024-02-21 21:07:32 +01:00
|
|
|
import { isSafari } from "react-device-detect";
|
2023-12-16 00:24:50 +01:00
|
|
|
import useSWR from "swr";
|
2023-12-08 14:33:22 +01:00
|
|
|
|
|
|
|
function Live() {
|
2023-12-16 00:24:50 +01:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
// recent events
|
2024-02-13 02:28:36 +01:00
|
|
|
const { payload: eventUpdate } = useFrigateEvents();
|
2024-02-24 14:50:33 +01:00
|
|
|
const { data: allEvents, mutate: updateEvents } = useSWR<FrigateEvent[]>([
|
|
|
|
"events",
|
|
|
|
{ limit: 10 },
|
|
|
|
]);
|
2024-02-10 13:30:53 +01:00
|
|
|
|
2024-02-13 02:28:36 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!eventUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if event is ended and was saved, update events list
|
|
|
|
if (
|
|
|
|
eventUpdate.type == "end" &&
|
|
|
|
(eventUpdate.after.has_clip || eventUpdate.after.has_snapshot)
|
|
|
|
) {
|
|
|
|
updateEvents();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if event is updated and has become a saved event, update events list
|
|
|
|
if (
|
|
|
|
!(eventUpdate.before.has_clip || eventUpdate.before.has_snapshot) &&
|
|
|
|
(eventUpdate.after.has_clip || eventUpdate.after.has_snapshot)
|
|
|
|
) {
|
|
|
|
updateEvents();
|
|
|
|
}
|
|
|
|
}, [eventUpdate]);
|
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
const events = useMemo(() => {
|
|
|
|
if (!allEvents) {
|
2023-12-16 15:40:00 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
const date = new Date();
|
|
|
|
date.setHours(date.getHours() - 1);
|
|
|
|
const cutoff = date.getTime() / 1000;
|
|
|
|
return allEvents.filter((event) => event.start_time > cutoff);
|
|
|
|
}, [allEvents]);
|
2024-02-01 13:44:10 +01:00
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
// camera live views
|
2023-12-16 00:24:50 +01:00
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
const cameras = useMemo(() => {
|
|
|
|
if (!config) {
|
|
|
|
return [];
|
2023-12-16 00:24:50 +01:00
|
|
|
}
|
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
return Object.values(config.cameras)
|
|
|
|
.filter((conf) => conf.ui.dashboard && conf.enabled)
|
|
|
|
.sort((aConf, bConf) => aConf.ui.order - bConf.ui.order);
|
|
|
|
}, [config]);
|
2023-12-16 00:24:50 +01:00
|
|
|
|
2024-02-13 02:28:36 +01:00
|
|
|
const [windowVisible, setWindowVisible] = useState(true);
|
|
|
|
const visibilityListener = useCallback(() => {
|
|
|
|
setWindowVisible(document.visibilityState == "visible");
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
addEventListener("visibilitychange", visibilityListener);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
removeEventListener("visibilitychange", visibilityListener);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-12-08 14:33:22 +01:00
|
|
|
return (
|
2024-02-23 04:15:50 +01:00
|
|
|
<div className="w-full h-full overflow-y-scroll px-2">
|
2024-02-10 13:30:53 +01:00
|
|
|
{events && events.length > 0 && (
|
|
|
|
<ScrollArea>
|
|
|
|
<TooltipProvider>
|
|
|
|
<div className="flex">
|
|
|
|
{events.map((event) => {
|
2024-02-11 14:23:45 +01:00
|
|
|
return <AnimatedEventThumbnail key={event.id} event={event} />;
|
2024-02-10 13:30:53 +01:00
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</TooltipProvider>
|
|
|
|
<ScrollBar orientation="horizontal" />
|
|
|
|
</ScrollArea>
|
2023-12-20 15:34:27 +01:00
|
|
|
)}
|
2024-02-10 13:30:53 +01:00
|
|
|
|
|
|
|
<div className="mt-4 md:grid md:grid-cols-2 xl:grid-cols-3 3xl:grid-cols-4 gap-4">
|
|
|
|
{cameras.map((camera) => {
|
|
|
|
let grow;
|
2024-02-10 17:28:23 +01:00
|
|
|
let aspectRatio = camera.detect.width / camera.detect.height;
|
|
|
|
if (aspectRatio > 2) {
|
|
|
|
grow = "md:col-span-2 aspect-wide";
|
|
|
|
} else if (aspectRatio < 1) {
|
2024-02-13 02:28:36 +01:00
|
|
|
grow = `md:row-span-2 aspect-tall md:h-full`;
|
2024-02-10 13:30:53 +01:00
|
|
|
} else {
|
|
|
|
grow = "aspect-video";
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<LivePlayer
|
|
|
|
key={camera.name}
|
|
|
|
className={`mb-2 md:mb-0 rounded-2xl bg-black ${grow}`}
|
2024-02-13 02:28:36 +01:00
|
|
|
windowVisible={windowVisible}
|
2024-02-10 13:30:53 +01:00
|
|
|
cameraConfig={camera}
|
2024-02-21 21:07:32 +01:00
|
|
|
preferredLiveMode={isSafari ? "webrtc" : "mse"}
|
2024-02-10 13:30:53 +01:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
2024-02-21 21:07:32 +01:00
|
|
|
</div>
|
2023-12-08 14:33:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Live;
|