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

gitReactQuery.ts

170 lines5.4 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};
22​
23export function invalidateGitQueries(queryClient: QueryClient) {
24 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
26​
27export function gitStatusQueryOptions(cwd: string | null) {
28 return queryOptions({
29 queryKey: gitQueryKeys.status(cwd),
30 queryFn: async () => {
31 const api = ensureNativeApi();
32 if (!cwd) throw new Error("Git status is unavailable.");
33 return api.git.status({ cwd });
34 },
35 enabled: cwd !== null,
36 staleTime: GIT_STATUS_STALE_TIME_MS,
37 refetchOnWindowFocus: "always",
38 refetchOnReconnect: "always",
39 refetchInterval: GIT_STATUS_REFETCH_INTERVAL_MS,
40 });
42​
43export function gitBranchesQueryOptions(cwd: string | null) {
44 return queryOptions({
45 queryKey: gitQueryKeys.branches(cwd),
46 queryFn: async () => {
47 const api = ensureNativeApi();
48 if (!cwd) throw new Error("Git branches are unavailable.");
49 return api.git.listBranches({ cwd });
50 },
51 enabled: cwd !== null,
52 staleTime: GIT_BRANCHES_STALE_TIME_MS,
53 refetchOnWindowFocus: true,
54 refetchOnReconnect: true,
55 refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,
56 });
58​
59export function gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
60 return mutationOptions({
61 mutationKey: gitMutationKeys.init(input.cwd),
62 mutationFn: async () => {
63 const api = ensureNativeApi();
64 if (!input.cwd) throw new Error("Git init is unavailable.");
65 return api.git.init({ cwd: input.cwd });
66 },
67 onSuccess: async () => {
68 await invalidateGitQueries(input.queryClient);
69 },
70 });
72​
73export function gitCheckoutMutationOptions(input: {
74 cwd: string | null;
75 queryClient: QueryClient;
76}) {
77 return mutationOptions({
78 mutationKey: gitMutationKeys.checkout(input.cwd),
79 mutationFn: async (branch: string) => {
80 const api = ensureNativeApi();
81 if (!input.cwd) throw new Error("Git checkout is unavailable.");
82 return api.git.checkout({ cwd: input.cwd, branch });
83 },
84 onSuccess: async () => {
85 await invalidateGitQueries(input.queryClient);
86 },
87 });
89​
90export function gitRunStackedActionMutationOptions(input: {
91 cwd: string | null;
92 queryClient: QueryClient;
93}) {
94 return mutationOptions({
95 mutationKey: gitMutationKeys.runStackedAction(input.cwd),
96 mutationFn: async ({
97 action,
98 commitMessage,
99 featureBranch,
100 }: {
101 action: GitStackedAction;
102 commitMessage?: string;
103 featureBranch?: boolean;
104 }) => {
105 const api = ensureNativeApi();
106 if (!input.cwd) throw new Error("Git action is unavailable.");
107 return api.git.runStackedAction({
108 cwd: input.cwd,
109 action,
110 ...(commitMessage ? { commitMessage } : {}),
111 ...(featureBranch ? { featureBranch } : {}),
112 });
113 },
114 onSettled: async () => {
115 await invalidateGitQueries(input.queryClient);
116 },
117 });
119​
120export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
121 return mutationOptions({
122 mutationKey: gitMutationKeys.pull(input.cwd),
123 mutationFn: async () => {
124 const api = ensureNativeApi();
125 if (!input.cwd) throw new Error("Git pull is unavailable.");
126 return api.git.pull({ cwd: input.cwd });
127 },
128 onSettled: async () => {
129 await invalidateGitQueries(input.queryClient);
130 },
131 });
133​
134export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {
135 return mutationOptions({
136 mutationFn: async ({
137 cwd,
138 branch,
139 newBranch,
140 path,
141 }: {
142 cwd: string;
143 branch: string;
144 newBranch: string;
145 path?: string | null;
146 }) => {
147 const api = ensureNativeApi();
148 if (!cwd) throw new Error("Git worktree creation is unavailable.");
149 return api.git.createWorktree({ cwd, branch, newBranch, path: path ?? null });
150 },
151 mutationKey: ["git", "mutation", "create-worktree"] as const,
152 onSettled: async () => {
153 await invalidateGitQueries(input.queryClient);
154 },
155 });
157​
158export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {
159 return mutationOptions({
160 mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {
161 const api = ensureNativeApi();
162 if (!cwd) throw new Error("Git worktree removal is unavailable.");
163 return api.git.removeWorktree({ cwd, path, force });
164 },
165 mutationKey: ["git", "mutation", "remove-worktree"] as const,
166 onSettled: async () => {
167 await invalidateGitQueries(input.queryClient);
168 },
169 });