@@ -19,15 +19,15 @@ import {
1919} from "lucide-react" ;
2020import { ChevronDown , ChevronRight , ChevronsDownUp , ChevronsUpDown } from "lucide" ;
2121import * as Schema from "effect/Schema" ;
22- import * as DateTime from "effect/DateTime" ;
2322import { useEffect , useRef , useState } from "react" ;
24- import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal" ;
23+ import { createCodeViewFileReveal } from "./diffs/useCodeViewFileReveal" ;
2524import { useFilesystemReadAccess } from "~/state/filesystem" ;
2625import { useOpenInPreferredEditor } from "../editorPreferences" ;
2726import { useFileContextMenuHandler } from "../fileContextMenu" ;
2827import { type DraftId } from "../composerDraftStore" ;
2928import { openDiffFilePrimaryAction } from "../diffFileActions" ;
3029import { useCheckpointDiff } from "~/lib/checkpointDiffState" ;
30+ import { revisionedQuery } from "~/state/revisionedQuery" ;
3131import { cn } from "~/lib/utils" ;
3232import {
3333 selectThreadDiffPanelSelection ,
@@ -48,7 +48,7 @@ import {
4848} from "../lib/diffRendering" ;
4949import { PREFERRED_HIGHLIGHTER } from "../lib/syntaxHighlighting" ;
5050import { areAllDiffFilesCollapsed , toggleAllDiffFiles } from "../lib/diffCollapse" ;
51- import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefresh " ;
51+ import { LIVE_REFRESH_MIN_INTERVAL_MS , useLiveRefresh } from "../hooks/useLiveRefresh " ;
5252import { useProject , useThreadCheckpointSummaries , useQuietThreadShell } from "../state/entities" ;
5353import { useClientSettings , updateClientSettings } from "../hooks/useSettings" ;
5454import { formatShortTimestamp } from "../timestampFormat" ;
@@ -322,20 +322,29 @@ export default function DiffPanel({
322322 } ,
323323 { enabled : isGitRepo && selectedTurn !== undefined } ,
324324 ) ;
325- const branchDiffPreview = useEnvironmentQuery (
325+ const branchDiffPreviewInput =
326326 canReadFiles && selectedRunId === null && activeThread && activeCwd
327- ? reviewEnvironment . diffPreview ( {
328- environmentId : activeThread . environmentId ,
329- input : {
330- cwd : activeCwd ,
331- ...( selectedBaseRef ? { baseRef : selectedBaseRef } : { } ) ,
332- ignoreWhitespace : diffIgnoreWhitespace ,
333- } ,
334- } )
335- : null ,
336- ) ;
327+ ? {
328+ cwd : activeCwd ,
329+ ...( selectedBaseRef ? { baseRef : selectedBaseRef } : { } ) ,
330+ ignoreWhitespace : diffIgnoreWhitespace ,
331+ }
332+ : undefined ;
337333 const canRefreshGitDiff =
338334 isGitRepo && selectedRunId === null && activeThread != null && activeCwd != null ;
335+ // Read under the workspace mutation id, so each mutation refetches the diff; the panel stays
336+ // mounted while hidden, so a mutation made then is already reflected when it shows.
337+ const branchDiffPreview = useEnvironmentQuery (
338+ branchDiffPreviewInput && activeThread
339+ ? revisionedQuery (
340+ reviewEnvironment . diffPreview ( {
341+ environmentId : activeThread . environmentId ,
342+ input : branchDiffPreviewInput ,
343+ } ) ,
344+ canRefreshGitDiff ? workspaceMutationId : undefined ,
345+ )
346+ : null ,
347+ ) ;
339348 const activeThreadRefreshKey = routeThreadRef
340349 ? `${ routeThreadRef . environmentId } :${ routeThreadRef . threadId } `
341350 : null ;
@@ -458,51 +467,30 @@ export default function DiffPanel({
458467 baseRef : lazySource ?. baseRef ?? selectedBaseRef ,
459468 ignoreWhitespace : diffIgnoreWhitespace ,
460469 theme : resolvedTheme ,
461- revision : branchDiffPreview . data
462- ? DateTime . formatIso ( branchDiffPreview . data . generatedAt )
463- : undefined ,
470+ previewInput : branchDiffPreviewInput ,
464471 preview : renderablePatch ,
465472 } ) ;
466473 const refreshBranchDiffPreview = refreshPreviewQuery ;
467- const previewOpenHandledRef = useRef ( false ) ;
468- const refreshDiffForWorkspaceMutation = useStableCallback ( ( ) => {
469- previewOpenHandledRef . current = true ;
470- refreshBranchDiffPreview ( ) ;
471- } ) ;
472-
473- // A mutation refresh also satisfies revalidation for the current opening.
474- useWorkspaceMutationRefresh ( {
475- enabled : canRefreshGitDiff ,
476- mutationId : workspaceMutationId ,
477- refresh : refreshDiffForWorkspaceMutation ,
478- resourceKey : `diff:${ activeThreadRefreshKey ?? "" } ` ,
479- } ) ;
480-
481- useEffect ( ( ) => {
482- if ( ! visible || ! canReadFiles || ! canRefreshGitDiff ) {
483- previewOpenHandledRef . current = false ;
484- return ;
485- }
486- if ( previewOpenHandledRef . current ) return ;
487- previewOpenHandledRef . current = true ;
488- if ( branchDiffPreview . data !== null && ! branchDiffPreview . isPending ) {
489- refreshBranchDiffPreview ( ) ;
490- }
491- } , [
492- branchDiffPreview . data ,
493- branchDiffPreview . isPending ,
494- canReadFiles ,
495- canRefreshGitDiff ,
496- refreshBranchDiffPreview ,
497- visible ,
498- ] ) ;
474+ const diffRefreshKey = `diff:${ activeThreadRefreshKey ?? "" } ` ;
499475
500- useEffect ( ( ) => {
501- if ( ! canRefreshGitDiff ) return ;
502- const refreshOnFocus = ( ) => refreshBranchDiffPreview ( ) ;
503- window . addEventListener ( "focus" , refreshOnFocus ) ;
504- return ( ) => window . removeEventListener ( "focus" , refreshOnFocus ) ;
505- } , [ canRefreshGitDiff , refreshBranchDiffPreview ] ) ;
476+ // The panel stays mounted while hidden, and edits made outside T3 Code bump no mutation id, so
477+ // showing it revalidates the diff; that also stands in for focus events missed while hidden.
478+ // A git diff is too costly to repeat on a timer nobody asked for. A diff read within the floor,
479+ // such as a mutation's refetch while hidden, already counts as that revalidation.
480+ useLiveRefresh (
481+ branchDiffPreview . data !== null && ! branchDiffPreview . isPending
482+ ? ( ) => {
483+ if ( Date . now ( ) - branchDiffPreview . dataUpdatedAt < LIVE_REFRESH_MIN_INTERVAL_MS ) return ;
484+ refreshBranchDiffPreview ( ) ;
485+ }
486+ : null ,
487+ {
488+ enabled : visible && canReadFiles && canRefreshGitDiff ,
489+ key : diffRefreshKey ,
490+ intervalMs : null ,
491+ refreshOnShow : true ,
492+ } ,
493+ ) ;
506494
507495 const isRefreshingDiff = branchDiffPreview . isPending || areFilePatchesPending ;
508496 const renderableFileEntries = renderableFiles . map ( getCachedFileEntry ) ;
@@ -542,13 +530,12 @@ export default function DiffPanel({
542530 collapsed :
543531 collapsedDiffFileKeys . has ( fileKey ) || fileDiff . cacheKey ?. endsWith ( ":pending" ) === true ,
544532 } ) ) ;
545- const codeViewFileKeys = codeViewFiles . map ( ( file ) => file . fileKey ) ;
546533 const pendingFiles = renderableFiles . filter ( ( file ) => file . cacheKey ?. endsWith ( ":pending" ) ) ;
547534 const allDiffFilesCollapsed = areAllDiffFilesCollapsed ( diffFileKeys , collapsedDiffFileKeys ) ;
548535 const diffLineStat = sourceLineStat ?? getDiffLineStat ( renderableFiles ) ;
549536 const fileTreeEntries = diffFileTreeEntries ( renderableFiles ) ;
550- const treeRevealScope = { collapseScopeKey , diffSelection } ;
551- const requestTreeReveal = useCodeViewFileReveal ( codeView , treeRevealScope , codeViewFileKeys ) ;
537+ const [ codeViewReveal ] = useState ( createCodeViewFileReveal ) ;
538+ const requestTreeReveal = ( fileKey : string ) => codeViewReveal . request ( codeView , fileKey ) ;
552539 const revealDiffFile = ( filePath : string ) => {
553540 const index = renderableFileEntries . findIndex (
554541 ( candidate ) => resolveFileDiffPath ( candidate . fileDiff ) === filePath ,
@@ -567,6 +554,12 @@ export default function DiffPanel({
567554 return true ;
568555 } ;
569556
557+ // The code view keeps the post-render handler it was given; a file it did not hold yet when
558+ // asked is positioned once it renders it.
559+ const flushCodeViewReveal = useStableCallback ( ( ) => codeViewReveal . flush ( codeView ) ) ;
560+
561+ // A file another view asked to reveal (a chat diff link): retried as the diff loads until it
562+ // holds the file, then not again until a new request, so it does not undo what the user did since.
570563 const externalRevealRef = useRef < string | null > ( null ) ;
571564 useEffect ( ( ) => {
572565 if ( ! selectedFilePath ) {
@@ -1152,6 +1145,7 @@ export default function DiffPanel({
11521145 preferredHighlighter : PREFERRED_HIGHLIGHTER ,
11531146 themeType : resolvedTheme as DiffThemeType ,
11541147 stickyHeaders : true ,
1148+ onPostRender : flushCodeViewReveal ,
11551149 ...( currentLoadDiffFiles ? { loadDiffFiles } : { } ) ,
11561150 } }
11571151 />
0 commit comments