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