Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@superset/ui/dropdown-menu";
import { cn } from "@superset/ui/utils";
Expand All @@ -12,6 +13,7 @@ import {
HiMiniCog6Tooth,
HiMiniPlay,
HiMiniStop,
HiMiniXMark,
} from "react-icons/hi2";
import { electronTrpc } from "renderer/lib/electron-trpc";
import { useWorkspaceRunCommand } from "renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/useWorkspaceRunCommand";
Expand All @@ -32,7 +34,13 @@ export const WorkspaceRunButton = memo(function WorkspaceRunButton({
const navigate = useNavigate();
const setSettingsSearchQuery = useSetSettingsSearchQuery();
const hotkeyText = useHotkeyText("RUN_WORKSPACE_COMMAND");
const { isRunning, isPending, toggleWorkspaceRun } = useWorkspaceRunCommand({
const {
canForceStop,
forceStopWorkspaceRun,
isRunning,
isPending,
toggleWorkspaceRun,
} = useWorkspaceRunCommand({
workspaceId,
worktreePath,
});
Expand Down Expand Up @@ -73,6 +81,10 @@ export const WorkspaceRunButton = memo(function WorkspaceRunButton({
});
}, [navigate, projectId, setSettingsSearchQuery]);

const handleForceStopClick = useCallback(() => {
void forceStopWorkspaceRun();
}, [forceStopWorkspaceRun]);

const buttonLabel = isRunning ? "Stop" : hasRunCommand ? "Run" : "Set Run";
const buttonAriaLabel = isRunning
? "Stop workspace run command"
Expand Down Expand Up @@ -137,6 +149,18 @@ export const WorkspaceRunButton = memo(function WorkspaceRunButton({
</DropdownMenuTrigger>

<DropdownMenuContent align="end" className="w-40">
{canForceStop && (
<>
<DropdownMenuItem
onClick={handleForceStopClick}
className="text-destructive focus:text-destructive"
>
<HiMiniXMark className="mr-2 size-4 text-destructive" />
Force Stop
</DropdownMenuItem>
<DropdownMenuSeparator />
</>
)}
<DropdownMenuItem onClick={handleConfigureClick}>
<HiMiniCog6Tooth className="mr-2 size-4" />
Configure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { toast } from "@superset/ui/sonner";
import { useCallback, useRef, useState } from "react";
import { buildTerminalCommand } from "renderer/lib/terminal/launch-command";
import {
buildTerminalCommand,
launchCommandInPane,
} from "renderer/lib/terminal/launch-command";
import { electronTrpcClient } from "renderer/lib/trpc-client";
import { useTabsStore } from "renderer/stores/tabs/store";
import { useTerminalCallbacksStore } from "renderer/stores/tabs/terminal-callbacks";
import {
clearPaneWorkspaceRunLaunchPending,
createWorkspaceRun,
markPaneWorkspaceRunLaunchPending,
setPaneWorkspaceRunState,
} from "renderer/stores/tabs/workspace-run";

Expand All @@ -14,6 +19,8 @@ interface UseWorkspaceRunCommandOptions {
worktreePath?: string | null;
}

const CTRL_C_INPUT = "\u0003";

export function useWorkspaceRunCommand({
workspaceId,
worktreePath,
Expand All @@ -40,19 +47,65 @@ export function useWorkspaceRunCommand({
});

const isRunning = runPane?.workspaceRun?.state === "running";
const canForceStop = isRunning && Boolean(runPane);

const launchWorkspaceRunInPane = useCallback(
async ({
paneId,
tabId,
command,
cwd,
}: {
paneId: string;
tabId: string;
command: string;
cwd?: string;
}) => {
markPaneWorkspaceRunLaunchPending(paneId);
try {
await launchCommandInPane({
paneId,
tabId,
workspaceId,
command,
cwd,
createOrAttach: (input) =>
electronTrpcClient.terminal.createOrAttach.mutate({
...input,
allowKilled: true,
}),
write: (input) => electronTrpcClient.terminal.write.mutate(input),
});
} finally {
clearPaneWorkspaceRunLaunchPending(paneId);
}
},
[workspaceId],
);

const toggleWorkspaceRun = useCallback(async () => {
if (isStartingRef.current) return;

// STOP: if currently running, kill it
// STOP: send Ctrl+C through the PTY so the run command stops the same
// way it would if the user interrupted it from the keyboard.
if (isRunning && runPane) {
setIsPending(true);
try {
await electronTrpcClient.terminal.kill.mutate({ paneId: runPane.id });
await electronTrpcClient.terminal.write.mutate({
paneId: runPane.id,
data: CTRL_C_INPUT,
throwOnError: true,
});
setPaneWorkspaceRunState(runPane.id, "stopped-by-user");
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
if (message.includes("not found") || message.includes("not alive")) {
setPaneWorkspaceRunState(runPane.id, "stopped-by-exit");
return;
}
toast.error("Failed to stop workspace run command", {
description: error instanceof Error ? error.message : "Unknown error",
description: message,
});
} finally {
setIsPending(false);
Expand Down Expand Up @@ -103,23 +156,12 @@ export function useWorkspaceRunCommand({
if (restartCallback) {
await restartCallback({ command });
} else {
const existingSession = await electronTrpcClient.terminal.getSession
.query(runPane.id)
.catch(() => null);
if (existingSession?.isAlive) {
await electronTrpcClient.terminal.kill.mutate({
paneId: runPane.id,
});
}
await electronTrpcClient.terminal.createOrAttach.mutate({
await launchWorkspaceRunInPane({
paneId: runPane.id,
tabId: runPane.tabId,
workspaceId,
allowKilled: true,
command,
cwd: initialCwd,
});
// Re-assert running state — the kill above may have triggered
// the exit listener which flipped state to stopped-by-user.
setPaneWorkspaceRun(
runPane.id,
createWorkspaceRun({
Expand Down Expand Up @@ -156,6 +198,19 @@ export function useWorkspaceRunCommand({
);
setActiveTab(workspaceId, tabId);
setFocusedPane(tabId, paneId);
try {
await launchWorkspaceRunInPane({
paneId,
tabId,
command,
cwd: initialCwd,
});
} catch (error) {
setPaneWorkspaceRunState(paneId, "stopped-by-exit");
toast.error("Failed to run workspace command", {
description: error instanceof Error ? error.message : "Unknown error",
});
}
} catch (error) {
toast.error("Failed to resolve workspace run command", {
description: error instanceof Error ? error.message : "Unknown error",
Expand All @@ -168,6 +223,7 @@ export function useWorkspaceRunCommand({
addTab,
getRestartCallback,
isRunning,
launchWorkspaceRunInPane,
runPane,
setActiveTab,
setFocusedPane,
Expand All @@ -177,7 +233,32 @@ export function useWorkspaceRunCommand({
worktreePath,
]);

const forceStopWorkspaceRun = useCallback(async () => {
if (!runPane || !isRunning || isStartingRef.current) return;

setIsPending(true);
try {
await electronTrpcClient.terminal.kill.mutate({
paneId: runPane.id,
});
setPaneWorkspaceRunState(runPane.id, "stopped-by-user");
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown error";
if (message.includes("not found") || message.includes("not alive")) {
setPaneWorkspaceRunState(runPane.id, "stopped-by-exit");
return;
}
toast.error("Failed to force stop workspace run command", {
description: message,
});
} finally {
setIsPending(false);
}
}, [isRunning, runPane]);

return {
canForceStop,
forceStopWorkspaceRun,
isRunning,
isPending,
toggleWorkspaceRun,
Expand Down
Loading
Loading