Co-authored-by: codex <[email protected]> Co-authored-by: Cursor Agent <[email protected]>
1056 lines
38 KiB
TypeScript
1056 lines
38 KiB
TypeScript
import type {
|
|
GitActionProgressEvent,
|
|
GitStackedAction,
|
|
GitStatusResult,
|
|
ThreadId,
|
|
} from "@t3tools/contracts";
|
|
import { useIsMutating, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { useCallback, useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
|
|
import { ChevronDownIcon, CloudUploadIcon, GitCommitIcon, InfoIcon } from "lucide-react";
|
|
import { GitHubIcon } from "./Icons";
|
|
import {
|
|
buildGitActionProgressStages,
|
|
buildMenuItems,
|
|
type GitActionIconName,
|
|
type GitActionMenuItem,
|
|
type GitQuickAction,
|
|
type DefaultBranchConfirmableAction,
|
|
requiresDefaultBranchConfirmation,
|
|
resolveDefaultBranchActionDialogCopy,
|
|
resolveQuickAction,
|
|
summarizeGitResult,
|
|
} from "./GitActionsControl.logic";
|
|
import { Button } from "~/components/ui/button";
|
|
import { Checkbox } from "~/components/ui/checkbox";
|
|
import {
|
|
Dialog,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogPanel,
|
|
DialogPopup,
|
|
DialogTitle,
|
|
} from "~/components/ui/dialog";
|
|
import { Group, GroupSeparator } from "~/components/ui/group";
|
|
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
|
|
import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
|
|
import { ScrollArea } from "~/components/ui/scroll-area";
|
|
import { Textarea } from "~/components/ui/textarea";
|
|
import { toastManager } from "~/components/ui/toast";
|
|
import { openInPreferredEditor } from "~/editorPreferences";
|
|
import {
|
|
gitBranchesQueryOptions,
|
|
gitInitMutationOptions,
|
|
gitMutationKeys,
|
|
gitPullMutationOptions,
|
|
gitRunStackedActionMutationOptions,
|
|
gitStatusQueryOptions,
|
|
invalidateGitQueries,
|
|
} from "~/lib/gitReactQuery";
|
|
import { randomUUID } from "~/lib/utils";
|
|
import { resolvePathLinkTarget } from "~/terminal-links";
|
|
import { readNativeApi } from "~/nativeApi";
|
|
|
|
interface GitActionsControlProps {
|
|
gitCwd: string | null;
|
|
activeThreadId: ThreadId | null;
|
|
}
|
|
|
|
interface PendingDefaultBranchAction {
|
|
action: DefaultBranchConfirmableAction;
|
|
branchName: string;
|
|
includesCommit: boolean;
|
|
commitMessage?: string;
|
|
forcePushOnlyProgress: boolean;
|
|
onConfirmed?: () => void;
|
|
filePaths?: string[];
|
|
}
|
|
|
|
type GitActionToastId = ReturnType<typeof toastManager.add>;
|
|
|
|
interface ActiveGitActionProgress {
|
|
toastId: GitActionToastId;
|
|
actionId: string;
|
|
title: string;
|
|
phaseStartedAtMs: number | null;
|
|
hookStartedAtMs: number | null;
|
|
hookName: string | null;
|
|
lastOutputLine: string | null;
|
|
currentPhaseLabel: string | null;
|
|
}
|
|
|
|
interface RunGitActionWithToastInput {
|
|
action: GitStackedAction;
|
|
commitMessage?: string;
|
|
forcePushOnlyProgress?: boolean;
|
|
onConfirmed?: () => void;
|
|
skipDefaultBranchPrompt?: boolean;
|
|
statusOverride?: GitStatusResult | null;
|
|
featureBranch?: boolean;
|
|
isDefaultBranchOverride?: boolean;
|
|
progressToastId?: GitActionToastId;
|
|
filePaths?: string[];
|
|
}
|
|
|
|
function formatElapsedDescription(startedAtMs: number | null): string | undefined {
|
|
if (startedAtMs === null) {
|
|
return undefined;
|
|
}
|
|
const elapsedSeconds = Math.max(0, Math.floor((Date.now() - startedAtMs) / 1000));
|
|
if (elapsedSeconds < 60) {
|
|
return `Running for ${elapsedSeconds}s`;
|
|
}
|
|
const minutes = Math.floor(elapsedSeconds / 60);
|
|
const seconds = elapsedSeconds % 60;
|
|
return `Running for ${minutes}m ${seconds}s`;
|
|
}
|
|
|
|
function resolveProgressDescription(progress: ActiveGitActionProgress): string | undefined {
|
|
if (progress.lastOutputLine) {
|
|
return progress.lastOutputLine;
|
|
}
|
|
return formatElapsedDescription(progress.hookStartedAtMs ?? progress.phaseStartedAtMs);
|
|
}
|
|
|
|
function getMenuActionDisabledReason({
|
|
item,
|
|
gitStatus,
|
|
isBusy,
|
|
hasOriginRemote,
|
|
}: {
|
|
item: GitActionMenuItem;
|
|
gitStatus: GitStatusResult | null;
|
|
isBusy: boolean;
|
|
hasOriginRemote: boolean;
|
|
}): string | null {
|
|
if (!item.disabled) return null;
|
|
if (isBusy) return "Git action in progress.";
|
|
if (!gitStatus) return "Git status is unavailable.";
|
|
|
|
const hasBranch = gitStatus.branch !== null;
|
|
const hasChanges = gitStatus.hasWorkingTreeChanges;
|
|
const hasOpenPr = gitStatus.pr?.state === "open";
|
|
const isAhead = gitStatus.aheadCount > 0;
|
|
const isBehind = gitStatus.behindCount > 0;
|
|
|
|
if (item.id === "commit") {
|
|
if (!hasChanges) {
|
|
return "Worktree is clean. Make changes before committing.";
|
|
}
|
|
return "Commit is currently unavailable.";
|
|
}
|
|
|
|
if (item.id === "push") {
|
|
if (!hasBranch) {
|
|
return "Detached HEAD: checkout a branch before pushing.";
|
|
}
|
|
if (hasChanges) {
|
|
return "Commit or stash local changes before pushing.";
|
|
}
|
|
if (isBehind) {
|
|
return "Branch is behind upstream. Pull/rebase before pushing.";
|
|
}
|
|
if (!gitStatus.hasUpstream && !hasOriginRemote) {
|
|
return 'Add an "origin" remote before pushing.';
|
|
}
|
|
if (!isAhead) {
|
|
return "No local commits to push.";
|
|
}
|
|
return "Push is currently unavailable.";
|
|
}
|
|
|
|
if (hasOpenPr) {
|
|
return "View PR is currently unavailable.";
|
|
}
|
|
if (!hasBranch) {
|
|
return "Detached HEAD: checkout a branch before creating a PR.";
|
|
}
|
|
if (hasChanges) {
|
|
return "Commit local changes before creating a PR.";
|
|
}
|
|
if (!gitStatus.hasUpstream && !hasOriginRemote) {
|
|
return 'Add an "origin" remote before creating a PR.';
|
|
}
|
|
if (!isAhead) {
|
|
return "No local commits to include in a PR.";
|
|
}
|
|
if (isBehind) {
|
|
return "Branch is behind upstream. Pull/rebase before creating a PR.";
|
|
}
|
|
return "Create PR is currently unavailable.";
|
|
}
|
|
|
|
const COMMIT_DIALOG_TITLE = "Commit changes";
|
|
const COMMIT_DIALOG_DESCRIPTION =
|
|
"Review and confirm your commit. Leave the message blank to auto-generate one.";
|
|
|
|
function GitActionItemIcon({ icon }: { icon: GitActionIconName }) {
|
|
if (icon === "commit") return <GitCommitIcon />;
|
|
if (icon === "push") return <CloudUploadIcon />;
|
|
return <GitHubIcon />;
|
|
}
|
|
|
|
function GitQuickActionIcon({ quickAction }: { quickAction: GitQuickAction }) {
|
|
const iconClassName = "size-3.5";
|
|
if (quickAction.kind === "open_pr") return <GitHubIcon className={iconClassName} />;
|
|
if (quickAction.kind === "run_pull") return <InfoIcon className={iconClassName} />;
|
|
if (quickAction.kind === "run_action") {
|
|
if (quickAction.action === "commit") return <GitCommitIcon className={iconClassName} />;
|
|
if (quickAction.action === "commit_push") return <CloudUploadIcon className={iconClassName} />;
|
|
return <GitHubIcon className={iconClassName} />;
|
|
}
|
|
if (quickAction.label === "Commit") return <GitCommitIcon className={iconClassName} />;
|
|
return <InfoIcon className={iconClassName} />;
|
|
}
|
|
|
|
export default function GitActionsControl({ gitCwd, activeThreadId }: GitActionsControlProps) {
|
|
const threadToastData = useMemo(
|
|
() => (activeThreadId ? { threadId: activeThreadId } : undefined),
|
|
[activeThreadId],
|
|
);
|
|
const queryClient = useQueryClient();
|
|
const [isCommitDialogOpen, setIsCommitDialogOpen] = useState(false);
|
|
const [dialogCommitMessage, setDialogCommitMessage] = useState("");
|
|
const [excludedFiles, setExcludedFiles] = useState<ReadonlySet<string>>(new Set());
|
|
const [isEditingFiles, setIsEditingFiles] = useState(false);
|
|
const [pendingDefaultBranchAction, setPendingDefaultBranchAction] =
|
|
useState<PendingDefaultBranchAction | null>(null);
|
|
const activeGitActionProgressRef = useRef<ActiveGitActionProgress | null>(null);
|
|
|
|
const updateActiveProgressToast = useCallback(() => {
|
|
const progress = activeGitActionProgressRef.current;
|
|
if (!progress) {
|
|
return;
|
|
}
|
|
toastManager.update(progress.toastId, {
|
|
type: "loading",
|
|
title: progress.title,
|
|
description: resolveProgressDescription(progress),
|
|
timeout: 0,
|
|
data: threadToastData,
|
|
});
|
|
}, [threadToastData]);
|
|
|
|
const { data: gitStatus = null, error: gitStatusError } = useQuery(gitStatusQueryOptions(gitCwd));
|
|
|
|
const { data: branchList = null } = useQuery(gitBranchesQueryOptions(gitCwd));
|
|
// Default to true while loading so we don't flash init controls.
|
|
const isRepo = branchList?.isRepo ?? true;
|
|
const hasOriginRemote = branchList?.hasOriginRemote ?? false;
|
|
const currentBranch = branchList?.branches.find((branch) => branch.current)?.name ?? null;
|
|
const isGitStatusOutOfSync =
|
|
!!gitStatus?.branch && !!currentBranch && gitStatus.branch !== currentBranch;
|
|
|
|
useEffect(() => {
|
|
if (!isGitStatusOutOfSync) return;
|
|
void invalidateGitQueries(queryClient);
|
|
}, [isGitStatusOutOfSync, queryClient]);
|
|
|
|
const gitStatusForActions = isGitStatusOutOfSync ? null : gitStatus;
|
|
|
|
const allFiles = gitStatusForActions?.workingTree.files ?? [];
|
|
const selectedFiles = allFiles.filter((f) => !excludedFiles.has(f.path));
|
|
const allSelected = excludedFiles.size === 0;
|
|
const noneSelected = selectedFiles.length === 0;
|
|
|
|
const initMutation = useMutation(gitInitMutationOptions({ cwd: gitCwd, queryClient }));
|
|
|
|
const runImmediateGitActionMutation = useMutation(
|
|
gitRunStackedActionMutationOptions({
|
|
cwd: gitCwd,
|
|
queryClient,
|
|
}),
|
|
);
|
|
const pullMutation = useMutation(gitPullMutationOptions({ cwd: gitCwd, queryClient }));
|
|
|
|
const isRunStackedActionRunning =
|
|
useIsMutating({ mutationKey: gitMutationKeys.runStackedAction(gitCwd) }) > 0;
|
|
const isPullRunning = useIsMutating({ mutationKey: gitMutationKeys.pull(gitCwd) }) > 0;
|
|
const isGitActionRunning = isRunStackedActionRunning || isPullRunning;
|
|
const isDefaultBranch = useMemo(() => {
|
|
const branchName = gitStatusForActions?.branch;
|
|
if (!branchName) return false;
|
|
const current = branchList?.branches.find((branch) => branch.name === branchName);
|
|
return current?.isDefault ?? (branchName === "main" || branchName === "master");
|
|
}, [branchList?.branches, gitStatusForActions?.branch]);
|
|
|
|
const gitActionMenuItems = useMemo(
|
|
() => buildMenuItems(gitStatusForActions, isGitActionRunning, hasOriginRemote),
|
|
[gitStatusForActions, hasOriginRemote, isGitActionRunning],
|
|
);
|
|
const quickAction = useMemo(
|
|
() =>
|
|
resolveQuickAction(gitStatusForActions, isGitActionRunning, isDefaultBranch, hasOriginRemote),
|
|
[gitStatusForActions, hasOriginRemote, isDefaultBranch, isGitActionRunning],
|
|
);
|
|
const quickActionDisabledReason = quickAction.disabled
|
|
? (quickAction.hint ?? "This action is currently unavailable.")
|
|
: null;
|
|
const pendingDefaultBranchActionCopy = pendingDefaultBranchAction
|
|
? resolveDefaultBranchActionDialogCopy({
|
|
action: pendingDefaultBranchAction.action,
|
|
branchName: pendingDefaultBranchAction.branchName,
|
|
includesCommit: pendingDefaultBranchAction.includesCommit,
|
|
})
|
|
: null;
|
|
|
|
useEffect(() => {
|
|
const interval = window.setInterval(() => {
|
|
if (!activeGitActionProgressRef.current) {
|
|
return;
|
|
}
|
|
updateActiveProgressToast();
|
|
}, 1000);
|
|
|
|
return () => {
|
|
window.clearInterval(interval);
|
|
};
|
|
}, [updateActiveProgressToast]);
|
|
|
|
const openExistingPr = useCallback(async () => {
|
|
const api = readNativeApi();
|
|
if (!api) {
|
|
toastManager.add({
|
|
type: "error",
|
|
title: "Link opening is unavailable.",
|
|
data: threadToastData,
|
|
});
|
|
return;
|
|
}
|
|
const prUrl = gitStatusForActions?.pr?.state === "open" ? gitStatusForActions.pr.url : null;
|
|
if (!prUrl) {
|
|
toastManager.add({
|
|
type: "error",
|
|
title: "No open PR found.",
|
|
data: threadToastData,
|
|
});
|
|
return;
|
|
}
|
|
void api.shell.openExternal(prUrl).catch((err) => {
|
|
toastManager.add({
|
|
type: "error",
|
|
title: "Unable to open PR link",
|
|
description: err instanceof Error ? err.message : "An error occurred.",
|
|
data: threadToastData,
|
|
});
|
|
});
|
|
}, [gitStatusForActions, threadToastData]);
|
|
|
|
const runGitActionWithToast = useEffectEvent(
|
|
async ({
|
|
action,
|
|
commitMessage,
|
|
forcePushOnlyProgress = false,
|
|
onConfirmed,
|
|
skipDefaultBranchPrompt = false,
|
|
statusOverride,
|
|
featureBranch = false,
|
|
isDefaultBranchOverride,
|
|
progressToastId,
|
|
filePaths,
|
|
}: RunGitActionWithToastInput) => {
|
|
const actionStatus = statusOverride ?? gitStatusForActions;
|
|
const actionBranch = actionStatus?.branch ?? null;
|
|
const actionIsDefaultBranch =
|
|
isDefaultBranchOverride ?? (featureBranch ? false : isDefaultBranch);
|
|
const includesCommit =
|
|
!forcePushOnlyProgress && (action === "commit" || !!actionStatus?.hasWorkingTreeChanges);
|
|
if (
|
|
!skipDefaultBranchPrompt &&
|
|
requiresDefaultBranchConfirmation(action, actionIsDefaultBranch) &&
|
|
actionBranch
|
|
) {
|
|
if (action !== "commit_push" && action !== "commit_push_pr") {
|
|
return;
|
|
}
|
|
setPendingDefaultBranchAction({
|
|
action,
|
|
branchName: actionBranch,
|
|
includesCommit,
|
|
...(commitMessage ? { commitMessage } : {}),
|
|
forcePushOnlyProgress,
|
|
...(onConfirmed ? { onConfirmed } : {}),
|
|
...(filePaths ? { filePaths } : {}),
|
|
});
|
|
return;
|
|
}
|
|
onConfirmed?.();
|
|
|
|
const progressStages = buildGitActionProgressStages({
|
|
action,
|
|
hasCustomCommitMessage: !!commitMessage?.trim(),
|
|
hasWorkingTreeChanges: !!actionStatus?.hasWorkingTreeChanges,
|
|
forcePushOnly: forcePushOnlyProgress,
|
|
featureBranch,
|
|
});
|
|
const actionId = randomUUID();
|
|
const resolvedProgressToastId =
|
|
progressToastId ??
|
|
toastManager.add({
|
|
type: "loading",
|
|
title: progressStages[0] ?? "Running git action...",
|
|
description: "Waiting for Git...",
|
|
timeout: 0,
|
|
data: threadToastData,
|
|
});
|
|
|
|
activeGitActionProgressRef.current = {
|
|
toastId: resolvedProgressToastId,
|
|
actionId,
|
|
title: progressStages[0] ?? "Running git action...",
|
|
phaseStartedAtMs: null,
|
|
hookStartedAtMs: null,
|
|
hookName: null,
|
|
lastOutputLine: null,
|
|
currentPhaseLabel: progressStages[0] ?? "Running git action...",
|
|
};
|
|
|
|
if (progressToastId) {
|
|
toastManager.update(progressToastId, {
|
|
type: "loading",
|
|
title: progressStages[0] ?? "Running git action...",
|
|
description: "Waiting for Git...",
|
|
timeout: 0,
|
|
data: threadToastData,
|
|
});
|
|
}
|
|
|
|
const applyProgressEvent = (event: GitActionProgressEvent) => {
|
|
const progress = activeGitActionProgressRef.current;
|
|
if (!progress) {
|
|
return;
|
|
}
|
|
if (gitCwd && event.cwd !== gitCwd) {
|
|
return;
|
|
}
|
|
if (progress.actionId !== event.actionId) {
|
|
return;
|
|
}
|
|
|
|
const now = Date.now();
|
|
switch (event.kind) {
|
|
case "action_started":
|
|
progress.phaseStartedAtMs = now;
|
|
progress.hookStartedAtMs = null;
|
|
progress.hookName = null;
|
|
progress.lastOutputLine = null;
|
|
break;
|
|
case "phase_started":
|
|
progress.title = event.label;
|
|
progress.currentPhaseLabel = event.label;
|
|
progress.phaseStartedAtMs = now;
|
|
progress.hookStartedAtMs = null;
|
|
progress.hookName = null;
|
|
progress.lastOutputLine = null;
|
|
break;
|
|
case "hook_started":
|
|
progress.title = `Running ${event.hookName}...`;
|
|
progress.hookName = event.hookName;
|
|
progress.hookStartedAtMs = now;
|
|
progress.lastOutputLine = null;
|
|
break;
|
|
case "hook_output":
|
|
progress.lastOutputLine = event.text;
|
|
break;
|
|
case "hook_finished":
|
|
progress.title = progress.currentPhaseLabel ?? "Committing...";
|
|
progress.hookName = null;
|
|
progress.hookStartedAtMs = null;
|
|
progress.lastOutputLine = null;
|
|
break;
|
|
case "action_finished":
|
|
// Let the resolved mutation update the toast so we keep the
|
|
// elapsed description visible until the final success state renders.
|
|
return;
|
|
case "action_failed":
|
|
// Let the rejected mutation publish the error toast to avoid a
|
|
// transient intermediate state before the final failure message.
|
|
return;
|
|
}
|
|
|
|
updateActiveProgressToast();
|
|
};
|
|
|
|
const promise = runImmediateGitActionMutation.mutateAsync({
|
|
actionId,
|
|
action,
|
|
...(commitMessage ? { commitMessage } : {}),
|
|
...(featureBranch ? { featureBranch } : {}),
|
|
...(filePaths ? { filePaths } : {}),
|
|
onProgress: applyProgressEvent,
|
|
});
|
|
|
|
try {
|
|
const result = await promise;
|
|
activeGitActionProgressRef.current = null;
|
|
const resultToast = summarizeGitResult(result);
|
|
|
|
const existingOpenPrUrl =
|
|
actionStatus?.pr?.state === "open" ? actionStatus.pr.url : undefined;
|
|
const prUrl = result.pr.url ?? existingOpenPrUrl;
|
|
const shouldOfferPushCta = action === "commit" && result.commit.status === "created";
|
|
const shouldOfferOpenPrCta =
|
|
(action === "commit_push" || action === "commit_push_pr") &&
|
|
!!prUrl &&
|
|
(!actionIsDefaultBranch ||
|
|
result.pr.status === "created" ||
|
|
result.pr.status === "opened_existing");
|
|
const shouldOfferCreatePrCta =
|
|
action === "commit_push" &&
|
|
!prUrl &&
|
|
result.push.status === "pushed" &&
|
|
!actionIsDefaultBranch;
|
|
const closeResultToast = () => {
|
|
toastManager.close(resolvedProgressToastId);
|
|
};
|
|
|
|
toastManager.update(resolvedProgressToastId, {
|
|
type: "success",
|
|
title: resultToast.title,
|
|
description: resultToast.description,
|
|
timeout: 0,
|
|
data: {
|
|
...threadToastData,
|
|
dismissAfterVisibleMs: 10_000,
|
|
},
|
|
...(shouldOfferPushCta
|
|
? {
|
|
actionProps: {
|
|
children: "Push",
|
|
onClick: () => {
|
|
void runGitActionWithToast({
|
|
action: "commit_push",
|
|
forcePushOnlyProgress: true,
|
|
onConfirmed: closeResultToast,
|
|
statusOverride: actionStatus,
|
|
isDefaultBranchOverride: actionIsDefaultBranch,
|
|
});
|
|
},
|
|
},
|
|
}
|
|
: shouldOfferOpenPrCta
|
|
? {
|
|
actionProps: {
|
|
children: "View PR",
|
|
onClick: () => {
|
|
const api = readNativeApi();
|
|
if (!api) return;
|
|
closeResultToast();
|
|
void api.shell.openExternal(prUrl);
|
|
},
|
|
},
|
|
}
|
|
: shouldOfferCreatePrCta
|
|
? {
|
|
actionProps: {
|
|
children: "Create PR",
|
|
onClick: () => {
|
|
closeResultToast();
|
|
void runGitActionWithToast({
|
|
action: "commit_push_pr",
|
|
forcePushOnlyProgress: true,
|
|
statusOverride: actionStatus,
|
|
isDefaultBranchOverride: actionIsDefaultBranch,
|
|
});
|
|
},
|
|
},
|
|
}
|
|
: {}),
|
|
});
|
|
} catch (err) {
|
|
activeGitActionProgressRef.current = null;
|
|
toastManager.update(resolvedProgressToastId, {
|
|
type: "error",
|
|
title: "Action failed",
|
|
description: err instanceof Error ? err.message : "An error occurred.",
|
|
data: threadToastData,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
|
|
const continuePendingDefaultBranchAction = useCallback(() => {
|
|
if (!pendingDefaultBranchAction) return;
|
|
const { action, commitMessage, forcePushOnlyProgress, onConfirmed, filePaths } =
|
|
pendingDefaultBranchAction;
|
|
setPendingDefaultBranchAction(null);
|
|
void runGitActionWithToast({
|
|
action,
|
|
...(commitMessage ? { commitMessage } : {}),
|
|
forcePushOnlyProgress,
|
|
...(onConfirmed ? { onConfirmed } : {}),
|
|
...(filePaths ? { filePaths } : {}),
|
|
skipDefaultBranchPrompt: true,
|
|
});
|
|
}, [pendingDefaultBranchAction]);
|
|
|
|
const checkoutFeatureBranchAndContinuePendingAction = useCallback(() => {
|
|
if (!pendingDefaultBranchAction) return;
|
|
const { action, commitMessage, forcePushOnlyProgress, onConfirmed, filePaths } =
|
|
pendingDefaultBranchAction;
|
|
setPendingDefaultBranchAction(null);
|
|
void runGitActionWithToast({
|
|
action,
|
|
...(commitMessage ? { commitMessage } : {}),
|
|
forcePushOnlyProgress,
|
|
...(onConfirmed ? { onConfirmed } : {}),
|
|
...(filePaths ? { filePaths } : {}),
|
|
featureBranch: true,
|
|
skipDefaultBranchPrompt: true,
|
|
});
|
|
}, [pendingDefaultBranchAction]);
|
|
|
|
const runDialogActionOnNewBranch = useCallback(() => {
|
|
if (!isCommitDialogOpen) return;
|
|
const commitMessage = dialogCommitMessage.trim();
|
|
|
|
setIsCommitDialogOpen(false);
|
|
setDialogCommitMessage("");
|
|
setExcludedFiles(new Set());
|
|
setIsEditingFiles(false);
|
|
|
|
void runGitActionWithToast({
|
|
action: "commit",
|
|
...(commitMessage ? { commitMessage } : {}),
|
|
...(!allSelected ? { filePaths: selectedFiles.map((f) => f.path) } : {}),
|
|
featureBranch: true,
|
|
skipDefaultBranchPrompt: true,
|
|
});
|
|
}, [allSelected, isCommitDialogOpen, dialogCommitMessage, selectedFiles]);
|
|
|
|
const runQuickAction = useCallback(() => {
|
|
if (quickAction.kind === "open_pr") {
|
|
void openExistingPr();
|
|
return;
|
|
}
|
|
if (quickAction.kind === "run_pull") {
|
|
const promise = pullMutation.mutateAsync();
|
|
toastManager.promise(promise, {
|
|
loading: { title: "Pulling...", data: threadToastData },
|
|
success: (result) => ({
|
|
title: result.status === "pulled" ? "Pulled" : "Already up to date",
|
|
description:
|
|
result.status === "pulled"
|
|
? `Updated ${result.branch} from ${result.upstreamBranch ?? "upstream"}`
|
|
: `${result.branch} is already synchronized.`,
|
|
data: threadToastData,
|
|
}),
|
|
error: (err) => ({
|
|
title: "Pull failed",
|
|
description: err instanceof Error ? err.message : "An error occurred.",
|
|
data: threadToastData,
|
|
}),
|
|
});
|
|
void promise.catch(() => undefined);
|
|
return;
|
|
}
|
|
if (quickAction.kind === "show_hint") {
|
|
toastManager.add({
|
|
type: "info",
|
|
title: quickAction.label,
|
|
description: quickAction.hint,
|
|
data: threadToastData,
|
|
});
|
|
return;
|
|
}
|
|
if (quickAction.action) {
|
|
void runGitActionWithToast({ action: quickAction.action });
|
|
}
|
|
}, [openExistingPr, pullMutation, quickAction, threadToastData]);
|
|
|
|
const openDialogForMenuItem = useCallback(
|
|
(item: GitActionMenuItem) => {
|
|
if (item.disabled) return;
|
|
if (item.kind === "open_pr") {
|
|
void openExistingPr();
|
|
return;
|
|
}
|
|
if (item.dialogAction === "push") {
|
|
void runGitActionWithToast({ action: "commit_push", forcePushOnlyProgress: true });
|
|
return;
|
|
}
|
|
if (item.dialogAction === "create_pr") {
|
|
void runGitActionWithToast({ action: "commit_push_pr" });
|
|
return;
|
|
}
|
|
setExcludedFiles(new Set());
|
|
setIsEditingFiles(false);
|
|
setIsCommitDialogOpen(true);
|
|
},
|
|
[openExistingPr, setIsCommitDialogOpen],
|
|
);
|
|
|
|
const runDialogAction = useCallback(() => {
|
|
if (!isCommitDialogOpen) return;
|
|
const commitMessage = dialogCommitMessage.trim();
|
|
setIsCommitDialogOpen(false);
|
|
setDialogCommitMessage("");
|
|
setExcludedFiles(new Set());
|
|
setIsEditingFiles(false);
|
|
void runGitActionWithToast({
|
|
action: "commit",
|
|
...(commitMessage ? { commitMessage } : {}),
|
|
...(!allSelected ? { filePaths: selectedFiles.map((f) => f.path) } : {}),
|
|
});
|
|
}, [
|
|
allSelected,
|
|
dialogCommitMessage,
|
|
isCommitDialogOpen,
|
|
selectedFiles,
|
|
setDialogCommitMessage,
|
|
setIsCommitDialogOpen,
|
|
]);
|
|
|
|
const openChangedFileInEditor = useCallback(
|
|
(filePath: string) => {
|
|
const api = readNativeApi();
|
|
if (!api || !gitCwd) {
|
|
toastManager.add({
|
|
type: "error",
|
|
title: "Editor opening is unavailable.",
|
|
data: threadToastData,
|
|
});
|
|
return;
|
|
}
|
|
const target = resolvePathLinkTarget(filePath, gitCwd);
|
|
void openInPreferredEditor(api, target).catch((error) => {
|
|
toastManager.add({
|
|
type: "error",
|
|
title: "Unable to open file",
|
|
description: error instanceof Error ? error.message : "An error occurred.",
|
|
data: threadToastData,
|
|
});
|
|
});
|
|
},
|
|
[gitCwd, threadToastData],
|
|
);
|
|
|
|
if (!gitCwd) return null;
|
|
|
|
return (
|
|
<>
|
|
{!isRepo ? (
|
|
<Button
|
|
variant="outline"
|
|
size="xs"
|
|
disabled={initMutation.isPending}
|
|
onClick={() => initMutation.mutate()}
|
|
>
|
|
{initMutation.isPending ? "Initializing..." : "Initialize Git"}
|
|
</Button>
|
|
) : (
|
|
<Group aria-label="Git actions" className="shrink-0">
|
|
{quickActionDisabledReason ? (
|
|
<Popover>
|
|
<PopoverTrigger
|
|
openOnHover
|
|
render={
|
|
<Button
|
|
aria-disabled="true"
|
|
className="cursor-not-allowed rounded-e-none border-e-0 opacity-64 before:rounded-e-none"
|
|
size="xs"
|
|
variant="outline"
|
|
/>
|
|
}
|
|
>
|
|
<GitQuickActionIcon quickAction={quickAction} />
|
|
<span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
|
|
{quickAction.label}
|
|
</span>
|
|
</PopoverTrigger>
|
|
<PopoverPopup tooltipStyle side="bottom" align="start">
|
|
{quickActionDisabledReason}
|
|
</PopoverPopup>
|
|
</Popover>
|
|
) : (
|
|
<Button
|
|
variant="outline"
|
|
size="xs"
|
|
disabled={isGitActionRunning || quickAction.disabled}
|
|
onClick={runQuickAction}
|
|
>
|
|
<GitQuickActionIcon quickAction={quickAction} />
|
|
<span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
|
|
{quickAction.label}
|
|
</span>
|
|
</Button>
|
|
)}
|
|
<GroupSeparator className="hidden @3xl/header-actions:block" />
|
|
<Menu
|
|
onOpenChange={(open) => {
|
|
if (open) void invalidateGitQueries(queryClient);
|
|
}}
|
|
>
|
|
<MenuTrigger
|
|
render={<Button aria-label="Git action options" size="icon-xs" variant="outline" />}
|
|
disabled={isGitActionRunning}
|
|
>
|
|
<ChevronDownIcon aria-hidden="true" className="size-4" />
|
|
</MenuTrigger>
|
|
<MenuPopup align="end" className="w-full">
|
|
{gitActionMenuItems.map((item) => {
|
|
const disabledReason = getMenuActionDisabledReason({
|
|
item,
|
|
gitStatus: gitStatusForActions,
|
|
isBusy: isGitActionRunning,
|
|
hasOriginRemote,
|
|
});
|
|
if (item.disabled && disabledReason) {
|
|
return (
|
|
<Popover key={`${item.id}-${item.label}`}>
|
|
<PopoverTrigger
|
|
openOnHover
|
|
nativeButton={false}
|
|
render={<span className="block w-max cursor-not-allowed" />}
|
|
>
|
|
<MenuItem className="w-full" disabled>
|
|
<GitActionItemIcon icon={item.icon} />
|
|
{item.label}
|
|
</MenuItem>
|
|
</PopoverTrigger>
|
|
<PopoverPopup tooltipStyle side="left" align="center">
|
|
{disabledReason}
|
|
</PopoverPopup>
|
|
</Popover>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<MenuItem
|
|
key={`${item.id}-${item.label}`}
|
|
disabled={item.disabled}
|
|
onClick={() => {
|
|
openDialogForMenuItem(item);
|
|
}}
|
|
>
|
|
<GitActionItemIcon icon={item.icon} />
|
|
{item.label}
|
|
</MenuItem>
|
|
);
|
|
})}
|
|
{gitStatusForActions?.branch === null && (
|
|
<p className="px-2 py-1.5 text-xs text-warning">
|
|
Detached HEAD: create and checkout a branch to enable push and PR actions.
|
|
</p>
|
|
)}
|
|
{gitStatusForActions &&
|
|
gitStatusForActions.branch !== null &&
|
|
!gitStatusForActions.hasWorkingTreeChanges &&
|
|
gitStatusForActions.behindCount > 0 &&
|
|
gitStatusForActions.aheadCount === 0 && (
|
|
<p className="px-2 py-1.5 text-xs text-warning">
|
|
Behind upstream. Pull/rebase first.
|
|
</p>
|
|
)}
|
|
{isGitStatusOutOfSync && (
|
|
<p className="px-2 py-1.5 text-xs text-muted-foreground">
|
|
Refreshing git status...
|
|
</p>
|
|
)}
|
|
{gitStatusError && (
|
|
<p className="px-2 py-1.5 text-xs text-destructive">{gitStatusError.message}</p>
|
|
)}
|
|
</MenuPopup>
|
|
</Menu>
|
|
</Group>
|
|
)}
|
|
|
|
<Dialog
|
|
open={isCommitDialogOpen}
|
|
onOpenChange={(open) => {
|
|
if (!open) {
|
|
setIsCommitDialogOpen(false);
|
|
setDialogCommitMessage("");
|
|
setExcludedFiles(new Set());
|
|
setIsEditingFiles(false);
|
|
}
|
|
}}
|
|
>
|
|
<DialogPopup>
|
|
<DialogHeader>
|
|
<DialogTitle>{COMMIT_DIALOG_TITLE}</DialogTitle>
|
|
<DialogDescription>{COMMIT_DIALOG_DESCRIPTION}</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogPanel className="space-y-4">
|
|
<div className="space-y-3 rounded-lg border border-input bg-muted/40 p-3 text-xs">
|
|
<div className="grid grid-cols-[auto_1fr] items-center gap-x-2 gap-y-1">
|
|
<span className="text-muted-foreground">Branch</span>
|
|
<span className="flex items-center justify-between gap-2">
|
|
<span className="font-medium">
|
|
{gitStatusForActions?.branch ?? "(detached HEAD)"}
|
|
</span>
|
|
{isDefaultBranch && (
|
|
<span className="text-right text-warning text-xs">Warning: default branch</span>
|
|
)}
|
|
</span>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
{isEditingFiles && allFiles.length > 0 && (
|
|
<Checkbox
|
|
checked={allSelected}
|
|
indeterminate={!allSelected && !noneSelected}
|
|
onCheckedChange={() => {
|
|
setExcludedFiles(
|
|
allSelected ? new Set(allFiles.map((f) => f.path)) : new Set(),
|
|
);
|
|
}}
|
|
/>
|
|
)}
|
|
<span className="text-muted-foreground">Files</span>
|
|
{!allSelected && !isEditingFiles && (
|
|
<span className="text-muted-foreground">
|
|
({selectedFiles.length} of {allFiles.length})
|
|
</span>
|
|
)}
|
|
</div>
|
|
{allFiles.length > 0 && (
|
|
<Button
|
|
variant="ghost"
|
|
size="xs"
|
|
onClick={() => setIsEditingFiles((prev) => !prev)}
|
|
>
|
|
{isEditingFiles ? "Done" : "Edit"}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
{!gitStatusForActions || allFiles.length === 0 ? (
|
|
<p className="font-medium">none</p>
|
|
) : (
|
|
<div className="space-y-2">
|
|
<ScrollArea className="h-44 rounded-md border border-input bg-background">
|
|
<div className="space-y-1 p-1">
|
|
{allFiles.map((file) => {
|
|
const isExcluded = excludedFiles.has(file.path);
|
|
return (
|
|
<div
|
|
key={file.path}
|
|
className="flex w-full items-center gap-2 rounded-md px-2 py-1 font-mono text-xs transition-colors hover:bg-accent/50"
|
|
>
|
|
{isEditingFiles && (
|
|
<Checkbox
|
|
checked={!excludedFiles.has(file.path)}
|
|
onCheckedChange={() => {
|
|
setExcludedFiles((prev) => {
|
|
const next = new Set(prev);
|
|
if (next.has(file.path)) {
|
|
next.delete(file.path);
|
|
} else {
|
|
next.add(file.path);
|
|
}
|
|
return next;
|
|
});
|
|
}}
|
|
/>
|
|
)}
|
|
<button
|
|
type="button"
|
|
className="flex flex-1 items-center justify-between gap-3 text-left truncate"
|
|
onClick={() => openChangedFileInEditor(file.path)}
|
|
>
|
|
<span
|
|
className={`truncate${isExcluded ? " text-muted-foreground" : ""}`}
|
|
>
|
|
{file.path}
|
|
</span>
|
|
<span className="shrink-0">
|
|
{isExcluded ? (
|
|
<span className="text-muted-foreground">Excluded</span>
|
|
) : (
|
|
<>
|
|
<span className="text-success">+{file.insertions}</span>
|
|
<span className="text-muted-foreground"> / </span>
|
|
<span className="text-destructive">-{file.deletions}</span>
|
|
</>
|
|
)}
|
|
</span>
|
|
</button>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</ScrollArea>
|
|
<div className="flex justify-end font-mono">
|
|
<span className="text-success">
|
|
+{selectedFiles.reduce((sum, f) => sum + f.insertions, 0)}
|
|
</span>
|
|
<span className="text-muted-foreground"> / </span>
|
|
<span className="text-destructive">
|
|
-{selectedFiles.reduce((sum, f) => sum + f.deletions, 0)}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<p className="text-xs font-medium">Commit message (optional)</p>
|
|
<Textarea
|
|
value={dialogCommitMessage}
|
|
onChange={(event) => setDialogCommitMessage(event.target.value)}
|
|
placeholder="Leave empty to auto-generate"
|
|
size="sm"
|
|
/>
|
|
</div>
|
|
</DialogPanel>
|
|
<DialogFooter>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => {
|
|
setIsCommitDialogOpen(false);
|
|
setDialogCommitMessage("");
|
|
setExcludedFiles(new Set());
|
|
setIsEditingFiles(false);
|
|
}}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
disabled={noneSelected}
|
|
onClick={runDialogActionOnNewBranch}
|
|
>
|
|
Commit on new branch
|
|
</Button>
|
|
<Button size="sm" disabled={noneSelected} onClick={runDialogAction}>
|
|
Commit
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogPopup>
|
|
</Dialog>
|
|
|
|
<Dialog
|
|
open={pendingDefaultBranchAction !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) {
|
|
setPendingDefaultBranchAction(null);
|
|
}
|
|
}}
|
|
>
|
|
<DialogPopup className="max-w-xl">
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
{pendingDefaultBranchActionCopy?.title ?? "Run action on default branch?"}
|
|
</DialogTitle>
|
|
<DialogDescription>{pendingDefaultBranchActionCopy?.description}</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogFooter>
|
|
<Button variant="outline" size="sm" onClick={() => setPendingDefaultBranchAction(null)}>
|
|
Abort
|
|
</Button>
|
|
<Button variant="outline" size="sm" onClick={continuePendingDefaultBranchAction}>
|
|
{pendingDefaultBranchActionCopy?.continueLabel ?? "Continue"}
|
|
</Button>
|
|
<Button size="sm" onClick={checkoutFeatureBranchAndContinuePendingAction}>
|
|
Checkout feature branch & continue
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogPopup>
|
|
</Dialog>
|
|
</>
|
|
);
|
|
}
|