use helper for chunked time range

This commit is contained in:
Josh Hawkins 2026-05-01 10:23:14 -05:00
parent 9c1fb2e5e9
commit 7aeca95434
2 changed files with 19 additions and 7 deletions

View File

@ -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,

View File

@ -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<TimeRange>(
() =>
@ -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) {