2024-02-13 02:28:36 +01:00
|
|
|
import { useFrigateEvents } from "@/api/ws";
|
2024-02-27 22:39:05 +01:00
|
|
|
import Logo from "@/components/Logo";
|
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-27 22:39:05 +01:00
|
|
|
import { Button } from "@/components/ui/button";
|
2024-02-10 13:30:53 +01:00
|
|
|
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
|
|
|
import { TooltipProvider } from "@/components/ui/tooltip";
|
2024-02-28 15:16:16 +01:00
|
|
|
import { usePersistence } from "@/hooks/use-persistence";
|
2024-02-10 13:30:53 +01:00
|
|
|
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-27 22:39:05 +01:00
|
|
|
import { isDesktop, isMobile, isSafari } from "react-device-detect";
|
|
|
|
import { CiGrid2H, CiGrid31 } from "react-icons/ci";
|
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-27 22:39:05 +01:00
|
|
|
// layout
|
|
|
|
|
2024-02-28 15:16:16 +01:00
|
|
|
const [layout, setLayout] = usePersistence<"grid" | "list">(
|
|
|
|
"live-layout",
|
2024-02-27 22:39:05 +01:00
|
|
|
isDesktop ? "grid" : "list"
|
|
|
|
);
|
|
|
|
|
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-28 04:40:57 +01:00
|
|
|
<div className="size-full overflow-y-scroll px-2">
|
2024-02-27 22:39:05 +01:00
|
|
|
{isMobile && (
|
|
|
|
<div className="relative h-9 flex items-center justify-between">
|
|
|
|
<Logo className="absolute inset-y-0 inset-x-1/2 -translate-x-1/2 h-8" />
|
|
|
|
<div />
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
<Button
|
|
|
|
className={layout == "grid" ? "text-blue-600 bg-blue-200" : ""}
|
|
|
|
size="xs"
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => setLayout("grid")}
|
|
|
|
>
|
|
|
|
<CiGrid31 className="m-1" />
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
className={layout == "list" ? "text-blue-600 bg-blue-200" : ""}
|
|
|
|
size="xs"
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => setLayout("list")}
|
|
|
|
>
|
|
|
|
<CiGrid2H className="m-1" />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
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
|
|
|
|
2024-02-27 22:39:05 +01:00
|
|
|
<div
|
2024-02-28 04:40:57 +01:00
|
|
|
className={`mt-4 grid ${layout == "grid" ? "grid-cols-2 xl:grid-cols-3 3xl:grid-cols-4" : ""} gap-2 md:gap-4 *:rounded-2xl *:bg-black`}
|
2024-02-27 22:39:05 +01:00
|
|
|
>
|
2024-02-10 13:30:53 +01:00
|
|
|
{cameras.map((camera) => {
|
|
|
|
let grow;
|
2024-02-10 17:28:23 +01:00
|
|
|
let aspectRatio = camera.detect.width / camera.detect.height;
|
|
|
|
if (aspectRatio > 2) {
|
2024-02-27 22:39:05 +01:00
|
|
|
grow = `${layout == "grid" ? "col-span-2" : ""} aspect-wide`;
|
2024-02-10 17:28:23 +01:00
|
|
|
} else if (aspectRatio < 1) {
|
2024-02-27 22:39:05 +01:00
|
|
|
grow = `${layout == "grid" ? "row-span-2 aspect-tall md:h-full" : ""} aspect-tall`;
|
2024-02-10 13:30:53 +01:00
|
|
|
} else {
|
|
|
|
grow = "aspect-video";
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<LivePlayer
|
|
|
|
key={camera.name}
|
2024-02-28 04:40:57 +01:00
|
|
|
className={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;
|