2024-02-10 13:30:53 +01:00
|
|
|
import { EventThumbnail } from "@/components/image/EventThumbnail";
|
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-10 13:30:53 +01:00
|
|
|
import axios from "axios";
|
|
|
|
import { useCallback, useMemo } from "react";
|
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
|
|
|
|
|
|
|
|
const { data: allEvents, mutate: updateEvents } = useSWR<FrigateEvent[]>(
|
|
|
|
["events", { limit: 10 }],
|
|
|
|
{ refreshInterval: 60000 }
|
2023-12-16 15:40:00 +01:00
|
|
|
);
|
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
|
|
|
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`);
|
2024-02-01 13:44:10 +01:00
|
|
|
}
|
2024-02-10 13:30:53 +01:00
|
|
|
if (response.status === 200) {
|
|
|
|
updateEvents();
|
|
|
|
}
|
|
|
|
}, []);
|
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
|
|
|
|
2023-12-08 14:33:22 +01:00
|
|
|
return (
|
2024-02-10 13:30:53 +01:00
|
|
|
<>
|
|
|
|
{events && events.length > 0 && (
|
|
|
|
<ScrollArea>
|
|
|
|
<TooltipProvider>
|
|
|
|
<div className="flex">
|
|
|
|
{events.map((event) => {
|
|
|
|
return (
|
|
|
|
<EventThumbnail
|
|
|
|
key={event.id}
|
|
|
|
event={event}
|
|
|
|
onFavorite={onFavorite}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</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;
|
|
|
|
if (camera.detect.width / camera.detect.height > 2) {
|
|
|
|
grow = "aspect-wide md:col-span-2";
|
|
|
|
} else if (camera.detect.width / camera.detect.height < 1) {
|
|
|
|
grow = "aspect-tall md:aspect-auto md:row-span-2";
|
|
|
|
} else {
|
|
|
|
grow = "aspect-video";
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<LivePlayer
|
|
|
|
key={camera.name}
|
|
|
|
className={`mb-2 md:mb-0 rounded-2xl bg-black ${grow}`}
|
|
|
|
cameraConfig={camera}
|
|
|
|
preferredLiveMode="mse"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</>
|
2023-12-08 14:33:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Live;
|