@@ -19,7 +19,6 @@ import { |
19 | 19 | } from "lucide-react"; |
20 | 20 | import { ChevronDown, ChevronRight, ChevronsDownUp, ChevronsUpDown } from "lucide"; |
21 | 21 | import * as Schema from "effect/Schema"; |
22 | | -import * as DateTime from "effect/DateTime"; |
23 | 22 | import { useEffect, useRef, useState } from "react"; |
24 | 23 | import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal"; |
25 | 24 | import { useFilesystemReadAccess } from "~/state/filesystem"; |
@@ -28,6 +27,7 @@ import { useFileContextMenuHandler } from "../fileContextMenu"; |
28 | 27 | import { type DraftId } from "../composerDraftStore"; |
29 | 28 | import { openDiffFilePrimaryAction } from "../diffFileActions"; |
30 | 29 | import { useCheckpointDiff } from "~/lib/checkpointDiffState"; |
| 30 | +import { revisionedQuery } from "~/state/revisionedQuery"; |
31 | 31 | import { cn } from "~/lib/utils"; |
32 | 32 | import { |
33 | 33 | selectThreadDiffPanelSelection, |
@@ -48,14 +48,14 @@ import { |
48 | 48 | } from "../lib/diffRendering"; |
49 | 49 | import { PREFERRED_HIGHLIGHTER } from "../lib/syntaxHighlighting"; |
50 | 50 | import { areAllDiffFilesCollapsed, toggleAllDiffFiles } from "../lib/diffCollapse"; |
51 | | -import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefresh"; |
| 51 | +import { LIVE_REFRESH_MIN_INTERVAL_MS, useLiveRefresh } from "../hooks/useLiveRefresh"; |
52 | 52 | import { useProject, useThreadCheckpointSummaries, useQuietThreadShell } from "../state/entities"; |
53 | 53 | import { useClientSettings, updateClientSettings } from "../hooks/useSettings"; |
54 | 54 | import { formatShortTimestamp } from "../timestampFormat"; |
55 | 55 | import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton"; |
56 | 56 | import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell"; |
57 | 57 | import { DiffStatLabel } from "./chat/DiffStatLabel"; |
58 | | -import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableCodeView"; |
| 58 | +import { AnnotatableCodeView } from "./diffs/AnnotatableCodeView"; |
59 | 59 | import { DiffFileTree } from "./diffs/DiffFileTree"; |
60 | 60 | import { diffFileTreeEntries } from "./diffs/diffFileTree.logic"; |
61 | 61 | import { Button } from "./ui/button"; |
@@ -216,7 +216,6 @@ export default function DiffPanel({ |
216 | 216 | ); |
217 | 217 | const [baseRefQuery, setBaseRefQuery] = useState(""); |
218 | 218 | const [codeViewRevision, setCodeViewRevision] = useState(0); |
219 | | - const [codeView, setCodeView] = useState<AnnotatableCodeViewHandle | null>(null); |
220 | 219 |
|
221 | 220 | const routeThreadRef = useThreadRouteRef(); |
222 | 221 | const activeThreadId = routeThreadRef?.threadId ?? null; |
@@ -322,20 +321,29 @@ export default function DiffPanel({ |
322 | 321 | }, |
323 | 322 | { enabled: isGitRepo && selectedTurn !== undefined }, |
324 | 323 | ); |
325 | | - const branchDiffPreview = useEnvironmentQuery( |
| 324 | + const branchDiffPreviewInput = |
326 | 325 | 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 | | - ); |
| 326 | + ? { |
| 327 | + cwd: activeCwd, |
| 328 | + ...(selectedBaseRef ? { baseRef: selectedBaseRef } : {}), |
| 329 | + ignoreWhitespace: diffIgnoreWhitespace, |
| 330 | + } |
| 331 | + : undefined; |
337 | 332 | const canRefreshGitDiff = |
338 | 333 | isGitRepo && selectedRunId === null && activeThread != null && activeCwd != null; |
| 334 | + // Read under the workspace mutation id, so each mutation refetches the diff; the panel stays |
| 335 | + // mounted while hidden, so a mutation made then is already reflected when it shows. |
| 336 | + const branchDiffPreview = useEnvironmentQuery( |
| 337 | + branchDiffPreviewInput && activeThread |
| 338 | + ? revisionedQuery( |
| 339 | + reviewEnvironment.diffPreview({ |
| 340 | + environmentId: activeThread.environmentId, |
| 341 | + input: branchDiffPreviewInput, |
| 342 | + }), |
| 343 | + canRefreshGitDiff ? workspaceMutationId : undefined, |
| 344 | + ) |
| 345 | + : null, |
| 346 | + ); |
339 | 347 | const activeThreadRefreshKey = routeThreadRef |
340 | 348 | ? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}` |
341 | 349 | : null; |
@@ -458,51 +466,30 @@ export default function DiffPanel({ |
458 | 466 | baseRef: lazySource?.baseRef ?? selectedBaseRef, |
459 | 467 | ignoreWhitespace: diffIgnoreWhitespace, |
460 | 468 | theme: resolvedTheme, |
461 | | - revision: branchDiffPreview.data |
462 | | - ? DateTime.formatIso(branchDiffPreview.data.generatedAt) |
463 | | - : undefined, |
| 469 | + previewInput: branchDiffPreviewInput, |
464 | 470 | preview: renderablePatch, |
465 | 471 | }); |
466 | 472 | 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 | | - ]); |
| 473 | + const diffRefreshKey = `diff:${activeThreadRefreshKey ?? ""}`; |
499 | 474 |
|
500 | | - useEffect(() => { |
501 | | - if (!canRefreshGitDiff) return; |
502 | | - const refreshOnFocus = () => refreshBranchDiffPreview(); |
503 | | - window.addEventListener("focus", refreshOnFocus); |
504 | | - return () => window.removeEventListener("focus", refreshOnFocus); |
505 | | - }, [canRefreshGitDiff, refreshBranchDiffPreview]); |
| 475 | + // The panel stays mounted while hidden, and edits made outside T3 Code bump no mutation id, so |
| 476 | + // showing it revalidates the diff; that also stands in for focus events missed while hidden. |
| 477 | + // A git diff is too costly to repeat on a timer nobody asked for. A diff read within the floor, |
| 478 | + // such as a mutation's refetch while hidden, already counts as that revalidation. |
| 479 | + useLiveRefresh( |
| 480 | + branchDiffPreview.data !== null && !branchDiffPreview.isPending |
| 481 | + ? () => { |
| 482 | + if (Date.now() - branchDiffPreview.dataUpdatedAt < LIVE_REFRESH_MIN_INTERVAL_MS) return; |
| 483 | + refreshBranchDiffPreview(); |
| 484 | + } |
| 485 | + : null, |
| 486 | + { |
| 487 | + enabled: visible && canReadFiles && canRefreshGitDiff, |
| 488 | + key: diffRefreshKey, |
| 489 | + intervalMs: null, |
| 490 | + refreshOnShow: true, |
| 491 | + }, |
| 492 | + ); |
506 | 493 |
|
507 | 494 | const isRefreshingDiff = branchDiffPreview.isPending || areFilePatchesPending; |
508 | 495 | const renderableFileEntries = renderableFiles.map(getCachedFileEntry); |
@@ -542,13 +529,11 @@ export default function DiffPanel({ |
542 | 529 | collapsed: |
543 | 530 | collapsedDiffFileKeys.has(fileKey) || fileDiff.cacheKey?.endsWith(":pending") === true, |
544 | 531 | })); |
545 | | - const codeViewFileKeys = codeViewFiles.map((file) => file.fileKey); |
546 | 532 | const pendingFiles = renderableFiles.filter((file) => file.cacheKey?.endsWith(":pending")); |
547 | 533 | const allDiffFilesCollapsed = areAllDiffFilesCollapsed(diffFileKeys, collapsedDiffFileKeys); |
548 | 534 | const diffLineStat = sourceLineStat ?? getDiffLineStat(renderableFiles); |
549 | 535 | const fileTreeEntries = diffFileTreeEntries(renderableFiles); |
550 | | - const treeRevealScope = { collapseScopeKey, diffSelection }; |
551 | | - const requestTreeReveal = useCodeViewFileReveal(codeView, treeRevealScope, codeViewFileKeys); |
| 536 | + const codeViewReveal = useCodeViewFileReveal(); |
552 | 537 | const revealDiffFile = (filePath: string) => { |
553 | 538 | const index = renderableFileEntries.findIndex( |
554 | 539 | (candidate) => resolveFileDiffPath(candidate.fileDiff) === filePath, |
@@ -563,10 +548,12 @@ export default function DiffPanel({ |
563 | 548 | if (lazySource && index >= settledFileCount) { |
564 | 549 | requestFile(index); |
565 | 550 | } |
566 | | - requestTreeReveal(file.fileKey); |
| 551 | + codeViewReveal.request(file.fileKey); |
567 | 552 | return true; |
568 | 553 | }; |
569 | 554 |
|
| 555 | + // A file another view asked to reveal (a chat diff link): retried as the diff loads until it |
| 556 | + // holds the file, then not again until a new request, so it does not undo what the user did since. |
570 | 557 | const externalRevealRef = useRef<string | null>(null); |
571 | 558 | useEffect(() => { |
572 | 559 | if (!selectedFilePath) { |
@@ -1093,7 +1080,7 @@ export default function DiffPanel({ |
1093 | 1080 | > |
1094 | 1081 | <AnnotatableCodeView |
1095 | 1082 | key={collapseScopeKey ?? reviewSectionId} |
1096 | | - viewerRef={setCodeView} |
| 1083 | + viewerRef={codeViewReveal.attach} |
1097 | 1084 | codeViewKey={`${codeViewMountKey}:${lazySource ? filePatchScope : "preview"}`} |
1098 | 1085 | className="h-full min-h-0 overflow-auto" |
1099 | 1086 | files={codeViewFiles} |
@@ -1152,6 +1139,7 @@ export default function DiffPanel({ |
1152 | 1139 | preferredHighlighter: PREFERRED_HIGHLIGHTER, |
1153 | 1140 | themeType: resolvedTheme as DiffThemeType, |
1154 | 1141 | stickyHeaders: true, |
| 1142 | + onPostRender: codeViewReveal.flush, |
1155 | 1143 | ...(currentLoadDiffFiles ? { loadDiffFiles } : {}), |
1156 | 1144 | }} |
1157 | 1145 | /> |
|