From cac5bccbe75d24c943cba3d89adef868b31e0ad3 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Mon, 11 Mar 2024 07:56:36 -0500 Subject: [PATCH] Fix segments on motion review (#10370) * fix segments on motion review * remove unneeded data attribute --- web/src/components/timeline/MotionSegment.tsx | 31 ++++++++++++++++--- web/src/hooks/use-motion-segment-utils.ts | 14 ++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/web/src/components/timeline/MotionSegment.tsx b/web/src/components/timeline/MotionSegment.tsx index 2efdd73d0..c66bbfc0b 100644 --- a/web/src/components/timeline/MotionSegment.tsx +++ b/web/src/components/timeline/MotionSegment.tsx @@ -82,7 +82,19 @@ export function MotionSegment({ return isMobile ? 30 : 50; }, []); - const segmentWidth = useMemo(() => { + const firstHalfSegmentWidth = useMemo(() => { + return interpolateMotionAudioData( + getMotionSegmentValue(segmentTime), + maxSegmentWidth, + ); + }, [ + segmentTime, + maxSegmentWidth, + getMotionSegmentValue, + interpolateMotionAudioData, + ]); + + const secondHalfSegmentWidth = useMemo(() => { return interpolateMotionAudioData( getMotionSegmentValue(segmentTime + segmentDuration / 2), maxSegmentWidth, @@ -167,10 +179,19 @@ export function MotionSegment({ }; const segmentClick = useCallback(() => { - if (startTimestamp && setHandlebarTime && segmentWidth > 1) { + if ( + startTimestamp && + setHandlebarTime && + (firstHalfSegmentWidth > 1 || secondHalfSegmentWidth > 1) + ) { setHandlebarTime(startTimestamp); } - }, [startTimestamp, setHandlebarTime, segmentWidth]); + }, [ + startTimestamp, + setHandlebarTime, + firstHalfSegmentWidth, + secondHalfSegmentWidth, + ]); return (
@@ -216,7 +237,7 @@ export function MotionSegment({ key={`${segmentKey}_motion_data_2`} className={`h-[2px] rounded-full bg-motion_review`} style={{ - width: segmentWidth, + width: firstHalfSegmentWidth, }} > diff --git a/web/src/hooks/use-motion-segment-utils.ts b/web/src/hooks/use-motion-segment-utils.ts index 4cf0c15e1..dfec48358 100644 --- a/web/src/hooks/use-motion-segment-utils.ts +++ b/web/src/hooks/use-motion-segment-utils.ts @@ -40,14 +40,20 @@ export const useMotionSegmentUtils = ( const getMotionSegmentValue = useCallback( (time: number): number => { - const matchingEvent = motion_events.find((event) => { + const segmentStart = getSegmentStart(time); + const segmentEnd = getSegmentEnd(time); + const matchingEvents = motion_events.filter((event) => { return ( - time >= getSegmentStart(event.start_time) && - time <= getSegmentEnd(event.start_time) + event.start_time >= segmentStart && event.start_time < segmentEnd ); }); - return matchingEvent?.motion ?? 0; + const totalMotion = matchingEvents.reduce( + (acc, curr) => acc + (curr.motion ?? 0), + 0, + ); + + return totalMotion; }, [motion_events, getSegmentStart, getSegmentEnd], );