add chips for active objects on live dashboard (#11162)

This commit is contained in:
Josh Hawkins 2024-04-29 16:23:14 -05:00 committed by GitHub
parent f946cf55be
commit 7745313cdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 10 deletions

View File

@ -6,9 +6,14 @@ import { useEffect, useMemo, useState } from "react";
import MSEPlayer from "./MsePlayer"; import MSEPlayer from "./MsePlayer";
import JSMpegPlayer from "./JSMpegPlayer"; import JSMpegPlayer from "./JSMpegPlayer";
import { MdCircle } from "react-icons/md"; import { MdCircle } from "react-icons/md";
import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";
import { useCameraActivity } from "@/hooks/use-camera-activity"; import { useCameraActivity } from "@/hooks/use-camera-activity";
import { LivePlayerMode } from "@/types/live"; import { LivePlayerMode } from "@/types/live";
import useCameraLiveMode from "@/hooks/use-camera-live-mode"; import useCameraLiveMode from "@/hooks/use-camera-live-mode";
import { getIconForLabel } from "@/utils/iconUtil";
import Chip from "../indicators/Chip";
import { isMobile } from "react-device-detect";
import { capitalizeFirstLetter } from "@/utils/stringUtil";
type LivePlayerProps = { type LivePlayerProps = {
cameraRef?: (ref: HTMLDivElement | null) => void; cameraRef?: (ref: HTMLDivElement | null) => void;
@ -37,9 +42,12 @@ export default function LivePlayer({
pip, pip,
onClick, onClick,
}: LivePlayerProps) { }: LivePlayerProps) {
const [cameraHovered, setCameraHovered] = useState(false);
// camera activity // camera activity
const { activeMotion, activeTracking } = useCameraActivity(cameraConfig); const { activeMotion, activeTracking, activeObjects } =
useCameraActivity(cameraConfig);
const cameraActive = useMemo( const cameraActive = useMemo(
() => () =>
@ -148,11 +156,54 @@ export default function LivePlayer({
: "outline-0 outline-background" : "outline-0 outline-background"
} transition-all duration-500 ${className}`} } transition-all duration-500 ${className}`}
onClick={onClick} onClick={onClick}
onMouseEnter={() => setCameraHovered(true)}
onMouseLeave={() => setCameraHovered(false)}
> >
<div className="absolute top-0 inset-x-0 rounded-lg md:rounded-2xl z-10 w-full h-[30%] bg-gradient-to-b from-black/20 to-transparent pointer-events-none"></div> <div className="absolute top-0 inset-x-0 rounded-lg md:rounded-2xl z-10 w-full h-[30%] bg-gradient-to-b from-black/20 to-transparent pointer-events-none"></div>
<div className="absolute bottom-0 inset-x-0 rounded-lg md:rounded-2xl z-10 w-full h-[10%] bg-gradient-to-t from-black/20 to-transparent pointer-events-none"></div> <div className="absolute bottom-0 inset-x-0 rounded-lg md:rounded-2xl z-10 w-full h-[10%] bg-gradient-to-t from-black/20 to-transparent pointer-events-none"></div>
{player} {player}
{activeObjects.length > 0 && (
<div className="absolute left-0 top-2 z-40">
<Tooltip>
<div className="flex">
<TooltipTrigger asChild>
<div className="mx-3 pb-1 text-white text-sm">
<Chip
className={`flex items-start justify-between space-x-1 ${cameraHovered || isMobile ? "" : "hidden"} bg-gradient-to-br from-gray-400 to-gray-500 bg-gray-500 z-0`}
>
{[
...new Set([
...(activeObjects || []).map(({ label }) => label),
]),
]
.map((label) => {
return getIconForLabel(label, "size-3 text-white");
})
.sort()}
</Chip>
</div>
</TooltipTrigger>
</div>
<TooltipContent className="capitalize">
{[
...new Set([
...(activeObjects || []).map(({ label }) => label),
]),
]
.filter(
(label) =>
label !== undefined && !label.includes("-verified"),
)
.map((label) => capitalizeFirstLetter(label))
.sort()
.join(", ")
.replaceAll("-verified", "")}
</TooltipContent>
</Tooltip>
</div>
)}
<div <div
className={`absolute inset-0 w-full ${ className={`absolute inset-0 w-full ${
showStillWithoutActivity && !liveReady ? "visible" : "invisible" showStillWithoutActivity && !liveReady ? "visible" : "invisible"

View File

@ -4,17 +4,24 @@ import { MotionData, ReviewSegment } from "@/types/review";
import { useEffect, useMemo, useState } from "react"; import { useEffect, useMemo, useState } from "react";
import { useTimelineUtils } from "./use-timeline-utils"; import { useTimelineUtils } from "./use-timeline-utils";
type ActiveObjectType = {
id: string;
label: string;
stationary: boolean;
};
type useCameraActivityReturn = { type useCameraActivityReturn = {
activeTracking: boolean; activeTracking: boolean;
activeMotion: boolean; activeMotion: boolean;
activeObjects: ActiveObjectType[];
}; };
export function useCameraActivity( export function useCameraActivity(
camera: CameraConfig, camera: CameraConfig,
): useCameraActivityReturn { ): useCameraActivityReturn {
const [activeObjects, setActiveObjects] = useState<string[]>([]); const [activeObjects, setActiveObjects] = useState<ActiveObjectType[]>([]);
const hasActiveObjects = useMemo( const hasActiveObjects = useMemo(
() => activeObjects.length > 0, () => activeObjects.filter((obj) => !obj.stationary).length > 0,
[activeObjects], [activeObjects],
); );
@ -30,7 +37,9 @@ export function useCameraActivity(
return; return;
} }
const eventIndex = activeObjects.indexOf(event.after.id); const eventIndex = activeObjects.findIndex(
(obj) => obj.id === event.after.id,
);
if (event.type == "end") { if (event.type == "end") {
if (eventIndex != -1) { if (eventIndex != -1) {
@ -42,14 +51,14 @@ export function useCameraActivity(
if (eventIndex == -1) { if (eventIndex == -1) {
// add unknown event to list if not stationary // add unknown event to list if not stationary
if (!event.after.stationary) { if (!event.after.stationary) {
const newActiveObjects = [...activeObjects, event.after.id]; const newActiveObject: ActiveObjectType = {
id: event.after.id,
label: event.after.label,
stationary: event.after.stationary,
};
const newActiveObjects = [...activeObjects, newActiveObject];
setActiveObjects(newActiveObjects); setActiveObjects(newActiveObjects);
} }
} else {
// remove known event from list if it has become stationary
if (event.after.stationary) {
activeObjects.splice(eventIndex, 1);
}
} }
} }
}, [camera, event, activeObjects]); }, [camera, event, activeObjects]);
@ -57,6 +66,7 @@ export function useCameraActivity(
return { return {
activeTracking: hasActiveObjects, activeTracking: hasActiveObjects,
activeMotion: detectingMotion == "ON", activeMotion: detectingMotion == "ON",
activeObjects,
}; };
} }