The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg } from "gt-react"; |
| 2 | import { m } from "~/i18n/messages"; |
| 3 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 4 | import type { UnifiedSettings } from "@t3tools/contracts"; |
| 5 | import { useStableCallback } from "../../hooks/useStableCallback"; |
| 6 | import { scopeProjectRef } from "@t3tools/client-runtime/environment"; |
| 7 | import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; |
| 8 | import { useNavigate } from "@tanstack/react-router"; |
| 9 | import { DEFAULT_INTERACTION_MODE } from "../../types"; |
| 10 | import { newDraftId, newThreadId } from "~/lib/utils"; |
| 11 | import { |
| 12 | deriveLogicalProjectKeyFromSettings, |
| 13 | selectProjectGroupingSettings, |
| 14 | } from "@t3tools/client-runtime/state/project-grouping"; |
| 15 | import { buildDraftThreadRouteParams } from "../../threadRoutes"; |
| 16 | import { |
| 17 | type DraftCheckoutPullRequest, |
| 18 | type DraftThreadEnvMode, |
| 19 | useComposerDraftStore, |
| 20 | DraftId, |
| 21 | } from "../../composerDraftStore"; |
| 22 | import { openPullRequestThreadDialog } from "./ChatOverlayHosts"; |
| 23 | interface PullRequestDraftThreadInput { |
| 24 | readonly routeKind: "server" | "draft"; |
| 25 | readonly draftId: DraftId | null; |
| 26 | readonly settings: UnifiedSettings; |
| 27 | readonly projectGroupingSettings: ReturnType<typeof selectProjectGroupingSettings>; |
| 28 | readonly activeProject: EnvironmentProject | null; |
| 29 | readonly activeThreadKey: string | null; |
| 30 | readonly isServerThread: boolean; |
| 31 | readonly canCheckoutPullRequestIntoThread: boolean; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Checking out a pull request into a thread: opens the dialog, then puts the prepared branch or |
| 36 | * worktree on the project's draft thread, reusing the one already open when there is one. |
| 37 | */ |
| 38 | export function usePullRequestDraftThread(input: PullRequestDraftThreadInput) { |
| 39 | const { |
| 40 | routeKind, |
| 41 | draftId, |
| 42 | settings, |
| 43 | projectGroupingSettings, |
| 44 | activeProject, |
| 45 | activeThreadKey, |
| 46 | isServerThread, |
| 47 | canCheckoutPullRequestIntoThread, |
| 48 | } = input; |
| 49 | const navigate = useNavigate(); |
| 50 | const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext); |
| 51 | const getDraftSessionByLogicalProjectKey = useComposerDraftStore( |
| 52 | (store) => store.getDraftSessionByLogicalProjectKey, |
| 53 | ); |
| 54 | const getDraftSession = useComposerDraftStore((store) => store.getDraftSession); |
| 55 | const setLogicalProjectDraftThreadId = useComposerDraftStore( |
| 56 | (store) => store.setLogicalProjectDraftThreadId, |
| 57 | ); |
| 58 | |
| 59 | const openPullRequestDialog = (reference?: string) => { |
| 60 | if (!canCheckoutPullRequestIntoThread) { |
| 61 | return; |
| 62 | } |
| 63 | if (activeThreadKey === null) return; |
| 64 | openPullRequestThreadDialog(activeThreadKey, reference); |
| 65 | }; |
| 66 | const openPullRequestDialogStable = useStableCallback(openPullRequestDialog); |
| 67 | |
| 68 | const openOrReuseProjectDraftThread = async (input: { |
| 69 | branch: string; |
| 70 | worktreePath: string | null; |
| 71 | envMode: DraftThreadEnvMode; |
| 72 | checkoutPullRequest: DraftCheckoutPullRequest; |
| 73 | }) => { |
| 74 | if (!activeProject) { |
| 75 | throw new Error(m(msg("No active project is available for this pull request."))); |
| 76 | } |
| 77 | const activeProjectRef = scopeProjectRef(activeProject.environmentId, activeProject.id); |
| 78 | const logicalProjectKey = deriveLogicalProjectKeyFromSettings( |
| 79 | activeProject, |
| 80 | projectGroupingSettings, |
| 81 | ); |
| 82 | const storedDraftSession = getDraftSessionByLogicalProjectKey(logicalProjectKey); |
| 83 | if (storedDraftSession) { |
| 84 | setDraftThreadContext(storedDraftSession.draftId, input); |
| 85 | setLogicalProjectDraftThreadId( |
| 86 | logicalProjectKey, |
| 87 | activeProjectRef, |
| 88 | storedDraftSession.draftId, |
| 89 | { |
| 90 | threadId: storedDraftSession.threadId, |
| 91 | ...input, |
| 92 | }, |
| 93 | ); |
| 94 | if (routeKind !== "draft" || draftId !== storedDraftSession.draftId) { |
| 95 | await navigate({ |
| 96 | to: "/draft/$draftId", |
| 97 | params: buildDraftThreadRouteParams(storedDraftSession.draftId), |
| 98 | }); |
| 99 | } |
| 100 | return storedDraftSession.threadId; |
| 101 | } |
| 102 | |
| 103 | const activeDraftSession = routeKind === "draft" && draftId ? getDraftSession(draftId) : null; |
| 104 | if (!isServerThread && activeDraftSession?.logicalProjectKey === logicalProjectKey && draftId) { |
| 105 | setDraftThreadContext(draftId, input); |
| 106 | setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, draftId, { |
| 107 | threadId: activeDraftSession.threadId, |
| 108 | createdAt: activeDraftSession.createdAt, |
| 109 | runtimeMode: activeDraftSession.runtimeMode, |
| 110 | interactionMode: activeDraftSession.interactionMode, |
| 111 | ...input, |
| 112 | }); |
| 113 | return activeDraftSession.threadId; |
| 114 | } |
| 115 | |
| 116 | const nextDraftId = newDraftId(); |
| 117 | const nextThreadId = newThreadId(); |
| 118 | setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, nextDraftId, { |
| 119 | threadId: nextThreadId, |
| 120 | createdAt: new Date().toISOString(), |
| 121 | runtimeMode: resolveProjectSettings(settings, activeProject.id, activeProject).settings |
| 122 | .defaultRuntimeMode, |
| 123 | interactionMode: DEFAULT_INTERACTION_MODE, |
| 124 | ...input, |
| 125 | }); |
| 126 | await navigate({ |
| 127 | to: "/draft/$draftId", |
| 128 | params: buildDraftThreadRouteParams(nextDraftId), |
| 129 | }); |
| 130 | return nextThreadId; |
| 131 | }; |
| 132 | |
| 133 | const handlePreparedPullRequestThread = async (input: { |
| 134 | branch: string; |
| 135 | worktreePath: string | null; |
| 136 | pullRequestUrl: string; |
| 137 | }) => { |
| 138 | // The draft carries the pull request until its first send makes it a thread, which links it. |
| 139 | await openOrReuseProjectDraftThread({ |
| 140 | branch: input.branch, |
| 141 | worktreePath: input.worktreePath, |
| 142 | envMode: input.worktreePath ? "worktree" : "local", |
| 143 | checkoutPullRequest: { url: input.pullRequestUrl, branch: input.branch }, |
| 144 | }); |
| 145 | }; |
| 146 | const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread); |
| 147 | return { |
| 148 | handlePreparedPullRequestThreadStable, |
| 149 | openPullRequestDialogStable, |
| 150 | }; |
| 151 | } |