Open History to see the last commit
1import type { GitStackedAction, GitStatusResult, ThreadId } from "@t3tools/contracts";
2import { useIsMutating, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
3import { useCallback, useEffect, useMemo, useState } from "react";
4import { ChevronDownIcon, CloudUploadIcon, GitCommitIcon, InfoIcon } from "lucide-react";
5import { GitHubIcon } from "./Icons";
6import {
7 buildGitActionProgressStages,
8 buildMenuItems,
9 type GitActionIconName,
10 type GitActionMenuItem,
11 type GitQuickAction,
12 type DefaultBranchConfirmableAction,
13 requiresDefaultBranchConfirmation,
14 resolveDefaultBranchActionDialogCopy,
15 resolveQuickAction,
16 summarizeGitResult,
17} from "./GitActionsControl.logic";
18import { Button } from "~/components/ui/button";
19import {
20 Dialog,
21 DialogDescription,
22 DialogFooter,
23 DialogHeader,
24 DialogPanel,
25 DialogPopup,
26 DialogTitle,
27} from "~/components/ui/dialog";
28import { Group, GroupSeparator } from "~/components/ui/group";
29import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
30import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
31import { ScrollArea } from "~/components/ui/scroll-area";
32import { Textarea } from "~/components/ui/textarea";
33import { toastManager } from "~/components/ui/toast";
34import {
35 gitBranchesQueryOptions,
36 gitInitMutationOptions,
37 gitMutationKeys,
38 gitPullMutationOptions,
39 gitRunStackedActionMutationOptions,
40 gitStatusQueryOptions,
41 invalidateGitQueries,
42} from "~/lib/gitReactQuery";
43import { preferredTerminalEditor, resolvePathLinkTarget } from "~/terminal-links";
44import { readNativeApi } from "~/nativeApi";
45
46interface GitActionsControlProps {
47 gitCwd: string | null;
48 activeThreadId: ThreadId | null;
49}
50
51interface PendingDefaultBranchAction {
52 action: DefaultBranchConfirmableAction;
53 branchName: string;
54 includesCommit: boolean;
55 commitMessage?: string;
56 forcePushOnlyProgress: boolean;
57 onConfirmed?: () => void;
58}
59
60type GitActionToastId = ReturnType<typeof toastManager.add>;
61
62function getMenuActionDisabledReason(
63 item: GitActionMenuItem,
64 gitStatus: GitStatusResult | null,
65 isBusy: boolean,
66): string | null {
67 if (!item.disabled) return null;
68 if (isBusy) return "Git action in progress.";
69 if (!gitStatus) return "Git status is unavailable.";
70
71 const hasBranch = gitStatus.branch !== null;
72 const hasChanges = gitStatus.hasWorkingTreeChanges;
73 const hasOpenPr = gitStatus.pr?.state === "open";
74 const isAhead = gitStatus.aheadCount > 0;
75 const isBehind = gitStatus.behindCount > 0;
76
77 if (item.id === "commit") {
78 if (!hasChanges) {
79 return "Worktree is clean. Make changes before committing.";
80 }
81 return "Commit is currently unavailable.";
82 }
83
84 if (item.id === "push") {
85 if (!hasBranch) {
86 return "Detached HEAD: checkout a branch before pushing.";
87 }
88 if (hasChanges) {
89 return "Commit or stash local changes before pushing.";
90 }
91 if (isBehind) {
92 return "Branch is behind upstream. Pull/rebase before pushing.";
93 }
94 if (!isAhead) {
95 return "No local commits to push.";
96 }
97 return "Push is currently unavailable.";
98 }
99
100 if (hasOpenPr) {
101 return "View PR is currently unavailable.";
102 }
103 if (!hasBranch) {
104 return "Detached HEAD: checkout a branch before creating a PR.";
105 }
106 if (hasChanges) {
107 return "Commit local changes before creating a PR.";
108 }
109 if (!isAhead) {
110 return "No local commits to include in a PR.";
111 }
112 if (isBehind) {
113 return "Branch is behind upstream. Pull/rebase before creating a PR.";
114 }
115 return "Create PR is currently unavailable.";
116}
117
118const COMMIT_DIALOG_TITLE = "Commit changes";
119const COMMIT_DIALOG_DESCRIPTION =
120 "Review and confirm your commit. Leave the message blank to auto-generate one.";
121
122function GitActionItemIcon({ icon }: { icon: GitActionIconName }) {
123 if (icon === "commit") return <GitCommitIcon />;
124 if (icon === "push") return <CloudUploadIcon />;
125 return <GitHubIcon />;
126}
127
128function GitQuickActionIcon({ quickAction }: { quickAction: GitQuickAction }) {
129 const iconClassName = "size-3.5";
130 if (quickAction.kind === "open_pr") return <GitHubIcon className={iconClassName} />;
131 if (quickAction.kind === "run_pull") return <InfoIcon className={iconClassName} />;
132 if (quickAction.kind === "run_action") {
133 if (quickAction.action === "commit") return <GitCommitIcon className={iconClassName} />;
134 if (quickAction.action === "commit_push") return <CloudUploadIcon className={iconClassName} />;
135 return <GitHubIcon className={iconClassName} />;
136 }
137 if (quickAction.label === "Commit") return <GitCommitIcon className={iconClassName} />;
138 return <InfoIcon className={iconClassName} />;
139}
140
141export default function GitActionsControl({ gitCwd, activeThreadId }: GitActionsControlProps) {
142 const threadToastData = useMemo(
143 () => (activeThreadId ? { threadId: activeThreadId } : undefined),
144 [activeThreadId],
145 );
146 const queryClient = useQueryClient();
147 const [isCommitDialogOpen, setIsCommitDialogOpen] = useState(false);
148 const [dialogCommitMessage, setDialogCommitMessage] = useState("");
149 const [pendingDefaultBranchAction, setPendingDefaultBranchAction] =
150 useState<PendingDefaultBranchAction | null>(null);
151
152 const { data: gitStatus = null, error: gitStatusError } = useQuery(gitStatusQueryOptions(gitCwd));
153
154 const { data: branchList = null } = useQuery(gitBranchesQueryOptions(gitCwd));
155 // Default to true while loading so we don't flash init controls.
156 const isRepo = branchList?.isRepo ?? true;
157 const currentBranch = branchList?.branches.find((branch) => branch.current)?.name ?? null;
158 const isGitStatusOutOfSync =
159 !!gitStatus?.branch && !!currentBranch && gitStatus.branch !== currentBranch;
160
161 useEffect(() => {
162 if (!isGitStatusOutOfSync) return;
163 void invalidateGitQueries(queryClient);
164 }, [isGitStatusOutOfSync, queryClient]);
165
166 const gitStatusForActions = isGitStatusOutOfSync ? null : gitStatus;
167
168 const initMutation = useMutation(gitInitMutationOptions({ cwd: gitCwd, queryClient }));
169
170 const runImmediateGitActionMutation = useMutation(
171 gitRunStackedActionMutationOptions({ cwd: gitCwd, queryClient }),
172 );
173 const pullMutation = useMutation(gitPullMutationOptions({ cwd: gitCwd, queryClient }));
174
175 const isRunStackedActionRunning =
176 useIsMutating({ mutationKey: gitMutationKeys.runStackedAction(gitCwd) }) > 0;
177 const isPullRunning = useIsMutating({ mutationKey: gitMutationKeys.pull(gitCwd) }) > 0;
178 const isGitActionRunning = isRunStackedActionRunning || isPullRunning;
179 const isDefaultBranch = useMemo(() => {
180 const branchName = gitStatusForActions?.branch;
181 if (!branchName) return false;
182 const current = branchList?.branches.find((branch) => branch.name === branchName);
183 return current?.isDefault ?? (branchName === "main" || branchName === "master");
184 }, [branchList?.branches, gitStatusForActions?.branch]);
185
186 const gitActionMenuItems = useMemo(
187 () => buildMenuItems(gitStatusForActions, isGitActionRunning),
188 [gitStatusForActions, isGitActionRunning],
189 );
190 const quickAction = useMemo(
191 () => resolveQuickAction(gitStatusForActions, isGitActionRunning, isDefaultBranch),
192 [gitStatusForActions, isDefaultBranch, isGitActionRunning],
193 );
194 const quickActionDisabledReason = quickAction.disabled
195 ? (quickAction.hint ?? "This action is currently unavailable.")
196 : null;
197 const pendingDefaultBranchActionCopy = pendingDefaultBranchAction
198 ? resolveDefaultBranchActionDialogCopy({
199 action: pendingDefaultBranchAction.action,
200 branchName: pendingDefaultBranchAction.branchName,
201 includesCommit: pendingDefaultBranchAction.includesCommit,
202 })
203 : null;
204
205 const openExistingPr = useCallback(async () => {
206 const api = readNativeApi();
207 if (!api) {
208 toastManager.add({
209 type: "error",
210 title: "Link opening is unavailable.",
211 data: threadToastData,
212 });
213 return;
214 }
215 const prUrl = gitStatusForActions?.pr?.state === "open" ? gitStatusForActions.pr.url : null;
216 if (!prUrl) {
217 toastManager.add({
218 type: "error",
219 title: "No open PR found.",
220 data: threadToastData,
221 });
222 return;
223 }
224 void api.shell.openExternal(prUrl).catch((err) => {
225 toastManager.add({
226 type: "error",
227 title: "Unable to open PR link",
228 description: err instanceof Error ? err.message : "An error occurred.",
229 data: threadToastData,
230 });
231 });
232 }, [gitStatusForActions?.pr?.state, gitStatusForActions?.pr?.url, threadToastData]);
233
234 const runGitActionWithToast = useCallback(
235 async ({
236 action,
237 commitMessage,
238 forcePushOnlyProgress = false,
239 onConfirmed,
240 skipDefaultBranchPrompt = false,
241 statusOverride,
242 featureBranch = false,
243 isDefaultBranchOverride,
244 progressToastId,
245 }: {
246 action: GitStackedAction;
247 commitMessage?: string;
248 forcePushOnlyProgress?: boolean;
249 onConfirmed?: () => void;
250 skipDefaultBranchPrompt?: boolean;
251 statusOverride?: GitStatusResult | null;
252 featureBranch?: boolean;
253 isDefaultBranchOverride?: boolean;
254 progressToastId?: GitActionToastId;
255 }) => {
256 const actionStatus = statusOverride ?? gitStatusForActions;
257 const actionBranch = actionStatus?.branch ?? null;
258 const actionIsDefaultBranch = isDefaultBranchOverride ?? (featureBranch ? false : isDefaultBranch);
259 const includesCommit =
260 !forcePushOnlyProgress && (action === "commit" || !!actionStatus?.hasWorkingTreeChanges);
261 if (
262 !skipDefaultBranchPrompt &&
263 requiresDefaultBranchConfirmation(action, actionIsDefaultBranch) &&
264 actionBranch
265 ) {
266 if (action !== "commit_push" && action !== "commit_push_pr") {
267 return;
268 }
269 setPendingDefaultBranchAction({
270 action,
271 branchName: actionBranch,
272 includesCommit,
273 ...(commitMessage ? { commitMessage } : {}),
274 forcePushOnlyProgress,
275 ...(onConfirmed ? { onConfirmed } : {}),
276 });
277 return;
278 }
279 onConfirmed?.();
280
281 const pushTarget = !featureBranch && actionBranch ? `origin/${actionBranch}` : undefined;
282 const progressStages = buildGitActionProgressStages({
283 action,
284 hasCustomCommitMessage: !!commitMessage?.trim(),
285 hasWorkingTreeChanges: !!actionStatus?.hasWorkingTreeChanges,
286 forcePushOnly: forcePushOnlyProgress,
287 featureBranch,
288 ...(pushTarget ? { pushTarget } : {}),
289 });
290 const resolvedProgressToastId =
291 progressToastId ??
292 toastManager.add({
293 type: "loading",
294 title: progressStages[0] ?? "Running git action...",
295 timeout: 0,
296 data: threadToastData,
297 });
298
299 if (progressToastId) {
300 toastManager.update(progressToastId, {
301 type: "loading",
302 title: progressStages[0] ?? "Running git action...",
303 timeout: 0,
304 data: threadToastData,
305 });
306 }
307
308 let stageIndex = 0;
309 const stageInterval = setInterval(() => {
310 stageIndex = Math.min(stageIndex + 1, progressStages.length - 1);
311 toastManager.update(resolvedProgressToastId, {
312 title: progressStages[stageIndex] ?? "Running git action...",
313 type: "loading",
314 timeout: 0,
315 data: threadToastData,
316 });
317 }, 1100);
318
319 const stopProgressUpdates = () => {
320 clearInterval(stageInterval);
321 };
322
323 const promise = runImmediateGitActionMutation.mutateAsync({
324 action,
325 ...(commitMessage ? { commitMessage } : {}),
326 ...(featureBranch ? { featureBranch } : {}),
327 });
328
329 try {
330 const result = await promise;
331 stopProgressUpdates();
332 const resultToast = summarizeGitResult(result);
333
334 const existingOpenPrUrl = actionStatus?.pr?.state === "open" ? actionStatus.pr.url : undefined;
335 const prUrl = result.pr.url ?? existingOpenPrUrl;
336 const shouldOfferPushCta = action === "commit" && result.commit.status === "created";
337 const shouldOfferOpenPrCta =
338 (action === "commit_push" || action === "commit_push_pr") &&
339 !!prUrl &&
340 (!actionIsDefaultBranch ||
341 result.pr.status === "created" ||
342 result.pr.status === "opened_existing");
343 const shouldOfferCreatePrCta =
344 action === "commit_push" &&
345 !prUrl &&
346 result.push.status === "pushed" &&
347 !actionIsDefaultBranch;
348 const closeResultToast = () => {
349 toastManager.close(resolvedProgressToastId);
350 };
351
352 toastManager.update(resolvedProgressToastId, {
353 type: "success",
354 title: resultToast.title,
355 description: resultToast.description,
356 timeout: 0,
357 data: {
358 ...threadToastData,
359 dismissAfterVisibleMs: 10_000,
360 },
361 ...(shouldOfferPushCta
362 ? {
363 actionProps: {
364 children: "Push",
365 onClick: () => {
366 void runGitActionWithToast({
367 action: "commit_push",
368 forcePushOnlyProgress: true,
369 onConfirmed: closeResultToast,
370 statusOverride: actionStatus,
371 isDefaultBranchOverride: actionIsDefaultBranch,
372 });
373 },
374 },
375 }
376 : shouldOfferOpenPrCta
377 ? {
378 actionProps: {
379 children: "View PR",
380 onClick: () => {
381 const api = readNativeApi();
382 if (!api) return;
383 closeResultToast();
384 void api.shell.openExternal(prUrl);
385 },
386 },
387 }
388 : shouldOfferCreatePrCta
389 ? {
390 actionProps: {
391 children: "Create PR",
392 onClick: () => {
393 closeResultToast();
394 void runGitActionWithToast({
395 action: "commit_push_pr",
396 forcePushOnlyProgress: true,
397 statusOverride: actionStatus,
398 isDefaultBranchOverride: actionIsDefaultBranch,
399 });
400 },
401 },
402 }
403 : {}),
404 });
405 } catch (err) {
406 stopProgressUpdates();
407 toastManager.update(resolvedProgressToastId, {
408 type: "error",
409 title: "Action failed",
410 description: err instanceof Error ? err.message : "An error occurred.",
411 data: threadToastData,
412 });
413 }
414 },
415
416 [
417 isDefaultBranch,
418 runImmediateGitActionMutation,
419 setPendingDefaultBranchAction,
420 threadToastData,
421 gitStatusForActions,
422 ],
423 );
424
425 const continuePendingDefaultBranchAction = useCallback(() => {
426 if (!pendingDefaultBranchAction) return;
427 const { action, commitMessage, forcePushOnlyProgress, onConfirmed } = pendingDefaultBranchAction;
428 setPendingDefaultBranchAction(null);
429 void runGitActionWithToast({
430 action,
431 ...(commitMessage ? { commitMessage } : {}),
432 forcePushOnlyProgress,
433 ...(onConfirmed ? { onConfirmed } : {}),
434 skipDefaultBranchPrompt: true,
435 });
436 }, [pendingDefaultBranchAction, runGitActionWithToast]);
437
438 const checkoutNewBranchAndRunAction = useCallback(
439 (actionParams: {
440 action: GitStackedAction;
441 commitMessage?: string;
442 forcePushOnlyProgress?: boolean;
443 onConfirmed?: () => void;
444 }) => {
445 void runGitActionWithToast({
446 ...actionParams,
447 featureBranch: true,
448 skipDefaultBranchPrompt: true,
449 });
450 },
451 [runGitActionWithToast],
452 );
453
454 const checkoutFeatureBranchAndContinuePendingAction = useCallback(() => {
455 if (!pendingDefaultBranchAction) return;
456 const { action, commitMessage, forcePushOnlyProgress, onConfirmed } = pendingDefaultBranchAction;
457 setPendingDefaultBranchAction(null);
458 checkoutNewBranchAndRunAction({
459 action,
460 ...(commitMessage ? { commitMessage } : {}),
461 forcePushOnlyProgress,
462 ...(onConfirmed ? { onConfirmed } : {}),
463 });
464 }, [pendingDefaultBranchAction, checkoutNewBranchAndRunAction]);
465
466 const runDialogActionOnNewBranch = useCallback(() => {
467 if (!isCommitDialogOpen) return;
468 const commitMessage = dialogCommitMessage.trim();
469
470 setIsCommitDialogOpen(false);
471 setDialogCommitMessage("");
472
473 checkoutNewBranchAndRunAction({
474 action: "commit",
475 ...(commitMessage ? { commitMessage } : {}),
476 });
477 }, [isCommitDialogOpen, dialogCommitMessage, checkoutNewBranchAndRunAction]);
478
479 const runQuickAction = useCallback(() => {
480 if (quickAction.kind === "open_pr") {
481 void openExistingPr();
482 return;
483 }
484 if (quickAction.kind === "run_pull") {
485 const promise = pullMutation.mutateAsync();
486 toastManager.promise(promise, {
487 loading: { title: "Pulling...", data: threadToastData },
488 success: (result) => ({
489 title: result.status === "pulled" ? "Pulled" : "Already up to date",
490 description:
491 result.status === "pulled"
492 ? `Updated ${result.branch} from ${result.upstreamBranch ?? "upstream"}`
493 : `${result.branch} is already synchronized.`,
494 data: threadToastData,
495 }),
496 error: (err) => ({
497 title: "Pull failed",
498 description: err instanceof Error ? err.message : "An error occurred.",
499 data: threadToastData,
500 }),
501 });
502 void promise.catch(() => undefined);
503 return;
504 }
505 if (quickAction.kind === "show_hint") {
506 toastManager.add({
507 type: "info",
508 title: quickAction.label,
509 description: quickAction.hint,
510 data: threadToastData,
511 });
512 return;
513 }
514 if (quickAction.action) {
515 void runGitActionWithToast({ action: quickAction.action });
516 }
517 }, [openExistingPr, pullMutation, quickAction, runGitActionWithToast, threadToastData]);
518
519 const openDialogForMenuItem = useCallback(
520 (item: GitActionMenuItem) => {
521 if (item.disabled) return;
522 if (item.kind === "open_pr") {
523 void openExistingPr();
524 return;
525 }
526 if (item.dialogAction === "push") {
527 void runGitActionWithToast({ action: "commit_push", forcePushOnlyProgress: true });
528 return;
529 }
530 if (item.dialogAction === "create_pr") {
531 void runGitActionWithToast({ action: "commit_push_pr" });
532 return;
533 }
534 setIsCommitDialogOpen(true);
535 },
536 [openExistingPr, runGitActionWithToast, setIsCommitDialogOpen],
537 );
538
539 const runDialogAction = useCallback(() => {
540 if (!isCommitDialogOpen) return;
541 const commitMessage = dialogCommitMessage.trim();
542 setIsCommitDialogOpen(false);
543 setDialogCommitMessage("");
544 void runGitActionWithToast({
545 action: "commit",
546 ...(commitMessage ? { commitMessage } : {}),
547 });
548 }, [
549 dialogCommitMessage,
550 isCommitDialogOpen,
551 runGitActionWithToast,
552 setDialogCommitMessage,
553 setIsCommitDialogOpen,
554 ]);
555
556 const openChangedFileInEditor = useCallback(
557 (filePath: string) => {
558 const api = readNativeApi();
559 if (!api || !gitCwd) {
560 toastManager.add({
561 type: "error",
562 title: "Editor opening is unavailable.",
563 data: threadToastData,
564 });
565 return;
566 }
567 const target = resolvePathLinkTarget(filePath, gitCwd);
568 void api.shell.openInEditor(target, preferredTerminalEditor()).catch((error) => {
569 toastManager.add({
570 type: "error",
571 title: "Unable to open file",
572 description: error instanceof Error ? error.message : "An error occurred.",
573 data: threadToastData,
574 });
575 });
576 },
577 [gitCwd, threadToastData],
578 );
579
580 if (!gitCwd) return null;
581
582 return (
583 <>
584 {!isRepo ? (
585 <Button
586 variant="outline"
587 size="xs"
588 disabled={initMutation.isPending}
589 onClick={() => initMutation.mutate()}
590 >
591 {initMutation.isPending ? "Initializing..." : "Initialize Git"}
592 </Button>
593 ) : (
594 <Group aria-label="Git actions">
595 {quickActionDisabledReason ? (
596 <Popover>
597 <PopoverTrigger
598 openOnHover
599 render={
600 <Button
601 aria-disabled="true"
602 className="cursor-not-allowed rounded-e-none border-e-0 opacity-64 before:rounded-e-none"
603 size="xs"
604 variant="outline"
605 />
606 }
607 >
608 <GitQuickActionIcon quickAction={quickAction} />
609 <span className="sr-only @sm/header-actions:not-sr-only @sm/header-actions:ml-0.5">
610 {quickAction.label}
611 </span>
612 </PopoverTrigger>
613 <PopoverPopup tooltipStyle side="bottom" align="start">
614 {quickActionDisabledReason}
615 </PopoverPopup>
616 </Popover>
617 ) : (
618 <Button
619 variant="outline"
620 size="xs"
621 disabled={isGitActionRunning || quickAction.disabled}
622 onClick={runQuickAction}
623 >
624 <GitQuickActionIcon quickAction={quickAction} />
625 <span className="sr-only @sm/header-actions:not-sr-only @sm/header-actions:ml-0.5">
626 {quickAction.label}
627 </span>
628 </Button>
629 )}
630 <GroupSeparator className="hidden @sm/header-actions:block" />
631 <Menu
632 onOpenChange={(open) => {
633 if (open) void invalidateGitQueries(queryClient);
634 }}
635 >
636 <MenuTrigger
637 render={<Button aria-label="Git action options" size="icon-xs" variant="outline" />}
638 disabled={isGitActionRunning}
639 >
640 <ChevronDownIcon aria-hidden="true" className="size-4" />
641 </MenuTrigger>
642 <MenuPopup align="end" className="w-full">
643 {gitActionMenuItems.map((item) => {
644 const disabledReason = getMenuActionDisabledReason(
645 item,
646 gitStatusForActions,
647 isGitActionRunning,
648 );
649 if (item.disabled && disabledReason) {
650 return (
651 <Popover key={`${item.id}-${item.label}`}>
652 <PopoverTrigger
653 openOnHover
654 nativeButton={false}
655 render={<span className="block w-max cursor-not-allowed" />}
656 >
657 <MenuItem className="w-full" disabled>
658 <GitActionItemIcon icon={item.icon} />
659 {item.label}
660 </MenuItem>
661 </PopoverTrigger>
662 <PopoverPopup tooltipStyle side="left" align="center">
663 {disabledReason}
664 </PopoverPopup>
665 </Popover>
666 );
667 }
668
669 return (
670 <MenuItem
671 key={`${item.id}-${item.label}`}
672 disabled={item.disabled}
673 onClick={() => {
674 openDialogForMenuItem(item);
675 }}
676 >
677 <GitActionItemIcon icon={item.icon} />
678 {item.label}
679 </MenuItem>
680 );
681 })}
682 {gitStatusForActions?.branch === null && (
683 <p className="px-2 py-1.5 text-xs text-warning">
684 Detached HEAD: create and checkout a branch to enable push and PR actions.
685 </p>
686 )}
687 {gitStatusForActions &&
688 gitStatusForActions.branch !== null &&
689 !gitStatusForActions.hasWorkingTreeChanges &&
690 gitStatusForActions.behindCount > 0 &&
691 gitStatusForActions.aheadCount === 0 && (
692 <p className="px-2 py-1.5 text-xs text-warning">
693 Behind upstream. Pull/rebase first.
694 </p>
695 )}
696 {isGitStatusOutOfSync && (
697 <p className="px-2 py-1.5 text-xs text-muted-foreground">
698 Refreshing git status...
699 </p>
700 )}
701 {gitStatusError && (
702 <p className="px-2 py-1.5 text-xs text-destructive">{gitStatusError.message}</p>
703 )}
704 </MenuPopup>
705 </Menu>
706 </Group>
707 )}
708
709 <Dialog
710 open={isCommitDialogOpen}
711 onOpenChange={(open) => {
712 if (!open) {
713 setIsCommitDialogOpen(false);
714 setDialogCommitMessage("");
715 }
716 }}
717 >
718 <DialogPopup>
719 <DialogHeader>
720 <DialogTitle>{COMMIT_DIALOG_TITLE}</DialogTitle>
721 <DialogDescription>{COMMIT_DIALOG_DESCRIPTION}</DialogDescription>
722 </DialogHeader>
723 <DialogPanel className="space-y-4">
724 <div className="space-y-3 rounded-lg border border-input bg-muted/40 p-3 text-xs">
725 <div className="grid grid-cols-[auto_1fr] items-center gap-x-2 gap-y-1">
726 <span className="text-muted-foreground">Branch</span>
727 <span className="flex items-center justify-between gap-2">
728 <span className="font-medium">{gitStatusForActions?.branch ?? "(detached HEAD)"}</span>
729 {isDefaultBranch && (
730 <span className="text-right text-warning text-xs">Warning: default branch</span>
731 )}
732 </span>
733 </div>
734 <div className="space-y-1">
735 <p className="text-muted-foreground">Files</p>
736 {!gitStatusForActions || gitStatusForActions.workingTree.files.length === 0 ? (
737 <p className="font-medium">none</p>
738 ) : (
739 <div className="space-y-2">
740 <ScrollArea className="h-44 rounded-md border border-input bg-background">
741 <div className="space-y-1 p-1">
742 {gitStatusForActions.workingTree.files.map((file) => (
743 <button
744 type="button"
745 key={file.path}
746 className="flex w-full items-center justify-between gap-3 rounded-md px-2 py-1 font-mono text-left transition-colors hover:bg-accent/50"
747 onClick={() => openChangedFileInEditor(file.path)}
748 >
749 <span className="truncate">{file.path}</span>
750 <span className="shrink-0">
751 <span className="text-success">+{file.insertions}</span>
752 <span className="text-muted-foreground"> / </span>
753 <span className="text-destructive">-{file.deletions}</span>
754 </span>
755 </button>
756 ))}
757 </div>
758 </ScrollArea>
759 <div className="flex justify-end font-mono">
760 <span className="text-success">
761 +{gitStatusForActions.workingTree.insertions}
762 </span>
763 <span className="text-muted-foreground"> / </span>
764 <span className="text-destructive">
765 -{gitStatusForActions.workingTree.deletions}
766 </span>
767 </div>
768 </div>
769 )}
770 </div>
771 </div>
772 <div className="space-y-1">
773 <p className="text-xs font-medium">Commit message (optional)</p>
774 <Textarea
775 value={dialogCommitMessage}
776 onChange={(event) => setDialogCommitMessage(event.target.value)}
777 placeholder="Leave empty to auto-generate"
778 size="sm"
779 />
780 </div>
781 </DialogPanel>
782 <DialogFooter>
783 <Button
784 variant="outline"
785 size="sm"
786 onClick={() => {
787 setIsCommitDialogOpen(false);
788 setDialogCommitMessage("");
789 }}
790 >
791 Cancel
792 </Button>
793 <Button variant="outline" size="sm" onClick={runDialogActionOnNewBranch}>
794 Commit on new branch
795 </Button>
796 <Button size="sm" onClick={runDialogAction}>
797 Commit
798 </Button>
799 </DialogFooter>
800 </DialogPopup>
801 </Dialog>
802
803 <Dialog
804 open={pendingDefaultBranchAction !== null}
805 onOpenChange={(open) => {
806 if (!open) {
807 setPendingDefaultBranchAction(null);
808 }
809 }}
810 >
811 <DialogPopup className="max-w-xl">
812 <DialogHeader>
813 <DialogTitle>
814 {pendingDefaultBranchActionCopy?.title ?? "Run action on default branch?"}
815 </DialogTitle>
816 <DialogDescription>{pendingDefaultBranchActionCopy?.description}</DialogDescription>
817 </DialogHeader>
818 <DialogFooter>
819 <Button variant="outline" size="sm" onClick={() => setPendingDefaultBranchAction(null)}>
820 Abort
821 </Button>
822 <Button variant="outline" size="sm" onClick={continuePendingDefaultBranchAction}>
823 {pendingDefaultBranchActionCopy?.continueLabel ?? "Continue"}
824 </Button>
825 <Button size="sm" onClick={checkoutFeatureBranchAndContinuePendingAction}>
826 Checkout feature branch & continue
827 </Button>
828 </DialogFooter>
829 </DialogPopup>
830 </Dialog>
831 </>
832 );
833}