I'll read all four files in parallel to thoroughly understand the drag-and-swap system.
Assistant
Now let me also look at useTimelineOrchestrator to understand how currentPositions is computed and passed back — specifically how dynamicPositions (which is calculateDragPreview) feeds into currentPositions.
Assistant
timeline-utils.test.ts
TRAILING_ENDPOINT_KEY and PENDING_POSITION_KEY > has expected key values
quantizePositions (5 tests): keeps first item at 0, quantizes gaps to 4N+1, preserves trailing endpoint, handles empty positions (2 variants)
applyFluidTimeline (3 tests): moves dragged item, limits to 50 frames per drag, first item always at 0
pixelToFrame (3 tests): converts pixels to frames, handles non-zero fullMin, rounds to nearest integer
getTimelineDimensions (5 tests): empty positions, calculates from positions, includes pending frames, ignores null pending, enforces minimum max of 30
getPairInfo (3 tests): single item, adjacent items, excludes trailing endpoint
calculateNewVideoPlacement (4 tests): first video at start, after existing, clips when no space, multiple existing videos
findTrailingVideoInfo (6 tests): empty outputs, null lastImageShotGenId, finds by pair_shot_generation_id, finds from params (legacy), ignores non-video types, ignores videos without location
Total: 39 tests passed, 0 failed across all 4 files.
I need to deeply understand a drag-and-swap bug in a timeline component. There are two symptoms:
When dragging item from position 1 (leftmost, frame 0) to position 2, the OTHER item (originally at position 2) does NOT visually move to position 0 during the drag — it only snaps there on drop.
The dragged item's visual thumbnail appears far to the RIGHT of where the "drop zone" indicator (the pair region / gap indicator) shows.
Please thoroughly explore and explain the following:
File 1: /Users/user_c042661f/Documents/reigh/src/tools/travel-between-images/components/Timeline/hooks/useTimelineDrag.ts
Read the whole file. Focus on:
How calculateDragPositions computes positions for ALL items (not just the dragged one) during preview
How applyFluidTimeline is called and what it returns
How currentDragFrame is computed (line ~429)
How dragOffset is computed (line ~425)
The dynamicPositions function (what it returns for non-dragged items)
File 2: /Users/user_c042661f/Documents/reigh/src/tools/travel-between-images/components/Timeline/utils/timeline-utils.ts
Read the whole file. Focus on:
applyFluidTimeline — does it actually move OTHER items during preview? Or does it only move the dragged item?
shrinkOversizedGaps — what does it do to item positions? Does it enforce frame-0 for the leftmost item?
What does skipQuantization=true (preview mode) actually change?
File 3: /Users/user_c042661f/Documents/reigh/src/tools/travel-between-images/components/Timeline/TimelineItem.tsx
Read lines 235-260. Focus on:
How the visual pixel position (finalX) is calculated
The dragOffset application: what is originalFramePos, what is originalPixel, how does it compute desiredPixel?
Could originalFramePos ever be wrong, causing the item to render far from where it should be?
File 4: /Users/user_c042661f/Documents/reigh/src/tools/travel-between-images/components/Timeline/TimelineContainer/TimelineContainer.tsx
Look at lines 726-768 (the TimelineItem rendering). Check:
What value is passed as framePosition (from currentPositions.get(imageKey) or image.timeline_frame)
What value is passed as originalFramePos (from framePositions.get(imageKey))
What value is passed as currentDragFrame
The core question I'm trying to answer:
Why doesn't the swap partner move during drag?applyFluidTimeline is called in calculateDragPositions. Does it move other items in preview mode? Look at shrinkOversizedGaps with skipQuantization=true — does the first item ALWAYS get forced to position 0? If so, and if the dragged item (originally at frame 0) moves right, does shrinkOversizedGaps push the other item LEFT to frame 0? Or does it not?
Why is the dragged item far to the right of the drop zone? The dragOffset is { x: currentX - startX }. The finalX calculation in TimelineItem uses originalFramePos to compute originalPixel. What is originalFramePos? It's framePositions.get(imageKey) ?? 0 from TimelineContainer. For the item at frame 0, originalFramePos=0, so originalPixel = TIMELINE_PADDING_OFFSET. Then finalX = originalPixel + dragOffset.x = TIMELINE_PADDING_OFFSET + (currentX - startX). This should be cursor-aligned. But what does framePosition (from currentPositions) show meanwhile? The pair region / drop zone uses positions from currentPositions... so if there's a discrepancy between where the item renders and where positions say it is, that's the bug.
Please trace this all the way through and identify the structural issues.
Now I have a complete picture. Let me trace every data flow path precisely before writing the analysis.
Here is the full, precise analysis of both bugs.
Bug Analysis: Drag-and-Swap in the Timeline
Architecture recap (data flow)
code
framePositions (committed Map)
→ useTimelineDrag.calculateDragPreview()
→ calls calculateDragPositions(currentX, isPreview=true)
→ builds newPositions (sets draggedId to targetFrame)
→ calls applyFluidTimeline(newPositions, draggedId, targetFrame, ..., skipQuantization=true)
→ shrinkOversizedGaps(result, excludeId=undefined, skipQuantization=true)
→ returns a new Map for ALL items
→ returns currentPositions (via dynamicPositions() call in orchestrator)
currentPositions → fed to:
1. TimelineContainer pair region rendering
2. TimelineItem via framePosition = currentPositions.get(imageKey)
framePositions (committed, unchanged during drag) → fed to:
1. TimelineItem as originalFramePos = framePositions.get(imageKey) ?? 0
Bug 1: Swap partner does NOT move during drag
The question to answer
When item A (originally at frame 0) is dragged right to frame N, does shrinkOversizedGaps push item B (originally at frame N) left to frame 0 during the preview?
What shrinkOversizedGaps actually does
shrinkOversizedGaps in /Users/user_c042661f/Documents/reigh/src/tools/travel-between-images/components/Timeline/utils/timeline-utils.ts lines 69–122:
typescript
const shrinkOversizedGaps = (
positions: Map<string, number>,
excludeId?: string,
skipQuantization: boolean = false,
): Map<string, number> => {
// ...
const entries = [...positions.entries()]
.filter(([id]) => id !== excludeId && id !== TRAILING_ENDPOINT_KEY);
// ...
entries.sort((a, b) => a[1] - b[1]);
const result = new Map<string, number>();
for (let i = 0; i < entries.length; i++) {
const [id, originalPos] = entries[i];
if (i === 0) {
result.set(id, 0); // ← ALWAYS forces first item to 0
} else {
// uses gap from *originalPos in the input* to compute spacing
}
}
// Re-add excluded id unchanged
if (excludeId && positions.has(excludeId)) {
result.set(excludeId, positions.get(excludeId)!);
}
The critical insight:shrinkOversizedGaps sorts all non-excluded items by their input position, then forces the item with the lowest input position to frame 0. It does NOT use excludeId here — excludeId is only for re-adding a specifically excluded item unchanged.
Now in applyFluidTimeline (lines 127–149):
typescript
export const applyFluidTimeline = (
positions: Map<string, number>,
draggedId: string,
targetFrame: number,
excludeId?: string,
...
): Map<string, number> => {
// ...
const result = new Map(positions);
result.set(draggedId, limitedTargetFrame); // ← draggedId is set to target
return shrinkOversizedGaps(result, excludeId, skipQuantization);
};
And in calculateDragPositions (single item path, lines 193–221):
typescript
const newPositions = new Map(framePositions);
// ... (sets dragState.activeId to finalPosition)
return applyFluidTimeline(newPositions, dragState.activeId, finalPosition, undefined, fullMin, fullMax, isPreview);
excludeId is passed as undefined. This means shrinkOversizedGaps does NOT exclude the dragged item. Every item including the dragged item participates in the "force first to 0" sort.
The scenario: dragging from position 0 to position N
Step 4: Forces first item (A) to position 0. Then places B at gap from A-to-B in original: gap = 50-30 = 20, so B = 0 + 20 = 20.
Result: {A: 0, B: 20}.
A snaps back to 0. B moves slightly left but NOT to a "swapped" position. B does NOT jump to the gap left by A moving right. The swap illusion never happens during preview.
This is the root cause of bug 1. The dragged item A is not excluded from shrinkOversizedGaps, so as soon as A moves right, shrinkOversizedGaps sees A as no longer the minimum-frame item... actually wait: A went from 0 to 30. B is at 50. A (30) is still less than B (50), so A is still sorted first and still gets forced to 0. shrinkOversizedGaps immediately undoes A's rightward motion by forcing it back to 0.
The only way A would ever NOT be the first item is if finalPosition > B's frame, meaning A has been dragged past B. Until that crossover point, A always sorts first and gets forced back to 0 — so A never visually moves during currentPositions until it crosses B.
During the drag preview, item A stays at frame 0 in currentPositions, and item B barely moves. Since framePosition prop passed to TimelineItem comes from currentPositions.get(imageKey), the pair region / drop zone indicator also stays at frame 0 on the left side, not following the cursor at all.
Bug 2: Dragged item renders far to the right of the drop zone indicator
The dual-position system
TimelineContainer (lines 726–766) passes TWO different position values to each TimelineItem:
This is indeed cursor-aligned as long as the user grabbed the item at its center (which is TIMELINE_PADDING_OFFSET pixels from the left edge, assuming frame 0 is at the left padding boundary).
The discrepancy between rendered position and drop zone
Here is the split:
Source
Meaning
Dragged item's visual position (finalX)
originalFramePos (committed) + dragOffset.x
Cursor-aligned, ignores currentPositions
Pair region / drop zone indicator
currentPositions (from applyFluidTimeline)
Frame-quantized, subject to shrinkOversizedGaps
currentDragFrame (passed to TimelineItem)
calculateTargetFrame(currentX, containerRect)
Raw pixel-to-frame mapping
The pair region is drawn based on imagePositionsWithPending which comes from imagePositions which comes from useTrailingEndpoint which uses currentPositions. Those are the fluid/clipped positions from applyFluidTimeline.
As established in Bug 1: when A is at frame 0 and dragged rightward, currentPositions.get(A) = 0 (because shrinkOversizedGaps forces it back). So the pair region indicator remains at frame 0 on screen.
But the dragged item's visualfinalX = TIMELINE_PADDING_OFFSET + dragOffset.x, which follows the cursor.
Result: the dragged item's thumbnail follows the cursor, while the pair region / drop zone indicator stays at frame 0. They diverge by the full drag distance. The further the user drags, the larger the gap between where the item renders and where the drop zone shows it will land.
Why currentDragFrame does not fix this
currentDragFrame in useTimelineDrag.ts (lines 426–428):
This is the raw cursor-to-frame conversion, computed independently of applyFluidTimeline. It is passed to TimelineItem as a display value for the frame timestamp label only. It does not affect finalX.
Summary: The Two Structural Issues
Issue 1: shrinkOversizedGaps does not exclude the dragged item
applyFluidTimeline is called with excludeId = undefined in the single-item drag path (calculateDragPositions, line 221). This means shrinkOversizedGaps includes the dragged item in its sort-and-normalize pass. Since item A (at frame 0) sorts first even after being set to finalPosition, it gets forced back to 0 by the "first item always at 0" rule.
The dragged item should be passed as excludeId so that shrinkOversizedGaps operates only on the non-dragged items. The dragged item gets re-added at the end (line 112–114 of timeline-utils.ts shows it is re-added unchanged when excludeId is set). This would allow item B to shift left into frame 0 once A vacates it, producing the live swap preview.
The fix site is in calculateDragPositions (useTimelineDrag.ts line 221):
Issue 2: The pair region uses currentPositions, but currentPositions for item A stays at 0
This is a direct consequence of Issue 1. Because shrinkOversizedGaps forces A back to 0, currentPositions.get(A) = 0 during the entire drag (until A crosses B). The pair region and gap indicator are drawn from currentPositions, so they stay at the pre-drag position. Meanwhile, the dragged thumbnail uses the independent originalFramePos + dragOffset.x path to follow the cursor.
The gap between the drop zone indicator and the thumbnail is the distance the user has dragged. Fixing Issue 1 (passing dragState.activeId as excludeId) would also fix the pair region position, because currentPositions for A would now correctly reflect the dragged frame rather than being forced to 0. The pair region starting from A's actual dragged position would advance in sync with the thumbnail.