History

fix: reuse git action toast after feature-branch checkout

Julius Marminge committed 8 months ago

Commit 075df4e1 parent e4ab1091 file · +39 −22Browse files

Unified · Split

apps/web/src/components/GitActionsControl.tsx+39−22
@@ -58,6 +58,8 @@ interface PendingDefaultBranchAction { · 57 unmodified lines
5858 onConfirmed?: () => void;
5959 }
6060
61+type GitActionToastId = ReturnType<typeof toastManager.add>;
62+
6163 function getMenuActionDisabledReason(
6264 item: GitActionMenuItem,
6365 gitStatus: GitStatusResult | null,
@@ -245,6 +247,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions · 181 unmodified lines
245247 skipDefaultBranchPrompt = false,
246248 statusOverride,
247249 isDefaultBranchOverride,
250+ progressToastId,
248251 }: {
249252 action: GitStackedAction;
250253 commitMessage?: string;
@@ -253,6 +256,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions · 2 unmodified lines
253256 skipDefaultBranchPrompt?: boolean;
254257 statusOverride?: GitStatusResult | null;
255258 isDefaultBranchOverride?: boolean;
259+ progressToastId?: GitActionToastId;
256260 }) => {
257261 const actionStatus = statusOverride ?? gitStatusForActions;
258262 const actionIsDefaultBranch = isDefaultBranchOverride ?? isDefaultBranch;
@@ -284,17 +288,28 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions · 25 unmodified lines
284288 forcePushOnly: forcePushOnlyProgress,
285289 ...(pushTarget ? { pushTarget } : {}),
286290 });
287− const progressToastId = toastManager.add({
288− type: "loading",
289− title: progressStages[0] ?? "Running git action...",
290− timeout: 0,
291− data: threadToastData,
292− });
291+ const resolvedProgressToastId =
292+ progressToastId ??
293+ toastManager.add({
294+ type: "loading",
295+ title: progressStages[0] ?? "Running git action...",
296+ timeout: 0,
297+ data: threadToastData,
298+ });
299+
300+ if (progressToastId) {
301+ toastManager.update(progressToastId, {
302+ type: "loading",
303+ title: progressStages[0] ?? "Running git action...",
304+ timeout: 0,
305+ data: threadToastData,
306+ });
307+ }
293308
294309 let stageIndex = 0;
295310 const stageInterval = setInterval(() => {
296311 stageIndex = Math.min(stageIndex + 1, progressStages.length - 1);
297− toastManager.update(progressToastId, {
312+ toastManager.update(resolvedProgressToastId, {
298313 title: progressStages[stageIndex] ?? "Running git action...",
299314 type: "loading",
300315 timeout: 0,
@@ -329,10 +344,10 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions · 28 unmodified lines
329344 result.push.status === "pushed" &&
330345 !actionIsDefaultBranch;
331346 const closeResultToast = () => {
332− toastManager.close(progressToastId);
347+ toastManager.close(resolvedProgressToastId);
333348 };
334349
335− toastManager.update(progressToastId, {
350+ toastManager.update(resolvedProgressToastId, {
336351 type: "success",
337352 title: resultToast.title,
338353 description: resultToast.description,
@@ -386,7 +401,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions · 47 unmodified lines
386401 });
387402 } catch (err) {
388403 stopProgressUpdates();
389− toastManager.update(progressToastId, {
404+ toastManager.update(resolvedProgressToastId, {
390405 type: "error",
391406 title: "Action failed",
392407 description: err instanceof Error ? err.message : "An error occurred.",
@@ -429,17 +444,11 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions · 36 unmodified lines
429444 );
430445
431446 const checkoutPromise = createBranchAndCheckoutMutation.mutateAsync(branchName);
432− toastManager.promise(checkoutPromise, {
433− loading: { title: `Creating ${branchName}...`, data: threadToastData },
434− success: () => ({
435− title: `Checked out ${branchName}`,
436− data: threadToastData,
437− }),
438− error: (error) => ({
439− title: "Failed to checkout feature branch",
440− description: error instanceof Error ? error.message : "An error occurred.",
441− data: threadToastData,
442− }),
447+ const checkoutToastId = toastManager.add({
448+ type: "loading",
449+ title: `Creating ${branchName}...`,
450+ timeout: 0,
451+ data: threadToastData,
443452 });
444453
445454 void checkoutPromise
@@ -459,9 +468,17 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions · 13 unmodified lines
459468 skipDefaultBranchPrompt: true,
460469 statusOverride,
461470 isDefaultBranchOverride: false,
471+ progressToastId: checkoutToastId,
462472 });
463473 })
464− .catch(() => undefined);
474+ .catch((error) => {
475+ toastManager.update(checkoutToastId, {
476+ type: "error",
477+ title: "Failed to checkout feature branch",
478+ description: error instanceof Error ? error.message : "An error occurred.",
479+ data: threadToastData,
480+ });
481+ });
465482 },
466483 [
467484 branchList?.branches,