Skip to content
TypeScript

refactor(web): ChatView's pull request checkout lives in usePullRequestDraftThread (#23070)

Julius Marminge114f77fd0563
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index e8f0cdb071a..bd9ff5cfe4b 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -1,3 +1,4 @@
+import { usePullRequestDraftThread } from "./chat/usePullRequestDraftThread";
import { useComposerSelection } from "./chat/useComposerSelection";
import { useProjectCloneBanner } from "./chat/useProjectCloneBanner";
import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";
@@ -183,7 +184,7 @@ import { cn } from "~/lib/utils";
import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
import { stackedThreadToast, toastManager } from "./ui/toast";
import { projectScriptIdFromCommand } from "~/projectScripts";
-import { newDraftId, newMessageId, newThreadId } from "~/lib/utils";
+import { newMessageId } from "~/lib/utils";
import { registerFaviconProjectForThread } from "~/browserFaviconStore";
import {
applyProviderInstanceSettings,
@@ -207,7 +208,6 @@ import {
deriveLogicalProjectKeyFromSettings,
selectProjectGroupingSettings,
} from "../logicalProject";
-import { buildDraftThreadRouteParams } from "../threadRoutes";
import {
isSameSidebarThreadRef,
useSidebarPendingFileDropStore,
@@ -259,7 +259,6 @@ import {
BranchRestoreConfirmHost,
DeviceSetupDialogHost,
openBranchRestoreConfirm,
- openPullRequestThreadDialog,
PullRequestThreadDialogHost,
RevertConfirmHost,
} from "./chat/ChatOverlayHosts";
@@ -635,13 +634,6 @@ export default function ChatView(props: ChatViewProps) {
});
const setComposerDraftPrompt = useComposerDraftStore((store) => store.setPrompt);
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
- const getDraftSessionByLogicalProjectKey = useComposerDraftStore(
- (store) => store.getDraftSessionByLogicalProjectKey,
- );
- const getDraftSession = useComposerDraftStore((store) => store.getDraftSession);
- const setLogicalProjectDraftThreadId = useComposerDraftStore(
- (store) => store.setLogicalProjectDraftThreadId,
- );
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
const composerFilesRef = useRef<ComposerFileAttachment[]>([]);
const composerTerminalContextsRef = useRef<TerminalContextDraft[]>([]);
@@ -1198,91 +1190,18 @@ export default function ChatView(props: ChatViewProps) {
activeEnvironment: activeEnvironmentOption,
canPickEnvironment: hasMultipleEnvironments,
});
- const openPullRequestDialog = (reference?: string) => {
- if (!canCheckoutPullRequestIntoThread) {
- return;
- }
- if (activeThreadKey === null) return;
- openPullRequestThreadDialog(activeThreadKey, reference);
- };
- const openPullRequestDialogStable = useStableCallback(openPullRequestDialog);
- const openOrReuseProjectDraftThread = async (input: {
- branch: string;
- worktreePath: string | null;
- envMode: DraftThreadEnvMode;
- }) => {
- if (!activeProject) {
- throw new Error("No active project is available for this pull request.");
- }
- const activeProjectRef = scopeProjectRef(activeProject.environmentId, activeProject.id);
- const logicalProjectKey = deriveLogicalProjectKeyFromSettings(
- activeProject,
+ const { handlePreparedPullRequestThreadStable, openPullRequestDialogStable } =
+ usePullRequestDraftThread({
+ routeKind,
+ draftId,
+ settings,
projectGroupingSettings,
- );
- const storedDraftSession = getDraftSessionByLogicalProjectKey(logicalProjectKey);
- if (storedDraftSession) {
- setDraftThreadContext(storedDraftSession.draftId, input);
- setLogicalProjectDraftThreadId(
- logicalProjectKey,
- activeProjectRef,
- storedDraftSession.draftId,
- {
- threadId: storedDraftSession.threadId,
- ...input,
- },
- );
- if (routeKind !== "draft" || draftId !== storedDraftSession.draftId) {
- await navigate({
- to: "/draft/$draftId",
- params: buildDraftThreadRouteParams(storedDraftSession.draftId),
- });
- }
- return storedDraftSession.threadId;
- }
-
- const activeDraftSession = routeKind === "draft" && draftId ? getDraftSession(draftId) : null;
- if (!isServerThread && activeDraftSession?.logicalProjectKey === logicalProjectKey && draftId) {
- setDraftThreadContext(draftId, input);
- setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, draftId, {
- threadId: activeDraftSession.threadId,
- createdAt: activeDraftSession.createdAt,
- runtimeMode: activeDraftSession.runtimeMode,
- interactionMode: activeDraftSession.interactionMode,
- ...input,
- });
- return activeDraftSession.threadId;
- }
-
- const nextDraftId = newDraftId();
- const nextThreadId = newThreadId();
- setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, nextDraftId, {
- threadId: nextThreadId,
- createdAt: new Date().toISOString(),
- runtimeMode: resolveProjectSettings(settings, activeProject.id, activeProject).settings
- .defaultRuntimeMode,
- interactionMode: DEFAULT_INTERACTION_MODE,
- ...input,
- });
- await navigate({
- to: "/draft/$draftId",
- params: buildDraftThreadRouteParams(nextDraftId),
- });
- return nextThreadId;
- };
-
- const handlePreparedPullRequestThread = async (input: {
- branch: string;
- worktreePath: string | null;
- }) => {
- await openOrReuseProjectDraftThread({
- branch: input.branch,
- worktreePath: input.worktreePath,
- envMode: input.worktreePath ? "worktree" : "local",
+ activeProject,
+ activeThreadKey,
+ isServerThread,
+ canCheckoutPullRequestIntoThread,
});
- };
- const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread);
-
const serverConfig = activeThread
? (activeEnvironment?.serverConfig ?? null)
: (primaryEnvironment?.serverConfig ?? null);
diff --git a/apps/web/src/components/chat/usePullRequestDraftThread.ts b/apps/web/src/components/chat/usePullRequestDraftThread.ts
new file mode 100644
index 00000000000..63a234556b3
--- /dev/null
+++ b/apps/web/src/components/chat/usePullRequestDraftThread.ts
@@ -0,0 +1,140 @@
+import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
+import type { UnifiedSettings } from "@t3tools/contracts";
+import { useStableCallback } from "../../hooks/useStableCallback";
+import { scopeProjectRef } from "@t3tools/client-runtime/environment";
+import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
+import { useNavigate } from "@tanstack/react-router";
+import { DEFAULT_INTERACTION_MODE } from "../../types";
+import { newDraftId, newThreadId } from "~/lib/utils";
+import {
+ deriveLogicalProjectKeyFromSettings,
+ selectProjectGroupingSettings,
+} from "../../logicalProject";
+import { buildDraftThreadRouteParams } from "../../threadRoutes";
+import { type DraftThreadEnvMode, useComposerDraftStore, DraftId } from "../../composerDraftStore";
+import { openPullRequestThreadDialog } from "./ChatOverlayHosts";
+interface PullRequestDraftThreadInput {
+ readonly routeKind: "server" | "draft";
+ readonly draftId: DraftId | null;
+ readonly settings: UnifiedSettings;
+ readonly projectGroupingSettings: ReturnType<typeof selectProjectGroupingSettings>;
+ readonly activeProject: EnvironmentProject | null;
+ readonly activeThreadKey: string | null;
+ readonly isServerThread: boolean;
+ readonly canCheckoutPullRequestIntoThread: boolean;
+}
+
+/**
+ * Checking out a pull request into a thread: opens the dialog, then puts the prepared branch or
+ * worktree on the project's draft thread, reusing the one already open when there is one.
+ */
+export function usePullRequestDraftThread(input: PullRequestDraftThreadInput) {
+ const {
+ routeKind,
+ draftId,
+ settings,
+ projectGroupingSettings,
+ activeProject,
+ activeThreadKey,
+ isServerThread,
+ canCheckoutPullRequestIntoThread,
+ } = input;
+ const navigate = useNavigate();
+ const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
+ const getDraftSessionByLogicalProjectKey = useComposerDraftStore(
+ (store) => store.getDraftSessionByLogicalProjectKey,
+ );
+ const getDraftSession = useComposerDraftStore((store) => store.getDraftSession);
+ const setLogicalProjectDraftThreadId = useComposerDraftStore(
+ (store) => store.setLogicalProjectDraftThreadId,
+ );
+
+ const openPullRequestDialog = (reference?: string) => {
+ if (!canCheckoutPullRequestIntoThread) {
+ return;
+ }
+ if (activeThreadKey === null) return;
+ openPullRequestThreadDialog(activeThreadKey, reference);
+ };
+ const openPullRequestDialogStable = useStableCallback(openPullRequestDialog);
+
+ const openOrReuseProjectDraftThread = async (input: {
+ branch: string;
+ worktreePath: string | null;
+ envMode: DraftThreadEnvMode;
+ }) => {
+ if (!activeProject) {
+ throw new Error("No active project is available for this pull request.");
+ }
+ const activeProjectRef = scopeProjectRef(activeProject.environmentId, activeProject.id);
+ const logicalProjectKey = deriveLogicalProjectKeyFromSettings(
+ activeProject,
+ projectGroupingSettings,
+ );
+ const storedDraftSession = getDraftSessionByLogicalProjectKey(logicalProjectKey);
+ if (storedDraftSession) {
+ setDraftThreadContext(storedDraftSession.draftId, input);
+ setLogicalProjectDraftThreadId(
+ logicalProjectKey,
+ activeProjectRef,
+ storedDraftSession.draftId,
+ {
+ threadId: storedDraftSession.threadId,
+ ...input,
+ },
+ );
+ if (routeKind !== "draft" || draftId !== storedDraftSession.draftId) {
+ await navigate({
+ to: "/draft/$draftId",
+ params: buildDraftThreadRouteParams(storedDraftSession.draftId),
+ });
+ }
+ return storedDraftSession.threadId;
+ }
+
+ const activeDraftSession = routeKind === "draft" && draftId ? getDraftSession(draftId) : null;
+ if (!isServerThread && activeDraftSession?.logicalProjectKey === logicalProjectKey && draftId) {
+ setDraftThreadContext(draftId, input);
+ setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, draftId, {
+ threadId: activeDraftSession.threadId,
+ createdAt: activeDraftSession.createdAt,
+ runtimeMode: activeDraftSession.runtimeMode,
+ interactionMode: activeDraftSession.interactionMode,
+ ...input,
+ });
+ return activeDraftSession.threadId;
+ }
+
+ const nextDraftId = newDraftId();
+ const nextThreadId = newThreadId();
+ setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, nextDraftId, {
+ threadId: nextThreadId,
+ createdAt: new Date().toISOString(),
+ runtimeMode: resolveProjectSettings(settings, activeProject.id, activeProject).settings
+ .defaultRuntimeMode,
+ interactionMode: DEFAULT_INTERACTION_MODE,
+ ...input,
+ });
+ await navigate({
+ to: "/draft/$draftId",
+ params: buildDraftThreadRouteParams(nextDraftId),
+ });
+ return nextThreadId;
+ };
+
+ const handlePreparedPullRequestThread = async (input: {
+ branch: string;
+ worktreePath: string | null;
+ }) => {
+ await openOrReuseProjectDraftThread({
+ branch: input.branch,
+ worktreePath: input.worktreePath,
+ envMode: input.worktreePath ? "worktree" : "local",
+ });
+ };
+ const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread);
+ return {
+ handlePreparedPullRequestThreadStable,
+ openPullRequestDialogStable,
+ };
+}