2024-02-07 16:46:45 +03:00
|
|
|
import { EventThumbnail } from "@/components/image/EventThumbnail";
|
2023-12-16 02:24:50 +03:00
|
|
|
import LivePlayer from "@/components/player/LivePlayer";
|
2024-02-07 00:30:41 +03:00
|
|
|
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
|
|
|
|
import { Event as FrigateEvent } from "@/types/event";
|
2024-02-07 01:14:10 +03:00
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
|
import axios from "axios";
|
2024-02-07 01:36:32 +03:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
2023-12-16 02:24:50 +03:00
|
|
|
import useSWR from "swr";
|
2023-12-08 16:33:22 +03:00
|
|
|
|
|
|
|
|
function Live() {
|
2023-12-16 02:24:50 +03:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
|
|
|
|
2024-02-07 00:30:41 +03:00
|
|
|
// recent events
|
2023-12-16 17:40:00 +03:00
|
|
|
|
2024-02-07 16:24:29 +03:00
|
|
|
const [recentCutoff, setRecentCutoff] = useState<number>(0);
|
2024-02-07 01:36:32 +03:00
|
|
|
useEffect(() => {
|
2024-02-07 16:24:29 +03:00
|
|
|
const date = new Date();
|
2024-02-07 16:46:45 +03:00
|
|
|
date.setHours(date.getHours() - 4);
|
2024-02-07 16:24:29 +03:00
|
|
|
setRecentCutoff(date.getTime() / 1000);
|
|
|
|
|
|
2024-02-07 01:36:32 +03:00
|
|
|
const intervalId: NodeJS.Timeout = setInterval(() => {
|
|
|
|
|
const date = new Date();
|
|
|
|
|
date.setHours(date.getHours() - 4);
|
2024-02-07 16:24:29 +03:00
|
|
|
setRecentCutoff(date.getTime() / 1000);
|
|
|
|
|
}, 30000);
|
2024-02-07 01:36:32 +03:00
|
|
|
return () => clearInterval(intervalId);
|
2024-02-07 16:24:29 +03:00
|
|
|
}, [30000]);
|
2024-02-07 00:30:41 +03:00
|
|
|
const { data: events, mutate: updateEvents } = useSWR<FrigateEvent[]>([
|
|
|
|
|
"events",
|
2024-02-07 01:36:32 +03:00
|
|
|
{ limit: 10, after: recentCutoff },
|
2024-02-07 00:30:41 +03:00
|
|
|
]);
|
2024-02-01 15:44:10 +03:00
|
|
|
|
2024-02-07 01:14:10 +03:00
|
|
|
const onFavorite = useCallback(
|
|
|
|
|
async (e: Event, event: FrigateEvent) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
let response;
|
|
|
|
|
if (!event.retain_indefinitely) {
|
|
|
|
|
response = await axios.post(`events/${event.id}/retain`);
|
|
|
|
|
} else {
|
|
|
|
|
response = await axios.delete(`events/${event.id}/retain`);
|
|
|
|
|
}
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
updateEvents();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[event]
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-07 00:30:41 +03:00
|
|
|
// camera live views
|
2023-12-16 02:24:50 +03:00
|
|
|
|
2024-02-07 01:14:10 +03:00
|
|
|
const cameras = useMemo(() => {
|
2024-02-07 00:30:41 +03:00
|
|
|
if (!config) {
|
|
|
|
|
return [];
|
2023-12-16 02:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
2024-02-07 01:14:10 +03:00
|
|
|
return Object.values(config.cameras)
|
|
|
|
|
.filter((conf) => conf.ui.dashboard && conf.enabled)
|
|
|
|
|
.sort((aConf, bConf) => aConf.ui.order - bConf.ui.order);
|
2024-02-07 00:30:41 +03:00
|
|
|
}, [config]);
|
2023-12-16 02:24:50 +03:00
|
|
|
|
2023-12-08 16:33:22 +03:00
|
|
|
return (
|
2024-02-07 00:30:41 +03:00
|
|
|
<>
|
|
|
|
|
{events && events.length > 0 && (
|
|
|
|
|
<ScrollArea>
|
|
|
|
|
<div className="flex">
|
|
|
|
|
{events.map((event) => {
|
|
|
|
|
return (
|
2024-02-07 16:46:45 +03:00
|
|
|
<EventThumbnail
|
2024-02-07 16:24:29 +03:00
|
|
|
key={event.id}
|
2024-02-07 16:46:45 +03:00
|
|
|
event={event}
|
|
|
|
|
onFavorite={onFavorite}
|
|
|
|
|
/>
|
2024-02-07 00:30:41 +03:00
|
|
|
);
|
|
|
|
|
})}
|
2024-01-31 15:29:18 +03:00
|
|
|
</div>
|
2024-02-07 00:30:41 +03:00
|
|
|
<ScrollBar orientation="horizontal" />
|
|
|
|
|
</ScrollArea>
|
2023-12-16 02:24:50 +03:00
|
|
|
)}
|
2024-02-07 00:30:41 +03:00
|
|
|
|
2024-02-07 19:15:14 +03:00
|
|
|
<div className="mt-4 grid grid-cols-3 gap-4">
|
2024-02-07 01:14:10 +03:00
|
|
|
{cameras.map((camera) => {
|
2024-02-07 18:21:40 +03:00
|
|
|
let grow;
|
|
|
|
|
if (camera.detect.width / camera.detect.height > 2) {
|
2024-02-07 19:15:14 +03:00
|
|
|
grow = "h-[424px] col-span-2";
|
|
|
|
|
} else if (camera.detect.width / camera.detect.height < 1) {
|
|
|
|
|
grow = "h-[840px] row-span-2";
|
2024-02-07 18:21:40 +03:00
|
|
|
} else {
|
|
|
|
|
grow = "h-[425px]";
|
|
|
|
|
}
|
2024-02-07 16:24:29 +03:00
|
|
|
return (
|
|
|
|
|
<LivePlayer
|
|
|
|
|
key={camera.name}
|
2024-02-07 18:21:40 +03:00
|
|
|
className={`rounded-2xl bg-black ${grow}`}
|
2024-02-07 16:24:29 +03:00
|
|
|
cameraConfig={camera}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2024-02-07 00:30:41 +03:00
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
2023-12-08 16:33:22 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Live;
|