2024-03-02 01:43:02 +01:00
|
|
|
import { useFrigateReviews } from "@/api/ws";
|
|
|
|
import Logo from "@/components/Logo";
|
2024-03-05 00:18:30 +01:00
|
|
|
import { CameraGroupSelector } from "@/components/filter/CameraGroupSelector";
|
2024-03-21 02:46:45 +01:00
|
|
|
import { LiveGridIcon, LiveListIcon } from "@/components/icons/LiveIcons";
|
2024-03-26 22:03:58 +01:00
|
|
|
import { AnimatedEventCard } from "@/components/card/AnimatedEventCard";
|
2024-03-12 20:53:01 +01:00
|
|
|
import BirdseyeLivePlayer from "@/components/player/BirdseyeLivePlayer";
|
2024-03-02 01:43:02 +01:00
|
|
|
import LivePlayer from "@/components/player/LivePlayer";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
|
|
|
import { TooltipProvider } from "@/components/ui/tooltip";
|
|
|
|
import { usePersistence } from "@/hooks/use-persistence";
|
2024-03-12 20:53:01 +01:00
|
|
|
import { CameraConfig, FrigateConfig } from "@/types/frigateConfig";
|
2024-03-02 01:43:02 +01:00
|
|
|
import { ReviewSegment } from "@/types/review";
|
2024-03-25 21:56:13 +01:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2024-05-07 16:28:10 +02:00
|
|
|
import {
|
|
|
|
isDesktop,
|
|
|
|
isMobile,
|
|
|
|
isMobileOnly,
|
|
|
|
isSafari,
|
|
|
|
} from "react-device-detect";
|
2024-03-02 01:43:02 +01:00
|
|
|
import useSWR from "swr";
|
2024-05-07 16:28:10 +02:00
|
|
|
import DraggableGridLayout from "./DraggableGridLayout";
|
2024-03-02 01:43:02 +01:00
|
|
|
|
|
|
|
type LiveDashboardViewProps = {
|
|
|
|
cameras: CameraConfig[];
|
2024-05-07 16:28:10 +02:00
|
|
|
cameraGroup?: string;
|
2024-03-12 20:53:01 +01:00
|
|
|
includeBirdseye: boolean;
|
2024-03-02 01:43:02 +01:00
|
|
|
onSelectCamera: (camera: string) => void;
|
|
|
|
};
|
|
|
|
export default function LiveDashboardView({
|
|
|
|
cameras,
|
2024-05-07 16:28:10 +02:00
|
|
|
cameraGroup,
|
2024-03-12 20:53:01 +01:00
|
|
|
includeBirdseye,
|
2024-03-02 01:43:02 +01:00
|
|
|
onSelectCamera,
|
|
|
|
}: LiveDashboardViewProps) {
|
2024-03-12 20:53:01 +01:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
|
|
|
2024-03-02 01:43:02 +01:00
|
|
|
// layout
|
|
|
|
|
2024-05-07 16:28:10 +02:00
|
|
|
const [mobileLayout, setMobileLayout] = usePersistence<"grid" | "list">(
|
2024-03-02 01:43:02 +01:00
|
|
|
"live-layout",
|
|
|
|
isDesktop ? "grid" : "list",
|
|
|
|
);
|
|
|
|
|
|
|
|
// recent events
|
|
|
|
const { payload: eventUpdate } = useFrigateReviews();
|
|
|
|
const { data: allEvents, mutate: updateEvents } = useSWR<ReviewSegment[]>([
|
|
|
|
"review",
|
|
|
|
{ limit: 10, severity: "alert" },
|
|
|
|
]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!eventUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if event is ended and was saved, update events list
|
2024-04-23 04:20:30 +02:00
|
|
|
if (eventUpdate.after.severity == "alert") {
|
|
|
|
if (eventUpdate.type == "end" || eventUpdate.type == "new") {
|
|
|
|
setTimeout(
|
|
|
|
() => updateEvents(),
|
|
|
|
eventUpdate.type == "end" ? 1000 : 6000,
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
eventUpdate.before.data.objects.length <
|
|
|
|
eventUpdate.after.data.objects.length
|
|
|
|
) {
|
|
|
|
setTimeout(() => updateEvents(), 5000);
|
|
|
|
}
|
|
|
|
|
2024-03-02 01:43:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, [eventUpdate, updateEvents]);
|
|
|
|
|
|
|
|
const events = useMemo(() => {
|
|
|
|
if (!allEvents) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const date = new Date();
|
|
|
|
date.setHours(date.getHours() - 1);
|
|
|
|
const cutoff = date.getTime() / 1000;
|
|
|
|
return allEvents.filter((event) => event.start_time > cutoff);
|
|
|
|
}, [allEvents]);
|
|
|
|
|
|
|
|
// camera live views
|
|
|
|
|
|
|
|
const [windowVisible, setWindowVisible] = useState(true);
|
|
|
|
const visibilityListener = useCallback(() => {
|
|
|
|
setWindowVisible(document.visibilityState == "visible");
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
addEventListener("visibilitychange", visibilityListener);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
removeEventListener("visibilitychange", visibilityListener);
|
|
|
|
};
|
|
|
|
}, [visibilityListener]);
|
|
|
|
|
2024-03-25 21:56:13 +01:00
|
|
|
const [visibleCameras, setVisibleCameras] = useState<string[]>([]);
|
|
|
|
const visibleCameraObserver = useRef<IntersectionObserver | null>(null);
|
|
|
|
useEffect(() => {
|
|
|
|
const visibleCameras = new Set<string>();
|
|
|
|
visibleCameraObserver.current = new IntersectionObserver(
|
|
|
|
(entries) => {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
const camera = (entry.target as HTMLElement).dataset.camera;
|
|
|
|
|
|
|
|
if (!camera) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.isIntersecting) {
|
|
|
|
visibleCameras.add(camera);
|
|
|
|
} else {
|
|
|
|
visibleCameras.delete(camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
setVisibleCameras([...visibleCameras]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{ threshold: 0.5 },
|
|
|
|
);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
visibleCameraObserver.current?.disconnect();
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const cameraRef = useCallback(
|
|
|
|
(node: HTMLElement | null) => {
|
|
|
|
if (!visibleCameraObserver.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (node) visibleCameraObserver.current.observe(node);
|
|
|
|
} catch (e) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// we need to listen on the value of the ref
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
[visibleCameraObserver.current],
|
|
|
|
);
|
|
|
|
|
2024-03-12 20:53:01 +01:00
|
|
|
const birdseyeConfig = useMemo(() => config?.birdseye, [config]);
|
|
|
|
|
2024-03-02 01:43:02 +01:00
|
|
|
return (
|
2024-03-30 19:44:12 +01:00
|
|
|
<div className="size-full p-2 overflow-y-auto">
|
2024-03-02 01:43:02 +01:00
|
|
|
{isMobile && (
|
2024-03-30 19:44:12 +01:00
|
|
|
<div className="h-11 relative flex items-center justify-between">
|
2024-03-28 18:53:36 +01:00
|
|
|
<Logo className="absolute inset-x-1/2 -translate-x-1/2 h-8" />
|
2024-04-26 01:19:31 +02:00
|
|
|
<div className="max-w-[45%]">
|
|
|
|
<CameraGroupSelector />
|
|
|
|
</div>
|
2024-03-02 01:43:02 +01:00
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
<Button
|
2024-03-21 02:46:45 +01:00
|
|
|
className={`p-1 ${
|
2024-05-07 16:28:10 +02:00
|
|
|
mobileLayout == "grid"
|
2024-03-21 02:46:45 +01:00
|
|
|
? "bg-blue-900 focus:bg-blue-900 bg-opacity-60 focus:bg-opacity-60"
|
2024-04-04 22:55:04 +02:00
|
|
|
: "bg-secondary"
|
2024-03-21 02:46:45 +01:00
|
|
|
}`}
|
2024-03-02 01:43:02 +01:00
|
|
|
size="xs"
|
2024-05-07 16:28:10 +02:00
|
|
|
onClick={() => setMobileLayout("grid")}
|
2024-03-02 01:43:02 +01:00
|
|
|
>
|
2024-05-07 16:28:10 +02:00
|
|
|
<LiveGridIcon layout={mobileLayout} />
|
2024-03-02 01:43:02 +01:00
|
|
|
</Button>
|
|
|
|
<Button
|
2024-03-21 02:46:45 +01:00
|
|
|
className={`p-1 ${
|
2024-05-07 16:28:10 +02:00
|
|
|
mobileLayout == "list"
|
2024-03-21 02:46:45 +01:00
|
|
|
? "bg-blue-900 focus:bg-blue-900 bg-opacity-60 focus:bg-opacity-60"
|
2024-04-04 22:55:04 +02:00
|
|
|
: "bg-secondary"
|
2024-03-21 02:46:45 +01:00
|
|
|
}`}
|
2024-03-02 01:43:02 +01:00
|
|
|
size="xs"
|
2024-05-07 16:28:10 +02:00
|
|
|
onClick={() => setMobileLayout("list")}
|
2024-03-02 01:43:02 +01:00
|
|
|
>
|
2024-05-07 16:28:10 +02:00
|
|
|
<LiveListIcon layout={mobileLayout} />
|
2024-03-02 01:43:02 +01:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{events && events.length > 0 && (
|
|
|
|
<ScrollArea>
|
|
|
|
<TooltipProvider>
|
2024-03-30 19:44:12 +01:00
|
|
|
<div className="px-1 flex gap-2 items-center">
|
2024-03-02 01:43:02 +01:00
|
|
|
{events.map((event) => {
|
2024-03-26 22:03:58 +01:00
|
|
|
return <AnimatedEventCard key={event.id} event={event} />;
|
2024-03-02 01:43:02 +01:00
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</TooltipProvider>
|
|
|
|
<ScrollBar orientation="horizontal" />
|
|
|
|
</ScrollArea>
|
|
|
|
)}
|
|
|
|
|
2024-05-07 16:28:10 +02:00
|
|
|
{!cameraGroup || cameraGroup == "default" || isMobileOnly ? (
|
|
|
|
<div
|
|
|
|
className={`mt-2 px-2 grid ${mobileLayout == "grid" ? "grid-cols-2 xl:grid-cols-3 3xl:grid-cols-4" : ""} gap-2 md:gap-4`}
|
|
|
|
>
|
|
|
|
{includeBirdseye && birdseyeConfig?.enabled && (
|
|
|
|
<BirdseyeLivePlayer
|
|
|
|
birdseyeConfig={birdseyeConfig}
|
|
|
|
liveMode={birdseyeConfig.restream ? "mse" : "jsmpeg"}
|
|
|
|
onClick={() => onSelectCamera("birdseye")}
|
2024-03-02 01:43:02 +01:00
|
|
|
/>
|
2024-05-07 16:28:10 +02:00
|
|
|
)}
|
|
|
|
{cameras.map((camera) => {
|
|
|
|
let grow;
|
|
|
|
const aspectRatio = camera.detect.width / camera.detect.height;
|
|
|
|
if (aspectRatio > 2) {
|
|
|
|
grow = `${mobileLayout == "grid" ? "col-span-2" : ""} aspect-wide`;
|
|
|
|
} else if (aspectRatio < 1) {
|
|
|
|
grow = `${mobileLayout == "grid" ? "row-span-2 aspect-tall md:h-full" : ""} aspect-tall`;
|
|
|
|
} else {
|
|
|
|
grow = "aspect-video";
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<LivePlayer
|
|
|
|
cameraRef={cameraRef}
|
|
|
|
key={camera.name}
|
|
|
|
className={`${grow} rounded-lg md:rounded-2xl bg-black`}
|
|
|
|
windowVisible={
|
|
|
|
windowVisible && visibleCameras.includes(camera.name)
|
|
|
|
}
|
|
|
|
cameraConfig={camera}
|
|
|
|
preferredLiveMode={isSafari ? "webrtc" : "mse"}
|
|
|
|
onClick={() => onSelectCamera(camera.name)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<DraggableGridLayout
|
|
|
|
cameras={cameras}
|
|
|
|
cameraGroup={cameraGroup}
|
|
|
|
cameraRef={cameraRef}
|
|
|
|
includeBirdseye={includeBirdseye}
|
|
|
|
onSelectCamera={onSelectCamera}
|
|
|
|
windowVisible={windowVisible}
|
|
|
|
visibleCameras={visibleCameras}
|
|
|
|
/>
|
|
|
|
)}
|
2024-03-02 01:43:02 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|