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 createBranchAndCheckout: (cwd: string | null) =>
20 ["git", "mutation", "create-branch-and-checkout", cwd] as const,
21 runStackedAction: (cwd: string | null) => ["git", "mutation", "run-stacked-action", cwd] as const,
22 pull: (cwd: string | null) => ["git", "mutation", "pull", 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 gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
62 return mutationOptions({
63 mutationKey: gitMutationKeys.init(input.cwd),
64 mutationFn: async () => {
65 const api = ensureNativeApi();
66 if (!input.cwd) throw new Error("Git init is unavailable.");
67 return api.git.init({ cwd: input.cwd });
68 },
69 onSuccess: async () => {
70 await invalidateGitQueries(input.queryClient);
71 },
72 });
73}
74
75export function gitCheckoutMutationOptions(input: {
76 cwd: string | null;
77 queryClient: QueryClient;
78}) {
79 return mutationOptions({
80 mutationKey: gitMutationKeys.checkout(input.cwd),
81 mutationFn: async (branch: string) => {
82 const api = ensureNativeApi();
83 if (!input.cwd) throw new Error("Git checkout is unavailable.");
84 return api.git.checkout({ cwd: input.cwd, branch });
85 },
86 onSuccess: async () => {
87 await invalidateGitQueries(input.queryClient);
88 },
89 });
90}
91
92export function gitCreateBranchAndCheckoutMutationOptions(input: {
93 cwd: string | null;
94 queryClient: QueryClient;
95}) {
96 return mutationOptions({
97 mutationKey: gitMutationKeys.createBranchAndCheckout(input.cwd),
98 mutationFn: async (branch: string) => {
99 const api = ensureNativeApi();
100 if (!input.cwd) throw new Error("Git branch creation is unavailable.");
101 await api.git.createBranch({ cwd: input.cwd, branch });
102 return api.git.checkout({ cwd: input.cwd, branch });
103 },
104 onSuccess: async () => {
105 await invalidateGitQueries(input.queryClient);
106 },
107 });
108}
109
110export function gitRunStackedActionMutationOptions(input: {
111 cwd: string | null;
112 queryClient: QueryClient;
113}) {
114 return mutationOptions({
115 mutationKey: gitMutationKeys.runStackedAction(input.cwd),
116 mutationFn: async ({
117 action,
118 commitMessage,
119 }: {
120 action: GitStackedAction;
121 commitMessage?: string;
122 }) => {
123 const api = ensureNativeApi();
124 if (!input.cwd) throw new Error("Git action is unavailable.");
125 return api.git.runStackedAction({
126 cwd: input.cwd,
127 action,
128 ...(commitMessage ? { commitMessage } : {}),
129 });
130 },
131 onSettled: async () => {
132 await invalidateGitQueries(input.queryClient);
133 },
134 });
135}
136
137export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
138 return mutationOptions({
139 mutationKey: gitMutationKeys.pull(input.cwd),
140 mutationFn: async () => {
141 const api = ensureNativeApi();
142 if (!input.cwd) throw new Error("Git pull is unavailable.");
143 return api.git.pull({ cwd: input.cwd });
144 },
145 onSettled: async () => {
146 await invalidateGitQueries(input.queryClient);
147 },
148 });
149}
150
151export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {
152 return mutationOptions({
153 mutationFn: async ({
154 cwd,
155 branch,
156 newBranch,
157 path,
158 }: {
159 cwd: string;
160 branch: string;
161 newBranch: string;
162 path?: string | null;
163 }) => {
164 const api = ensureNativeApi();
165 if (!cwd) throw new Error("Git worktree creation is unavailable.");
166 return api.git.createWorktree({ cwd, branch, newBranch, path: path ?? null });
167 },
168 mutationKey: ["git", "mutation", "create-worktree"] as const,
169 onSettled: async () => {
170 await invalidateGitQueries(input.queryClient);
171 },
172 });
173}
174
175export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {
176 return mutationOptions({
177 mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {
178 const api = ensureNativeApi();
179 if (!cwd) throw new Error("Git worktree removal is unavailable.");
180 return api.git.removeWorktree({ cwd, path, force });
181 },
182 mutationKey: ["git", "mutation", "remove-worktree"] as const,
183 onSettled: async () => {
184 await invalidateGitQueries(input.queryClient);
185 },
186 });
187}