Skip to content

Commit 86d0b38

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-25558
2 parents 613cd23 + f6a9315 commit 86d0b38

25 files changed

Lines changed: 1661 additions & 561 deletions

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

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ import { BackgroundThreadContext } from "./chat/backgroundThread";
121121
import { useLocation, useNavigate } from "@tanstack/react-router";
122122
import { assistantCitationFromLocation } from "../lib/assistantCitationNavigation";
123123
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
124+
import { useStore } from "zustand";
124125
import { useShallow } from "zustand/react/shallow";
125126
import {
126127
isAtomCommandInterrupted,
@@ -150,10 +151,7 @@ import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
150151
import { subscribeSnapShotComposerFocus } from "../lib/desktopSnapShot";
151152
import { useMediaQuery } from "../hooks/useMediaQuery";
152153
import { FileMetadataThreadProvider } from "../hooks/FileMetadataThreadProvider";
153-
import {
154-
RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY,
155-
type ThreadPanelPresentation,
156-
} from "../rightPanelLayout";
154+
import { RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY } from "../rightPanelLayout";
157155
import { PopoverCreateHandle } from "./ui/popover";
158156
import {
159157
selectActiveRightPanel,
@@ -164,7 +162,11 @@ import {
164162
} from "../rightPanelStore";
165163
import { isPreviewSupportedInRuntime, useThreadPreviewState } from "../previewStateStore";
166164
import { previewRuntimeTabId } from "../browser/previewRuntimeTabId";
167-
import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";
165+
import {
166+
ThreadPreviewMiniPlayer,
167+
useThreadPreviewCanvasSource,
168+
} from "./preview/ThreadPreviewMiniPlayer";
169+
import { createChatCanvasStore, resolveChatCanvasModel } from "./chat/canvasStore";
168170
import { usePreviewSession } from "./preview/usePreviewSession";
169171
import { getConfiguredPreviewUrls } from "./preview/previewEmptyStateLogic";
170172

@@ -1004,16 +1006,8 @@ export default function ChatView(props: ChatViewProps) {
10041006
const canMaximizeRightPanel = rightPanelOpen && !shouldUsePlanSidebarSheet;
10051007
const rightPanelMaximized = canMaximizeRightPanel && rightPanelState.maximized === true;
10061008
const inlineRightPanelOwnsTitleBar = rightPanelOpen && !shouldUsePlanSidebarSheet;
1007-
const [threadPanelPresentation, setThreadPanelPresentation] =
1008-
useState<ThreadPanelPresentation>("inline");
10091009
const [threadPanelPopoverHandle] = useState(PopoverCreateHandle);
1010-
const threadPanelOpen = useRightPanelStore((state) =>
1011-
selectThreadPanelOpen(
1012-
state.threadPanelVisibilityByThreadKey,
1013-
activeThreadRef,
1014-
threadPanelPresentation,
1015-
),
1016-
);
1010+
const [chatCanvasStore] = useState(createChatCanvasStore);
10171011

10181012
const latestRunSettled = isLatestRunSettled(activeLatestRun, activeRuntime);
10191013
const activeThreadEnvironmentId = activeThread?.environmentId;
@@ -1856,6 +1850,39 @@ export default function ChatView(props: ChatViewProps) {
18561850
activeThreadRef?.environmentId ?? null,
18571851
AuthPreviewOperateScope,
18581852
);
1853+
const shownPreviewMiniPlayer =
1854+
activeThreadRef &&
1855+
activePreviewMiniPlayer &&
1856+
previewMiniPlayerVisible &&
1857+
(activePreviewMiniPlayer.source.kind === "device" || canOperatePreview)
1858+
? activePreviewMiniPlayer
1859+
: null;
1860+
const canvasPreview = useThreadPreviewCanvasSource(activeThreadRef, shownPreviewMiniPlayer);
1861+
const threadPanelInlineOpen = useRightPanelStore((state) =>
1862+
selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, activeThreadRef, "inline"),
1863+
);
1864+
const centerChatBesideDetails = useClientSettings((s) => s.redesignEnabled);
1865+
// Selects only the mode, so canvas resizes rerender the chat view only when the
1866+
// card switches presentation.
1867+
const threadPanelPresentation = useStore(
1868+
chatCanvasStore,
1869+
(state) =>
1870+
resolveChatCanvasModel({
1871+
measurements: state.measurements,
1872+
detailsCardMeasurement: state.detailsCardMeasurement,
1873+
threadRef: activeThreadRef,
1874+
preview: canvasPreview,
1875+
detailsCardInlineOpen: threadPanelInlineOpen,
1876+
centerBesideDetails: centerChatBesideDetails,
1877+
}).detailsCardMode,
1878+
);
1879+
const threadPanelOpen = useRightPanelStore((state) =>
1880+
selectThreadPanelOpen(
1881+
state.threadPanelVisibilityByThreadKey,
1882+
activeThreadRef,
1883+
threadPanelPresentation,
1884+
),
1885+
);
18591886
const supportsThreadPullRequests =
18601887
serverConfig?.environment.capabilities.threadPullRequests === true;
18611888
const visiblePullRequests = visibleThreadPullRequests(
@@ -3184,6 +3211,9 @@ export default function ChatView(props: ChatViewProps) {
31843211
thread: activeThreadRef,
31853212
enabled: threadFindEnabled,
31863213
}}
3214+
store={chatCanvasStore}
3215+
threadRef={activeThreadRef}
3216+
preview={canvasPreview}
31873217
detailsCardTopInset={isThreadFindActive ? THREAD_FIND_BAR_RESERVED_HEIGHT : 0}
31883218
composerOverlayElement={isDraftHeroState ? null : composerOverlayElement}
31893219
data-chat-workspace-drop-target="true"
@@ -3690,14 +3720,11 @@ export default function ChatView(props: ChatViewProps) {
36903720
{canOperatePreview && activeThreadRef && activeEnvironmentServerBrowser ? (
36913721
<PreviewSessionSync threadRef={activeThreadRef} />
36923722
) : null}
3693-
{activeThreadRef &&
3694-
activePreviewMiniPlayer &&
3695-
previewMiniPlayerVisible &&
3696-
(activePreviewMiniPlayer.source.kind === "device" || canOperatePreview) ? (
3723+
{activeThreadRef && shownPreviewMiniPlayer ? (
36973724
<ThreadPreviewMiniPlayer
3698-
key={`${activeThreadKey}:${previewMiniPlayerSourceKey(activePreviewMiniPlayer.source)}`}
3725+
key={`${activeThreadKey}:${previewMiniPlayerSourceKey(shownPreviewMiniPlayer.source)}`}
36993726
threadRef={activeThreadRef}
3700-
miniPlayer={activePreviewMiniPlayer}
3727+
miniPlayer={shownPreviewMiniPlayer}
37013728
/>
37023729
) : null}
37033730

@@ -3711,7 +3738,6 @@ export default function ChatView(props: ChatViewProps) {
37113738
<ThreadDetailsPanel
37123739
anchor={threadPanelPopoverAnchorRef}
37133740
handle={threadPanelPopoverHandle}
3714-
onPresentationChange={setThreadPanelPresentation}
37153741
forceNewWorktree={multipleModelSelections !== null}
37163742
environmentId={activeThread.environmentId}
37173743
threadId={activeThread.id}

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

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
} from "lucide-react";
2020
import { ChevronDown, ChevronRight, ChevronsDownUp, ChevronsUpDown } from "lucide";
2121
import * as Schema from "effect/Schema";
22-
import * as DateTime from "effect/DateTime";
2322
import { useEffect, useRef, useState } from "react";
2423
import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal";
2524
import { useFilesystemReadAccess } from "~/state/filesystem";
@@ -28,6 +27,7 @@ import { useFileContextMenuHandler } from "../fileContextMenu";
2827
import { type DraftId } from "../composerDraftStore";
2928
import { openDiffFilePrimaryAction } from "../diffFileActions";
3029
import { useCheckpointDiff } from "~/lib/checkpointDiffState";
30+
import { revisionedQuery } from "~/state/revisionedQuery";
3131
import { cn } from "~/lib/utils";
3232
import {
3333
selectThreadDiffPanelSelection,
@@ -48,14 +48,14 @@ import {
4848
} from "../lib/diffRendering";
4949
import { PREFERRED_HIGHLIGHTER } from "../lib/syntaxHighlighting";
5050
import { areAllDiffFilesCollapsed, toggleAllDiffFiles } from "../lib/diffCollapse";
51-
import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefresh";
51+
import { LIVE_REFRESH_MIN_INTERVAL_MS, useLiveRefresh } from "../hooks/useLiveRefresh";
5252
import { useProject, useThreadCheckpointSummaries, useQuietThreadShell } from "../state/entities";
5353
import { useClientSettings, updateClientSettings } from "../hooks/useSettings";
5454
import { formatShortTimestamp } from "../timestampFormat";
5555
import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton";
5656
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
5757
import { DiffStatLabel } from "./chat/DiffStatLabel";
58-
import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableCodeView";
58+
import { AnnotatableCodeView } from "./diffs/AnnotatableCodeView";
5959
import { DiffFileTree } from "./diffs/DiffFileTree";
6060
import { diffFileTreeEntries } from "./diffs/diffFileTree.logic";
6161
import { Button } from "./ui/button";
@@ -216,7 +216,6 @@ export default function DiffPanel({
216216
);
217217
const [baseRefQuery, setBaseRefQuery] = useState("");
218218
const [codeViewRevision, setCodeViewRevision] = useState(0);
219-
const [codeView, setCodeView] = useState<AnnotatableCodeViewHandle | null>(null);
220219

221220
const routeThreadRef = useThreadRouteRef();
222221
const activeThreadId = routeThreadRef?.threadId ?? null;
@@ -322,20 +321,29 @@ export default function DiffPanel({
322321
},
323322
{ enabled: isGitRepo && selectedTurn !== undefined },
324323
);
325-
const branchDiffPreview = useEnvironmentQuery(
324+
const branchDiffPreviewInput =
326325
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;
337332
const canRefreshGitDiff =
338333
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+
);
339347
const activeThreadRefreshKey = routeThreadRef
340348
? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}`
341349
: null;
@@ -458,51 +466,30 @@ export default function DiffPanel({
458466
baseRef: lazySource?.baseRef ?? selectedBaseRef,
459467
ignoreWhitespace: diffIgnoreWhitespace,
460468
theme: resolvedTheme,
461-
revision: branchDiffPreview.data
462-
? DateTime.formatIso(branchDiffPreview.data.generatedAt)
463-
: undefined,
469+
previewInput: branchDiffPreviewInput,
464470
preview: renderablePatch,
465471
});
466472
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 ?? ""}`;
499474

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+
);
506493

507494
const isRefreshingDiff = branchDiffPreview.isPending || areFilePatchesPending;
508495
const renderableFileEntries = renderableFiles.map(getCachedFileEntry);
@@ -542,13 +529,11 @@ export default function DiffPanel({
542529
collapsed:
543530
collapsedDiffFileKeys.has(fileKey) || fileDiff.cacheKey?.endsWith(":pending") === true,
544531
}));
545-
const codeViewFileKeys = codeViewFiles.map((file) => file.fileKey);
546532
const pendingFiles = renderableFiles.filter((file) => file.cacheKey?.endsWith(":pending"));
547533
const allDiffFilesCollapsed = areAllDiffFilesCollapsed(diffFileKeys, collapsedDiffFileKeys);
548534
const diffLineStat = sourceLineStat ?? getDiffLineStat(renderableFiles);
549535
const fileTreeEntries = diffFileTreeEntries(renderableFiles);
550-
const treeRevealScope = { collapseScopeKey, diffSelection };
551-
const requestTreeReveal = useCodeViewFileReveal(codeView, treeRevealScope, codeViewFileKeys);
536+
const codeViewReveal = useCodeViewFileReveal();
552537
const revealDiffFile = (filePath: string) => {
553538
const index = renderableFileEntries.findIndex(
554539
(candidate) => resolveFileDiffPath(candidate.fileDiff) === filePath,
@@ -563,10 +548,12 @@ export default function DiffPanel({
563548
if (lazySource && index >= settledFileCount) {
564549
requestFile(index);
565550
}
566-
requestTreeReveal(file.fileKey);
551+
codeViewReveal.request(file.fileKey);
567552
return true;
568553
};
569554

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.
570557
const externalRevealRef = useRef<string | null>(null);
571558
useEffect(() => {
572559
if (!selectedFilePath) {
@@ -1093,7 +1080,7 @@ export default function DiffPanel({
10931080
>
10941081
<AnnotatableCodeView
10951082
key={collapseScopeKey ?? reviewSectionId}
1096-
viewerRef={setCodeView}
1083+
viewerRef={codeViewReveal.attach}
10971084
codeViewKey={`${codeViewMountKey}:${lazySource ? filePatchScope : "preview"}`}
10981085
className="h-full min-h-0 overflow-auto"
10991086
files={codeViewFiles}
@@ -1152,6 +1139,7 @@ export default function DiffPanel({
11521139
preferredHighlighter: PREFERRED_HIGHLIGHTER,
11531140
themeType: resolvedTheme as DiffThemeType,
11541141
stickyHeaders: true,
1142+
onPostRender: codeViewReveal.flush,
11551143
...(currentLoadDiffFiles ? { loadDiffFiles } : {}),
11561144
}}
11571145
/>

0 commit comments

Comments
 (0)