fix(code-editor): clamp diff nav index after chunk recompute

`editorToolbarPanel` keeps `currentIndex` as local state while diff chunks are
recomputed from `getChunks(view.state)` on each `updatePanel()` call.

When the diff shrank (for example after resolving edits), `currentIndex` could
remain greater than `chunkCount - 1`. That caused two user-facing issues:
- The counter could render impossible values like `4/2 changes`.
- Prev/next handlers could read `chunks[currentIndex]` as `undefined`, so
  navigation would fail to scroll to a valid chunk.

Repro in UI:
1. Open a file with multiple diff chunks.
2. Navigate to a high chunk index using Next.
3. Edit content so the number of chunks decreases.
4. Observe stale index in the counter and broken nav behavior.

Fix:
- After recomputing chunks, clamp the index immediately:
  `currentIndex = Math.max(0, Math.min(currentIndex, Math.max(0, chunkCount - 1)))`
- Keep all downstream uses (counter rendering and prev/next logic) based on the
  clamped index.

Result:
- Counter always stays within valid bounds.
- Navigation never references an out-of-range chunk.
- Zero-chunk behavior remains intact (counter and disabled buttons).

File:
- src/components/code-editor/utils/editorToolbarPanel.ts
This commit is contained in:
Haileyesus
2026-02-23 09:48:15 +03:00
parent 4178ccdd5e
commit f1eca2f14d

View File

@@ -65,6 +65,8 @@ export const createEditorToolbarPanelExtension = ({
const chunksData = hasDiff ? getChunks(view.state) : null; const chunksData = hasDiff ? getChunks(view.state) : null;
const chunks = chunksData?.chunks || []; const chunks = chunksData?.chunks || [];
const chunkCount = chunks.length; const chunkCount = chunks.length;
const maxChunkIndex = Math.max(0, chunkCount - 1);
currentIndex = Math.max(0, Math.min(currentIndex, maxChunkIndex));
let toolbarHtml = '<div style="display: flex; align-items: center; justify-content: space-between; width: 100%;">'; let toolbarHtml = '<div style="display: flex; align-items: center; justify-content: space-between; width: 100%;">';
toolbarHtml += '<div style="display: flex; align-items: center; gap: 8px;">'; toolbarHtml += '<div style="display: flex; align-items: center; gap: 8px;">';