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,
|
2024-05-08 14:53:22 +02:00
|
|
|
isTablet,
|
2024-05-07 16:28:10 +02:00
|
|
|
} 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-05-08 14:53:22 +02:00
|
|
|
import { IoClose } from "react-icons/io5";
|
2024-05-10 18:54:37 +02:00
|
|
|
import { LuLayoutDashboard } from "react-icons/lu";
|
|
|
|
import { cn } from "@/lib/utils";
|
2024-06-30 14:04:45 +02:00
|
|
|
import { LivePlayerError, LivePlayerMode } from "@/types/live";
|
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;
|
2024-05-31 14:58:33 +02:00
|
|
|
fullscreen: boolean;
|
|
|
|
toggleFullscreen: () => void;
|
2024-03-02 01:43:02 +01:00
|
|
|
};
|
|
|
|
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,
|
2024-05-31 14:58:33 +02:00
|
|
|
fullscreen,
|
|
|
|
toggleFullscreen,
|
2024-03-02 01:43:02 +01:00
|
|
|
}: 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",
|
|
|
|
);
|
|
|
|
|
2024-05-08 14:53:22 +02:00
|
|
|
const [isEditMode, setIsEditMode] = useState<boolean>(false);
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
2024-06-02 05:13:37 +02:00
|
|
|
const birdseyeContainerRef = useRef<HTMLDivElement>(null);
|
2024-05-08 14:53:22 +02:00
|
|
|
|
2024-03-02 01:43:02 +01:00
|
|
|
// recent events
|
2024-05-29 16:01:39 +02:00
|
|
|
|
2024-03-02 01:43:02 +01:00
|
|
|
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
|
|
|
|
|
2024-05-29 16:01:39 +02:00
|
|
|
const [autoLiveView] = usePersistence("autoLiveView", true);
|
2024-06-29 17:02:30 +02:00
|
|
|
const [preferredLiveModes, setPreferredLiveModes] = useState<{
|
|
|
|
[key: string]: LivePlayerMode;
|
|
|
|
}>({});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!cameras) return;
|
|
|
|
const newPreferredLiveModes = cameras.reduce(
|
|
|
|
(acc, camera) => {
|
|
|
|
const isRestreamed =
|
|
|
|
config &&
|
|
|
|
Object.keys(config.go2rtc.streams || {}).includes(
|
|
|
|
camera.live.stream_name,
|
|
|
|
);
|
|
|
|
|
|
|
|
acc[camera.name] = isRestreamed ? "mse" : "jsmpeg";
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{} as { [key: string]: LivePlayerMode },
|
|
|
|
);
|
|
|
|
|
|
|
|
setPreferredLiveModes(newPreferredLiveModes);
|
|
|
|
}, [cameras, config]);
|
|
|
|
|
2024-03-02 01:43:02 +01:00
|
|
|
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-06-30 14:04:45 +02:00
|
|
|
const handleError = useCallback(
|
|
|
|
(cameraName: string, error: LivePlayerError) => {
|
|
|
|
setPreferredLiveModes((prevModes) => {
|
|
|
|
const newModes = { ...prevModes };
|
|
|
|
if (error === "mse-decode") {
|
|
|
|
newModes[cameraName] = "webrtc";
|
|
|
|
} else {
|
|
|
|
newModes[cameraName] = "jsmpeg";
|
|
|
|
}
|
|
|
|
return newModes;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[setPreferredLiveModes],
|
|
|
|
);
|
|
|
|
|
2024-03-02 01:43:02 +01:00
|
|
|
return (
|
2024-05-16 18:51:57 +02:00
|
|
|
<div
|
2024-06-03 20:43:30 +02:00
|
|
|
className="scrollbar-container size-full overflow-y-auto px-1 pt-2 md:p-2"
|
2024-05-16 18:51:57 +02:00
|
|
|
ref={containerRef}
|
|
|
|
>
|
2024-03-02 01:43:02 +01:00
|
|
|
{isMobile && (
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="relative flex h-11 items-center justify-between">
|
|
|
|
<Logo className="absolute inset-x-1/2 h-8 -translate-x-1/2" />
|
2024-04-26 01:19:31 +02:00
|
|
|
<div className="max-w-[45%]">
|
|
|
|
<CameraGroupSelector />
|
|
|
|
</div>
|
2024-05-08 14:53:22 +02:00
|
|
|
{(!cameraGroup || cameraGroup == "default" || isMobileOnly) && (
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
<Button
|
|
|
|
className={`p-1 ${
|
|
|
|
mobileLayout == "grid"
|
2024-05-14 17:06:44 +02:00
|
|
|
? "bg-blue-900 bg-opacity-60 focus:bg-blue-900 focus:bg-opacity-60"
|
2024-05-08 14:53:22 +02:00
|
|
|
: "bg-secondary"
|
|
|
|
}`}
|
|
|
|
size="xs"
|
|
|
|
onClick={() => setMobileLayout("grid")}
|
|
|
|
>
|
|
|
|
<LiveGridIcon layout={mobileLayout} />
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
className={`p-1 ${
|
|
|
|
mobileLayout == "list"
|
2024-05-14 17:06:44 +02:00
|
|
|
? "bg-blue-900 bg-opacity-60 focus:bg-blue-900 focus:bg-opacity-60"
|
2024-05-08 14:53:22 +02:00
|
|
|
: "bg-secondary"
|
|
|
|
}`}
|
|
|
|
size="xs"
|
|
|
|
onClick={() => setMobileLayout("list")}
|
|
|
|
>
|
|
|
|
<LiveListIcon layout={mobileLayout} />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{cameraGroup && cameraGroup !== "default" && isTablet && (
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
<Button
|
2024-05-10 18:54:37 +02:00
|
|
|
className={cn(
|
|
|
|
"p-1",
|
|
|
|
isEditMode
|
2024-05-14 17:06:44 +02:00
|
|
|
? "bg-selected text-primary"
|
|
|
|
: "bg-secondary text-secondary-foreground",
|
2024-05-10 18:54:37 +02:00
|
|
|
)}
|
2024-05-08 14:53:22 +02:00
|
|
|
size="xs"
|
|
|
|
onClick={() =>
|
|
|
|
setIsEditMode((prevIsEditMode) => !prevIsEditMode)
|
|
|
|
}
|
|
|
|
>
|
2024-05-10 18:54:37 +02:00
|
|
|
{isEditMode ? <IoClose /> : <LuLayoutDashboard />}
|
2024-05-08 14:53:22 +02:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-03-02 01:43:02 +01:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2024-05-31 14:58:33 +02:00
|
|
|
{!fullscreen && events && events.length > 0 && (
|
2024-03-02 01:43:02 +01:00
|
|
|
<ScrollArea>
|
|
|
|
<TooltipProvider>
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="flex items-center gap-2 px-1">
|
2024-03-02 01:43:02 +01:00
|
|
|
{events.map((event) => {
|
2024-06-04 17:10:19 +02:00
|
|
|
return (
|
|
|
|
<AnimatedEventCard
|
|
|
|
key={event.id}
|
|
|
|
event={event}
|
|
|
|
selectedGroup={cameraGroup}
|
|
|
|
/>
|
|
|
|
);
|
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
|
2024-05-17 00:00:14 +02:00
|
|
|
className={cn(
|
2024-05-30 00:03:59 +02:00
|
|
|
"mt-2 grid grid-cols-1 gap-2 px-2 md:gap-4",
|
2024-05-17 00:00:14 +02:00
|
|
|
mobileLayout == "grid" &&
|
|
|
|
"grid-cols-2 xl:grid-cols-3 3xl:grid-cols-4",
|
|
|
|
isMobile && "px-0",
|
|
|
|
)}
|
2024-05-07 16:28:10 +02:00
|
|
|
>
|
|
|
|
{includeBirdseye && birdseyeConfig?.enabled && (
|
2024-06-02 05:13:37 +02:00
|
|
|
<div
|
2024-06-02 19:00:59 +02:00
|
|
|
className={(() => {
|
|
|
|
const aspectRatio =
|
|
|
|
birdseyeConfig.width / birdseyeConfig.height;
|
|
|
|
if (aspectRatio > 2) {
|
|
|
|
return `${mobileLayout == "grid" && "col-span-2"} aspect-wide`;
|
|
|
|
} else if (aspectRatio < 1) {
|
2024-06-05 16:53:17 +02:00
|
|
|
return `${mobileLayout == "grid" && "row-span-2 h-full"} aspect-tall`;
|
2024-06-02 19:00:59 +02:00
|
|
|
} else {
|
|
|
|
return "aspect-video";
|
|
|
|
}
|
|
|
|
})()}
|
2024-06-02 05:13:37 +02:00
|
|
|
ref={birdseyeContainerRef}
|
|
|
|
>
|
|
|
|
<BirdseyeLivePlayer
|
|
|
|
birdseyeConfig={birdseyeConfig}
|
|
|
|
liveMode={birdseyeConfig.restream ? "mse" : "jsmpeg"}
|
|
|
|
onClick={() => onSelectCamera("birdseye")}
|
|
|
|
containerRef={birdseyeContainerRef}
|
|
|
|
/>
|
|
|
|
</div>
|
2024-05-07 16:28:10 +02:00
|
|
|
)}
|
|
|
|
{cameras.map((camera) => {
|
|
|
|
let grow;
|
|
|
|
const aspectRatio = camera.detect.width / camera.detect.height;
|
|
|
|
if (aspectRatio > 2) {
|
2024-06-02 19:00:59 +02:00
|
|
|
grow = `${mobileLayout == "grid" && "col-span-2"} aspect-wide`;
|
2024-05-07 16:28:10 +02:00
|
|
|
} else if (aspectRatio < 1) {
|
2024-06-05 16:53:17 +02:00
|
|
|
grow = `${mobileLayout == "grid" && "row-span-2 h-full"} aspect-tall`;
|
2024-05-07 16:28:10 +02:00
|
|
|
} else {
|
|
|
|
grow = "aspect-video";
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<LivePlayer
|
|
|
|
cameraRef={cameraRef}
|
|
|
|
key={camera.name}
|
2024-05-14 17:06:44 +02:00
|
|
|
className={`${grow} rounded-lg bg-black md:rounded-2xl`}
|
2024-05-07 16:28:10 +02:00
|
|
|
windowVisible={
|
|
|
|
windowVisible && visibleCameras.includes(camera.name)
|
|
|
|
}
|
|
|
|
cameraConfig={camera}
|
2024-06-29 17:02:30 +02:00
|
|
|
preferredLiveMode={preferredLiveModes[camera.name] ?? "mse"}
|
2024-05-29 16:01:39 +02:00
|
|
|
autoLive={autoLiveView}
|
2024-05-07 16:28:10 +02:00
|
|
|
onClick={() => onSelectCamera(camera.name)}
|
2024-06-30 14:04:45 +02:00
|
|
|
onError={(e) => handleError(camera.name, e)}
|
2024-05-07 16:28:10 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<DraggableGridLayout
|
|
|
|
cameras={cameras}
|
|
|
|
cameraGroup={cameraGroup}
|
2024-05-08 14:53:22 +02:00
|
|
|
containerRef={containerRef}
|
2024-05-07 16:28:10 +02:00
|
|
|
cameraRef={cameraRef}
|
|
|
|
includeBirdseye={includeBirdseye}
|
|
|
|
onSelectCamera={onSelectCamera}
|
|
|
|
windowVisible={windowVisible}
|
|
|
|
visibleCameras={visibleCameras}
|
2024-05-08 14:53:22 +02:00
|
|
|
isEditMode={isEditMode}
|
|
|
|
setIsEditMode={setIsEditMode}
|
2024-05-31 14:58:33 +02:00
|
|
|
fullscreen={fullscreen}
|
|
|
|
toggleFullscreen={toggleFullscreen}
|
2024-05-07 16:28:10 +02:00
|
|
|
/>
|
|
|
|
)}
|
2024-03-02 01:43:02 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|