import DynamicVideoPlayer, { DynamicVideoController, } from "@/components/player/DynamicVideoPlayer"; import EventReviewTimeline from "@/components/timeline/EventReviewTimeline"; import MotionReviewTimeline from "@/components/timeline/MotionReviewTimeline"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Preview } from "@/types/preview"; import { MotionData, ReviewSegment, ReviewSeverity } from "@/types/review"; import { getChunkedTimeDay } from "@/utils/timelineUtil"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { IoMdArrowRoundBack } from "react-icons/io"; import { useNavigate } from "react-router-dom"; import useSWR from "swr"; const SEGMENT_DURATION = 30; type DesktopRecordingViewProps = { startCamera: string; startTime: number; severity: ReviewSeverity; reviewItems: ReviewSegment[]; allCameras: string[]; allPreviews?: Preview[]; }; export function DesktopRecordingView({ startCamera, startTime, severity, reviewItems, allCameras, allPreviews, }: DesktopRecordingViewProps) { const navigate = useNavigate(); const contentRef = useRef(null); // controller state const [mainCamera, setMainCamera] = useState(startCamera); const videoPlayersRef = useRef<{ [camera: string]: DynamicVideoController }>( {}, ); const [playbackStart, setPlaybackStart] = useState(startTime); // timeline time const timeRange = useMemo(() => getChunkedTimeDay(startTime), [startTime]); const [selectedRangeIdx, setSelectedRangeIdx] = useState( timeRange.ranges.findIndex((chunk) => { return chunk.start <= startTime && chunk.end >= startTime; }), ); const currentTimeRange = useMemo( () => timeRange.ranges[selectedRangeIdx], [selectedRangeIdx, timeRange], ); // move to next clip useEffect(() => { if ( !videoPlayersRef.current && Object.values(videoPlayersRef.current).length > 0 ) { return; } const mainController = videoPlayersRef.current[mainCamera]; if (mainController) { mainController.onClipChangedEvent((dir) => { if (dir == "forward") { if (selectedRangeIdx < timeRange.ranges.length - 1) { setSelectedRangeIdx(selectedRangeIdx + 1); } } }); } // we only want to fire once when players are ready // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedRangeIdx, timeRange, videoPlayersRef.current, mainCamera]); // scrubbing and timeline state const [scrubbing, setScrubbing] = useState(false); const [currentTime, setCurrentTime] = useState(startTime); useEffect(() => { if (scrubbing) { if ( currentTime > currentTimeRange.end + 60 || currentTime < currentTimeRange.start - 60 ) { const index = timeRange.ranges.findIndex( (seg) => seg.start <= currentTime && seg.end >= currentTime, ); if (index != -1) { setSelectedRangeIdx(index); } return; } Object.values(videoPlayersRef.current).forEach((controller) => { controller.scrubToTimestamp(currentTime); }); } }, [currentTime, scrubbing, timeRange, currentTimeRange]); useEffect(() => { if (!scrubbing) { videoPlayersRef.current[mainCamera]?.seekToTimestamp(currentTime, true); } // we only want to seek when user stops scrubbing // eslint-disable-next-line react-hooks/exhaustive-deps }, [scrubbing]); const onSelectCamera = useCallback( (newCam: string) => { const lastController = videoPlayersRef.current[mainCamera]; const newController = videoPlayersRef.current[newCam]; lastController.onPlayerTimeUpdate(null); lastController.onClipChangedEvent(null); lastController.scrubToTimestamp(currentTime); newController.onPlayerTimeUpdate((timestamp: number) => { setCurrentTime(timestamp); allCameras.forEach((cam) => { if (cam != newCam) { videoPlayersRef.current[cam]?.scrubToTimestamp( Math.floor(timestamp), ); } }); }); newController.seekToTimestamp(currentTime, true); setPlaybackStart(currentTime); setMainCamera(newCam); }, [allCameras, currentTime, mainCamera], ); // motion timeline data const { data: motionData } = useSWR( severity == "significant_motion" ? [ "review/activity", { before: timeRange.end, after: timeRange.start, scale: SEGMENT_DURATION / 2, }, ] : null, ); return (
{allCameras.map((cam) => { if (cam == mainCamera) { return (
{ videoPlayersRef.current[cam] = controller; controller.onPlayerTimeUpdate((timestamp: number) => { setCurrentTime(timestamp); allCameras.forEach((otherCam) => { if (cam != otherCam) { videoPlayersRef.current[otherCam]?.scrubToTimestamp( Math.floor(timestamp), ); } }); }); }} />
); } return (
{ videoPlayersRef.current[cam] = controller; controller.scrubToTimestamp(startTime, true); }} onClick={() => onSelectCamera(cam)} />
); })}
{severity != "significant_motion" ? ( setScrubbing(scrubbing)} /> ) : ( setScrubbing(scrubbing)} /> )}
); } type MobileRecordingViewProps = { startCamera: string; startTime: number; severity: ReviewSeverity; reviewItems: ReviewSegment[]; relevantPreviews?: Preview[]; allCameras: string[]; }; export function MobileRecordingView({ startCamera, startTime, severity, reviewItems, relevantPreviews, allCameras, }: MobileRecordingViewProps) { const navigate = useNavigate(); const contentRef = useRef(null); // controller state const [playerReady, setPlayerReady] = useState(false); const controllerRef = useRef(undefined); const [playbackCamera, setPlaybackCamera] = useState(startCamera); const [playbackStart, setPlaybackStart] = useState(startTime); // timeline time const timeRange = useMemo(() => getChunkedTimeDay(startTime), [startTime]); const [selectedRangeIdx, setSelectedRangeIdx] = useState( timeRange.ranges.findIndex((chunk) => { return chunk.start <= startTime && chunk.end >= startTime; }), ); const currentTimeRange = useMemo( () => timeRange.ranges[selectedRangeIdx], [selectedRangeIdx, timeRange], ); // move to next clip useEffect(() => { if (!controllerRef.current) { return; } controllerRef.current.onClipChangedEvent((dir) => { if (dir == "forward") { if (selectedRangeIdx < timeRange.ranges.length - 1) { setSelectedRangeIdx(selectedRangeIdx + 1); } } }); }, [playerReady, selectedRangeIdx, timeRange]); // scrubbing and timeline state const [scrubbing, setScrubbing] = useState(false); const [currentTime, setCurrentTime] = useState( startTime || Date.now() / 1000, ); useEffect(() => { if (scrubbing) { if ( currentTime > currentTimeRange.end + 60 || currentTime < currentTimeRange.start - 60 ) { const index = timeRange.ranges.findIndex( (seg) => seg.start <= currentTime && seg.end >= currentTime, ); if (index != -1) { setSelectedRangeIdx(index); } return; } controllerRef.current?.scrubToTimestamp(currentTime); } }, [currentTime, scrubbing, currentTimeRange, timeRange]); useEffect(() => { if (!scrubbing) { controllerRef.current?.seekToTimestamp(currentTime, true); } // we only want to seek when user stops scrubbing // eslint-disable-next-line react-hooks/exhaustive-deps }, [scrubbing]); // motion timeline data const { data: motionData } = useSWR( severity == "significant_motion" ? [ "review/activity", { before: timeRange.end, after: timeRange.start, scale: SEGMENT_DURATION / 2, }, ] : null, ); return (
{ setPlaybackStart(currentTime); setPlaybackCamera(cam); }} > {allCameras.map((cam) => ( {cam.replaceAll("_", " ")} ))}
{ controllerRef.current = controller; setPlayerReady(true); controllerRef.current.onPlayerTimeUpdate((timestamp: number) => { setCurrentTime(timestamp); }); controllerRef.current?.seekToTimestamp(startTime, true); }} />
{severity != "significant_motion" ? ( setScrubbing(scrubbing)} /> ) : ( setScrubbing(scrubbing)} /> )}
); }