Skip to content

Commit ba60a74

Browse files
refactor(web): ChatView's right panel actions live in useRightPanelActions
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 089570e commit ba60a74

2 files changed

Lines changed: 359 additions & 217 deletions

File tree

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

Lines changed: 46 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { useRightPanelActions } from "./chat/useRightPanelActions";
12
import { type TerminalLaunchContext, useChatTerminalActions } from "./chat/useChatTerminalActions";
23
import { useComposerSend } from "./chat/useComposerSend";
3-
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
4-
import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
54
import { ThreadFind, ThreadFindCanvas, type ThreadFindControls } from "./chat/ThreadFindProvider";
65
import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar";
76
import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner";
@@ -12,7 +11,6 @@ import {
1211
peekHeldThreadTimeline,
1312
peekRememberedThreadTimeline,
1413
rememberReadyThreadTimeline,
15-
resolveExplicitDiffOpenOnActivate,
1614
resolveThreadSwitchTimeline,
1715
timelineHasEphemeralPreviewUrls,
1816
recallCheckoutIsRepo,
@@ -159,24 +157,15 @@ import {
159157
selectActiveRightPanelSurface,
160158
selectThreadPanelOpen,
161159
selectThreadRightPanelState,
162-
type RightPanelSurface,
163160
useRightPanelStore,
164161
} from "../rightPanelStore";
165-
import {
166-
isPreviewSupportedInRuntime,
167-
setActivePreviewTab,
168-
useThreadPreviewState,
169-
} from "../previewStateStore";
170-
import { BrowserSettingsReadError } from "../browser/openFileInPreview";
162+
import { isPreviewSupportedInRuntime, useThreadPreviewState } from "../previewStateStore";
171163
import { previewRuntimeTabId } from "../browser/previewRuntimeTabId";
172-
import { addBrowserSurface } from "./preview/addBrowserSurface";
173164
import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";
174165
import { usePreviewSession } from "./preview/usePreviewSession";
175-
import { subscribePreviewAction } from "./preview/previewActionBus";
176166
import { getConfiguredPreviewUrls } from "./preview/previewEmptyStateLogic";
177167

178168
import {
179-
browserMiniPlayerSource,
180169
previewMiniPlayerSourceKey,
181170
selectThreadPreviewMiniPlayer,
182171
usePreviewMiniPlayerStore,
@@ -286,7 +275,6 @@ import {
286275
BranchRestoreConfirmHost,
287276
DeviceSetupDialogHost,
288277
openBranchRestoreConfirm,
289-
openDeviceSetupDialog,
290278
openPullRequestThreadDialog,
291279
openRevertConfirm,
292280
PullRequestThreadDialogHost,
@@ -380,7 +368,6 @@ import {
380368
} from "./ChatView.logic";
381369
import { useLocalStorage } from "~/hooks/useLocalStorage";
382370
import { useComposerHandleContext } from "../composerHandleContext";
383-
import { previewEnvironment } from "../state/preview";
384371
import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments";
385372
import { appAtomRegistry } from "../rpc/atomRegistry";
386373
import { assetEnvironment } from "../state/assets";
@@ -509,7 +496,6 @@ export default function ChatView(props: ChatViewProps) {
509496
const forkThreadFromRun = useAtomCommand(threadEnvironment.forkFromRun, {
510497
reportFailure: false,
511498
});
512-
const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
513499
const { environments } = useEnvironments();
514500
const providersByEnvironment = useEnvironmentProviders();
515501
const primaryEnvironment = usePrimaryEnvironment();
@@ -2305,18 +2291,6 @@ export default function ChatView(props: ChatViewProps) {
23052291
"terminal.close",
23062292
terminalShortcutLabelOptions,
23072293
);
2308-
const onToggleDiff = () => {
2309-
if (!isServerThread) {
2310-
return;
2311-
}
2312-
if (!diffOpen) {
2313-
onDiffPanelOpen?.();
2314-
}
2315-
if (activeThreadRef) {
2316-
useRightPanelStore.getState().toggle(activeThreadRef, "diff");
2317-
}
2318-
};
2319-
const onToggleDiffStable = useStableCallback(onToggleDiff);
23202294

23212295
const {
23222296
needsLoadBalancing,
@@ -2498,43 +2472,6 @@ export default function ChatView(props: ChatViewProps) {
24982472
activeThreadRef?.environmentId ?? null,
24992473
AuthPreviewOperateScope,
25002474
);
2501-
const createBrowserSurface = (profileId?: string) => {
2502-
if (!activeThreadRef || !canOperatePreview) return;
2503-
void addBrowserSurface({
2504-
threadRef: activeThreadRef,
2505-
openPreview,
2506-
...(profileId === undefined ? {} : { profileId }),
2507-
}).then((result) => {
2508-
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
2509-
const error = squashAtomCommandFailure(result);
2510-
if (error instanceof BrowserSettingsReadError) {
2511-
toastManager.add(
2512-
stackedThreadToast({
2513-
type: "error",
2514-
title: "Unable to open browser",
2515-
description: error.message,
2516-
}),
2517-
);
2518-
}
2519-
});
2520-
};
2521-
const createBrowserSurfaceStable = useStableCallback(createBrowserSurface);
2522-
const addDiffSurface = () => {
2523-
if (!activeThreadRef || !isServerThread || !isGitRepo) return;
2524-
useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");
2525-
useRightPanelStore.getState().open(activeThreadRef, "diff");
2526-
onDiffPanelOpen?.();
2527-
};
2528-
const addDiffSurfaceStable = useStableCallback(addDiffSurface);
2529-
const openChangesFromThreadPanel = () => {
2530-
addDiffSurface();
2531-
};
2532-
const openChangesFromThreadPanelStable = useStableCallback(openChangesFromThreadPanel);
2533-
const addFilesSurface = () => {
2534-
if (!activeThreadRef || !activeProject) return;
2535-
useRightPanelStore.getState().open(activeThreadRef, "files");
2536-
};
2537-
const addFilesSurfaceStable = useStableCallback(addFilesSurface);
25382475
const supportsThreadPullRequests =
25392476
serverConfig?.environment.capabilities.threadPullRequests === true;
25402477
const visiblePullRequests = visibleThreadPullRequests(
@@ -2543,23 +2480,9 @@ export default function ChatView(props: ChatViewProps) {
25432480
const visiblePullRequestCount = visiblePullRequests.length;
25442481
const pullRequestsSurfaceAvailable =
25452482
isServerThread && supportsThreadPullRequests && visiblePullRequestCount > 0;
2546-
const addPullRequestsSurface = () => {
2547-
if (!activeThreadRef || !pullRequestsSurfaceAvailable) return;
2548-
useRightPanelStore.getState().open(activeThreadRef, "pull-requests");
2549-
};
2550-
const addPullRequestsSurfaceStable = useStableCallback(addPullRequestsSurface);
25512483
const { state: deviceState, loaded: deviceStateLoaded } = useDeviceState(
25522484
activeThreadRef?.environmentId ?? null,
25532485
);
2554-
const addDeviceSurface = () => {
2555-
if (!activeThreadRef) return;
2556-
if (!deviceState.onboardingCompleted || deviceState.hostStatus === "disabled") {
2557-
openDeviceSetupDialog(scopedThreadKey(activeThreadRef));
2558-
return;
2559-
}
2560-
useRightPanelStore.getState().open(activeThreadRef, "device");
2561-
};
2562-
const addDeviceSurfaceStable = useStableCallback(addDeviceSurface);
25632486
useAgentOpenedPreviews({
25642487
threadRef: activeThreadRef,
25652488
deviceState,
@@ -2570,11 +2493,6 @@ export default function ChatView(props: ChatViewProps) {
25702493
miniPlayerSource: activePreviewMiniPlayer?.source ?? null,
25712494
sheetLayout: shouldUsePlanSidebarSheet,
25722495
});
2573-
const openFileSurface = (relativePath: string) => {
2574-
if (!activeThreadRef || !activeProject) return;
2575-
useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
2576-
};
2577-
const openFileSurfaceStable = useStableCallback(openFileSurface);
25782496
// The thread's own change request, placed against the project it belongs to. Without a
25792497
// project there is nothing to resolve it against, so the caller falls back to the browser.
25802498
const activeProjectRepository = sourceControlRepositorySelector(
@@ -2620,81 +2538,12 @@ export default function ChatView(props: ChatViewProps) {
26202538
visiblePullRequestCount,
26212539
onDiffPanelOpen,
26222540
});
2623-
const closePreviewPanel = () => {
2624-
if (activeThreadRef) {
2625-
if (activeRightPanelSurface?.kind === "preview" && activeRightPanelSurface.resourceId) {
2626-
usePreviewMiniPlayerStore
2627-
.getState()
2628-
.open(activeThreadRef, browserMiniPlayerSource(activeRightPanelSurface.resourceId));
2629-
} else if (activeRightPanelSurface?.kind === "device" && activeRightPanelSurface.target) {
2630-
usePreviewMiniPlayerStore
2631-
.getState()
2632-
.open(activeThreadRef, { kind: "device", ...activeRightPanelSurface.target });
2633-
}
2634-
useRightPanelStore.getState().setMaximized(activeThreadRef, false);
2635-
useRightPanelStore.getState().close(activeThreadRef);
2636-
}
2637-
};
2638-
const closePreviewPanelStable = useStableCallback(closePreviewPanel);
2639-
const togglePreviewPanel = () => {
2640-
if (!activeThreadRef || !browserAvailable) return;
2641-
if (previewPanelOpen) {
2642-
closePreviewPanel();
2643-
return;
2644-
}
2645-
if (!canOperatePreview) return;
2646-
const activeTabId = activePreviewState.activeTabId;
2647-
if (activeTabId) {
2648-
useRightPanelStore.getState().openBrowser(activeThreadRef, activeTabId);
2649-
} else {
2650-
createBrowserSurface();
2651-
}
2652-
};
2653-
const togglePreviewPanelStable = useStableCallback(togglePreviewPanel);
2654-
const activateRightPanelSurface = (surface: RightPanelSurface) => {
2655-
if (!activeThreadRef) return;
2656-
explicitDiffOpenRef.current = resolveExplicitDiffOpenOnActivate({
2657-
surfaceKind: surface.kind,
2658-
diffOpen,
2659-
activeThreadRef,
2660-
});
2661-
useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
2662-
if (surface.kind === "preview" && surface.resourceId) {
2663-
setActivePreviewTab(activeThreadRef, surface.resourceId);
2664-
}
2665-
if (surface.kind === "terminal") {
2666-
setTerminalFocusRequestId((value) => value + 1);
2667-
}
2668-
if (surface.kind === "diff" && !diffOpen) {
2669-
onDiffPanelOpen?.();
2670-
}
2671-
};
2672-
const activateRightPanelSurfaceStable = useStableCallback(activateRightPanelSurface);
2673-
const toggleRightPanel = () => {
2674-
if (!activeThreadRef) return;
2675-
if (rightPanelOpen) {
2676-
closePreviewPanel();
2677-
return;
2678-
}
2679-
useRightPanelStore.getState().toggleVisibility(activeThreadRef);
2680-
};
2681-
const toggleRightPanelStable = useStableCallback(toggleRightPanel);
2682-
const toggleThreadPanel = () => {
2683-
if (!activeThreadRef) return;
2684-
useRightPanelStore.getState().toggleThreadPanel(activeThreadRef, threadPanelPresentation);
2685-
};
2686-
const toggleThreadPanelStable = useStableCallback(toggleThreadPanel);
26872541
useEffect(() => {
26882542
if (!canMaximizeRightPanel) return;
26892543
if (useRightPanelStore.getState().consumeMaximizeRequest(routeThreadRef)) {
26902544
useRightPanelStore.getState().setMaximized(routeThreadRef, true);
26912545
}
26922546
}, [canMaximizeRightPanel, routeThreadRef]);
2693-
const toggleRightPanelMaximized = () => {
2694-
if (!canMaximizeRightPanel || !activeThreadRef) return;
2695-
useRightPanelStore.getState().setMaximized(activeThreadRef, !rightPanelMaximized);
2696-
};
2697-
const toggleRightPanelMaximizedStable = useStableCallback(toggleRightPanelMaximized);
26982547
const {
26992548
closeSurface: closeRightPanelSurface,
27002549
closeOtherSurfaces: closeOtherRightPanelSurfacesStable,
@@ -2705,54 +2554,6 @@ export default function ChatView(props: ChatViewProps) {
27052554
terminalLabelsById: activeTerminalLabelsById,
27062555
});
27072556
const closeRightPanelSurfaceStable = closeRightPanelSurface;
2708-
const moveRightPanelSurface = (surfaceId: string, toIndex: number) => {
2709-
if (activeThreadRef)
2710-
useRightPanelStore.getState().moveSurface(activeThreadRef, surfaceId, toIndex);
2711-
};
2712-
const moveRightPanelSurfaceStable = useStableCallback(moveRightPanelSurface);
2713-
const copyRightPanelFilePath = (relativePath: string) => {
2714-
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
2715-
toastManager.add(
2716-
stackedThreadToast({
2717-
type: "error",
2718-
title: "Failed to copy path",
2719-
description: "Clipboard API unavailable.",
2720-
}),
2721-
);
2722-
return;
2723-
}
2724-
2725-
const clipboardPath = formatFilePathForClipboard(
2726-
relativePath,
2727-
resolvePathLinkTarget(relativePath, gitCwd ?? activeProjectCwd ?? ""),
2728-
);
2729-
void navigator.clipboard.writeText(clipboardPath).then(
2730-
() => {
2731-
toastManager.add({
2732-
type: "success",
2733-
title: "Path copied",
2734-
description: clipboardPath,
2735-
});
2736-
},
2737-
(error) => {
2738-
toastManager.add(
2739-
stackedThreadToast({
2740-
type: "error",
2741-
title: "Failed to copy path",
2742-
description: error instanceof Error ? error.message : "An error occurred.",
2743-
}),
2744-
);
2745-
},
2746-
);
2747-
};
2748-
const copyRightPanelFilePathStable = useStableCallback(copyRightPanelFilePath);
2749-
useEffect(
2750-
() =>
2751-
subscribePreviewAction((action) => {
2752-
if (action === "toggle-panel") togglePreviewPanelStable();
2753-
}),
2754-
[togglePreviewPanelStable],
2755-
);
27562557
const persistThreadSettingsForNextTurn = async (input: {
27572558
threadId: ThreadId;
27582559
createdAt: string;
@@ -3171,12 +2972,50 @@ export default function ChatView(props: ChatViewProps) {
31712972
branchPullRequest: activeThreadShell?.branchPullRequest ?? activeThread.branchPullRequest,
31722973
})
31732974
: null;
3174-
const addPullRequestSurface = () => {
3175-
if (!supportsPullRequests || activeThreadRef === null || pullRequestPanelTarget === null)
3176-
return;
3177-
useRightPanelStore.getState().openPullRequest(activeThreadRef, pullRequestPanelTarget);
3178-
};
3179-
const addPullRequestSurfaceStable = useStableCallback(addPullRequestSurface);
2975+
const {
2976+
activateRightPanelSurfaceStable,
2977+
addDeviceSurfaceStable,
2978+
addDiffSurfaceStable,
2979+
addFilesSurfaceStable,
2980+
addPullRequestSurfaceStable,
2981+
addPullRequestsSurfaceStable,
2982+
closePreviewPanelStable,
2983+
copyRightPanelFilePathStable,
2984+
createBrowserSurface,
2985+
createBrowserSurfaceStable,
2986+
moveRightPanelSurfaceStable,
2987+
onOpenTurnDiffStable,
2988+
onToggleDiffStable,
2989+
openChangesFromThreadPanelStable,
2990+
openFileSurfaceStable,
2991+
toggleRightPanelMaximizedStable,
2992+
toggleRightPanelStable,
2993+
toggleThreadPanelStable,
2994+
} = useRightPanelActions({
2995+
activeThreadRef,
2996+
activeProject,
2997+
activeRightPanelSurface,
2998+
activePreviewState,
2999+
deviceState,
3000+
pullRequestPanelTarget,
3001+
threadPanelPresentation,
3002+
isServerThread,
3003+
isGitRepo,
3004+
browserAvailable,
3005+
canOperatePreview,
3006+
diffOpen,
3007+
previewPanelOpen,
3008+
rightPanelOpen,
3009+
canMaximizeRightPanel,
3010+
rightPanelMaximized,
3011+
supportsPullRequests,
3012+
pullRequestsSurfaceAvailable,
3013+
gitCwd,
3014+
activeProjectCwd,
3015+
explicitDiffOpenRef,
3016+
setTerminalFocusRequestId,
3017+
onDiffPanelOpen,
3018+
});
31803019
const pullRequestSurfaceAvailable = supportsPullRequests && pullRequestPanelTarget !== null;
31813020
const supportsSettlement = serverConfig?.environment.capabilities.threadSettlement === true;
31823021
const supportsSnooze = serverConfig?.environment.capabilities.threadSnooze === true;
@@ -4462,16 +4301,6 @@ export default function ChatView(props: ChatViewProps) {
44624301
openExpandedImagePreview(routeThreadKey, preview);
44634302
};
44644303
const onExpandTimelineImageStable = useStableCallback(onExpandTimelineImage);
4465-
const onOpenTurnDiff = (runId: RunId, filePath?: string) => {
4466-
if (!isServerThread || !activeThreadRef) return;
4467-
const diffOpen =
4468-
selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
4469-
explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
4470-
useDiffPanelStore.getState().selectTurn(activeThreadRef, runId, filePath);
4471-
useRightPanelStore.getState().open(activeThreadRef, "diff");
4472-
onDiffPanelOpen?.();
4473-
};
4474-
const onOpenTurnDiffStable = useStableCallback(onOpenTurnDiff);
44754304
const onTimelineRollbackCheckpoint = (input: {
44764305
readonly checkpointId: string;
44774306
readonly scopeId: string;

0 commit comments

Comments
 (0)