Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usePullRequestDraftThread.ts

149 lines5.6 KB

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