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

gitReactQuery.ts

240 lines7.7 KB

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