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

gitReactQuery.ts

219 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}) {
116 return mutationOptions({
117 mutationKey: gitMutationKeys.runStackedAction(input.cwd),
118 mutationFn: async ({
119 actionId,
120 action,
121 commitMessage,
122 featureBranch,
123 filePaths,
124 }: {
125 actionId: string;
126 action: GitStackedAction;
127 commitMessage?: string;
128 featureBranch?: boolean;
129 filePaths?: string[];
130 }) => {
131 const api = ensureNativeApi();
132 if (!input.cwd) throw new Error("Git action is unavailable.");
133 return api.git.runStackedAction({
134 actionId,
135 cwd: input.cwd,
136 action,
137 ...(commitMessage ? { commitMessage } : {}),
138 ...(featureBranch ? { featureBranch } : {}),
139 ...(filePaths ? { filePaths } : {}),
140 });
141 },
142 onSettled: async () => {
143 await invalidateGitQueries(input.queryClient);
144 },
145 });
147​
148export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
149 return mutationOptions({
150 mutationKey: gitMutationKeys.pull(input.cwd),
151 mutationFn: async () => {
152 const api = ensureNativeApi();
153 if (!input.cwd) throw new Error("Git pull is unavailable.");
154 return api.git.pull({ cwd: input.cwd });
155 },
156 onSettled: async () => {
157 await invalidateGitQueries(input.queryClient);
158 },
159 });
161​
162export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {
163 return mutationOptions({
164 mutationFn: async ({
165 cwd,
166 branch,
167 newBranch,
168 path,
169 }: {
170 cwd: string;
171 branch: string;
172 newBranch: string;
173 path?: string | null;
174 }) => {
175 const api = ensureNativeApi();
176 if (!cwd) throw new Error("Git worktree creation is unavailable.");
177 return api.git.createWorktree({ cwd, branch, newBranch, path: path ?? null });
178 },
179 mutationKey: ["git", "mutation", "create-worktree"] as const,
180 onSettled: async () => {
181 await invalidateGitQueries(input.queryClient);
182 },
183 });
185​
186export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {
187 return mutationOptions({
188 mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {
189 const api = ensureNativeApi();
190 if (!cwd) throw new Error("Git worktree removal is unavailable.");
191 return api.git.removeWorktree({ cwd, path, force });
192 },
193 mutationKey: ["git", "mutation", "remove-worktree"] as const,
194 onSettled: async () => {
195 await invalidateGitQueries(input.queryClient);
196 },
197 });
199​
200export function gitPreparePullRequestThreadMutationOptions(input: {
201 cwd: string | null;
202 queryClient: QueryClient;
203}) {
204 return mutationOptions({
205 mutationFn: async ({ reference, mode }: { reference: string; mode: "local" | "worktree" }) => {
206 const api = ensureNativeApi();
207 if (!input.cwd) throw new Error("Pull request thread preparation is unavailable.");
208 return api.git.preparePullRequestThread({
209 cwd: input.cwd,
210 reference,
211 mode,
212 });
213 },
214 mutationKey: gitMutationKeys.preparePullRequestThread(input.cwd),
215 onSettled: async () => {
216 await invalidateGitQueries(input.queryClient);
217 },
218 });