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