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

gitReactQuery.ts

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