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

gitReactQuery.ts

173 lines4.7 KB

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