From 49530dc2e4093a047b7d190b0e9c262598fc9176 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Fri, 1 Mar 2024 07:19:26 -0700 Subject: [PATCH] Handle case where review spans across two hours (#10169) --- .../player/PreviewThumbnailPlayer.tsx | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/web/src/components/player/PreviewThumbnailPlayer.tsx b/web/src/components/player/PreviewThumbnailPlayer.tsx index c7fb8fbed..5e7c01dda 100644 --- a/web/src/components/player/PreviewThumbnailPlayer.tsx +++ b/web/src/components/player/PreviewThumbnailPlayer.tsx @@ -87,16 +87,48 @@ export default function PreviewThumbnailPlayer({ // playback - const relevantPreview = useMemo( - () => - Object.values(allPreviews || []).find( - (preview) => - preview.camera == review.camera && - preview.start < review.start_time && - preview.end > review.end_time, - ), - [allPreviews], - ); + const relevantPreview = useMemo(() => { + if (!allPreviews) { + return undefined; + } + + let multiHour = false; + const firstIndex = Object.values(allPreviews).findIndex((preview) => { + if (preview.camera != review.camera || preview.end < review.start_time) { + return false; + } + + if (review.end_time > preview.end) { + multiHour = true; + } + + return true; + }); + + if (firstIndex == -1) { + return undefined; + } + + if (!multiHour) { + return allPreviews[firstIndex]; + } + + const firstPrev = allPreviews[firstIndex]; + const firstDuration = firstPrev.end - review.start_time; + const secondDuration = review.end_time - firstPrev.end; + + if (firstDuration > secondDuration) { + // the first preview is longer than the second, return the first + return firstPrev; + } else { + // the second preview is longer, return the second if it exists + if (firstIndex < allPreviews.length - 1) { + return allPreviews[firstIndex + 1]; + } + + return undefined; + } + }, [allPreviews]); const playingBack = useMemo(() => playback, [playback]);