Open History to see the last commit
1import type { GitStackedAction, NativeApi } from "@t3tools/contracts";
2import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
3
4const GIT_STATUS_STALE_TIME_MS = 5_000;
5const GIT_STATUS_REFETCH_INTERVAL_MS = 15_000;
6const GIT_BRANCHES_STALE_TIME_MS = 15_000;
7const GIT_BRANCHES_REFETCH_INTERVAL_MS = 60_000;
8
9export const gitQueryKeys = {
10 all: ["git"] as const,
11 status: (cwd: string | null) => ["git", "status", cwd] as const,
12 branches: (cwd: string | null) => ["git", "branches", cwd] as const,
13};
14
15export function invalidateGitQueries(queryClient: QueryClient) {
16 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
17}
18
19export function gitStatusQueryOptions(api: NativeApi | undefined, cwd: string | null) {
20 return queryOptions({
21 queryKey: gitQueryKeys.status(cwd),
22 queryFn: async () => {
23 if (!api || !cwd) {
24 throw new Error("Git status is unavailable.");
25 }
26 return api.git.status({ cwd });
27 },
28 enabled: !!api && !!cwd,
29 staleTime: GIT_STATUS_STALE_TIME_MS,
30 refetchOnWindowFocus: "always",
31 refetchOnReconnect: "always",
32 refetchInterval: GIT_STATUS_REFETCH_INTERVAL_MS,
33 });
34}
35
36export function gitBranchesQueryOptions(api: NativeApi | undefined, cwd: string | null) {
37 return queryOptions({
38 queryKey: gitQueryKeys.branches(cwd),
39 queryFn: async () => {
40 if (!api || !cwd) {
41 throw new Error("Git branches are unavailable.");
42 }
43 return api.git.listBranches({ cwd });
44 },
45 enabled: !!api && !!cwd,
46 staleTime: GIT_BRANCHES_STALE_TIME_MS,
47 refetchOnWindowFocus: true,
48 refetchOnReconnect: true,
49 refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,
50 });
51}
52
53export function gitInitMutationOptions(input: {
54 api: NativeApi | undefined;
55 cwd: string | null;
56 queryClient: QueryClient;
57}) {
58 return mutationOptions({
59 mutationFn: async () => {
60 if (!input.api || !input.cwd) throw new Error("Git init is unavailable.");
61 return input.api.git.init({ cwd: input.cwd });
62 },
63 onSuccess: async () => {
64 await invalidateGitQueries(input.queryClient);
65 },
66 });
67}
68
69export function gitCheckoutMutationOptions(input: {
70 api: NativeApi | undefined;
71 cwd: string | null;
72 queryClient: QueryClient;
73}) {
74 return mutationOptions({
75 mutationFn: async (branch: string) => {
76 if (!input.api || !input.cwd) throw new Error("Git checkout is unavailable.");
77 return input.api.git.checkout({ cwd: input.cwd, branch });
78 },
79 onSuccess: async () => {
80 await invalidateGitQueries(input.queryClient);
81 },
82 });
83}
84
85export function gitCreateBranchAndCheckoutMutationOptions(input: {
86 api: NativeApi | undefined;
87 cwd: string | null;
88 queryClient: QueryClient;
89}) {
90 return mutationOptions({
91 mutationFn: async (branch: string) => {
92 if (!input.api || !input.cwd) throw new Error("Git branch creation is unavailable.");
93 await input.api.git.createBranch({ cwd: input.cwd, branch });
94 return input.api.git.checkout({ cwd: input.cwd, branch });
95 },
96 onSuccess: async () => {
97 await invalidateGitQueries(input.queryClient);
98 },
99 });
100}
101
102export function gitRunStackedActionMutationOptions(input: {
103 api: NativeApi | undefined;
104 cwd: string | null;
105 queryClient: QueryClient;
106}) {
107 return mutationOptions({
108 mutationFn: async ({
109 action,
110 commitMessage,
111 }: {
112 action: GitStackedAction;
113 commitMessage?: string;
114 }) => {
115 if (!input.api || !input.cwd) {
116 throw new Error("Git action is unavailable.");
117 }
118 return input.api.git.runStackedAction({
119 cwd: input.cwd,
120 action,
121 ...(commitMessage ? { commitMessage } : {}),
122 });
123 },
124 onSettled: async () => {
125 await invalidateGitQueries(input.queryClient);
126 },
127 });
128}
129
130export function gitPullMutationOptions(input: {
131 api: NativeApi | undefined;
132 cwd: string | null;
133 queryClient: QueryClient;
134}) {
135 return mutationOptions({
136 mutationFn: async () => {
137 if (!input.api || !input.cwd) throw new Error("Git pull is unavailable.");
138 return input.api.git.pull({ cwd: input.cwd });
139 },
140 onSettled: async () => {
141 await invalidateGitQueries(input.queryClient);
142 },
143 });
144}
145
146export function gitCreateWorktreeMutationOptions(input: {
147 api: NativeApi | undefined;
148 queryClient: QueryClient;
149}) {
150 return mutationOptions({
151 mutationFn: async ({
152 cwd,
153 branch,
154 newBranch,
155 }: {
156 cwd: string;
157 branch: string;
158 newBranch: string;
159 }) => {
160 if (!input.api) {
161 throw new Error("Git worktree creation is unavailable.");
162 }
163 return input.api.git.createWorktree({ cwd, branch, newBranch });
164 },
165 onSettled: async () => {
166 await invalidateGitQueries(input.queryClient);
167 },
168 });
169}
170
171export function gitRemoveWorktreeMutationOptions(input: {
172 api: NativeApi | undefined;
173 queryClient: QueryClient;
174}) {
175 return mutationOptions({
176 mutationFn: async ({ cwd, path }: { cwd: string; path: string }) => {
177 if (!input.api) {
178 throw new Error("Git worktree removal is unavailable.");
179 }
180 return input.api.git.removeWorktree({ cwd, path });
181 },
182 onSettled: async () => {
183 await invalidateGitQueries(input.queryClient);
184 },
185 });
186}