@@ -188,12 +188,14 @@ interface DiffPanelProps {
188188 mode ?: DiffPanelMode ;
189189 composerDraftTarget : ScopedThreadRef | DraftId ;
190190 workspaceMutationId : string | null ;
191+ visible : boolean ;
191192}
192193
193194export 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 ) ;
0 commit comments