History

Prepare datamodel for multi-environment (#1765)

Julius Marminge committed 7 months ago

Commit b96308f1 parent 72b7f90121 files · +10206 −4969Browse files

Showing the changes to apps/web/src/lib/gitReactQuery.ts (1 of 121 files). Show all

Unified · Split

apps/web/src/lib/gitReactQuery.ts+105−53
@@ -1,4 +1,5 @@
11 import {
2+ type EnvironmentId,
23 type GitActionProgressEvent,
34 type GitStackedAction,
45 type ThreadId,
@@ -9,8 +10,8 @@ import { · 4 unmodified lines
910 queryOptions,
1011 type QueryClient,
1112 } from "@tanstack/react-query";
12−import { ensureNativeApi } from "../nativeApi";
13−import { getWsRpcClient } from "../wsRpcClient";
13+import { ensureEnvironmentApi } from "../environmentApi";
14+import { getWsRpcClientForEnvironment } from "../wsRpcClient";
1415
1516 const GIT_BRANCHES_STALE_TIME_MS = 15_000;
1617 const GIT_BRANCHES_REFETCH_INTERVAL_MS = 60_000;
@@ -18,38 +19,52 @@ const GIT_BRANCHES_PAGE_SIZE = 100; · 1 unmodified line
1819
1920 export const gitQueryKeys = {
2021 all: ["git"] as const,
21− branches: (cwd: string | null) => ["git", "branches", cwd] as const,
22− branchSearch: (cwd: string | null, query: string) =>
23− ["git", "branches", cwd, "search", query] as const,
22+ branches: (environmentId: EnvironmentId | null, cwd: string | null) =>
23+ ["git", "branches", environmentId ?? null, cwd] as const,
24+ branchSearch: (environmentId: EnvironmentId | null, cwd: string | null, query: string) =>
25+ ["git", "branches", environmentId ?? null, cwd, "search", query] as const,
2426 };
2527
2628 export const gitMutationKeys = {
27− init: (cwd: string | null) => ["git", "mutation", "init", cwd] as const,
28− checkout: (cwd: string | null) => ["git", "mutation", "checkout", cwd] as const,
29− runStackedAction: (cwd: string | null) => ["git", "mutation", "run-stacked-action", cwd] as const,
30− pull: (cwd: string | null) => ["git", "mutation", "pull", cwd] as const,
31− preparePullRequestThread: (cwd: string | null) =>
32− ["git", "mutation", "prepare-pull-request-thread", cwd] as const,
29+ init: (environmentId: EnvironmentId | null, cwd: string | null) =>
30+ ["git", "mutation", "init", environmentId ?? null, cwd] as const,
31+ checkout: (environmentId: EnvironmentId | null, cwd: string | null) =>
32+ ["git", "mutation", "checkout", environmentId ?? null, cwd] as const,
33+ runStackedAction: (environmentId: EnvironmentId | null, cwd: string | null) =>
34+ ["git", "mutation", "run-stacked-action", environmentId ?? null, cwd] as const,
35+ pull: (environmentId: EnvironmentId | null, cwd: string | null) =>
36+ ["git", "mutation", "pull", environmentId ?? null, cwd] as const,
37+ preparePullRequestThread: (environmentId: EnvironmentId | null, cwd: string | null) =>
38+ ["git", "mutation", "prepare-pull-request-thread", environmentId ?? null, cwd] as const,
3339 };
3440
35−export function invalidateGitQueries(queryClient: QueryClient, input?: { cwd?: string | null }) {
41+export function invalidateGitQueries(
42+ queryClient: QueryClient,
43+ input?: { environmentId?: EnvironmentId | null; cwd?: string | null },
44+) {
45+ const environmentId = input?.environmentId ?? null;
3646 const cwd = input?.cwd ?? null;
3747 if (cwd !== null) {
38− return queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(cwd) });
48+ return queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(environmentId, cwd) });
3949 }
4050
4151 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
4252 }
4353
44−function invalidateGitBranchQueries(queryClient: QueryClient, cwd: string | null) {
54+function invalidateGitBranchQueries(
55+ queryClient: QueryClient,
56+ environmentId: EnvironmentId | null,
57+ cwd: string | null,
58+) {
4559 if (cwd === null) {
4660 return Promise.resolve();
4761 }
4862
49− return queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(cwd) });
63+ return queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(environmentId, cwd) });
5064 }
5165
5266 export function gitBranchSearchInfiniteQueryOptions(input: {
67+ environmentId: EnvironmentId | null;
5368 cwd: string | null;
5469 query: string;
5570 enabled?: boolean;
@@ -57,11 +72,12 @@ export function gitBranchSearchInfiniteQueryOptions(input: { · 1 unmodified line
5772 const normalizedQuery = input.query.trim();
5873
5974 return infiniteQueryOptions({
60− queryKey: gitQueryKeys.branchSearch(input.cwd, normalizedQuery),
75+ queryKey: gitQueryKeys.branchSearch(input.environmentId, input.cwd, normalizedQuery),
6176 initialPageParam: 0,
6277 queryFn: async ({ pageParam }) => {
63− const api = ensureNativeApi();
6478 if (!input.cwd) throw new Error("Git branches are unavailable.");
79+ if (!input.environmentId) throw new Error("Git branches are unavailable.");
80+ const api = ensureEnvironmentApi(input.environmentId);
6581 return api.git.listBranches({
6682 cwd: input.cwd,
6783 ...(normalizedQuery.length > 0 ? { query: normalizedQuery } : {}),
@@ -79,62 +95,75 @@ export function gitBranchSearchInfiniteQueryOptions(input: { · 11 unmodified lines
7995 }
8096
8197 export function gitResolvePullRequestQueryOptions(input: {
98+ environmentId: EnvironmentId | null;
8299 cwd: string | null;
83100 reference: string | null;
84101 }) {
85102 return queryOptions({
86− queryKey: ["git", "pull-request", input.cwd, input.reference] as const,
103+ queryKey: [
104+ "git",
105+ "pull-request",
106+ input.environmentId ?? null,
107+ input.cwd,
108+ input.reference,
109+ ] as const,
87110 queryFn: async () => {
88− const api = ensureNativeApi();
89− if (!input.cwd || !input.reference) {
111+ if (!input.cwd || !input.reference || !input.environmentId) {
90112 throw new Error("Pull request lookup is unavailable.");
91113 }
114+ const api = ensureEnvironmentApi(input.environmentId);
92115 return api.git.resolvePullRequest({ cwd: input.cwd, reference: input.reference });
93116 },
94− enabled: input.cwd !== null && input.reference !== null,
117+ enabled: input.environmentId !== null && input.cwd !== null && input.reference !== null,
95118 staleTime: 30_000,
96119 refetchOnWindowFocus: false,
97120 refetchOnReconnect: false,
98121 });
99122 }
100123
101−export function gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
124+export function gitInitMutationOptions(input: {
125+ environmentId: EnvironmentId | null;
126+ cwd: string | null;
127+ queryClient: QueryClient;
128+}) {
102129 return mutationOptions({
103− mutationKey: gitMutationKeys.init(input.cwd),
130+ mutationKey: gitMutationKeys.init(input.environmentId, input.cwd),
104131 mutationFn: async () => {
105− const api = ensureNativeApi();
106− if (!input.cwd) throw new Error("Git init is unavailable.");
132+ if (!input.cwd || !input.environmentId) throw new Error("Git init is unavailable.");
133+ const api = ensureEnvironmentApi(input.environmentId);
107134 return api.git.init({ cwd: input.cwd });
108135 },
109136 onSettled: async () => {
110− await invalidateGitBranchQueries(input.queryClient, input.cwd);
137+ await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
111138 },
112139 });
113140 }
114141
115142 export function gitCheckoutMutationOptions(input: {
143+ environmentId: EnvironmentId | null;
116144 cwd: string | null;
117145 queryClient: QueryClient;
118146 }) {
119147 return mutationOptions({
120− mutationKey: gitMutationKeys.checkout(input.cwd),
148+ mutationKey: gitMutationKeys.checkout(input.environmentId, input.cwd),
121149 mutationFn: async (branch: string) => {
122− const api = ensureNativeApi();
123− if (!input.cwd) throw new Error("Git checkout is unavailable.");
150+ if (!input.cwd || !input.environmentId) throw new Error("Git checkout is unavailable.");
151+ const api = ensureEnvironmentApi(input.environmentId);
124152 return api.git.checkout({ cwd: input.cwd, branch });
125153 },
126154 onSettled: async () => {
127− await invalidateGitBranchQueries(input.queryClient, input.cwd);
155+ await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
128156 },
129157 });
130158 }
131159
132160 export function gitRunStackedActionMutationOptions(input: {
161+ environmentId: EnvironmentId | null;
133162 cwd: string | null;
134163 queryClient: QueryClient;
135164 }) {
136165 return mutationOptions({
137− mutationKey: gitMutationKeys.runStackedAction(input.cwd),
166+ mutationKey: gitMutationKeys.runStackedAction(input.environmentId, input.cwd),
138167 mutationFn: async ({
139168 actionId,
140169 action,
@@ -150,8 +179,8 @@ export function gitRunStackedActionMutationOptions(input: { · 9 unmodified lines
150179 filePaths?: string[];
151180 onProgress?: (event: GitActionProgressEvent) => void;
152181 }) => {
153− if (!input.cwd) throw new Error("Git action is unavailable.");
154− return getWsRpcClient().git.runStackedAction(
182+ if (!input.cwd || !input.environmentId) throw new Error("Git action is unavailable.");
183+ return getWsRpcClientForEnvironment(input.environmentId).git.runStackedAction(
155184 {
156185 action,
157186 actionId,
@@ -164,62 +193,85 @@ export function gitRunStackedActionMutationOptions(input: { · 6 unmodified lines
164193 );
165194 },
166195 onSuccess: async () => {
167− await invalidateGitBranchQueries(input.queryClient, input.cwd);
196+ await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
168197 },
169198 });
170199 }
171200
172−export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
201+export function gitPullMutationOptions(input: {
202+ environmentId: EnvironmentId | null;
203+ cwd: string | null;
204+ queryClient: QueryClient;
205+}) {
173206 return mutationOptions({
174− mutationKey: gitMutationKeys.pull(input.cwd),
207+ mutationKey: gitMutationKeys.pull(input.environmentId, input.cwd),
175208 mutationFn: async () => {
176− const api = ensureNativeApi();
177− if (!input.cwd) throw new Error("Git pull is unavailable.");
209+ if (!input.cwd || !input.environmentId) throw new Error("Git pull is unavailable.");
210+ const api = ensureEnvironmentApi(input.environmentId);
178211 return api.git.pull({ cwd: input.cwd });
179212 },
180213 onSuccess: async () => {
181− await invalidateGitBranchQueries(input.queryClient, input.cwd);
214+ await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
182215 },
183216 });
184217 }
185218
186−export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {
219+export function gitCreateWorktreeMutationOptions(input: {
220+ environmentId: EnvironmentId | null;
221+ queryClient: QueryClient;
222+}) {
187223 return mutationOptions({
188− mutationKey: ["git", "mutation", "create-worktree"] as const,
224+ mutationKey: ["git", "mutation", "create-worktree", input.environmentId ?? null] as const,
189225 mutationFn: (
190− args: Parameters<ReturnType<typeof ensureNativeApi>["git"]["createWorktree"]>[0],
191− ) => ensureNativeApi().git.createWorktree(args),
226+ args: Parameters<ReturnType<typeof ensureEnvironmentApi>["git"]["createWorktree"]>[0],
227+ ) => {
228+ if (!input.environmentId) {
229+ throw new Error("Worktree creation is unavailable.");
230+ }
231+ return ensureEnvironmentApi(input.environmentId).git.createWorktree(args);
232+ },
192233 onSuccess: async () => {
193− await invalidateGitQueries(input.queryClient);
234+ await invalidateGitQueries(input.queryClient, { environmentId: input.environmentId });
194235 },
195236 });
196237 }
197238
198−export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {
239+export function gitRemoveWorktreeMutationOptions(input: {
240+ environmentId: EnvironmentId | null;
241+ queryClient: QueryClient;
242+}) {
199243 return mutationOptions({
200− mutationKey: ["git", "mutation", "remove-worktree"] as const,
244+ mutationKey: ["git", "mutation", "remove-worktree", input.environmentId ?? null] as const,
201245 mutationFn: (
202− args: Parameters<ReturnType<typeof ensureNativeApi>["git"]["removeWorktree"]>[0],
203− ) => ensureNativeApi().git.removeWorktree(args),
246+ args: Parameters<ReturnType<typeof ensureEnvironmentApi>["git"]["removeWorktree"]>[0],
247+ ) => {
248+ if (!input.environmentId) {
249+ throw new Error("Worktree removal is unavailable.");
250+ }
251+ return ensureEnvironmentApi(input.environmentId).git.removeWorktree(args);
252+ },
204253 onSuccess: async () => {
205− await invalidateGitQueries(input.queryClient);
254+ await invalidateGitQueries(input.queryClient, { environmentId: input.environmentId });
206255 },
207256 });
208257 }
209258
210259 export function gitPreparePullRequestThreadMutationOptions(input: {
260+ environmentId: EnvironmentId | null;
211261 cwd: string | null;
212262 queryClient: QueryClient;
213263 }) {
214264 return mutationOptions({
215− mutationKey: gitMutationKeys.preparePullRequestThread(input.cwd),
265+ mutationKey: gitMutationKeys.preparePullRequestThread(input.environmentId, input.cwd),
216266 mutationFn: async (args: {
217267 reference: string;
218268 mode: "local" | "worktree";
219269 threadId?: ThreadId;
220270 }) => {
221− const api = ensureNativeApi();
222− if (!input.cwd) throw new Error("Pull request thread preparation is unavailable.");
271+ if (!input.cwd || !input.environmentId) {
272+ throw new Error("Pull request thread preparation is unavailable.");
273+ }
274+ const api = ensureEnvironmentApi(input.environmentId);
223275 return api.git.preparePullRequestThread({
224276 cwd: input.cwd,
225277 reference: args.reference,
@@ -228,7 +280,7 @@ export function gitPreparePullRequestThreadMutationOptions(input: { · 2 unmodified lines
228280 });
229281 },
230282 onSuccess: async () => {
231− await invalidateGitBranchQueries(input.queryClient, input.cwd);
283+ await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
232284 },
233285 });
234286 }