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