Skip to content
TypeScript

refactor(web): move the right panel's surface switch out of ChatView (#20421)

Julius Marmingef5cb328aac19
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index ac4cd781825..e9efaa8de12 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -11,6 +11,7 @@ export const REACT_COMPILED_FILES = [
"apps/web/src/components/chat/ChatComposer.tsx",
"apps/web/src/components/chat/usePendingThreadRequests.ts",
"apps/web/src/components/chat/MessagesTimeline.tsx",
+ "apps/web/src/components/chat/RightPanelSurfaceContent.tsx",
"apps/web/src/components/chat/composerFocusRedirect.ts",
"apps/web/src/components/chat/useChatShortcuts.ts",
"apps/web/src/components/chat/useProjectScriptEditing.ts",
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index eb99bd26f58..c6b4d37dfa6 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -26,6 +26,7 @@ import { useLoadBalancedEnvironment } from "../hooks/useLoadBalancedEnvironment"
import { useScratchProject } from "../hooks/useScratchProject";
import { useStableCallback } from "../hooks/useStableCallback";
import { useProjectScriptEditing } from "./chat/useProjectScriptEditing";
+import { RightPanelSurfaceContent } from "./chat/RightPanelSurfaceContent";
import { useThreadTerminalActions } from "./terminal/useThreadTerminalActions";
import { usePendingThreadRequests } from "./chat/usePendingThreadRequests";
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
@@ -141,9 +142,7 @@ import { useAtomValue } from "@effect/atom-react";
import { Atom } from "effect/reactivity";
import {
Fragment,
- lazy,
type SetStateAction,
- Suspense,
useEffect,
useEffectEvent,
useLayoutEffect,
@@ -249,16 +248,9 @@ import {
selectThreadPreviewMiniPlayer,
usePreviewMiniPlayerStore,
} from "../previewMiniPlayerStore";
-import {
- pullRequestPanelContext,
- threadPullRequestPanelTarget,
-} from "./pullRequest/pullRequestDetail.logic";
-import { PullRequestDetailPanel } from "./pullRequest/PullRequestDetailPanel";
-import { PullRequestDetailGhost } from "./pullRequest/PullRequestGhosts";
-import { PullRequestsUnavailableState } from "./pullRequest/PullRequestsUnavailableState";
+import { threadPullRequestPanelTarget } from "./pullRequest/pullRequestDetail.logic";
import { RightPanelTabs } from "./RightPanelTabs";
import { LinkPullRequestDialogHost } from "./pullRequest/LinkPullRequestDialog";
-import { ThreadPullRequestsPanel } from "./pullRequest/ThreadPullRequestsPanel";
import { useDeviceState } from "~/state/device";
import { DeviceSetup } from "./device/DeviceSetup";
import { Dialog } from "./ui/dialog";
@@ -268,10 +260,7 @@ import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
import { isEditableFocused } from "../lib/editableFocus";
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
import { shortcutLabelForCommand } from "../keybindings";
-import {
- PersistentThreadTerminalDrawer,
- PersistentThreadTerminalPanel,
-} from "./PersistentThreadTerminals";
+import { PersistentThreadTerminalDrawer } from "./PersistentThreadTerminals";
import {
AlarmClockIcon,
CheckCircle2Icon,
@@ -638,14 +627,6 @@ function useDraftHeroLayoutTransition(
return { transitionGroupRef, composerAnchorRef, captureComposerRect } as const;
}
-const PreviewPanel = lazy(() =>
- import("./preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
-);
-const DiffPanel = lazy(() => import("./DiffPanel"));
-const DevicePanel = lazy(() =>
- import("./device/DevicePanel").then((module) => ({ default: module.DevicePanel })),
-);
-const FilePreviewPanel = lazy(() => import("./files/FilePreviewPanel"));
const EMPTY_PENDING_FILE_SURFACE_IDS: ReadonlySet<string> = new Set();
type EnvironmentUnavailableState = {
readonly environmentId: EnvironmentId;
@@ -7616,6 +7597,13 @@ export default function ChatView(props: ChatViewProps) {
};
const onWorktreeSetupWorkLocallyStable = useStableCallback(onWorktreeSetupWorkLocally);
const onComposerSendStable = useStableCallback(onSend);
+ const sendPreviewAnnotation = (
+ annotation: PreviewAnnotationPayload,
+ image: ComposerImageAttachment | null,
+ ) => {
+ void onSend(undefined, "auto", "foreground", { annotation, image });
+ };
+ const sendPreviewAnnotationStable = useStableCallback(sendPreviewAnnotation);
const onComposerResumeStable = useStableCallback(onResume);
const onComposerCompactContextStable = useStableCallback(onCompactContext);
const queuedRunsControl =
@@ -7768,169 +7756,55 @@ export default function ChatView(props: ChatViewProps) {
}
const rightPanelContent = activeThreadRef ? (
- renderedRightPanelSurface?.kind === "preview" ? (
- <Suspense fallback={null}>
- <PreviewPanel
- mode="embedded"
- threadRef={activeThreadRef}
- tabId={renderedRightPanelSurface.resourceId}
- configuredUrls={configuredPreviewUrls}
- visible={rightPanelOpen}
- onSendAnnotation={(annotation, image) => {
- void onSend(undefined, "auto", "foreground", { annotation, image });
- }}
- />
- </Suspense>
- ) : renderedRightPanelSurface?.kind === "terminal" ? (
- <PersistentThreadTerminalPanel
- visible={rightPanelOpen}
- threadRef={activeThreadRef}
- surface={renderedRightPanelSurface}
- launchContext={activeTerminalLaunchContext ?? null}
- focusRequestId={terminalFocusRequestId}
- keybindings={keybindings}
- onAddTerminalContext={addTerminalContextToDraftStable}
- onSplitTerminal={splitPanelTerminalStable}
- onSplitTerminalVertical={splitPanelTerminalVerticalStable}
- onNewTerminal={addTerminalSurfaceStable}
- onActiveTerminalChange={activatePanelTerminalStable}
- onCloseTerminal={closePanelTerminalStable}
- splitShortcutLabel={splitTerminalShortcutLabel ?? undefined}
- splitVerticalShortcutLabel={splitTerminalVerticalShortcutLabel ?? undefined}
- newShortcutLabel={newTerminalShortcutLabel ?? undefined}
- closeShortcutLabel={closeTerminalShortcutLabel ?? undefined}
- />
- ) : renderedRightPanelSurface?.kind === "diff" ? (
- <Suspense fallback={null}>
- <DiffPanel
- key={activeThreadKey}
- mode="embedded"
- composerDraftTarget={composerDraftTarget}
- workspaceMutationId={workspaceMutationId}
- />
- </Suspense>
- ) : renderedRightPanelSurface?.kind === "pull-request" && !pullRequestsCapabilityKnown ? (
- <PullRequestDetailGhost />
- ) : renderedRightPanelSurface?.kind === "pull-request" && !supportsPullRequests ? (
- <PullRequestsUnavailableState
- title="Pull requests unavailable"
- error="Update this environment's T3 Code server to browse pull requests."
- />
- ) : renderedRightPanelSurface?.kind === "pull-request" ? (
- <PullRequestDetailPanel
- getShortcutContext={getShortcutContextStable}
- shortcutsEnabled={
- rightPanelOpen && activeRightPanelSurface?.id === renderedRightPanelSurface.id
- }
- key={`${renderedRightPanelSurface.host ?? ""}:${renderedRightPanelSurface.repository}#${renderedRightPanelSurface.number}`}
- environmentId={activeThread.environmentId}
- onSelectPullRequest={(reference) => {
- if (activeThreadRef)
- useRightPanelStore.getState().openPullRequest(activeThreadRef, {
- projectId: reference.projectId,
- repository: reference.repository,
- number: reference.number,
- ...(reference.host ? { host: reference.host } : {}),
- });
- }}
- threadRef={activeThreadRef}
- reference={{
- projectId: renderedRightPanelSurface.projectId as ProjectId,
- ...(renderedRightPanelSurface.host ? { host: renderedRightPanelSurface.host } : {}),
- repository: renderedRightPanelSurface.repository,
- number: renderedRightPanelSurface.number,
- }}
- context={pullRequestPanelContext(
- {
- projectId: activeThread.projectId,
- pullRequests: visiblePullRequests,
- linkedPullRequest: linkedThreadPullRequest,
- branchPullRequest:
- activeThreadShell?.branchPullRequest ?? activeThread.branchPullRequest,
- },
- renderedRightPanelSurface,
- )}
- composerDraftTarget={composerDraftTarget}
- onBack={
- activeThreadRef !== null && pullRequestsSurfaceAvailable && visiblePullRequestCount > 1
+ <RightPanelSurfaceContent
+ surface={renderedRightPanelSurface}
+ activeSurfaceId={activeRightPanelSurface?.id ?? null}
+ threadRef={activeThreadRef}
+ environmentId={activeThread.environmentId}
+ visible={rightPanelOpen}
+ composerDraftTarget={composerDraftTarget}
+ keybindings={keybindings}
+ configuredPreviewUrls={configuredPreviewUrls}
+ workspaceMutationId={workspaceMutationId}
+ getShortcutContext={getShortcutContextStable}
+ onSendAnnotation={sendPreviewAnnotationStable}
+ onCloseSurface={closeRightPanelSurface}
+ terminal={{
+ launchContext: activeTerminalLaunchContext ?? null,
+ focusRequestId: terminalFocusRequestId,
+ keybindings,
+ onAddTerminalContext: addTerminalContextToDraftStable,
+ onSplitTerminal: splitPanelTerminalStable,
+ onSplitTerminalVertical: splitPanelTerminalVerticalStable,
+ onNewTerminal: addTerminalSurfaceStable,
+ onActiveTerminalChange: activatePanelTerminalStable,
+ onCloseTerminal: closePanelTerminalStable,
+ splitShortcutLabel: splitTerminalShortcutLabel ?? undefined,
+ splitVerticalShortcutLabel: splitTerminalVerticalShortcutLabel ?? undefined,
+ newShortcutLabel: newTerminalShortcutLabel ?? undefined,
+ closeShortcutLabel: closeTerminalShortcutLabel ?? undefined,
+ }}
+ pullRequest={{
+ capabilityKnown: pullRequestsCapabilityKnown,
+ supported: supportsPullRequests,
+ projectId: activeThread.projectId,
+ pullRequests: visiblePullRequests,
+ linkedPullRequest: linkedThreadPullRequest,
+ branchPullRequest: activeThreadShell?.branchPullRequest ?? activeThread.branchPullRequest,
+ onBack:
+ pullRequestsSurfaceAvailable && visiblePullRequestCount > 1
? addPullRequestsSurfaceStable
- : undefined
- }
- />
- ) : renderedRightPanelSurface?.kind === "pull-requests" && activeThreadRef ? (
- <ThreadPullRequestsPanel threadRef={activeThreadRef} />
- ) : renderedRightPanelSurface?.kind === "device" ? (
- <Suspense fallback={null}>
- <DevicePanel
- mode="embedded"
- threadRef={activeThreadRef}
- key={renderedRightPanelSurface.id}
- surface={renderedRightPanelSurface}
- visible={rightPanelOpen}
- onDismissSetup={() => {
- closeRightPanelSurface(renderedRightPanelSurface);
- useRightPanelStore.getState().show(activeThreadRef);
- }}
- />
- </Suspense>
- ) : (renderedRightPanelSurface?.kind === "files" ||
- renderedRightPanelSurface?.kind === "file") &&
- ((activeProject && activeWorkspaceRoot) ||
- (renderedRightPanelSurface.kind === "file" && renderedRightPanelSurface.attachment)) ? (
- <Suspense fallback={null}>
- <FilePreviewPanel
- key={`${activeThread.environmentId}:${
- renderedRightPanelSurface.kind === "file" && renderedRightPanelSurface.attachment
- ? `attachment:${renderedRightPanelSurface.attachment.id}`
- : activeWorkspaceRoot
- }`}
- environmentId={activeThread.environmentId}
- cwd={activeWorkspaceRoot ?? ""}
- projectName={activeProject?.title ?? ""}
- threadRef={activeThreadRef}
- composerDraftTarget={composerDraftTarget}
- keybindings={keybindings}
- availableEditors={availableEditors}
- relativePath={
- renderedRightPanelSurface.kind === "file"
- ? renderedRightPanelSurface.relativePath
- : null
- }
- {...(renderedRightPanelSurface.kind === "file" && renderedRightPanelSurface.attachment
- ? { attachment: renderedRightPanelSurface.attachment }
- : {})}
- revealLine={
- renderedRightPanelSurface.kind === "file"
- ? (renderedRightPanelSurface.revealLine ?? null)
- : null
- }
- revealRequestId={
- renderedRightPanelSurface.kind === "file"
- ? renderedRightPanelSurface.revealRequestId
- : 0
- }
- onOpenFile={openFileSurfaceStable}
- onNavigateFolder={(relativePath, folderTrail, keepSource) =>
- useRightPanelStore
- .getState()
- .navigateFolder(
- activeThreadRef,
- renderedRightPanelSurface.id,
- relativePath,
- folderTrail,
- keepSource,
- )
- }
- folderTrail={renderedRightPanelSurface.folderTrail}
- onPendingChange={handleFilePendingChangeStable}
- selectedFilePending={
- renderedRightPanelSurface.kind === "file" &&
- pendingFileSurfaceIds.has(renderedRightPanelSurface.id)
- }
- workspaceMutationId={workspaceMutationId}
- />
- </Suspense>
- ) : null
+ : undefined,
+ }}
+ files={{
+ projectName: activeProject?.title ?? null,
+ workspaceRoot: activeWorkspaceRoot,
+ availableEditors,
+ pendingSurfaceIds: pendingFileSurfaceIds,
+ onOpenFile: openFileSurfaceStable,
+ onPendingChange: handleFilePendingChangeStable,
+ }}
+ />
) : null;
const threadDetailsPanelProps: ThreadDetailsPanelProps = {
anchor: threadPanelPopoverAnchorRef,
diff --git a/apps/web/src/components/PersistentThreadTerminals.tsx b/apps/web/src/components/PersistentThreadTerminals.tsx
index 7e539e22995..bdacfc53225 100644
--- a/apps/web/src/components/PersistentThreadTerminals.tsx
+++ b/apps/web/src/components/PersistentThreadTerminals.tsx
@@ -293,7 +293,7 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
);
});
-interface PersistentThreadTerminalPanelProps {
+export interface PersistentThreadTerminalPanelProps {
visible: boolean;
threadRef: ScopedThreadRef;
surface: Extract<RightPanelSurface, { kind: "terminal" }>;
diff --git a/apps/web/src/components/chat/RightPanelSurfaceContent.tsx b/apps/web/src/components/chat/RightPanelSurfaceContent.tsx
new file mode 100644
index 00000000000..2fc588777a7
--- /dev/null
+++ b/apps/web/src/components/chat/RightPanelSurfaceContent.tsx
@@ -0,0 +1,218 @@
+import type {
+ EditorId,
+ EnvironmentId,
+ PreviewAnnotationPayload,
+ ProjectId,
+ ResolvedKeybindingsConfig,
+ ScopedThreadRef,
+ ThreadLinkedPullRequest,
+ ThreadPullRequestLink,
+} from "@t3tools/contracts";
+import { scopedThreadKey } from "@t3tools/client-runtime/environment";
+import { lazy, Suspense } from "react";
+import type { ComposerImageAttachment, DraftId } from "../../composerDraftStore";
+import type { ShortcutMatchContext } from "../../keybindings";
+import { type RightPanelSurface, useRightPanelStore } from "../../rightPanelStore";
+import {
+ PersistentThreadTerminalPanel,
+ type PersistentThreadTerminalPanelProps,
+} from "../PersistentThreadTerminals";
+import { pullRequestPanelContext } from "../pullRequest/pullRequestDetail.logic";
+import { PullRequestDetailPanel } from "../pullRequest/PullRequestDetailPanel";
+import { PullRequestDetailGhost } from "../pullRequest/PullRequestGhosts";
+import { PullRequestsUnavailableState } from "../pullRequest/PullRequestsUnavailableState";
+import { ThreadPullRequestsPanel } from "../pullRequest/ThreadPullRequestsPanel";
+
+const PreviewPanel = lazy(() =>
+ import("../preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
+);
+const DiffPanel = lazy(() => import("../DiffPanel"));
+const DevicePanel = lazy(() =>
+ import("../device/DevicePanel").then((module) => ({ default: module.DevicePanel })),
+);
+const FilePreviewPanel = lazy(() => import("../files/FilePreviewPanel"));
+
+export type RightPanelTerminalProps = Omit<
+ PersistentThreadTerminalPanelProps,
+ "visible" | "threadRef" | "surface"
+>;
+
+export interface RightPanelPullRequestProps {
+ readonly capabilityKnown: boolean;
+ readonly supported: boolean;
+ readonly projectId: ProjectId | null;
+ readonly pullRequests: ReadonlyArray<ThreadPullRequestLink>;
+ readonly linkedPullRequest: ThreadLinkedPullRequest | null;
+ readonly branchPullRequest: ThreadLinkedPullRequest | null | undefined;
+ /** Back to the pull request list; absent when there is no list to go back to. */
+ readonly onBack: (() => void) | undefined;
+}
+
+export interface RightPanelFileProps {
+ readonly projectName: string | null;
+ readonly workspaceRoot: string | undefined;
+ readonly availableEditors: ReadonlyArray<EditorId>;
+ readonly pendingSurfaceIds: ReadonlySet<string>;
+ readonly onOpenFile: (relativePath: string) => void;
+ readonly onPendingChange: (relativePath: string, pending: boolean) => void;
+}
+
+/** The body of the thread's right panel for the surface it is showing. */
+export function RightPanelSurfaceContent(props: {
+ readonly surface: RightPanelSurface | null;
+ readonly activeSurfaceId: string | null;
+ readonly threadRef: ScopedThreadRef;
+ readonly environmentId: EnvironmentId;
+ readonly visible: boolean;
+ readonly composerDraftTarget: ScopedThreadRef | DraftId;
+ readonly keybindings: ResolvedKeybindingsConfig;
+ readonly configuredPreviewUrls: ReadonlyArray<string>;
+ readonly workspaceMutationId: string | null;
+ readonly getShortcutContext: () => ShortcutMatchContext;
+ readonly onSendAnnotation: (
+ annotation: PreviewAnnotationPayload,
+ image: ComposerImageAttachment | null,
+ ) => void;
+ readonly onCloseSurface: (surface: RightPanelSurface) => void;
+ readonly terminal: RightPanelTerminalProps;
+ readonly pullRequest: RightPanelPullRequestProps;
+ readonly files: RightPanelFileProps;
+}) {
+ const { surface, threadRef, visible } = props;
+ if (surface === null) return null;
+ switch (surface.kind) {
+ case "preview":
+ return (
+ <Suspense fallback={null}>
+ <PreviewPanel
+ mode="embedded"
+ threadRef={threadRef}
+ tabId={surface.resourceId}
+ configuredUrls={props.configuredPreviewUrls}
+ visible={visible}
+ onSendAnnotation={props.onSendAnnotation}
+ />
+ </Suspense>
+ );
+ case "terminal":
+ return (
+ <PersistentThreadTerminalPanel
+ {...props.terminal}
+ visible={visible}
+ threadRef={threadRef}
+ surface={surface}
+ />
+ );
+ case "diff":
+ return (
+ <Suspense fallback={null}>
+ <DiffPanel
+ key={scopedThreadKey(threadRef)}
+ mode="embedded"
+ composerDraftTarget={props.composerDraftTarget}
+ workspaceMutationId={props.workspaceMutationId}
+ />
+ </Suspense>
+ );
+ case "pull-request": {
+ const pullRequest = props.pullRequest;
+ if (!pullRequest.capabilityKnown) return <PullRequestDetailGhost />;
+ if (!pullRequest.supported) {
+ return (
+ <PullRequestsUnavailableState
+ title="Pull requests unavailable"
+ error="Update this environment's T3 Code server to browse pull requests."
+ />
+ );
+ }
+ return (
+ <PullRequestDetailPanel
+ getShortcutContext={props.getShortcutContext}
+ shortcutsEnabled={visible && props.activeSurfaceId === surface.id}
+ key={`${surface.host ?? ""}:${surface.repository}#${surface.number}`}
+ environmentId={props.environmentId}
+ onSelectPullRequest={(reference) => {
+ useRightPanelStore.getState().openPullRequest(threadRef, {
+ projectId: reference.projectId,
+ repository: reference.repository,
+ number: reference.number,
+ ...(reference.host ? { host: reference.host } : {}),
+ });
+ }}
+ threadRef={threadRef}
+ reference={{
+ projectId: surface.projectId as ProjectId,
+ ...(surface.host ? { host: surface.host } : {}),
+ repository: surface.repository,
+ number: surface.number,
+ }}
+ context={pullRequestPanelContext(
+ {
+ projectId: pullRequest.projectId,
+ pullRequests: pullRequest.pullRequests,
+ linkedPullRequest: pullRequest.linkedPullRequest,
+ branchPullRequest: pullRequest.branchPullRequest,
+ },
+ surface,
+ )}
+ composerDraftTarget={props.composerDraftTarget}
+ onBack={pullRequest.onBack}
+ />
+ );
+ }
+ case "pull-requests":
+ return <ThreadPullRequestsPanel threadRef={threadRef} />;
+ case "device":
+ return (
+ <Suspense fallback={null}>
+ <DevicePanel
+ mode="embedded"
+ threadRef={threadRef}
+ key={surface.id}
+ surface={surface}
+ visible={visible}
+ onDismissSetup={() => {
+ props.onCloseSurface(surface);
+ useRightPanelStore.getState().show(threadRef);
+ }}
+ />
+ </Suspense>
+ );
+ case "files":
+ case "file": {
+ const files = props.files;
+ const attachment = surface.kind === "file" ? surface.attachment : undefined;
+ if (!attachment && (files.projectName === null || !files.workspaceRoot)) return null;
+ return (
+ <Suspense fallback={null}>
+ <FilePreviewPanel
+ key={`${props.environmentId}:${
+ attachment ? `attachment:${attachment.id}` : files.workspaceRoot
+ }`}
+ environmentId={props.environmentId}
+ cwd={files.workspaceRoot ?? ""}
+ projectName={files.projectName ?? ""}
+ threadRef={threadRef}
+ composerDraftTarget={props.composerDraftTarget}
+ keybindings={props.keybindings}
+ availableEditors={files.availableEditors}
+ relativePath={surface.kind === "file" ? surface.relativePath : null}
+ {...(attachment ? { attachment } : {})}
+ revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}
+ revealRequestId={surface.kind === "file" ? surface.revealRequestId : 0}
+ onOpenFile={files.onOpenFile}
+ onNavigateFolder={(relativePath, folderTrail, keepSource) =>
+ useRightPanelStore
+ .getState()
+ .navigateFolder(threadRef, surface.id, relativePath, folderTrail, keepSource)
+ }
+ folderTrail={surface.folderTrail}
+ onPendingChange={files.onPendingChange}
+ selectedFilePending={surface.kind === "file" && files.pendingSurfaceIds.has(surface.id)}
+ workspaceMutationId={props.workspaceMutationId}
+ />
+ </Suspense>
+ );
+ }
+ }
+}