Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84ee10d7ed | ||
|
|
9e3fa3e67f | ||
|
|
38de3417b8 | ||
|
|
4fff00c8c3 | ||
|
|
6b916d3a6f | ||
|
|
21580dc1b0 | ||
|
|
ba9585966c | ||
|
|
293f7b1507 | ||
|
|
4f5eb3be4d | ||
|
|
b6523a15dc | ||
|
|
8a4fed1bf2 | ||
|
|
0068a93cfa | ||
|
|
dd912666f2 | ||
|
|
521238cf8e |
No files matched your search
@@ -15,10 +15,13 @@ await waitOn({
|
||||
});
|
||||
|
||||
const command = process.platform === "win32" ? "electronmon.cmd" : "electronmon";
|
||||
const childEnv = { ...process.env };
|
||||
delete childEnv.ELECTRON_RUN_AS_NODE;
|
||||
|
||||
const child = spawn(command, ["dist-electron/main.js"], {
|
||||
stdio: "inherit",
|
||||
env: {
|
||||
...process.env,
|
||||
...childEnv,
|
||||
VITE_DEV_SERVER_URL: devServerUrl,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import type { BrowserWindow } from "electron";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { showMessageBoxMock } = vi.hoisted(() => ({
|
||||
showMessageBoxMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("electron", () => ({
|
||||
dialog: {
|
||||
showMessageBox: showMessageBoxMock,
|
||||
},
|
||||
}));
|
||||
|
||||
import { showDesktopConfirmDialog } from "./confirmDialog";
|
||||
|
||||
describe("showDesktopConfirmDialog", () => {
|
||||
beforeEach(() => {
|
||||
showMessageBoxMock.mockReset();
|
||||
});
|
||||
|
||||
it("returns false and does not open a dialog for empty messages", async () => {
|
||||
const result = await showDesktopConfirmDialog(" ", null);
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(showMessageBoxMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("opens a dialog for the focused window and returns true on confirm", async () => {
|
||||
const ownerWindow = { id: 1 } as BrowserWindow;
|
||||
showMessageBoxMock.mockResolvedValue({ response: 1 });
|
||||
|
||||
const result = await showDesktopConfirmDialog("Delete worktree?", ownerWindow);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(showMessageBoxMock).toHaveBeenCalledWith(
|
||||
ownerWindow,
|
||||
expect.objectContaining({
|
||||
buttons: ["No", "Yes"],
|
||||
message: "Delete worktree?",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("opens an app-level dialog when there is no focused window", async () => {
|
||||
showMessageBoxMock.mockResolvedValue({ response: 0 });
|
||||
|
||||
const result = await showDesktopConfirmDialog("Delete worktree?", null);
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(showMessageBoxMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
buttons: ["No", "Yes"],
|
||||
message: "Delete worktree?",
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { type BrowserWindow, dialog } from "electron";
|
||||
|
||||
const CONFIRM_BUTTON_INDEX = 1;
|
||||
|
||||
export async function showDesktopConfirmDialog(
|
||||
message: string,
|
||||
ownerWindow: BrowserWindow | null,
|
||||
): Promise<boolean> {
|
||||
const normalizedMessage = message.trim();
|
||||
if (normalizedMessage.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const options = {
|
||||
type: "question" as const,
|
||||
buttons: ["No", "Yes"],
|
||||
defaultId: CONFIRM_BUTTON_INDEX,
|
||||
cancelId: 0,
|
||||
noLink: true,
|
||||
message: normalizedMessage,
|
||||
};
|
||||
const result = ownerWindow
|
||||
? await dialog.showMessageBox(ownerWindow, options)
|
||||
: await dialog.showMessageBox(options);
|
||||
return result.response === CONFIRM_BUTTON_INDEX;
|
||||
}
|
||||
@@ -7,11 +7,13 @@ import path from "node:path";
|
||||
|
||||
import { app, BrowserWindow, dialog, ipcMain, Menu, shell } from "electron";
|
||||
|
||||
import { showDesktopConfirmDialog } from "./confirmDialog";
|
||||
import { fixPath } from "./fixPath";
|
||||
|
||||
fixPath();
|
||||
|
||||
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
const CONFIRM_CHANNEL = "desktop:confirm";
|
||||
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
||||
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
||||
const ROOT_DIR = path.resolve(__dirname, "../../..");
|
||||
@@ -81,7 +83,12 @@ function startBackend(): void {
|
||||
|
||||
const child = spawn(process.execPath, [BACKEND_ENTRY], {
|
||||
cwd: ROOT_DIR,
|
||||
env: backendEnv(),
|
||||
// In Electron main, process.execPath points to the Electron binary.
|
||||
// Run the child in Node mode so this backend process does not become a GUI app instance.
|
||||
env: {
|
||||
...backendEnv(),
|
||||
ELECTRON_RUN_AS_NODE: "1",
|
||||
},
|
||||
stdio: "inherit",
|
||||
});
|
||||
backendProcess = child;
|
||||
@@ -142,6 +149,16 @@ function registerIpcHandlers(): void {
|
||||
return result.filePaths[0] ?? null;
|
||||
});
|
||||
|
||||
ipcMain.removeHandler(CONFIRM_CHANNEL);
|
||||
ipcMain.handle(CONFIRM_CHANNEL, async (_event, message: unknown) => {
|
||||
if (typeof message !== "string") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const owner = BrowserWindow.getFocusedWindow() ?? mainWindow;
|
||||
return showDesktopConfirmDialog(message, owner);
|
||||
});
|
||||
|
||||
ipcMain.removeHandler(CONTEXT_MENU_CHANNEL);
|
||||
ipcMain.handle(CONTEXT_MENU_CHANNEL, async (_event, items: { id: string; label: string }[]) => {
|
||||
const window = BrowserWindow.getFocusedWindow() ?? mainWindow;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { contextBridge, ipcRenderer } from "electron";
|
||||
|
||||
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
const CONFIRM_CHANNEL = "desktop:confirm";
|
||||
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
||||
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
||||
const wsUrl = process.env.T3CODE_DESKTOP_WS_URL ?? null;
|
||||
@@ -8,6 +9,7 @@ const wsUrl = process.env.T3CODE_DESKTOP_WS_URL ?? null;
|
||||
contextBridge.exposeInMainWorld("desktopBridge", {
|
||||
getWsUrl: () => wsUrl,
|
||||
pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL) as Promise<string | null>,
|
||||
confirm: (message: string) => ipcRenderer.invoke(CONFIRM_CHANNEL, message) as Promise<boolean>,
|
||||
showContextMenu: (items: { id: string; label: string }[]) =>
|
||||
ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items) as Promise<string | null>,
|
||||
openExternal: (url: string) => ipcRenderer.invoke(OPEN_EXTERNAL_CHANNEL, url) as Promise<boolean>,
|
||||
|
||||
@@ -343,7 +343,7 @@ describe("GitManager", () => {
|
||||
expect(result.push.status).toBe("skipped_up_to_date");
|
||||
});
|
||||
|
||||
it("opens existing PR for commit/push/pr action", async () => {
|
||||
it("returns existing PR metadata for commit/push/pr action", async () => {
|
||||
const repoDir = makeTempDir("t3code-git-manager-");
|
||||
await initRepo(repoDir);
|
||||
await runGit(repoDir, ["checkout", "-b", "feature/existing-pr"]);
|
||||
@@ -376,9 +376,7 @@ describe("GitManager", () => {
|
||||
|
||||
expect(result.pr.status).toBe("opened_existing");
|
||||
expect(result.pr.number).toBe(42);
|
||||
expect(
|
||||
ghCalls.some((call) => call.startsWith("pr view feature/existing-pr --web")),
|
||||
).toBe(true);
|
||||
expect(ghCalls.some((call) => call.startsWith("pr view "))).toBe(false);
|
||||
});
|
||||
|
||||
it("creates PR when one does not already exist", async () => {
|
||||
@@ -422,6 +420,7 @@ describe("GitManager", () => {
|
||||
expect(
|
||||
ghCalls.some((call) => call.includes("pr create --base main --head feature-create-pr")),
|
||||
).toBe(true);
|
||||
expect(ghCalls.some((call) => call.startsWith("pr view "))).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects push/pr actions from detached HEAD", async () => {
|
||||
|
||||
@@ -325,7 +325,6 @@ export class GitManager {
|
||||
|
||||
const existing = await this.findOpenPr(cwd, branch);
|
||||
if (existing) {
|
||||
await this.openPrInBrowser(cwd, branch);
|
||||
return {
|
||||
status: "opened_existing",
|
||||
url: existing.url,
|
||||
@@ -381,7 +380,6 @@ export class GitManager {
|
||||
}
|
||||
|
||||
const created = await this.findOpenPr(cwd, branch);
|
||||
await this.openPrInBrowser(cwd, branch);
|
||||
|
||||
if (!created) {
|
||||
return {
|
||||
@@ -402,14 +400,6 @@ export class GitManager {
|
||||
};
|
||||
}
|
||||
|
||||
private async openPrInBrowser(cwd: string, branch: string): Promise<void> {
|
||||
try {
|
||||
await this.runGh(cwd, ["pr", "view", branch, "--web"]);
|
||||
} catch {
|
||||
// Opening the browser is best-effort.
|
||||
}
|
||||
}
|
||||
|
||||
private async findOpenPr(
|
||||
cwd: string,
|
||||
branch: string,
|
||||
|
||||
@@ -262,24 +262,12 @@ export default function ChatView() {
|
||||
}, []);
|
||||
const toggleTerminalVisibility = useCallback(() => {
|
||||
if (!activeThreadId) return;
|
||||
const isOpen = Boolean(activeThread?.terminalOpen);
|
||||
|
||||
if (isOpen && api) {
|
||||
if (typeof api.terminal.close === "function") {
|
||||
void api.terminal.close({ threadId: activeThreadId }).catch(() => {
|
||||
void api.terminal.write({ threadId: activeThreadId, data: "exit\n" }).catch(() => undefined);
|
||||
});
|
||||
} else {
|
||||
void api.terminal.write({ threadId: activeThreadId, data: "exit\n" }).catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_OPEN",
|
||||
threadId: activeThreadId,
|
||||
open: !isOpen,
|
||||
open: !activeThread?.terminalOpen,
|
||||
});
|
||||
}, [activeThread?.terminalOpen, activeThreadId, api, dispatch]);
|
||||
}, [activeThread?.terminalOpen, activeThreadId, dispatch]);
|
||||
const splitTerminal = useCallback(() => {
|
||||
if (!activeThreadId) return;
|
||||
dispatch({
|
||||
|
||||
@@ -4,15 +4,13 @@ import {
|
||||
type GitStatusResult,
|
||||
type NativeApi,
|
||||
} from "@t3tools/contracts";
|
||||
import { queryOptions, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { queryOptions, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import {
|
||||
CheckIcon,
|
||||
CircleIcon,
|
||||
CloudUploadIcon,
|
||||
GitCommitIcon,
|
||||
Loader2Icon,
|
||||
MinusIcon,
|
||||
XIcon,
|
||||
} from "lucide-react";
|
||||
import { GitHubIcon } from "./Icons";
|
||||
@@ -50,6 +48,11 @@ interface GitProgressStep {
|
||||
detail?: string;
|
||||
}
|
||||
|
||||
interface GitProgressToastAction {
|
||||
kind: "open_pr" | "create_pr";
|
||||
label: "Open PR" | "Create PR";
|
||||
}
|
||||
|
||||
function GitActionIcon(props: { icon: GitActionIconName; disabled: boolean }) {
|
||||
const toneClass = props.disabled ? "text-muted-foreground/45" : "text-foreground/85";
|
||||
|
||||
@@ -64,18 +67,21 @@ function GitActionIcon(props: { icon: GitActionIconName; disabled: boolean }) {
|
||||
return <GitHubIcon className={`h-5 w-5 shrink-0 ${toneClass}`} />;
|
||||
}
|
||||
|
||||
function runningStepMessage(stepId: GitProgressStep["id"]): string {
|
||||
if (stepId === "generate") return "Generating commit message...";
|
||||
if (stepId === "commit") return "Committing changes...";
|
||||
if (stepId === "push") return "Pushing branch...";
|
||||
return "Preparing pull request...";
|
||||
}
|
||||
|
||||
function gitActionModalTitle(): string {
|
||||
return "Commit your changes";
|
||||
}
|
||||
|
||||
function initialGitProgressSteps(
|
||||
action: GitStackedAction,
|
||||
commitMessage: string,
|
||||
): GitProgressStep[] {
|
||||
const hasCustomMessage = commitMessage.trim().length > 0;
|
||||
function initialGitProgressSteps(action: GitStackedAction, includeGenerateStep: boolean): GitProgressStep[] {
|
||||
const steps: GitProgressStep[] = [];
|
||||
|
||||
if (!hasCustomMessage) {
|
||||
if (includeGenerateStep) {
|
||||
steps.push({
|
||||
id: "generate",
|
||||
label: "Generate commit message",
|
||||
@@ -130,6 +136,48 @@ function runActionLabel(action: GitStackedAction): string {
|
||||
return "Commit and create PR";
|
||||
}
|
||||
|
||||
function canCreatePrFromStatus(gitStatus: GitStatusResult | null): boolean {
|
||||
if (!gitStatus) return false;
|
||||
const hasBranch = gitStatus.branch !== null;
|
||||
const hasOpenPr = gitStatus.openPr !== null;
|
||||
|
||||
return (
|
||||
hasBranch &&
|
||||
!hasOpenPr &&
|
||||
!gitStatus.hasWorkingTreeChanges &&
|
||||
gitStatus.hasUpstream &&
|
||||
gitStatus.behindCount === 0
|
||||
);
|
||||
}
|
||||
|
||||
export function getGitProgressToastAction(params: {
|
||||
isRunning: boolean;
|
||||
hasError: boolean;
|
||||
result: GitRunStackedActionResult | null;
|
||||
gitStatus: GitStatusResult | null;
|
||||
openPrUrl: string | null;
|
||||
}): GitProgressToastAction | null {
|
||||
if (params.isRunning || params.hasError || !params.result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (params.openPrUrl) {
|
||||
return {
|
||||
kind: "open_pr",
|
||||
label: "Open PR",
|
||||
};
|
||||
}
|
||||
|
||||
if (params.result.action === "commit_push" && canCreatePrFromStatus(params.gitStatus)) {
|
||||
return {
|
||||
kind: "create_pr",
|
||||
label: "Create PR",
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildGitActionMenuItems(
|
||||
gitStatus: GitStatusResult | null,
|
||||
isDisabled: boolean,
|
||||
@@ -143,13 +191,7 @@ function buildGitActionMenuItems(
|
||||
const canCommit = !isDisabled && hasChanges;
|
||||
const canPush = !isDisabled && hasBranch && !hasChanges && hasAhead;
|
||||
const canOpenPr = !isDisabled && hasOpenPr;
|
||||
const canCreatePr =
|
||||
!isDisabled &&
|
||||
hasBranch &&
|
||||
!hasOpenPr &&
|
||||
!hasChanges &&
|
||||
gitStatus.hasUpstream &&
|
||||
gitStatus.behindCount === 0;
|
||||
const canCreatePr = !isDisabled && canCreatePrFromStatus(gitStatus);
|
||||
|
||||
return [
|
||||
{
|
||||
@@ -249,33 +291,14 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
const [gitModalProgress, setGitModalProgress] = useState<GitProgressStep[]>([]);
|
||||
const [gitModalError, setGitModalError] = useState<string | null>(null);
|
||||
const [gitModalResult, setGitModalResult] = useState<GitRunStackedActionResult | null>(null);
|
||||
const [isGitProgressToastVisible, setIsGitProgressToastVisible] = useState(false);
|
||||
const gitMenuRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const { data: gitStatus = null, error: gitStatusError } = useQuery(
|
||||
gitStatusQueryOptions(api, gitCwd),
|
||||
);
|
||||
|
||||
const runImmediateGitActionMutation = useMutation({
|
||||
mutationFn: async (action: GitStackedAction) => {
|
||||
if (!api || !gitCwd) {
|
||||
throw new Error("Git action is unavailable.");
|
||||
}
|
||||
return api.git.runStackedAction({ cwd: gitCwd, action });
|
||||
},
|
||||
onMutate: () => {
|
||||
setIsGitMenuOpen(false);
|
||||
setGitActionError(null);
|
||||
setGitModalError(null);
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["git"] });
|
||||
},
|
||||
onError: (error) => {
|
||||
setGitActionError(error instanceof Error ? error.message : "Git action failed.");
|
||||
},
|
||||
});
|
||||
|
||||
const isGitActionRunning = isGitModalActionRunning || runImmediateGitActionMutation.isPending;
|
||||
const isGitActionRunning = isGitModalActionRunning;
|
||||
|
||||
const gitBaseDisabled = !api || !gitCwd || !gitStatus || isGitActionRunning;
|
||||
const gitActionMenuItems = useMemo(
|
||||
@@ -291,10 +314,44 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
[gitModalActionOptions, gitModalSelectedAction],
|
||||
);
|
||||
const visibleGitActionError = gitActionError ?? gitStatusError?.message;
|
||||
const gitModalOpenPrUrl = gitModalResult?.pr.url ?? gitStatus?.openPr?.url ?? null;
|
||||
const gitModalHasProgress = gitModalProgress.length > 0;
|
||||
const gitModalSelectionMode = !gitModalHasProgress && gitModalResult === null;
|
||||
const gitModalSteps = gitModalProgress;
|
||||
const gitProgressToastOpenPrUrl = gitModalResult?.pr.url ?? gitStatus?.openPr?.url ?? null;
|
||||
const showGitProgressToast =
|
||||
isGitProgressToastVisible &&
|
||||
(isGitModalActionRunning ||
|
||||
gitModalProgress.length > 0 ||
|
||||
gitModalError !== null ||
|
||||
gitModalResult !== null);
|
||||
const activeGitProgressStep = useMemo(
|
||||
() => gitModalProgress.find((step) => step.status === "running") ?? null,
|
||||
[gitModalProgress],
|
||||
);
|
||||
const gitProgressToastStatus = isGitModalActionRunning
|
||||
? "running"
|
||||
: gitModalError
|
||||
? "failed"
|
||||
: "done";
|
||||
const gitProgressToastMessage = isGitModalActionRunning
|
||||
? runningStepMessage(activeGitProgressStep?.id ?? "commit")
|
||||
: gitModalError
|
||||
? "Git action failed."
|
||||
: "Done.";
|
||||
const gitProgressToastAction = useMemo(
|
||||
() =>
|
||||
getGitProgressToastAction({
|
||||
isRunning: isGitModalActionRunning,
|
||||
hasError: gitModalError !== null,
|
||||
result: gitModalResult,
|
||||
gitStatus,
|
||||
openPrUrl: gitProgressToastOpenPrUrl,
|
||||
}),
|
||||
[
|
||||
gitModalError,
|
||||
gitModalResult,
|
||||
gitProgressToastOpenPrUrl,
|
||||
gitStatus,
|
||||
isGitModalActionRunning,
|
||||
],
|
||||
);
|
||||
|
||||
const refreshGitStatus = useCallback(async () => {
|
||||
if (!api || !gitCwd) return;
|
||||
@@ -307,6 +364,7 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
setIsGitModalOpen(true);
|
||||
setGitModalSelectedAction("commit");
|
||||
setGitModalCommitMessage("");
|
||||
setIsGitProgressToastVisible(false);
|
||||
setGitModalProgress([]);
|
||||
setGitModalError(null);
|
||||
setGitModalResult(null);
|
||||
@@ -318,18 +376,8 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
setIsGitModalOpen(false);
|
||||
setGitModalSelectedAction("commit");
|
||||
setGitModalCommitMessage("");
|
||||
setGitModalProgress([]);
|
||||
setGitModalError(null);
|
||||
setGitModalResult(null);
|
||||
}, [isGitActionRunning]);
|
||||
|
||||
const runGitActionImmediately = useCallback(
|
||||
(action: GitStackedAction) => {
|
||||
runImmediateGitActionMutation.mutate(action);
|
||||
},
|
||||
[runImmediateGitActionMutation],
|
||||
);
|
||||
|
||||
const openExistingPr = useCallback(() => {
|
||||
setIsGitMenuOpen(false);
|
||||
setGitActionError(null);
|
||||
@@ -349,154 +397,189 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
});
|
||||
}, [api, gitStatus?.openPr?.url]);
|
||||
|
||||
const openPrFromModal = useCallback(() => {
|
||||
const openPrFromToast = useCallback(() => {
|
||||
if (!api) {
|
||||
setGitModalError("Link opening is unavailable.");
|
||||
return;
|
||||
}
|
||||
|
||||
const prUrl = gitModalOpenPrUrl;
|
||||
const prUrl = gitProgressToastOpenPrUrl;
|
||||
if (!prUrl) return;
|
||||
|
||||
void api.shell.openExternal(prUrl).catch((error) => {
|
||||
setGitModalError(error instanceof Error ? error.message : "Unable to open PR link.");
|
||||
});
|
||||
}, [api, gitModalOpenPrUrl]);
|
||||
}, [api, gitProgressToastOpenPrUrl]);
|
||||
|
||||
const runGitAction = useCallback(async () => {
|
||||
if (!api || !gitCwd || !isGitModalOpen) return;
|
||||
if (!selectedGitModalActionOption || selectedGitModalActionOption.disabled) return;
|
||||
const actionCwd = gitCwd;
|
||||
const action = selectedGitModalActionOption.action;
|
||||
const commitMessage = gitModalCommitMessage.trim();
|
||||
const includeGeneratedCommitMessage = commitMessage.length === 0;
|
||||
|
||||
setIsGitModalActionRunning(true);
|
||||
const dismissGitProgressToast = useCallback(() => {
|
||||
if (isGitModalActionRunning) return;
|
||||
setIsGitProgressToastVisible(false);
|
||||
setGitModalProgress([]);
|
||||
setGitModalError(null);
|
||||
setGitActionError(null);
|
||||
setGitModalResult(null);
|
||||
setGitModalProgress(initialGitProgressSteps(action, commitMessage));
|
||||
}, [isGitModalActionRunning]);
|
||||
|
||||
let commit: GitRunStackedActionResult["commit"] = {
|
||||
status: "skipped_no_changes",
|
||||
};
|
||||
let push: GitRunStackedActionResult["push"] = {
|
||||
status: "skipped_not_requested",
|
||||
};
|
||||
let pr: GitRunStackedActionResult["pr"] = {
|
||||
status: "skipped_not_requested",
|
||||
};
|
||||
const executeGitAction = useCallback(
|
||||
async (params: {
|
||||
action: GitStackedAction;
|
||||
commitMessage: string;
|
||||
includeGeneratedCommitMessage: boolean;
|
||||
closeModal: boolean;
|
||||
}) => {
|
||||
if (!api || !gitCwd) return;
|
||||
const actionCwd = gitCwd;
|
||||
const { action, closeModal, commitMessage, includeGeneratedCommitMessage } = params;
|
||||
|
||||
const updateStep = (
|
||||
id: GitProgressStep["id"],
|
||||
status: GitProgressStepStatus,
|
||||
detail?: string,
|
||||
) => {
|
||||
setGitModalProgress((steps) => updateProgressStep(steps, id, status, detail));
|
||||
};
|
||||
|
||||
try {
|
||||
if (includeGeneratedCommitMessage) {
|
||||
updateStep("generate", "running");
|
||||
} else {
|
||||
updateStep("commit", "running");
|
||||
setIsGitMenuOpen(false);
|
||||
if (closeModal) {
|
||||
setIsGitModalOpen(false);
|
||||
}
|
||||
setIsGitProgressToastVisible(true);
|
||||
setIsGitModalActionRunning(true);
|
||||
setGitModalError(null);
|
||||
setGitActionError(null);
|
||||
setGitModalResult(null);
|
||||
setGitModalProgress(initialGitProgressSteps(action, includeGeneratedCommitMessage));
|
||||
|
||||
const commitRun = await api.git.runStackedAction({
|
||||
cwd: actionCwd,
|
||||
action: "commit",
|
||||
...(commitMessage.length > 0 ? { commitMessage } : {}),
|
||||
});
|
||||
commit = commitRun.commit;
|
||||
let commit: GitRunStackedActionResult["commit"] = {
|
||||
status: "skipped_no_changes",
|
||||
};
|
||||
let push: GitRunStackedActionResult["push"] = {
|
||||
status: "skipped_not_requested",
|
||||
};
|
||||
let pr: GitRunStackedActionResult["pr"] = {
|
||||
status: "skipped_not_requested",
|
||||
};
|
||||
|
||||
if (includeGeneratedCommitMessage) {
|
||||
if (commitRun.commit.status === "created") {
|
||||
if (commitRun.commit.subject) {
|
||||
setGitModalCommitMessage(commitRun.commit.subject);
|
||||
const updateStep = (
|
||||
id: GitProgressStep["id"],
|
||||
status: GitProgressStepStatus,
|
||||
detail?: string,
|
||||
) => {
|
||||
setGitModalProgress((steps) => updateProgressStep(steps, id, status, detail));
|
||||
};
|
||||
|
||||
try {
|
||||
if (includeGeneratedCommitMessage) {
|
||||
updateStep("generate", "running");
|
||||
} else {
|
||||
updateStep("commit", "running");
|
||||
}
|
||||
|
||||
const commitRun = await api.git.runStackedAction({
|
||||
cwd: actionCwd,
|
||||
action: "commit",
|
||||
...(commitMessage.length > 0 ? { commitMessage } : {}),
|
||||
});
|
||||
commit = commitRun.commit;
|
||||
|
||||
if (includeGeneratedCommitMessage) {
|
||||
if (commitRun.commit.status === "created") {
|
||||
if (commitRun.commit.subject) {
|
||||
setGitModalCommitMessage(commitRun.commit.subject);
|
||||
}
|
||||
updateStep("generate", "completed");
|
||||
} else {
|
||||
updateStep("generate", "skipped", "No local changes to commit.");
|
||||
}
|
||||
updateStep("generate", "completed");
|
||||
} else {
|
||||
updateStep("generate", "skipped", "No local changes to commit.");
|
||||
}
|
||||
}
|
||||
|
||||
if (commitRun.commit.status === "created") {
|
||||
updateStep("commit", "completed", commitRun.commit.subject ?? "Committed local changes.");
|
||||
} else {
|
||||
updateStep("commit", "skipped", "No local changes to commit.");
|
||||
}
|
||||
if (commitRun.commit.status === "created") {
|
||||
updateStep("commit", "completed", commitRun.commit.subject ?? "Committed local changes.");
|
||||
} else {
|
||||
updateStep("commit", "skipped", "No local changes to commit.");
|
||||
}
|
||||
|
||||
if (action !== "commit") {
|
||||
updateStep("push", "running");
|
||||
const pushRun = await api.git.runStackedAction({
|
||||
cwd: actionCwd,
|
||||
action: "commit_push",
|
||||
if (action !== "commit") {
|
||||
updateStep("push", "running");
|
||||
const pushRun = await api.git.runStackedAction({
|
||||
cwd: actionCwd,
|
||||
action: "commit_push",
|
||||
});
|
||||
push = pushRun.push;
|
||||
if (pushRun.push.status === "pushed") {
|
||||
updateStep(
|
||||
"push",
|
||||
"completed",
|
||||
pushRun.push.upstreamBranch
|
||||
? `Pushed to ${pushRun.push.upstreamBranch}.`
|
||||
: "Pushed latest commits.",
|
||||
);
|
||||
} else {
|
||||
updateStep("push", "skipped", "Branch already up to date.");
|
||||
}
|
||||
}
|
||||
|
||||
if (action === "commit_push_pr") {
|
||||
updateStep("pr", "running");
|
||||
const prRun = await api.git.runStackedAction({
|
||||
cwd: actionCwd,
|
||||
action: "commit_push_pr",
|
||||
});
|
||||
pr = prRun.pr;
|
||||
if (prRun.pr.status === "opened_existing") {
|
||||
updateStep(
|
||||
"pr",
|
||||
"completed",
|
||||
prRun.pr.number ? `Opened existing PR #${prRun.pr.number}.` : "Opened existing PR.",
|
||||
);
|
||||
} else if (prRun.pr.status === "created") {
|
||||
updateStep(
|
||||
"pr",
|
||||
"completed",
|
||||
prRun.pr.number ? `Created PR #${prRun.pr.number}.` : "Created PR.",
|
||||
);
|
||||
} else {
|
||||
updateStep("pr", "skipped", "PR step was not requested.");
|
||||
}
|
||||
}
|
||||
|
||||
setGitModalResult({ action, commit, push, pr });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Git action failed.";
|
||||
setGitModalError(message);
|
||||
setGitActionError(message);
|
||||
setGitModalProgress((steps) => {
|
||||
const active = steps.find((step) => step.status === "running");
|
||||
if (!active) return steps;
|
||||
return updateProgressStep(steps, active.id, "failed", message);
|
||||
});
|
||||
push = pushRun.push;
|
||||
if (pushRun.push.status === "pushed") {
|
||||
updateStep(
|
||||
"push",
|
||||
"completed",
|
||||
pushRun.push.upstreamBranch
|
||||
? `Pushed to ${pushRun.push.upstreamBranch}.`
|
||||
: "Pushed latest commits.",
|
||||
);
|
||||
} else {
|
||||
updateStep("push", "skipped", "Branch already up to date.");
|
||||
}
|
||||
} finally {
|
||||
setIsGitModalActionRunning(false);
|
||||
await refreshGitStatus().catch(() => undefined);
|
||||
}
|
||||
},
|
||||
[api, gitCwd, refreshGitStatus],
|
||||
);
|
||||
|
||||
if (action === "commit_push_pr") {
|
||||
updateStep("pr", "running");
|
||||
const prRun = await api.git.runStackedAction({
|
||||
cwd: actionCwd,
|
||||
action: "commit_push_pr",
|
||||
});
|
||||
pr = prRun.pr;
|
||||
if (prRun.pr.status === "opened_existing") {
|
||||
updateStep(
|
||||
"pr",
|
||||
"completed",
|
||||
prRun.pr.number ? `Opened existing PR #${prRun.pr.number}.` : "Opened existing PR.",
|
||||
);
|
||||
} else if (prRun.pr.status === "created") {
|
||||
updateStep(
|
||||
"pr",
|
||||
"completed",
|
||||
prRun.pr.number ? `Created PR #${prRun.pr.number}.` : "Created PR.",
|
||||
);
|
||||
} else {
|
||||
updateStep("pr", "skipped", "PR step was not requested.");
|
||||
}
|
||||
}
|
||||
|
||||
setGitModalResult({ action, commit, push, pr });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Git action failed.";
|
||||
setGitModalError(message);
|
||||
setGitActionError(message);
|
||||
setGitModalProgress((steps) => {
|
||||
const active = steps.find((step) => step.status === "running");
|
||||
if (!active) return steps;
|
||||
return updateProgressStep(steps, active.id, "failed", message);
|
||||
const runGitActionImmediately = useCallback(
|
||||
(action: GitStackedAction) => {
|
||||
void executeGitAction({
|
||||
action,
|
||||
commitMessage: "",
|
||||
includeGeneratedCommitMessage: false,
|
||||
closeModal: false,
|
||||
});
|
||||
} finally {
|
||||
setIsGitModalActionRunning(false);
|
||||
await refreshGitStatus().catch(() => undefined);
|
||||
}
|
||||
}, [
|
||||
api,
|
||||
gitCwd,
|
||||
gitModalCommitMessage,
|
||||
isGitModalOpen,
|
||||
refreshGitStatus,
|
||||
selectedGitModalActionOption,
|
||||
]);
|
||||
},
|
||||
[executeGitAction],
|
||||
);
|
||||
|
||||
const runGitAction = useCallback(() => {
|
||||
if (!selectedGitModalActionOption || selectedGitModalActionOption.disabled) return;
|
||||
const commitMessage = gitModalCommitMessage.trim();
|
||||
void executeGitAction({
|
||||
action: selectedGitModalActionOption.action,
|
||||
commitMessage,
|
||||
includeGeneratedCommitMessage: commitMessage.length === 0,
|
||||
closeModal: true,
|
||||
});
|
||||
}, [executeGitAction, gitModalCommitMessage, selectedGitModalActionOption]);
|
||||
|
||||
useEffect(() => {
|
||||
setGitActionError(null);
|
||||
setGitModalError(null);
|
||||
setIsGitModalOpen(false);
|
||||
setIsGitProgressToastVisible(false);
|
||||
setGitModalSelectedAction("commit");
|
||||
setGitModalCommitMessage("");
|
||||
setGitModalProgress([]);
|
||||
@@ -560,7 +643,7 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
}}
|
||||
disabled={!gitStatus || isGitActionRunning}
|
||||
>
|
||||
{isGitActionRunning ? "Running..." : "Git actions"}
|
||||
Git actions
|
||||
<span aria-hidden="true">▾</span>
|
||||
</button>
|
||||
{isGitMenuOpen && (
|
||||
@@ -690,92 +773,57 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
placeholder="Leave blank to autogenerate a commit message"
|
||||
value={gitModalCommitMessage}
|
||||
onChange={(event) => setGitModalCommitMessage(event.target.value)}
|
||||
disabled={!gitModalSelectionMode || isGitActionRunning}
|
||||
disabled={isGitActionRunning}
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-6">
|
||||
<p className="text-sm font-medium text-foreground">Next steps</p>
|
||||
<div className="mt-2 overflow-hidden rounded-2xl border border-border">
|
||||
{gitModalSelectionMode &&
|
||||
gitModalActionOptions.map((option, index) => {
|
||||
const borderClass =
|
||||
index < gitModalActionOptions.length - 1 ? "border-b border-border/70" : "";
|
||||
const selected = option.action === gitModalSelectedAction && !option.disabled;
|
||||
const disabled = option.disabled || isGitActionRunning;
|
||||
{gitModalActionOptions.map((option, index) => {
|
||||
const borderClass =
|
||||
index < gitModalActionOptions.length - 1 ? "border-b border-border/70" : "";
|
||||
const selected = option.action === gitModalSelectedAction && !option.disabled;
|
||||
const disabled = option.disabled || isGitActionRunning;
|
||||
|
||||
return (
|
||||
<button
|
||||
key={option.action}
|
||||
type="button"
|
||||
className={`flex w-full items-start gap-3 px-4 py-3 text-left transition-colors duration-150 ${borderClass} ${
|
||||
selected
|
||||
? "bg-accent/55"
|
||||
: disabled
|
||||
? "bg-card/25"
|
||||
: "bg-card/45 hover:bg-accent/35"
|
||||
} disabled:cursor-not-allowed`}
|
||||
disabled={disabled}
|
||||
onClick={() => {
|
||||
setGitModalSelectedAction(option.action);
|
||||
}}
|
||||
>
|
||||
<span className="mt-0.5">
|
||||
<GitActionIcon icon={option.icon} disabled={option.disabled} />
|
||||
</span>
|
||||
<div className="min-w-0 flex-1">
|
||||
return (
|
||||
<button
|
||||
key={option.action}
|
||||
type="button"
|
||||
className={`flex w-full items-start gap-3 px-4 py-3 text-left transition-colors duration-150 ${borderClass} ${
|
||||
selected
|
||||
? "bg-accent/55"
|
||||
: disabled
|
||||
? "bg-card/25"
|
||||
: "bg-card/45 hover:bg-accent/35"
|
||||
} disabled:cursor-not-allowed`}
|
||||
disabled={disabled}
|
||||
onClick={() => {
|
||||
setGitModalSelectedAction(option.action);
|
||||
}}
|
||||
>
|
||||
<span className="mt-0.5">
|
||||
<GitActionIcon icon={option.icon} disabled={option.disabled} />
|
||||
</span>
|
||||
<div className="min-w-0 flex-1">
|
||||
<p
|
||||
className={`text-sm ${disabled ? "text-muted-foreground/60" : "text-foreground"}`}
|
||||
>
|
||||
{option.label}
|
||||
</p>
|
||||
{option.detail && (
|
||||
<p
|
||||
className={`text-sm ${disabled ? "text-muted-foreground/60" : "text-foreground"}`}
|
||||
className={`mt-0.5 text-xs ${disabled ? "text-muted-foreground/55" : "text-muted-foreground/70"}`}
|
||||
>
|
||||
{option.label}
|
||||
{option.detail}
|
||||
</p>
|
||||
{option.detail && (
|
||||
<p
|
||||
className={`mt-0.5 text-xs ${disabled ? "text-muted-foreground/55" : "text-muted-foreground/70"}`}
|
||||
>
|
||||
{option.detail}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
{selected && !disabled ? (
|
||||
<CheckIcon className="mt-0.5 h-4 w-4 shrink-0 text-foreground" />
|
||||
) : null}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
{!gitModalSelectionMode &&
|
||||
gitModalSteps.map((step, index) => {
|
||||
const borderClass =
|
||||
index < gitModalSteps.length - 1 ? "border-b border-border/70" : "";
|
||||
|
||||
return (
|
||||
<div
|
||||
key={step.id}
|
||||
className={`flex items-start gap-3 bg-card/45 px-4 py-3 ${borderClass}`}
|
||||
>
|
||||
<span className="mt-0.5">
|
||||
{step.status === "running" ? (
|
||||
<Loader2Icon className="h-4 w-4 animate-spin text-foreground" />
|
||||
) : step.status === "completed" ? (
|
||||
<CheckIcon className="h-4 w-4 text-emerald-600 dark:text-emerald-300" />
|
||||
) : step.status === "skipped" ? (
|
||||
<MinusIcon className="h-4 w-4 text-muted-foreground/70" />
|
||||
) : step.status === "failed" ? (
|
||||
<XIcon className="h-4 w-4 text-rose-500 dark:text-rose-300" />
|
||||
) : (
|
||||
<CircleIcon className="h-4 w-4 text-muted-foreground/60" />
|
||||
)}
|
||||
</span>
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm text-foreground">{step.label}</p>
|
||||
{step.status === "failed" && step.detail && (
|
||||
<p className="mt-0.5 text-xs text-rose-500 dark:text-rose-300">
|
||||
{step.detail}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{selected && !disabled ? (
|
||||
<CheckIcon className="mt-0.5 h-4 w-4 shrink-0 text-foreground" />
|
||||
) : null}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
{(gitModalError ?? visibleGitActionError) && (
|
||||
@@ -784,48 +832,74 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
</div>
|
||||
)}
|
||||
<div className="mt-6 flex justify-end gap-2">
|
||||
{gitModalResult && gitModalOpenPrUrl && (
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-xl border border-border px-4 py-2 text-sm text-foreground transition-colors duration-150 hover:bg-accent disabled:cursor-not-allowed disabled:opacity-60"
|
||||
onClick={openPrFromModal}
|
||||
disabled={isGitActionRunning}
|
||||
>
|
||||
Open PR
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className={
|
||||
gitModalResult
|
||||
? "rounded-xl bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
||||
: "rounded-xl border border-border px-4 py-2 text-sm text-foreground transition-colors duration-150 hover:bg-accent disabled:cursor-not-allowed disabled:opacity-60"
|
||||
}
|
||||
className="rounded-xl border border-border px-4 py-2 text-sm text-foreground transition-colors duration-150 hover:bg-accent disabled:cursor-not-allowed disabled:opacity-60"
|
||||
onClick={closeGitActionModal}
|
||||
disabled={isGitActionRunning}
|
||||
>
|
||||
{gitModalResult ? "Done" : "Cancel"}
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-xl bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
||||
onClick={() => {
|
||||
runGitAction();
|
||||
}}
|
||||
disabled={isGitActionRunning || !selectedGitModalActionOption}
|
||||
>
|
||||
{runActionLabel(gitModalSelectedAction)}
|
||||
</button>
|
||||
{!gitModalResult && (
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-xl bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
||||
onClick={() => {
|
||||
void runGitAction();
|
||||
}}
|
||||
disabled={isGitActionRunning || !selectedGitModalActionOption}
|
||||
>
|
||||
{isGitActionRunning
|
||||
? "Running..."
|
||||
: gitModalSelectionMode
|
||||
? "Continue"
|
||||
: runActionLabel(gitModalSelectedAction)}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{showGitProgressToast && (
|
||||
<div
|
||||
className="fixed bottom-4 right-4 z-[90]"
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
>
|
||||
<div className="flex max-w-[calc(100vw-2rem)] items-center gap-2 rounded-xl border border-border bg-popover/95 px-3 py-2 shadow-xl backdrop-blur">
|
||||
<span className="shrink-0">
|
||||
{gitProgressToastStatus === "running" ? (
|
||||
<Loader2Icon className="h-4 w-4 animate-spin text-foreground" />
|
||||
) : gitProgressToastStatus === "failed" ? (
|
||||
<XIcon className="h-4 w-4 text-rose-500 dark:text-rose-300" />
|
||||
) : (
|
||||
<CheckIcon className="h-4 w-4 text-emerald-600 dark:text-emerald-300" />
|
||||
)}
|
||||
</span>
|
||||
<span className="min-w-0 truncate text-sm text-foreground">{gitProgressToastMessage}</span>
|
||||
{!isGitModalActionRunning && gitProgressToastAction && (
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-md bg-foreground px-2.5 py-1 text-xs font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
||||
onClick={() => {
|
||||
if (gitProgressToastAction.kind === "open_pr") {
|
||||
openPrFromToast();
|
||||
return;
|
||||
}
|
||||
runGitActionImmediately("commit_push_pr");
|
||||
}}
|
||||
disabled={isGitModalActionRunning}
|
||||
>
|
||||
{gitProgressToastAction.label}
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="rounded p-1 text-muted-foreground/60 transition-colors duration-150 hover:bg-accent hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50"
|
||||
onClick={dismissGitProgressToast}
|
||||
disabled={isGitModalActionRunning}
|
||||
aria-label="Dismiss git action progress toast"
|
||||
>
|
||||
<XIcon className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
type Thread,
|
||||
} from "../types";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "../worktreeCleanup";
|
||||
|
||||
const THEME_CYCLE = { system: "light", light: "dark", dark: "system" } as const;
|
||||
function formatRelativeTime(iso: string): string {
|
||||
@@ -262,6 +263,22 @@ export default function Sidebar() {
|
||||
|
||||
const thread = state.threads.find((t) => t.id === threadId);
|
||||
if (!thread) return;
|
||||
const threadProject = state.projects.find((project) => project.id === thread.projectId);
|
||||
const orphanedWorktreePath = getOrphanedWorktreePathForThread(state.threads, threadId);
|
||||
const displayWorktreePath = orphanedWorktreePath
|
||||
? formatWorktreePathForDisplay(orphanedWorktreePath)
|
||||
: null;
|
||||
const canDeleteWorktree = orphanedWorktreePath !== null && threadProject !== undefined;
|
||||
const shouldDeleteWorktree =
|
||||
canDeleteWorktree &&
|
||||
(await api.dialogs.confirm(
|
||||
[
|
||||
"This thread is the only one linked to this worktree:",
|
||||
displayWorktreePath ?? orphanedWorktreePath,
|
||||
"",
|
||||
"Delete the worktree too?",
|
||||
].join("\n"),
|
||||
));
|
||||
|
||||
// Stop active session if running
|
||||
if (thread.session?.sessionId) {
|
||||
@@ -284,8 +301,21 @@ export default function Sidebar() {
|
||||
}
|
||||
|
||||
dispatch({ type: "DELETE_THREAD", threadId });
|
||||
|
||||
if (!shouldDeleteWorktree || !orphanedWorktreePath || !threadProject) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await api.git.removeWorktree({
|
||||
cwd: threadProject.cwd,
|
||||
path: orphanedWorktreePath,
|
||||
});
|
||||
} catch {
|
||||
// Worktree deletion is best-effort and should not block thread deletion.
|
||||
}
|
||||
},
|
||||
[api, dispatch, state.threads],
|
||||
[api, dispatch, state.projects, state.threads],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import type { GitRunStackedActionResult, GitStatusResult } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getGitProgressToastAction } from "./components/GitActionsControl";
|
||||
|
||||
function makeStatus(overrides?: Partial<GitStatusResult>): GitStatusResult {
|
||||
return {
|
||||
branch: "feature/test-branch",
|
||||
hasWorkingTreeChanges: false,
|
||||
hasUpstream: true,
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
openPr: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeResult(overrides?: Partial<GitRunStackedActionResult>): GitRunStackedActionResult {
|
||||
return {
|
||||
action: "commit_push",
|
||||
commit: { status: "skipped_no_changes" },
|
||||
push: { status: "pushed", branch: "feature/test-branch" },
|
||||
pr: { status: "skipped_not_requested" },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("getGitProgressToastAction", () => {
|
||||
it("returns open PR action when a PR URL is available", () => {
|
||||
const action = getGitProgressToastAction({
|
||||
isRunning: false,
|
||||
hasError: false,
|
||||
result: makeResult(),
|
||||
gitStatus: makeStatus(),
|
||||
openPrUrl: "https://github.com/pingdotgg/codething-mvp/pull/42",
|
||||
});
|
||||
|
||||
expect(action).toEqual({
|
||||
kind: "open_pr",
|
||||
label: "Open PR",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns create PR action after push when branch can open a PR", () => {
|
||||
const action = getGitProgressToastAction({
|
||||
isRunning: false,
|
||||
hasError: false,
|
||||
result: makeResult({ action: "commit_push" }),
|
||||
gitStatus: makeStatus(),
|
||||
openPrUrl: null,
|
||||
});
|
||||
|
||||
expect(action).toEqual({
|
||||
kind: "create_pr",
|
||||
label: "Create PR",
|
||||
});
|
||||
});
|
||||
|
||||
it("does not return create PR action for non-push actions", () => {
|
||||
const action = getGitProgressToastAction({
|
||||
isRunning: false,
|
||||
hasError: false,
|
||||
result: makeResult({ action: "commit" }),
|
||||
gitStatus: makeStatus(),
|
||||
openPrUrl: null,
|
||||
});
|
||||
|
||||
expect(action).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null while running or failed", () => {
|
||||
const running = getGitProgressToastAction({
|
||||
isRunning: true,
|
||||
hasError: false,
|
||||
result: makeResult(),
|
||||
gitStatus: makeStatus(),
|
||||
openPrUrl: "https://github.com/pingdotgg/codething-mvp/pull/42",
|
||||
});
|
||||
|
||||
const failed = getGitProgressToastAction({
|
||||
isRunning: false,
|
||||
hasError: true,
|
||||
result: makeResult(),
|
||||
gitStatus: makeStatus(),
|
||||
openPrUrl: "https://github.com/pingdotgg/codething-mvp/pull/42",
|
||||
});
|
||||
|
||||
expect(running).toBeNull();
|
||||
expect(failed).toBeNull();
|
||||
});
|
||||
});
|
||||
Vendored
+1
@@ -5,6 +5,7 @@ import type { NativeApi } from "@t3tools/contracts";
|
||||
interface DesktopBridge {
|
||||
getWsUrl: () => string | null;
|
||||
pickFolder: () => Promise<string | null>;
|
||||
confirm: (message: string) => Promise<boolean>;
|
||||
showContextMenu: (items: readonly { id: string; label: string }[]) => Promise<string | null>;
|
||||
openExternal: (url: string) => Promise<boolean>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { DEFAULT_THREAD_TERMINAL_HEIGHT, DEFAULT_THREAD_TERMINAL_ID, type Thread } from "./types";
|
||||
import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "./worktreeCleanup";
|
||||
|
||||
function makeThread(overrides: Partial<Thread> = {}): Thread {
|
||||
return {
|
||||
id: "thread-1",
|
||||
codexThreadId: null,
|
||||
projectId: "project-1",
|
||||
title: "Thread",
|
||||
model: "gpt-5.3-codex",
|
||||
terminalOpen: false,
|
||||
terminalHeight: DEFAULT_THREAD_TERMINAL_HEIGHT,
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
runningTerminalIds: [],
|
||||
activeTerminalId: DEFAULT_THREAD_TERMINAL_ID,
|
||||
terminalGroups: [
|
||||
{
|
||||
id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
},
|
||||
],
|
||||
activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
|
||||
session: null,
|
||||
messages: [],
|
||||
events: [],
|
||||
error: null,
|
||||
createdAt: "2026-02-13T00:00:00.000Z",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("getOrphanedWorktreePathForThread", () => {
|
||||
it("returns null when the target thread does not exist", () => {
|
||||
const result = getOrphanedWorktreePathForThread([], "missing-thread");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when the target thread has no worktree", () => {
|
||||
const threads = [makeThread()];
|
||||
const result = getOrphanedWorktreePathForThread(threads, "thread-1");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns the path when no other thread links to that worktree", () => {
|
||||
const threads = [makeThread({ worktreePath: "/tmp/repo/worktrees/feature-a" })];
|
||||
const result = getOrphanedWorktreePathForThread(threads, "thread-1");
|
||||
expect(result).toBe("/tmp/repo/worktrees/feature-a");
|
||||
});
|
||||
|
||||
it("returns null when another thread links to the same worktree", () => {
|
||||
const threads = [
|
||||
makeThread({ id: "thread-1", worktreePath: "/tmp/repo/worktrees/feature-a" }),
|
||||
makeThread({ id: "thread-2", worktreePath: "/tmp/repo/worktrees/feature-a" }),
|
||||
];
|
||||
const result = getOrphanedWorktreePathForThread(threads, "thread-1");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("ignores threads linked to different worktrees", () => {
|
||||
const threads = [
|
||||
makeThread({ id: "thread-1", worktreePath: "/tmp/repo/worktrees/feature-a" }),
|
||||
makeThread({ id: "thread-2", worktreePath: "/tmp/repo/worktrees/feature-b" }),
|
||||
];
|
||||
const result = getOrphanedWorktreePathForThread(threads, "thread-1");
|
||||
expect(result).toBe("/tmp/repo/worktrees/feature-a");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatWorktreePathForDisplay", () => {
|
||||
it("shows only the last path segment for unix-like paths", () => {
|
||||
const result = formatWorktreePathForDisplay(
|
||||
"/Users/julius/.t3/worktrees/codething-mvp/codething-4e609bb8",
|
||||
);
|
||||
expect(result).toBe("codething-4e609bb8");
|
||||
});
|
||||
|
||||
it("normalizes windows separators before selecting the final segment", () => {
|
||||
const result = formatWorktreePathForDisplay(
|
||||
"C:\\Users\\julius\\.t3\\worktrees\\codething-mvp\\codething-4e609bb8",
|
||||
);
|
||||
expect(result).toBe("codething-4e609bb8");
|
||||
});
|
||||
|
||||
it("uses the final segment even when outside ~/.t3/worktrees", () => {
|
||||
const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree");
|
||||
expect(result).toBe("my-worktree");
|
||||
});
|
||||
|
||||
it("ignores trailing slashes", () => {
|
||||
const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree/");
|
||||
expect(result).toBe("my-worktree");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { Thread } from "./types";
|
||||
|
||||
function normalizeWorktreePath(path: string | null): string | null {
|
||||
const trimmed = path?.trim();
|
||||
if (!trimmed) {
|
||||
return null;
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
export function getOrphanedWorktreePathForThread(
|
||||
threads: readonly Thread[],
|
||||
threadId: string,
|
||||
): string | null {
|
||||
const targetThread = threads.find((thread) => thread.id === threadId);
|
||||
if (!targetThread) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const targetWorktreePath = normalizeWorktreePath(targetThread.worktreePath);
|
||||
if (!targetWorktreePath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const isShared = threads.some((thread) => {
|
||||
if (thread.id === threadId) {
|
||||
return false;
|
||||
}
|
||||
return normalizeWorktreePath(thread.worktreePath) === targetWorktreePath;
|
||||
});
|
||||
|
||||
return isShared ? null : targetWorktreePath;
|
||||
}
|
||||
|
||||
export function formatWorktreePathForDisplay(worktreePath: string): string {
|
||||
const trimmed = worktreePath.trim();
|
||||
if (!trimmed) {
|
||||
return worktreePath;
|
||||
}
|
||||
|
||||
const normalized = trimmed.replace(/\\/g, "/").replace(/\/+$/, "");
|
||||
const parts = normalized.split("/");
|
||||
const lastPart = parts[parts.length - 1]?.trim() ?? "";
|
||||
return lastPart.length > 0 ? lastPart : trimmed;
|
||||
}
|
||||
@@ -60,6 +60,12 @@ export function createWsNativeApi(): NativeApi {
|
||||
if (!window.desktopBridge) return null;
|
||||
return window.desktopBridge.pickFolder();
|
||||
},
|
||||
confirm: async (message) => {
|
||||
if (window.desktopBridge) {
|
||||
return window.desktopBridge.confirm(message);
|
||||
}
|
||||
return window.confirm(message);
|
||||
},
|
||||
},
|
||||
terminal: {
|
||||
open: (input) => transport.request(WS_METHODS.terminalOpen, input),
|
||||
|
||||
@@ -56,6 +56,7 @@ export interface NativeApi {
|
||||
};
|
||||
dialogs: {
|
||||
pickFolder: () => Promise<string | null>;
|
||||
confirm: (message: string) => Promise<boolean>;
|
||||
};
|
||||
terminal: {
|
||||
open: (input: TerminalOpenInput) => Promise<TerminalSessionSnapshot>;
|
||||
|
||||
Reference in new issue
Block a user