Skip to content

Commit 6812b02

Browse files
fix(web): refresh cached diffs whenever Changes reopens
1 parent 632b7c7 commit 6812b02

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ interface DiffPanelProps {
188188
mode?: DiffPanelMode;
189189
composerDraftTarget: ScopedThreadRef | DraftId;
190190
workspaceMutationId: string | null;
191+
visible: boolean;
191192
}
192193

193194
export default function DiffPanel({
194195
mode = "inline",
195196
composerDraftTarget,
196197
workspaceMutationId,
198+
visible,
197199
}: DiffPanelProps) {
198200
const { resolvedTheme } = useTheme();
199201
const settings = useClientSettings((settings) => ({
@@ -467,32 +469,46 @@ export default function DiffPanel({
467469
preview: renderablePatch,
468470
});
469471
const refreshBranchDiffPreview = refreshPreviewQuery;
470-
// Reopening within the query's idle TTL reuses its cached result, which SWR
471-
// only revalidates when the atom is built. A fresh atom is already fetching,
472-
// and once the thread has a workspace mutation, useWorkspaceMutationRefresh
473-
// below refreshes on mount, so this covers threads without one.
474-
const revalidateCachedPreviewOnOpenRef = useRef(
475-
workspaceMutationId === null && branchDiffPreview.data !== null && !branchDiffPreview.isPending,
476-
);
472+
const previewOpenHandledRef = useRef(false);
473+
const refreshDiffForWorkspaceMutation = useStableCallback(() => {
474+
previewOpenHandledRef.current = true;
475+
refreshBranchDiffPreview();
476+
});
477+
478+
// A mutation refresh also satisfies revalidation for the current opening.
479+
useWorkspaceMutationRefresh({
480+
enabled: canRefreshGitDiff,
481+
mutationId: workspaceMutationId,
482+
refresh: refreshDiffForWorkspaceMutation,
483+
resourceKey: `diff:${activeThreadRefreshKey ?? ""}`,
484+
});
477485

478486
useEffect(() => {
479-
if (!canRefreshGitDiff) return;
480-
if (revalidateCachedPreviewOnOpenRef.current) {
481-
revalidateCachedPreviewOnOpenRef.current = false;
487+
if (!visible || !canReadFiles || !canRefreshGitDiff) {
488+
previewOpenHandledRef.current = false;
489+
return;
490+
}
491+
if (previewOpenHandledRef.current) return;
492+
previewOpenHandledRef.current = true;
493+
if (branchDiffPreview.data !== null && !branchDiffPreview.isPending) {
482494
refreshBranchDiffPreview();
483495
}
496+
}, [
497+
branchDiffPreview.data,
498+
branchDiffPreview.isPending,
499+
canReadFiles,
500+
canRefreshGitDiff,
501+
refreshBranchDiffPreview,
502+
visible,
503+
]);
504+
505+
useEffect(() => {
506+
if (!canRefreshGitDiff) return;
484507
const refreshOnFocus = () => refreshBranchDiffPreview();
485508
window.addEventListener("focus", refreshOnFocus);
486509
return () => window.removeEventListener("focus", refreshOnFocus);
487510
}, [canRefreshGitDiff, refreshBranchDiffPreview]);
488511

489-
useWorkspaceMutationRefresh({
490-
enabled: canRefreshGitDiff,
491-
mutationId: workspaceMutationId,
492-
refresh: refreshBranchDiffPreview,
493-
resourceKey: `diff:${activeThreadRefreshKey ?? ""}`,
494-
});
495-
496512
const isRefreshingDiff = branchDiffPreview.isPending || areFilePatchesPending;
497513
const renderableFileEntries = renderableFiles.map(getCachedFileEntry);
498514
const diffFileKeys = renderableFileEntries.map((file) => file.fileKey);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export function RightPanelSurfaceContent(props: {
111111
mode="embedded"
112112
composerDraftTarget={props.composerDraftTarget}
113113
workspaceMutationId={props.workspaceMutationId}
114+
visible={visible}
114115
/>
115116
</Suspense>
116117
);

0 commit comments

Comments
 (0)