Skip to content

Commit 7128cfb

Browse files
fix(mobile): review keeps loaded turn diffs offline and drops stale selections
- Cache each answered single-turn diff per thread so a disconnected review sheet still shows turns it loaded, like git sections. - Scope comment selections to the section's diff, so a section that comes back with different lines starts without a selection. - Diff preview failures keep the last revision instead of refreshing every file patch. - Restore the native highlighting hook to main; its two effects stay. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9f3badc commit 7128cfb

12 files changed

Lines changed: 400 additions & 266 deletions

‎apps/mobile/src/features/review/reviewCommentSelection.test.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,12 @@ describe("review comment serialization", () => {
153153
});
154154

155155
describe("review comment selection store", () => {
156-
const scope = reviewCommentSelectionScope(1, "env:thread", "git:branch-range")!;
157-
const otherScope = reviewCommentSelectionScope(1, "env:thread", "turn:2")!;
156+
const changes = { id: "git:branch-range", content: "hash-1" };
157+
const scope = reviewCommentSelectionScope(1, "env:thread", changes)!;
158+
const otherScope = reviewCommentSelectionScope(1, "env:thread", {
159+
id: "turn:2",
160+
content: "diff",
161+
})!;
158162
const target = makeTarget();
159163
const pending = {
160164
sectionId: target.sectionId,
@@ -199,13 +203,28 @@ describe("review comment selection store", () => {
199203

200204
it("reads as empty in a reopened review sheet", () => {
201205
setReviewCommentTarget(scope, target);
202-
const reopenedScope = reviewCommentSelectionScope(2, "env:thread", "git:branch-range");
206+
const reopenedScope = reviewCommentSelectionScope(2, "env:thread", changes);
203207

204208
expect(
205209
selectReviewCommentSelection(getReviewCommentSelection(), reopenedScope).target,
206210
).toBeNull();
207211
});
208212

213+
it("reads as empty when the section disappears and comes back over a different diff", () => {
214+
setReviewCommentTarget(scope, target);
215+
// The working tree went clean, so the default showed; the section later returns.
216+
const returned = reviewCommentSelectionScope(1, "env:thread", {
217+
...changes,
218+
content: "hash-2",
219+
});
220+
const unchanged = reviewCommentSelectionScope(1, "env:thread", { ...changes });
221+
222+
expect(selectReviewCommentSelection(getReviewCommentSelection(), returned).target).toBeNull();
223+
expect(selectReviewCommentSelection(getReviewCommentSelection(), unchanged).target).toBe(
224+
target,
225+
);
226+
});
227+
209228
it("drops the selection when the section changes, so switching back starts empty", () => {
210229
setReviewCommentTarget(scope, target);
211230
setReviewSelectedSectionId("env:thread", "turn:2");

‎apps/mobile/src/features/review/reviewCommentSelection.ts‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ export interface PendingReviewCommentRange extends Pick<
4545
readonly rowId: string;
4646
}
4747

48+
/** The sheet, thread and section a selection was made in, and the diff it was made over. */
49+
export interface ReviewCommentSelectionScope {
50+
readonly key: string;
51+
/** Compared by `===`: a git section's diff hash, or a turn's diff as answered. */
52+
readonly content: string | null;
53+
}
54+
4855
export interface ReviewCommentSelection {
49-
/** Identifies the sheet, thread and section the selection was made in. */
50-
readonly scope: string | null;
56+
readonly scope: ReviewCommentSelectionScope | null;
5157
readonly target: ReviewCommentTarget | null;
5258
readonly pending: PendingReviewCommentRange | null;
5359
}
@@ -86,46 +92,64 @@ export function nextReviewSheetId(): number {
8692
export function reviewCommentSelectionScope(
8793
sheetId: number,
8894
threadKey: string | null,
89-
sectionId: string | null,
90-
): string | null {
91-
return threadKey === null || sectionId === null
95+
section: { readonly id: string; readonly content: string | null } | null,
96+
): ReviewCommentSelectionScope | null {
97+
return threadKey === null || section === null
9298
? null
93-
: JSON.stringify([sheetId, threadKey, sectionId]);
99+
: { key: JSON.stringify([sheetId, threadKey, section.id]), content: section.content };
100+
}
101+
102+
function isSameScope(
103+
a: ReviewCommentSelectionScope | null,
104+
b: ReviewCommentSelectionScope | null,
105+
): boolean {
106+
return a !== null && b !== null && a.key === b.key && a.content === b.content;
94107
}
95108

96-
export function setReviewCommentTarget(scope: string, target: ReviewCommentTarget) {
109+
export function setReviewCommentTarget(
110+
scope: ReviewCommentSelectionScope,
111+
target: ReviewCommentTarget,
112+
) {
97113
setSelection({ scope, target, pending: null });
98114
}
99115

100116
/**
101117
* Keeps the pending range: when both ends are the same line the target is a single line, and
102118
* the range still waits for a different end.
103119
*/
104-
export function completeReviewCommentRange(scope: string, target: ReviewCommentTarget) {
120+
export function completeReviewCommentRange(
121+
scope: ReviewCommentSelectionScope,
122+
target: ReviewCommentTarget,
123+
) {
105124
setSelection({
106125
scope,
107126
target,
108-
pending: selection.scope === scope ? selection.pending : null,
127+
pending: isSameScope(selection.scope, scope) ? selection.pending : null,
109128
});
110129
}
111130

112-
export function startReviewCommentRange(scope: string, pending: PendingReviewCommentRange) {
131+
export function startReviewCommentRange(
132+
scope: ReviewCommentSelectionScope,
133+
pending: PendingReviewCommentRange,
134+
) {
113135
setSelection({ scope, target: null, pending });
114136
}
115137

116138
export function clearReviewCommentTarget() {
117139
setSelection(EMPTY_SELECTION);
118140
}
119141

120-
/** A selection made in another sheet, thread or section reads as empty. */
142+
/** A selection made in another sheet, thread or section, or over another diff, reads as empty. */
121143
export function selectReviewCommentSelection(
122144
current: ReviewCommentSelection,
123-
scope: string | null,
145+
scope: ReviewCommentSelectionScope | null,
124146
): ReviewCommentSelection {
125-
return scope !== null && current.scope === scope ? current : EMPTY_SELECTION;
147+
return isSameScope(current.scope, scope) ? current : EMPTY_SELECTION;
126148
}
127149

128-
export function useReviewCommentSelection(scope: string | null): ReviewCommentSelection {
150+
export function useReviewCommentSelection(
151+
scope: ReviewCommentSelectionScope | null,
152+
): ReviewCommentSelection {
129153
return selectReviewCommentSelection(
130154
useSyncExternalStore(
131155
subscribeReviewCommentSelection,

‎apps/mobile/src/features/review/reviewDiffHighlightScheduler.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export function createReviewDiffHighlightScheduler(
4545
timer = setTimeout(flush, 150);
4646
}
4747
},
48+
reset() {
49+
cancel();
50+
const previousRange = visibleRange;
51+
requestedRange = undefined;
52+
visibleRange = requestedRange;
53+
return previousRange;
54+
},
4855
cancel,
4956
};
5057
}

‎apps/mobile/src/features/review/reviewModel.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,18 @@ export function getReadyReviewCheckpoints(
499499
);
500500
}
501501

502+
export function reviewTurnDiffsBySectionId(
503+
checkpoints: ReadonlyArray<Pick<ThreadCheckpointSummary, "checkpointTurnCount">>,
504+
diffByTurnCount: Readonly<Record<number, string | undefined>>,
505+
): Readonly<Record<string, string | undefined>> {
506+
return Object.fromEntries(
507+
checkpoints.map((checkpoint) => [
508+
getReviewSectionIdForCheckpoint(checkpoint),
509+
diffByTurnCount[checkpoint.checkpointTurnCount],
510+
]),
511+
);
512+
}
513+
502514
export function buildReviewSectionItems(input: {
503515
readonly checkpoints: ReadonlyArray<ThreadCheckpointSummary>;
504516
readonly gitSections: ReadonlyArray<ReviewDiffPreviewSource>;

0 commit comments

Comments
 (0)