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

gitReactQuery.ts

221 lines7.0 KB

1import type { GitStackedAction } from "@t3tools/contracts";
2import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
3import { ensureNativeApi } from "../nativeApi";
4​
5const GIT_STATUS_STALE_TIME_MS = 5_000;
6const GIT_STATUS_REFETCH_INTERVAL_MS = 15_000;
7const GIT_BRANCHES_STALE_TIME_MS = 15_000;
8const GIT_BRANCHES_REFETCH_INTERVAL_MS = 60_000;
9​
10export const gitQueryKeys = {
11 all: ["git"] as const,
12 status: (cwd: string | null) => ["git", "status", cwd] as const,
13 branches: (cwd: string | null) => ["git", "branches", cwd] as const,
14};
15​
16export const gitMutationKeys = {
17 init: (cwd: string | null) => ["git", "mutation", "init", cwd] as const,
18 checkout: (cwd: string | null) => ["git", "mutation", "checkout", cwd] as const,
19 runStackedAction: (cwd: string | null) => ["git", "mutation", "run-stacked-action", cwd] as const,
20 pull: (cwd: string | null) => ["git", "mutation", "pull", cwd] as const,
21 preparePullRequestThread: (cwd: string | null) =>
22 ["git", "mutation", "prepare-pull-request-thread", cwd] as const,
23};
24​
25export function invalidateGitQueries(queryClient: QueryClient) {
26 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
28​
29export function gitStatusQueryOptions(cwd: string | null) {
30 return queryOptions({
31 queryKey: gitQueryKeys.status(cwd),
32 queryFn: async () => {
33 const api = ensureNativeApi();
34 if (!cwd) throw new Error("Git status is unavailable.");
35 return api.git.status({ cwd });
36 },
37 enabled: cwd !== null,
38 staleTime: GIT_STATUS_STALE_TIME_MS,
39 refetchOnWindowFocus: "always",
40 refetchOnReconnect: "always",
41 refetchInterval: GIT_STATUS_REFETCH_INTERVAL_MS,
42 });
44​
45export function gitBranchesQueryOptions(cwd: string | null) {
46 return queryOptions({
47 queryKey: gitQueryKeys.branches(cwd),
48 queryFn: async () => {
49 const api = ensureNativeApi();
50 if (!cwd) throw new Error("Git branches are unavailable.");
51 return api.git.listBranches({ cwd });
52 },
53 enabled: cwd !== null,
54 staleTime: GIT_BRANCHES_STALE_TIME_MS,
55 refetchOnWindowFocus: true,
56 refetchOnReconnect: true,
57 refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,
58 });
60​
61export function gitResolvePullRequestQueryOptions(input: {
62 cwd: string | null;
63 reference: string | null;
64}) {
65 return queryOptions({
66 queryKey: ["git", "pull-request", input.cwd, input.reference] as const,
67 queryFn: async () => {
68 const api = ensureNativeApi();
69 if (!input.cwd || !input.reference) {
70 throw new Error("Pull request lookup is unavailable.");
71 }
72 return api.git.resolvePullRequest({ cwd: input.cwd, reference: input.reference });
73 },
74 enabled: input.cwd !== null && input.reference !== null,
75 staleTime: 30_000,
76 refetchOnWindowFocus: false,
77 refetchOnReconnect: false,
78 });
80​
81export function gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
82 return mutationOptions({
83 mutationKey: gitMutationKeys.init(input.cwd),
84 mutationFn: async () => {
85 const api = ensureNativeApi();
86 if (!input.cwd) throw new Error("Git init is unavailable.");
87 return api.git.init({ cwd: input.cwd });
88 },
89 onSuccess: async () => {
90 await invalidateGitQueries(input.queryClient);
91 },
92 });
94​
95export function gitCheckoutMutationOptions(input: {
96 cwd: string | null;
97 queryClient: QueryClient;
98}) {
99 return mutationOptions({
100 mutationKey: gitMutationKeys.checkout(input.cwd),
101 mutationFn: async (branch: string) => {
102 const api = ensureNativeApi();
103 if (!input.cwd) throw new Error("Git checkout is unavailable.");
104 return api.git.checkout({ cwd: input.cwd, branch });
105 },
106 onSuccess: async () => {
107 await invalidateGitQueries(input.queryClient);
108 },
109 });
111​
112export function gitRunStackedActionMutationOptions(input: {
113 cwd: string | null;
114 queryClient: QueryClient;
115 model?: string | null;
116}) {
117 return mutationOptions({
118 mutationKey: gitMutationKeys.runStackedAction(input.cwd),
119 mutationFn: async ({
120 actionId,
121 action,
122 commitMessage,
123 featureBranch,
124 filePaths,
125 }: {
126 actionId: string;
127 action: GitStackedAction;
128 commitMessage?: string;
129 featureBranch?: boolean;
130 filePaths?: string[];
131 }) => {
132 const api = ensureNativeApi();
133 if (!input.cwd) throw new Error("Git action is unavailable.");
134 return api.git.runStackedAction({
135 actionId,
136 cwd: input.cwd,
137 action,
138 ...(commitMessage ? { commitMessage } : {}),
139 ...(featureBranch ? { featureBranch } : {}),
140 ...(filePaths ? { filePaths } : {}),
141 ...(input.model ? { model: input.model } : {}),
142 });
143 },
144 onSettled: async () => {
145 await invalidateGitQueries(input.queryClient);
146 },
147 });
149​
150export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
151 return mutationOptions({
152 mutationKey: gitMutationKeys.pull(input.cwd),
153 mutationFn: async () => {
154 const api = ensureNativeApi();
155 if (!input.cwd) throw new Error("Git pull is unavailable.");
156 return api.git.pull({ cwd: input.cwd });
157 },
158 onSettled: async () => {
159 await invalidateGitQueries(input.queryClient);
160 },
161 });
163​
164export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {
165 return mutationOptions({
166 mutationFn: async ({
167 cwd,
168 branch,
169 newBranch,
170 path,
171 }: {
172 cwd: string;
173 branch: string;
174 newBranch: string;
175 path?: string | null;
176 }) => {
177 const api = ensureNativeApi();
178 if (!cwd) throw new Error("Git worktree creation is unavailable.");
179 return api.git.createWorktree({ cwd, branch, newBranch, path: path ?? null });
180 },
181 mutationKey: ["git", "mutation", "create-worktree"] as const,
182 onSettled: async () => {
183 await invalidateGitQueries(input.queryClient);
184 },
185 });
187​
188export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {
189 return mutationOptions({
190 mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {
191 const api = ensureNativeApi();
192 if (!cwd) throw new Error("Git worktree removal is unavailable.");
193 return api.git.removeWorktree({ cwd, path, force });
194 },
195 mutationKey: ["git", "mutation", "remove-worktree"] as const,
196 onSettled: async () => {
197 await invalidateGitQueries(input.queryClient);
198 },
199 });
201​
202export function gitPreparePullRequestThreadMutationOptions(input: {
203 cwd: string | null;
204 queryClient: QueryClient;
205}) {
206 return mutationOptions({
207 mutationFn: async ({ reference, mode }: { reference: string; mode: "local" | "worktree" }) => {
208 const api = ensureNativeApi();
209 if (!input.cwd) throw new Error("Pull request thread preparation is unavailable.");
210 return api.git.preparePullRequestThread({
211 cwd: input.cwd,
212 reference,
213 mode,
214 });
215 },
216 mutationKey: gitMutationKeys.preparePullRequestThread(input.cwd),
217 onSettled: async () => {
218 await invalidateGitQueries(input.queryClient);
219 },
220 });