diff --git a/web/src/utils/timelineUtil.tsx b/web/src/utils/timelineUtil.tsx index 502121471..466dc7dd9 100644 --- a/web/src/utils/timelineUtil.tsx +++ b/web/src/utils/timelineUtil.tsx @@ -38,6 +38,22 @@ export function getChunkedTimeDay(timeRange: TimeRange): TimeRange[] { return data; } +/** + * Find the chunk index that contains the given timestamp. + * Uses half-open intervals [after, before) for all chunks except the last, + * which uses a closed interval [after, before] so the terminal boundary + * is always reachable. + */ +export function findChunkIndex(chunks: TimeRange[], timestamp: number): number { + return chunks.findIndex((chunk, i) => { + const isLast = i === chunks.length - 1; + return ( + chunk.after <= timestamp && + (isLast ? chunk.before >= timestamp : chunk.before > timestamp) + ); + }); +} + export function getChunkedTimeRange( startTimestamp: number, endTimestamp: number, diff --git a/web/src/views/recording/RecordingView.tsx b/web/src/views/recording/RecordingView.tsx index 0445a477b..7f382dee1 100644 --- a/web/src/views/recording/RecordingView.tsx +++ b/web/src/views/recording/RecordingView.tsx @@ -26,7 +26,7 @@ import { ReviewSummary, ZoomLevel, } from "@/types/review"; -import { getChunkedTimeDay } from "@/utils/timelineUtil"; +import { findChunkIndex, getChunkedTimeDay } from "@/utils/timelineUtil"; import { MutableRefObject, useCallback, @@ -169,9 +169,7 @@ export function RecordingView({ [timeRange], ); const [selectedRangeIdx, setSelectedRangeIdx] = useState( - chunkedTimeRange.findIndex((chunk) => { - return chunk.after <= startTime && chunk.before > startTime; - }), + findChunkIndex(chunkedTimeRange, startTime), ); const currentTimeRange = useMemo( () => @@ -274,9 +272,7 @@ export function RecordingView({ const updateSelectedSegment = useCallback( (currentTime: number, updateStartTime: boolean) => { - const index = chunkedTimeRange.findIndex( - (seg) => seg.after <= currentTime && seg.before > currentTime, - ); + const index = findChunkIndex(chunkedTimeRange, currentTime); if (index != -1) { if (updateStartTime) {