From bafae077e3b5bd65e244dd48d8b2eb50119b8976 Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Tue, 12 May 2026 19:30:21 +1000 Subject: [PATCH 1/2] fix(staged): select first file from sidebar order instead of raw git order When a diff opens, the initial file selection used `state.files[0]` from the raw git response (sorted by full path), which could differ from the first file shown in the sidebar (sorted per-directory with dirs first). Add a reactive effect in DiffModal that overrides the initial selection with `orderedFiles[0]` once files finish loading, keeping the viewer and sidebar in sync. The flag resets on context switches so it re-fires for new file sets. Co-Authored-By: Claude Opus 4.6 (1M context) Signed-off-by: Matt Toohey --- apps/staged/src/lib/features/diff/DiffModal.svelte | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/apps/staged/src/lib/features/diff/DiffModal.svelte b/apps/staged/src/lib/features/diff/DiffModal.svelte index f0f4d34f..213e6a61 100644 --- a/apps/staged/src/lib/features/diff/DiffModal.svelte +++ b/apps/staged/src/lib/features/diff/DiffModal.svelte @@ -214,6 +214,9 @@ // Reset review state — the $effect will recreate it once the new commitSha resolves reviewHandle = null; + // Reset so the sidebar-order effect picks the first file after reload + initialSelectionApplied = false; + // Switch diff context (reloads file list) try { await diffViewer.switchContext(newScope, newCommitSha); @@ -740,6 +743,16 @@ : [...flattenTreeFiles(needsReviewTree), ...flattenTreeFiles(reviewedTree)] ); + // Once files finish loading, override the initial selection with the first + // file in sidebar order so the viewer and sidebar stay in sync. + let initialSelectionApplied = false; + $effect(() => { + if (!diffViewer.state.loading && orderedFiles.length > 0 && !initialSelectionApplied) { + initialSelectionApplied = true; + diffViewer.selectFile(orderedFiles[0].path); + } + }); + // ========================================================================== // Sidebar interactions // ========================================================================== From 2bd9c1b4115b505b3b35724637c687bcc49a6f60 Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Wed, 13 May 2026 12:09:42 +1000 Subject: [PATCH 2/2] fix(staged): wait for review state before selecting initial diff file When opening an existing review, the initial file selection effect fired as soon as diff files loaded, before reviewedPaths finished loading from the backend. This meant orderedFiles was computed with all files in the needs-review bucket, so the selected file could differ from the actual sidebar order once reviewed paths arrived. Add a guard that also waits for reviewHandle.state.loading to be false before applying the initial selection, ensuring the needs-review vs reviewed split is accurate when picking the first file. Co-Authored-By: Claude Opus 4.6 (1M context) Signed-off-by: Matt Toohey --- apps/staged/src/lib/features/diff/DiffModal.svelte | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/staged/src/lib/features/diff/DiffModal.svelte b/apps/staged/src/lib/features/diff/DiffModal.svelte index 213e6a61..e97ec697 100644 --- a/apps/staged/src/lib/features/diff/DiffModal.svelte +++ b/apps/staged/src/lib/features/diff/DiffModal.svelte @@ -745,9 +745,16 @@ // Once files finish loading, override the initial selection with the first // file in sidebar order so the viewer and sidebar stay in sync. + // For non-readonly reviews, also wait for reviewedPaths to load so the + // needs-review / reviewed split is accurate before picking the first file. let initialSelectionApplied = false; $effect(() => { - if (!diffViewer.state.loading && orderedFiles.length > 0 && !initialSelectionApplied) { + if ( + !diffViewer.state.loading && + orderedFiles.length > 0 && + !initialSelectionApplied && + (readonly || !reviewHandle || !reviewHandle.state.loading) + ) { initialSelectionApplied = true; diffViewer.selectFile(orderedFiles[0].path); }