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

gitReactQuery.ts

327 lines11.1 KB

1import {
2 type EnvironmentId,
3 type GitActionProgressEvent,
4 type GitStackedAction,
5 type SourceControlPublishRepositoryInput,
6 type ThreadId,
7} from "@t3tools/contracts";
8import {
9 infiniteQueryOptions,
10 mutationOptions,
11 queryOptions,
12 type QueryClient,
13} from "@tanstack/react-query";
14import { ensureEnvironmentApi } from "../environmentApi";
15import { requireEnvironmentConnection } from "../environments/runtime";
16​
17const GIT_BRANCHES_STALE_TIME_MS = 15_000;
18const GIT_BRANCHES_REFETCH_INTERVAL_MS = 60_000;
19const GIT_BRANCHES_PAGE_SIZE = 100;
20​
21export const gitQueryKeys = {
22 all: ["git"] as const,
23 refs: (environmentId: EnvironmentId | null, cwd: string | null) =>
24 ["git", "refs", environmentId ?? null, cwd] as const,
25 branchSearch: (environmentId: EnvironmentId | null, cwd: string | null, query: string) =>
26 ["git", "refs", environmentId ?? null, cwd, "search", query] as const,
27};
28​
29export const gitMutationKeys = {
30 init: (environmentId: EnvironmentId | null, cwd: string | null) =>
31 ["git", "mutation", "init", environmentId ?? null, cwd] as const,
32 switchRef: (environmentId: EnvironmentId | null, cwd: string | null) =>
33 ["git", "mutation", "switchRef", environmentId ?? null, cwd] as const,
34 runStackedAction: (environmentId: EnvironmentId | null, cwd: string | null) =>
35 ["git", "mutation", "run-stacked-action", environmentId ?? null, cwd] as const,
36 pull: (environmentId: EnvironmentId | null, cwd: string | null) =>
37 ["git", "mutation", "pull", environmentId ?? null, cwd] as const,
38 preparePullRequestThread: (environmentId: EnvironmentId | null, cwd: string | null) =>
39 ["git", "mutation", "prepare-pull-request-thread", environmentId ?? null, cwd] as const,
40 publishRepository: (environmentId: EnvironmentId | null, cwd: string | null) =>
41 ["git", "mutation", "publish-repository", environmentId ?? null, cwd] as const,
42};
43​
44export function invalidateGitQueries(
45 queryClient: QueryClient,
46 input?: { environmentId?: EnvironmentId | null; cwd?: string | null },
47) {
48 const environmentId = input?.environmentId ?? null;
49 const cwd = input?.cwd ?? null;
50 if (cwd !== null) {
51 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.refs(environmentId, cwd) });
52 }
53​
54 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
56​
57function invalidateGitBranchQueries(
58 queryClient: QueryClient,
59 environmentId: EnvironmentId | null,
60 cwd: string | null,
61) {
62 if (cwd === null) {
63 return Promise.resolve();
64 }
65​
66 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.refs(environmentId, cwd) });
68​
69/**
70 * @deprecated Use a VCS-named query helper once the UI naming migration lands.
71 */
72export function gitBranchSearchInfiniteQueryOptions(input: {
73 environmentId: EnvironmentId | null;
74 cwd: string | null;
75 query: string;
76 enabled?: boolean;
77}) {
78 const normalizedQuery = input.query.trim();
79​
80 return infiniteQueryOptions({
81 queryKey: gitQueryKeys.branchSearch(input.environmentId, input.cwd, normalizedQuery),
82 initialPageParam: 0,
83 queryFn: async ({ pageParam }) => {
84 if (!input.cwd) throw new Error("Git refs are unavailable.");
85 if (!input.environmentId) throw new Error("Git refs are unavailable.");
86 const api = ensureEnvironmentApi(input.environmentId);
87 return api.vcs.listRefs({
88 cwd: input.cwd,
89 ...(normalizedQuery.length > 0 ? { query: normalizedQuery } : {}),
90 cursor: pageParam,
91 limit: GIT_BRANCHES_PAGE_SIZE,
92 });
93 },
94 getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined,
95 enabled: input.cwd !== null && (input.enabled ?? true),
96 staleTime: GIT_BRANCHES_STALE_TIME_MS,
97 refetchOnWindowFocus: true,
98 refetchOnReconnect: true,
99 refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,
100 });
102​
103export function gitResolvePullRequestQueryOptions(input: {
104 environmentId: EnvironmentId | null;
105 cwd: string | null;
106 reference: string | null;
107}) {
108 return queryOptions({
109 queryKey: [
110 "git",
111 "pull-request",
112 input.environmentId ?? null,
113 input.cwd,
114 input.reference,
115 ] as const,
116 queryFn: async () => {
117 if (!input.cwd || !input.reference || !input.environmentId) {
118 throw new Error("Pull request lookup is unavailable.");
119 }
120 const api = ensureEnvironmentApi(input.environmentId);
121 return api.git.resolvePullRequest({ cwd: input.cwd, reference: input.reference });
122 },
123 enabled: input.environmentId !== null && input.cwd !== null && input.reference !== null,
124 staleTime: 30_000,
125 refetchOnWindowFocus: false,
126 refetchOnReconnect: false,
127 });
129​
130/**
131 * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
132 */
133export function gitInitMutationOptions(input: {
134 environmentId: EnvironmentId | null;
135 cwd: string | null;
136 queryClient: QueryClient;
137}) {
138 return mutationOptions({
139 mutationKey: gitMutationKeys.init(input.environmentId, input.cwd),
140 mutationFn: async () => {
141 if (!input.cwd || !input.environmentId) throw new Error("Git init is unavailable.");
142 const api = ensureEnvironmentApi(input.environmentId);
143 return api.vcs.init({ cwd: input.cwd });
144 },
145 onSettled: async () => {
146 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
147 },
148 });
150​
151/**
152 * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
153 */
154export function gitCheckoutMutationOptions(input: {
155 environmentId: EnvironmentId | null;
156 cwd: string | null;
157 queryClient: QueryClient;
158}) {
159 return mutationOptions({
160 mutationKey: gitMutationKeys.switchRef(input.environmentId, input.cwd),
161 mutationFn: async (refName: string) => {
162 if (!input.cwd || !input.environmentId) throw new Error("Git switchRef is unavailable.");
163 const api = ensureEnvironmentApi(input.environmentId);
164 return api.vcs.switchRef({ cwd: input.cwd, refName });
165 },
166 onSettled: async () => {
167 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
168 },
169 });
171​
172export function gitRunStackedActionMutationOptions(input: {
173 environmentId: EnvironmentId | null;
174 cwd: string | null;
175 queryClient: QueryClient;
176}) {
177 return mutationOptions({
178 mutationKey: gitMutationKeys.runStackedAction(input.environmentId, input.cwd),
179 mutationFn: async ({
180 actionId,
181 action,
182 commitMessage,
183 featureBranch,
184 filePaths,
185 onProgress,
186 }: {
187 actionId: string;
188 action: GitStackedAction;
189 commitMessage?: string;
190 featureBranch?: boolean;
191 filePaths?: string[];
192 onProgress?: (event: GitActionProgressEvent) => void;
193 }) => {
194 if (!input.cwd || !input.environmentId) throw new Error("Git action is unavailable.");
195 return requireEnvironmentConnection(input.environmentId).client.git.runStackedAction(
196 {
197 action,
198 actionId,
199 cwd: input.cwd,
200 ...(commitMessage ? { commitMessage } : {}),
201 ...(featureBranch ? { featureBranch: true } : {}),
202 ...(filePaths && filePaths.length > 0 ? { filePaths } : {}),
203 },
204 ...(onProgress ? [{ onProgress }] : []),
205 );
206 },
207 onSuccess: async () => {
208 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
209 },
210 });
212​
213/**
214 * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
215 */
216export function gitPullMutationOptions(input: {
217 environmentId: EnvironmentId | null;
218 cwd: string | null;
219 queryClient: QueryClient;
220}) {
221 return mutationOptions({
222 mutationKey: gitMutationKeys.pull(input.environmentId, input.cwd),
223 mutationFn: async () => {
224 if (!input.cwd || !input.environmentId) throw new Error("Git pull is unavailable.");
225 const api = ensureEnvironmentApi(input.environmentId);
226 return api.vcs.pull({ cwd: input.cwd });
227 },
228 onSuccess: async () => {
229 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
230 },
231 });
233​
234export function sourceControlPublishRepositoryMutationOptions(input: {
235 environmentId: EnvironmentId | null;
236 cwd: string | null;
237 queryClient: QueryClient;
238}) {
239 return mutationOptions({
240 mutationKey: gitMutationKeys.publishRepository(input.environmentId, input.cwd),
241 mutationFn: async (args: Omit<SourceControlPublishRepositoryInput, "cwd">) => {
242 if (!input.cwd || !input.environmentId) {
243 throw new Error("Repository publishing is unavailable.");
244 }
245 const api = ensureEnvironmentApi(input.environmentId);
246 return api.sourceControl.publishRepository({ cwd: input.cwd, ...args });
247 },
248 onSuccess: async () => {
249 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
250 },
251 });
253​
254/**
255 * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
256 */
257export function gitCreateWorktreeMutationOptions(input: {
258 environmentId: EnvironmentId | null;
259 queryClient: QueryClient;
260}) {
261 return mutationOptions({
262 mutationKey: ["git", "mutation", "create-worktree", input.environmentId ?? null] as const,
263 mutationFn: (
264 args: Parameters<ReturnType<typeof ensureEnvironmentApi>["vcs"]["createWorktree"]>[0],
265 ) => {
266 if (!input.environmentId) {
267 throw new Error("Worktree creation is unavailable.");
268 }
269 return ensureEnvironmentApi(input.environmentId).vcs.createWorktree(args);
270 },
271 onSuccess: async () => {
272 await invalidateGitQueries(input.queryClient, { environmentId: input.environmentId });
273 },
274 });
276​
277/**
278 * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
279 */
280export function gitRemoveWorktreeMutationOptions(input: {
281 environmentId: EnvironmentId | null;
282 queryClient: QueryClient;
283}) {
284 return mutationOptions({
285 mutationKey: ["git", "mutation", "remove-worktree", input.environmentId ?? null] as const,
286 mutationFn: (
287 args: Parameters<ReturnType<typeof ensureEnvironmentApi>["vcs"]["removeWorktree"]>[0],
288 ) => {
289 if (!input.environmentId) {
290 throw new Error("Worktree removal is unavailable.");
291 }
292 return ensureEnvironmentApi(input.environmentId).vcs.removeWorktree(args);
293 },
294 onSuccess: async () => {
295 await invalidateGitQueries(input.queryClient, { environmentId: input.environmentId });
296 },
297 });
299​
300export function gitPreparePullRequestThreadMutationOptions(input: {
301 environmentId: EnvironmentId | null;
302 cwd: string | null;
303 queryClient: QueryClient;
304}) {
305 return mutationOptions({
306 mutationKey: gitMutationKeys.preparePullRequestThread(input.environmentId, input.cwd),
307 mutationFn: async (args: {
308 reference: string;
309 mode: "local" | "worktree";
310 threadId?: ThreadId;
311 }) => {
312 if (!input.cwd || !input.environmentId) {
313 throw new Error("Pull request thread preparation is unavailable.");
314 }
315 const api = ensureEnvironmentApi(input.environmentId);
316 return api.git.preparePullRequestThread({
317 cwd: input.cwd,
318 reference: args.reference,
319 mode: args.mode,
320 ...(args.threadId ? { threadId: args.threadId } : {}),
321 });
322 },
323 onSuccess: async () => {
324 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
325 },
326 });