Skip to content

Commit 49bab99

Browse files
whoisaldojuliusmarmingeclaude
authored
fix(web): Changes row matches the diff comparison (#19553)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 4382138 commit 49bab99

7 files changed

Lines changed: 115 additions & 4 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3836,6 +3836,7 @@ export default function ChatView(props: ChatViewProps) {
38363836
)
38373837
}
38383838
gitCwd={gitCwd}
3839+
workspaceMutationId={workspaceMutationId}
38393840
isGitRepo={isGitRepo}
38403841
envLocked={envLocked}
38413842
availableEnvironments={logicalProjectEnvironments}

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ export default function DiffPanel({
232232
}));
233233
const diffLayout = settings.diffLayout;
234234
const [wordWrap, setWordWrap] = useState(settings.wordWrap);
235-
const [diffIgnoreWhitespace, setDiffIgnoreWhitespace] = useState(settings.diffIgnoreWhitespace);
235+
const diffIgnoreWhitespace =
236+
useDiffPanelSessionStore((state) => state.ignoreWhitespace) ?? settings.diffIgnoreWhitespace;
236237
const [fileTreeOpen, setFileTreeOpen] = useLocalStorage(
237238
DIFF_FILE_TREE_STORAGE_KEY,
238239
false,
@@ -1222,7 +1223,7 @@ export default function DiffPanel({
12221223
size="sm"
12231224
pressed={diffIgnoreWhitespace}
12241225
onPressedChange={(pressed) => {
1225-
setDiffIgnoreWhitespace(Boolean(pressed));
1226+
useDiffPanelSessionStore.getState().setIgnoreWhitespace(Boolean(pressed));
12261227
}}
12271228
/>
12281229
}

‎apps/web/src/components/GitActionsControl.logic.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,37 @@ describe("resolveChangesTotals", () => {
307307
);
308308
});
309309

310+
it("uses the panel's branch comparison when it has file stats", () => {
311+
const dirty = status({
312+
branchChanges: { baseRef: "main", insertions: 20, deletions: 3 },
313+
});
314+
const source = {
315+
id: "branch-range",
316+
kind: "branch-range",
317+
title: "Changes vs release",
318+
baseRef: "release",
319+
headRef: "feature/test",
320+
diff: "",
321+
diffHash: "hash",
322+
truncated: false,
323+
} as const;
324+
assert.deepEqual(
325+
resolveChangesTotals(dirty, {
326+
...source,
327+
files: [
328+
{ path: "a.ts", previousPath: null, additions: 4, deletions: 1 },
329+
{ path: "b.ts", previousPath: null, additions: 2, deletions: 0 },
330+
],
331+
}),
332+
{ insertions: 6, deletions: 1, baseRef: "release" },
333+
);
334+
assert.deepEqual(resolveChangesTotals(dirty, source), {
335+
insertions: 20,
336+
deletions: 3,
337+
baseRef: "main",
338+
});
339+
});
340+
310341
it("falls back to the working tree when branchChanges is omitted", () => {
311342
assert.deepInclude(
312343
resolveChangesTotals(

‎apps/web/src/components/GitActionsControl.logic.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
GitRunStackedActionResult,
33
GitStackedAction,
4+
ReviewDiffPreviewSource,
45
VcsStatusResult,
56
} from "@t3tools/contracts";
67
import { isTemporaryWorktreeBranch } from "@t3tools/shared/git";
@@ -448,9 +449,21 @@ export type ChangesLineTotals = {
448449

449450
// `branchChanges ?? workingTree` treats `{0,0}` as present, so a zero branch total hid a dirty
450451
// working tree and the Changes row read +0 while Diff still showed the work.
452+
// The Diff panel's branch comparison wins when it has file stats, so a remembered base or
453+
// hidden whitespace reads the same in the row as in the panel.
451454
export function resolveChangesTotals(
452455
status: VcsStatusResult | null | undefined,
456+
branchPreview?: ReviewDiffPreviewSource | null,
453457
): ChangesLineTotals | null {
458+
if (branchPreview?.files) {
459+
let insertions = 0;
460+
let deletions = 0;
461+
for (const file of branchPreview.files) {
462+
insertions += file.additions;
463+
deletions += file.deletions;
464+
}
465+
return { insertions, deletions, baseRef: branchPreview.baseRef };
466+
}
454467
if (!status) return null;
455468
const branch = status.branchChanges;
456469
const tree = { ...status.workingTree, baseRef: null };

‎apps/web/src/components/GitActionsControl.tsx‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ import {
9494
import { useQuietThreadProjection, useQuietThreadShell } from "~/state/entities";
9595
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
9696
import { useEnvironmentQuery } from "~/state/query";
97+
import { useFilesystemReadAccess } from "~/state/filesystem";
98+
import { reviewEnvironment } from "~/state/review";
99+
import { revisionedQuery } from "~/state/revisionedQuery";
100+
import {
101+
selectThreadBranchBaseRef,
102+
useDiffPanelSessionStore,
103+
useDiffPanelStore,
104+
} from "~/diffPanelStore";
105+
import { useClientSettings } from "~/hooks/useSettings";
97106
import { serverEnvironment } from "~/state/server";
98107
import { sourceControlEnvironment } from "~/state/sourceControl";
99108
import { threadEnvironment } from "~/state/threads";
@@ -122,6 +131,7 @@ interface GitActionsControlProps {
122131
showActions?: boolean;
123132
showChanges?: boolean;
124133
onOpenChanges?: () => void;
134+
workspaceMutationId?: string | null;
125135
}
126136

127137
interface PendingDefaultBranchAction {
@@ -933,6 +943,7 @@ export default function GitActionsControl({
933943
showActions = true,
934944
showChanges = true,
935945
onOpenChanges,
946+
workspaceMutationId = null,
936947
}: GitActionsControlProps) {
937948
const panelAnchorRef = useRef<HTMLDivElement | null>(null);
938949
const updateThreadMetadata = useAtomCommand(
@@ -1050,7 +1061,53 @@ export default function GitActionsControl({
10501061
const isRepo = gitStatus?.isRepo ?? true;
10511062
const hasPrimaryRemote = gitStatus?.hasPrimaryRemote ?? false;
10521063
const gitStatusForActions = gitStatus;
1053-
const resolvedChangesTotals = resolveChangesTotals(gitStatusForActions);
1064+
const changesBaseRef = useDiffPanelStore((state) =>
1065+
selectThreadBranchBaseRef(state, activeThreadRef),
1066+
);
1067+
const ignoreWhitespaceSetting = useClientSettings((settings) => settings.diffIgnoreWhitespace);
1068+
const ignoreWhitespace =
1069+
useDiffPanelSessionStore((state) => state.ignoreWhitespace) ?? ignoreWhitespaceSetting;
1070+
const { canReadFiles } = useFilesystemReadAccess(activeEnvironmentId);
1071+
// Same input as the Diff panel's branch query, so both read one cached result. Status
1072+
// totals join the revision because edits, pulls and resets outside a turn leave the mutation
1073+
// id as is.
1074+
const changesRevision = gitStatusForActions
1075+
? JSON.stringify([
1076+
workspaceMutationId,
1077+
gitStatusForActions.refName,
1078+
gitStatusForActions.workingTree.insertions,
1079+
gitStatusForActions.workingTree.deletions,
1080+
gitStatusForActions.branchChanges?.baseRef,
1081+
gitStatusForActions.branchChanges?.insertions,
1082+
gitStatusForActions.branchChanges?.deletions,
1083+
])
1084+
: null;
1085+
const changesPreview = useEnvironmentQuery(
1086+
showChanges &&
1087+
isRepo &&
1088+
canReadFiles &&
1089+
changesRevision !== null &&
1090+
activeEnvironmentId !== null &&
1091+
gitCwd !== null
1092+
? revisionedQuery(
1093+
reviewEnvironment.diffPreview({
1094+
environmentId: activeEnvironmentId,
1095+
input: {
1096+
cwd: gitCwd,
1097+
...(changesBaseRef ? { baseRef: changesBaseRef } : {}),
1098+
ignoreWhitespace,
1099+
metadataOnly: true,
1100+
sourceKind: "branch-range",
1101+
},
1102+
}),
1103+
changesRevision,
1104+
)
1105+
: null,
1106+
);
1107+
const resolvedChangesTotals = resolveChangesTotals(
1108+
gitStatusForActions,
1109+
changesPreview.data?.sources.find((source) => source.kind === "branch-range"),
1110+
);
10541111
const changesTotals = resolvedChangesTotals ?? { insertions: 0, deletions: 0 };
10551112
const changesBaseLabel = resolvedChangesTotals
10561113
? resolvedChangesTotals.baseRef === null

‎apps/web/src/components/chat/ThreadDetailsPanel.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ interface ThreadDetailsPanelProps extends Pick<
6161
showOpenInPicker: boolean;
6262
openInShortcutEnabled: boolean;
6363
gitCwd: string | null;
64+
workspaceMutationId: string | null;
6465
isGitRepo: boolean;
6566
envLocked: boolean;
6667
availableEnvironments: readonly EnvironmentOption[];
@@ -229,6 +230,7 @@ export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) {
229230
showActions={visible.gitActions}
230231
showChanges={visible.changes}
231232
gitCwd={props.gitCwd}
233+
workspaceMutationId={props.workspaceMutationId}
232234
activeThreadRef={activeThreadRef}
233235
{...(props.draftId ? { draftId: props.draftId } : {})}
234236
{...(props.onOpenChanges ? { onOpenChanges: props.onOpenChanges } : {})}

‎apps/web/src/diffPanelStore.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,13 @@ interface DiffPanelSessionStoreState {
175175
defaults: ReadonlySet<string>,
176176
update: (current: ReadonlySet<string>) => ReadonlySet<string>,
177177
) => void;
178+
// Null follows the diffIgnoreWhitespace setting until the panel toggle is used.
179+
ignoreWhitespace: boolean | null;
180+
setIgnoreWhitespace: (ignoreWhitespace: boolean) => void;
178181
}
179182

180-
// Session-only so folds survive right-panel switches without changing reload defaults.
183+
// Session-only so folds and the whitespace toggle survive right-panel switches without
184+
// changing reload defaults.
181185
export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((set) => ({
182186
collapsedFileKeysByScopeKey: {},
183187
updateCollapsedFileKeys: (scopeKey, defaults, update) =>
@@ -189,6 +193,8 @@ export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((se
189193
collapsedFileKeysByScopeKey: { ...state.collapsedFileKeysByScopeKey, [scopeKey]: next },
190194
};
191195
}),
196+
ignoreWhitespace: null,
197+
setIgnoreWhitespace: (ignoreWhitespace) => set({ ignoreWhitespace }),
192198
}));
193199

194200
export function selectThreadDiffPanelSelection(

0 commit comments

Comments
 (0)