2024-03-18 23:58:54 +03:00
|
|
|
import { useTimelineUtils } from "@/hooks/use-timeline-utils";
|
2024-03-04 19:42:51 +03:00
|
|
|
import { useEventSegmentUtils } from "@/hooks/use-event-segment-utils";
|
2024-04-01 18:57:35 +03:00
|
|
|
import { ReviewSegment } from "@/types/review";
|
2025-02-10 00:13:32 +03:00
|
|
|
import React, { useCallback, useEffect, useMemo, useRef } from "react";
|
2024-03-04 19:42:51 +03:00
|
|
|
import { MinimapBounds, Tick, Timestamp } from "./segment-metadata";
|
|
|
|
|
import { useMotionSegmentUtils } from "@/hooks/use-motion-segment-utils";
|
2024-06-20 02:14:32 +03:00
|
|
|
import { isMobile } from "react-device-detect";
|
2024-03-07 00:35:10 +03:00
|
|
|
import useTapUtils from "@/hooks/use-tap-utils";
|
2024-05-29 21:05:28 +03:00
|
|
|
import { cn } from "@/lib/utils";
|
2024-03-04 19:42:51 +03:00
|
|
|
|
|
|
|
|
type MotionSegmentProps = {
|
|
|
|
|
events: ReviewSegment[];
|
|
|
|
|
segmentTime: number;
|
|
|
|
|
segmentDuration: number;
|
|
|
|
|
timestampSpread: number;
|
2024-04-01 18:57:35 +03:00
|
|
|
firstHalfMotionValue: number;
|
|
|
|
|
secondHalfMotionValue: number;
|
2025-05-23 17:55:48 +03:00
|
|
|
hasRecording?: boolean;
|
2025-10-29 17:39:07 +03:00
|
|
|
prevIsNoRecording?: boolean;
|
|
|
|
|
nextIsNoRecording?: boolean;
|
2024-03-23 16:33:50 +03:00
|
|
|
motionOnly: boolean;
|
2024-03-04 19:42:51 +03:00
|
|
|
showMinimap: boolean;
|
|
|
|
|
minimapStartTime?: number;
|
|
|
|
|
minimapEndTime?: number;
|
2024-03-07 00:35:10 +03:00
|
|
|
setHandlebarTime?: React.Dispatch<React.SetStateAction<number>>;
|
2025-02-10 00:13:32 +03:00
|
|
|
scrollToSegment: (segmentTime: number, ifNeeded?: boolean) => void;
|
2024-03-28 18:03:06 +03:00
|
|
|
dense: boolean;
|
2025-10-29 17:39:07 +03:00
|
|
|
alwaysShowMotionLine?: boolean;
|
2024-03-04 19:42:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function MotionSegment({
|
|
|
|
|
events,
|
|
|
|
|
segmentTime,
|
|
|
|
|
segmentDuration,
|
|
|
|
|
timestampSpread,
|
2024-04-01 18:57:35 +03:00
|
|
|
firstHalfMotionValue,
|
|
|
|
|
secondHalfMotionValue,
|
2025-05-23 17:55:48 +03:00
|
|
|
hasRecording,
|
2025-10-29 17:39:07 +03:00
|
|
|
prevIsNoRecording,
|
|
|
|
|
nextIsNoRecording,
|
2024-03-23 16:33:50 +03:00
|
|
|
motionOnly,
|
2024-03-04 19:42:51 +03:00
|
|
|
showMinimap,
|
|
|
|
|
minimapStartTime,
|
|
|
|
|
minimapEndTime,
|
2024-03-07 00:35:10 +03:00
|
|
|
setHandlebarTime,
|
2025-02-10 00:13:32 +03:00
|
|
|
scrollToSegment,
|
2024-03-28 18:03:06 +03:00
|
|
|
dense,
|
2025-10-29 17:39:07 +03:00
|
|
|
alwaysShowMotionLine = false,
|
2024-03-04 19:42:51 +03:00
|
|
|
}: MotionSegmentProps) {
|
|
|
|
|
const severityType = "all";
|
2024-05-01 03:38:22 +03:00
|
|
|
const { getSeverity, getReviewed, displaySeverityType } =
|
|
|
|
|
useEventSegmentUtils(segmentDuration, events, severityType);
|
2024-03-04 19:42:51 +03:00
|
|
|
|
2024-04-01 18:57:35 +03:00
|
|
|
const { interpolateMotionAudioData } = useMotionSegmentUtils(
|
|
|
|
|
segmentDuration,
|
|
|
|
|
[],
|
|
|
|
|
);
|
2024-03-04 19:42:51 +03:00
|
|
|
|
2024-03-21 20:49:04 +03:00
|
|
|
const { alignStartDateToTimeline, alignEndDateToTimeline } = useTimelineUtils(
|
|
|
|
|
{ segmentDuration },
|
|
|
|
|
);
|
2024-03-04 19:42:51 +03:00
|
|
|
|
2024-03-07 00:35:10 +03:00
|
|
|
const { handleTouchStart } = useTapUtils();
|
|
|
|
|
|
2024-03-04 19:42:51 +03:00
|
|
|
const severity = useMemo(
|
|
|
|
|
() => getSeverity(segmentTime, displaySeverityType),
|
|
|
|
|
// we know that these deps are correct
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
[getSeverity, segmentTime],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const reviewed = useMemo(
|
|
|
|
|
() => getReviewed(segmentTime),
|
|
|
|
|
[getReviewed, segmentTime],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const timestamp = useMemo(() => new Date(segmentTime * 1000), [segmentTime]);
|
2025-02-10 00:13:32 +03:00
|
|
|
const segmentKey = useMemo(
|
|
|
|
|
() => `${segmentTime}_${segmentDuration}`,
|
|
|
|
|
[segmentTime, segmentDuration],
|
|
|
|
|
);
|
2024-03-04 19:42:51 +03:00
|
|
|
|
|
|
|
|
const maxSegmentWidth = useMemo(() => {
|
2024-03-07 00:35:10 +03:00
|
|
|
return isMobile ? 30 : 50;
|
2024-03-04 19:42:51 +03:00
|
|
|
}, []);
|
|
|
|
|
|
2024-03-11 15:56:36 +03:00
|
|
|
const firstHalfSegmentWidth = useMemo(() => {
|
2024-04-01 18:57:35 +03:00
|
|
|
return interpolateMotionAudioData(firstHalfMotionValue, maxSegmentWidth);
|
|
|
|
|
}, [maxSegmentWidth, firstHalfMotionValue, interpolateMotionAudioData]);
|
2024-03-11 15:56:36 +03:00
|
|
|
|
|
|
|
|
const secondHalfSegmentWidth = useMemo(() => {
|
2024-04-01 18:57:35 +03:00
|
|
|
return interpolateMotionAudioData(secondHalfMotionValue, maxSegmentWidth);
|
|
|
|
|
}, [maxSegmentWidth, secondHalfMotionValue, interpolateMotionAudioData]);
|
2024-03-08 07:02:29 +03:00
|
|
|
|
2024-03-04 19:42:51 +03:00
|
|
|
const alignedMinimapStartTime = useMemo(
|
|
|
|
|
() => alignStartDateToTimeline(minimapStartTime ?? 0),
|
|
|
|
|
[minimapStartTime, alignStartDateToTimeline],
|
|
|
|
|
);
|
|
|
|
|
const alignedMinimapEndTime = useMemo(
|
|
|
|
|
() => alignEndDateToTimeline(minimapEndTime ?? 0),
|
|
|
|
|
[minimapEndTime, alignEndDateToTimeline],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const isInMinimapRange = useMemo(() => {
|
|
|
|
|
return (
|
|
|
|
|
showMinimap &&
|
|
|
|
|
segmentTime >= alignedMinimapStartTime &&
|
|
|
|
|
segmentTime < alignedMinimapEndTime
|
|
|
|
|
);
|
|
|
|
|
}, [
|
|
|
|
|
showMinimap,
|
|
|
|
|
alignedMinimapStartTime,
|
|
|
|
|
alignedMinimapEndTime,
|
|
|
|
|
segmentTime,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const isFirstSegmentInMinimap = useMemo(() => {
|
|
|
|
|
return showMinimap && segmentTime === alignedMinimapStartTime;
|
|
|
|
|
}, [showMinimap, segmentTime, alignedMinimapStartTime]);
|
|
|
|
|
|
|
|
|
|
const isLastSegmentInMinimap = useMemo(() => {
|
|
|
|
|
return showMinimap && segmentTime === alignedMinimapEndTime;
|
|
|
|
|
}, [showMinimap, segmentTime, alignedMinimapEndTime]);
|
|
|
|
|
|
2025-10-29 17:39:07 +03:00
|
|
|
// Bottom border: current segment HAS recording, but next segment (below/earlier) has NO recording
|
|
|
|
|
const isFirstSegmentWithoutRecording = useMemo(() => {
|
|
|
|
|
return hasRecording === true && nextIsNoRecording === true;
|
|
|
|
|
}, [hasRecording, nextIsNoRecording]);
|
|
|
|
|
|
|
|
|
|
// Top border: current segment HAS recording, but prev segment (above/later) has NO recording
|
|
|
|
|
const isLastSegmentWithoutRecording = useMemo(() => {
|
|
|
|
|
return hasRecording === true && prevIsNoRecording === true;
|
|
|
|
|
}, [hasRecording, prevIsNoRecording]);
|
|
|
|
|
|
2024-03-04 19:42:51 +03:00
|
|
|
const firstMinimapSegmentRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Check if the first segment is out of view
|
|
|
|
|
const firstSegment = firstMinimapSegmentRef.current;
|
|
|
|
|
if (firstSegment && showMinimap && isFirstSegmentInMinimap) {
|
2025-02-10 00:13:32 +03:00
|
|
|
scrollToSegment(alignedMinimapStartTime);
|
2024-03-04 19:42:51 +03:00
|
|
|
}
|
|
|
|
|
// we know that these deps are correct
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [showMinimap, isFirstSegmentInMinimap, events, segmentDuration]);
|
|
|
|
|
|
2024-03-27 00:36:28 +03:00
|
|
|
const segmentClasses = `h-[8px] relative w-full ${
|
2024-03-04 19:42:51 +03:00
|
|
|
showMinimap
|
|
|
|
|
? isInMinimapRange
|
|
|
|
|
? "bg-secondary-highlight"
|
|
|
|
|
: isLastSegmentInMinimap
|
|
|
|
|
? ""
|
|
|
|
|
: "opacity-70"
|
|
|
|
|
: ""
|
|
|
|
|
} ${
|
|
|
|
|
isFirstSegmentInMinimap || isLastSegmentInMinimap
|
2024-03-27 00:36:28 +03:00
|
|
|
? "relative h-[8px] border-b-2 border-gray-500"
|
2024-03-04 19:42:51 +03:00
|
|
|
: ""
|
|
|
|
|
}`;
|
|
|
|
|
|
2024-05-01 03:38:22 +03:00
|
|
|
const severityColorsBg: { [key: number]: string } = {
|
2024-03-04 19:42:51 +03:00
|
|
|
1: reviewed
|
2024-05-01 03:38:22 +03:00
|
|
|
? "from-severity_significant_motion-dimmed/10 to-severity_significant_motion/10"
|
|
|
|
|
: "from-severity_significant_motion-dimmed/20 to-severity_significant_motion/20",
|
2024-03-04 19:42:51 +03:00
|
|
|
2: reviewed
|
2024-05-01 03:38:22 +03:00
|
|
|
? "from-severity_detection-dimmed/10 to-severity_detection/10"
|
|
|
|
|
: "from-severity_detection-dimmed/20 to-severity_detection/20",
|
2024-03-04 19:42:51 +03:00
|
|
|
3: reviewed
|
2024-05-01 03:38:22 +03:00
|
|
|
? "from-severity_alert-dimmed/10 to-severity_alert/10"
|
|
|
|
|
: "from-severity_alert-dimmed/20 to-severity_alert/20",
|
2024-03-04 19:42:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const segmentClick = useCallback(() => {
|
2024-03-16 04:26:13 +03:00
|
|
|
if (setHandlebarTime) {
|
|
|
|
|
setHandlebarTime(segmentTime);
|
2024-03-04 19:42:51 +03:00
|
|
|
}
|
2024-03-16 04:26:13 +03:00
|
|
|
}, [segmentTime, setHandlebarTime]);
|
2024-03-04 19:42:51 +03:00
|
|
|
|
|
|
|
|
return (
|
2024-03-23 16:33:50 +03:00
|
|
|
<>
|
2024-03-25 05:37:44 +03:00
|
|
|
{(((firstHalfSegmentWidth > 0 || secondHalfSegmentWidth > 0) &&
|
2024-03-23 16:33:50 +03:00
|
|
|
motionOnly &&
|
|
|
|
|
severity[0] < 2) ||
|
|
|
|
|
!motionOnly) && (
|
|
|
|
|
<div
|
|
|
|
|
key={segmentKey}
|
2025-02-10 00:13:32 +03:00
|
|
|
data-segment-id={segmentTime}
|
2024-05-29 21:05:28 +03:00
|
|
|
className={cn(
|
|
|
|
|
"segment",
|
|
|
|
|
{
|
|
|
|
|
"has-data":
|
|
|
|
|
firstHalfSegmentWidth > 0 || secondHalfSegmentWidth > 0,
|
|
|
|
|
},
|
|
|
|
|
segmentClasses,
|
|
|
|
|
severity[0] && "bg-gradient-to-r",
|
|
|
|
|
severity[0] && severityColorsBg[severity[0]],
|
2025-10-29 17:39:07 +03:00
|
|
|
hasRecording == false && "bg-background",
|
2024-05-29 21:05:28 +03:00
|
|
|
)}
|
2024-03-23 16:33:50 +03:00
|
|
|
onClick={segmentClick}
|
|
|
|
|
onTouchEnd={(event) => handleTouchStart(event, segmentClick)}
|
|
|
|
|
>
|
2025-10-29 17:39:07 +03:00
|
|
|
{isFirstSegmentWithoutRecording && (
|
|
|
|
|
<div className="absolute bottom-[0px] left-0 right-0 h-[1px] bg-primary-variant/40" />
|
|
|
|
|
)}
|
|
|
|
|
{isLastSegmentWithoutRecording && (
|
|
|
|
|
<div className="absolute -top-[1px] left-0 right-0 h-[1px] bg-primary-variant/50" />
|
|
|
|
|
)}
|
2024-03-23 16:33:50 +03:00
|
|
|
{!motionOnly && (
|
|
|
|
|
<>
|
2024-03-24 20:39:28 +03:00
|
|
|
{showMinimap && (
|
|
|
|
|
<MinimapBounds
|
|
|
|
|
isFirstSegmentInMinimap={isFirstSegmentInMinimap}
|
|
|
|
|
isLastSegmentInMinimap={isLastSegmentInMinimap}
|
|
|
|
|
alignedMinimapStartTime={alignedMinimapStartTime}
|
|
|
|
|
alignedMinimapEndTime={alignedMinimapEndTime}
|
|
|
|
|
firstMinimapSegmentRef={firstMinimapSegmentRef}
|
2024-03-28 18:03:06 +03:00
|
|
|
dense={dense}
|
2024-03-24 20:39:28 +03:00
|
|
|
/>
|
|
|
|
|
)}
|
2024-03-04 19:42:51 +03:00
|
|
|
|
2024-03-23 16:33:50 +03:00
|
|
|
<Tick
|
|
|
|
|
key={`${segmentKey}_tick`}
|
|
|
|
|
timestamp={timestamp}
|
|
|
|
|
timestampSpread={timestampSpread}
|
|
|
|
|
/>
|
2024-03-04 19:42:51 +03:00
|
|
|
|
2024-03-23 16:33:50 +03:00
|
|
|
<Timestamp
|
|
|
|
|
key={`${segmentKey}_timestamp`}
|
|
|
|
|
isFirstSegmentInMinimap={isFirstSegmentInMinimap}
|
|
|
|
|
isLastSegmentInMinimap={isLastSegmentInMinimap}
|
|
|
|
|
timestamp={timestamp}
|
|
|
|
|
timestampSpread={timestampSpread}
|
|
|
|
|
segmentKey={segmentKey}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2024-03-04 19:42:51 +03:00
|
|
|
|
2025-10-29 17:39:07 +03:00
|
|
|
{(hasRecording ||
|
|
|
|
|
firstHalfSegmentWidth > 0 ||
|
|
|
|
|
secondHalfSegmentWidth > 0 ||
|
|
|
|
|
alwaysShowMotionLine) && (
|
|
|
|
|
<div className="absolute left-1/2 z-10 h-[8px] w-[20px] -translate-x-1/2 transform cursor-pointer md:w-[40px]">
|
|
|
|
|
<div className="mb-[1px] flex w-[20px] flex-row justify-center pt-[1px] md:w-[40px]">
|
|
|
|
|
<div className="mb-[1px] flex justify-center">
|
|
|
|
|
<div
|
|
|
|
|
key={`${segmentKey}_motion_data_1`}
|
|
|
|
|
data-motion-value={secondHalfSegmentWidth}
|
|
|
|
|
className={cn(
|
|
|
|
|
"h-[2px]",
|
|
|
|
|
"rounded-full",
|
|
|
|
|
secondHalfSegmentWidth
|
|
|
|
|
? "bg-motion_review"
|
|
|
|
|
: "bg-muted-foreground",
|
|
|
|
|
)}
|
|
|
|
|
style={{
|
|
|
|
|
width: secondHalfSegmentWidth || 1,
|
|
|
|
|
}}
|
|
|
|
|
></div>
|
|
|
|
|
</div>
|
2024-03-23 16:33:50 +03:00
|
|
|
</div>
|
2024-03-04 19:42:51 +03:00
|
|
|
|
2025-10-29 17:39:07 +03:00
|
|
|
<div className="flex w-[20px] flex-row justify-center pb-[1px] md:w-[40px]">
|
|
|
|
|
<div className="flex justify-center">
|
|
|
|
|
<div
|
|
|
|
|
key={`${segmentKey}_motion_data_2`}
|
|
|
|
|
data-motion-value={firstHalfSegmentWidth}
|
|
|
|
|
className={cn(
|
|
|
|
|
"h-[2px]",
|
|
|
|
|
"rounded-full",
|
|
|
|
|
firstHalfSegmentWidth
|
|
|
|
|
? "bg-motion_review"
|
|
|
|
|
: "bg-muted-foreground",
|
|
|
|
|
)}
|
|
|
|
|
style={{
|
|
|
|
|
width: firstHalfSegmentWidth || 1,
|
|
|
|
|
}}
|
|
|
|
|
></div>
|
|
|
|
|
</div>
|
2024-03-12 18:23:54 +03:00
|
|
|
</div>
|
2024-03-23 16:33:50 +03:00
|
|
|
</div>
|
2025-10-29 17:39:07 +03:00
|
|
|
)}
|
2024-03-23 16:33:50 +03:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2024-03-04 19:42:51 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MotionSegment;
|