diff --git a/src/renderer/src/components/run-actions/RunActionButton.tsx b/src/renderer/src/components/run-actions/RunActionButton.tsx index 2b2accd5885..b095f3a528d 100644 --- a/src/renderer/src/components/run-actions/RunActionButton.tsx +++ b/src/renderer/src/components/run-actions/RunActionButton.tsx @@ -1,17 +1,5 @@ -import React, { useCallback, useEffect, useMemo, useState } from 'react' -import { ChevronDown, Play, Plug, RotateCw, Square, SquareTerminal } from 'lucide-react' +import React, { useEffect, useMemo, useState } from 'react' import { useAppStore } from '@/store' -import { useTranslate } from '@/i18n/use-translate' -import { cn } from '@/lib/utils' -import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuItem, - DropdownMenuLabel, - DropdownMenuSeparator, - DropdownMenuTrigger -} from '@/components/ui/dropdown-menu' import { useShortcutLabel } from '@/hooks/useShortcutLabel' import { useWorktreeRuntimeTarget } from '@/runtime/rpc/targets/use-worktree-runtime-target' import { workspacePortScanKeyForTarget } from '@/lib/workspace-ports/workspace-port-actions' @@ -22,29 +10,18 @@ import { type WorktreeRunActions } from '@/lib/run-actions/run-action-config' import { startRunAction, stopRunAction } from '@/lib/run-actions/run-action-controller' -import { - useWorktreeRunActionRuns, - type RunActionRun, - type RunActionStatus -} from '@/lib/run-actions/run-actions-store' -import type { OrcaRunAction } from '../../../../shared/orca-yaml-hook-types' +import { useWorktreeRunActionRuns, type RunActionRun } from '@/lib/run-actions/run-actions-store' import type { WorkspacePort } from '../../../../shared/workspace-ports' +import { RunActionButtonView } from './RunActionButtonView' const EMPTY_PORTS: WorkspacePort[] = [] -function isLive(status: RunActionStatus | undefined): boolean { - return status === 'running' || status === 'stopping' -} - /** Reloads on workspace switch and window focus, so an orca.yaml edit shows up without a restart. */ -function useWorktreeRunActions(worktreeId: string | null): WorktreeRunActions | null { +function useWorktreeRunActions(worktreeId: string): WorktreeRunActions | null { const [config, setConfig] = useState<{ worktreeId: string; value: WorktreeRunActions } | null>( null ) useEffect(() => { - if (!worktreeId) { - return - } let cancelled = false const load = (): void => { void loadWorktreeRunActions(worktreeId, { background: true }).then((value) => { @@ -74,50 +51,10 @@ function useWorktreePorts(worktreeId: string): WorkspacePort[] { ) } -function StatusDot({ status }: { status: RunActionStatus | undefined }): React.JSX.Element | null { - if (!status) { - return null - } - return ( - - ) -} - -function useStatusLabel(): (run: RunActionRun | undefined) => string { - const t = useTranslate() - return (run) => { - switch (run?.status) { - case 'running': - return t('runActions.status.running', 'Running') - case 'stopping': - return t('runActions.status.stopping', 'Stopping…') - case 'stopped': - return t('runActions.status.stopped', 'Stopped') - case 'exited': - return t('runActions.status.exited', 'Exited') - case 'failed': - return run.exitCode === null - ? t('runActions.status.failed', 'Failed') - : t('runActions.status.failedWithCode', 'Failed (exit {{code}})', { code: run.exitCode }) - default: - return t('runActions.status.notRunning', 'Not running') - } - } -} - -function focusRunTab(worktreeId: string, tabId: string): void { +function focusRunTab(run: RunActionRun): void { const state = useAppStore.getState() - state.setActiveTabType('terminal', worktreeId) - state.setActiveTab(tabId) + state.setActiveTabType('terminal', run.worktreeId) + state.setActiveTab(run.tabId) } function openPortsPanel(): void { @@ -126,69 +63,8 @@ function openPortsPanel(): void { state.setRightSidebarTab('ports') } -function ActionRow({ - worktreeId, - action, - run, - config -}: { - worktreeId: string - action: OrcaRunAction - run: RunActionRun | undefined - config: WorktreeRunActions -}): React.JSX.Element { - const t = useTranslate() - const statusLabel = useStatusLabel() - const live = isLive(run?.status) - return ( - <> - - - {action.name} - {statusLabel(run)} - -
- {action.command} -
- {live ? ( - <> - void startRunAction(worktreeId, action, config)}> - - {t('runActions.restart', 'Restart')} - - void stopRunAction(worktreeId, action.id)} - > - - {t('runActions.stop', 'Stop')} - - - ) : ( - void startRunAction(worktreeId, action, config)}> - - {run ? t('runActions.runAgain', 'Run again') : t('runActions.run', 'Run')} - - )} - {run ? ( - focusRunTab(worktreeId, run.tabId)}> - - {t('runActions.showOutput', 'Show output')} - - ) : null} - - ) -} - -/** - * Titlebar control for orca.yaml run actions. The main button starts the primary action (or shows - * its terminal while it runs); the menu holds every action, Stop/Restart, and the ports it opened. - */ +/** The workspace's Run button; renders nothing when the project defines no run actions. */ export function RunActionButton({ worktreeId }: { worktreeId: string }): React.JSX.Element | null { - const t = useTranslate() const config = useWorktreeRunActions(worktreeId) const runs = useWorktreeRunActionRuns(worktreeId) const ports = useWorktreePorts(worktreeId) @@ -199,94 +75,20 @@ export function RunActionButton({ worktreeId }: { worktreeId: string }): React.J // eslint-disable-next-line react-hooks/exhaustive-deps [config, worktreeId, runs] ) - const primaryRun = primary ? runs[primary.id] : undefined - const anyLive = Object.values(runs).some((run) => isLive(run.status)) - - const onPrimaryClick = useCallback(() => { - if (!config || !primary) { - return - } - if (primaryRun && isLive(primaryRun.status)) { - focusRunTab(worktreeId, primaryRun.tabId) - return - } - void startRunAction(worktreeId, primary, config) - }, [config, primary, primaryRun, worktreeId]) - if (!config || !primary) { return null } - - const primaryLive = isLive(primaryRun?.status) - const primaryLabel = primaryLive - ? t('runActions.showRunning', 'Show {{name}}', { name: primary.name }) - : t('runActions.runNamed', 'Run {{name}}', { name: primary.name }) - return ( -
- - - - - - {shortcut ? `${primaryLabel} (${shortcut})` : primaryLabel} - - - - - - - - {config.actions.map((action, index) => ( - - {index > 0 ? : null} - - - ))} - - - {t('runActions.ports', 'Ports')} - - {ports.length === 0 ? ( -
- {t('runActions.noPorts', 'No servers listening in this workspace yet.')} -
- ) : ( - ports.slice(0, 6).map((port) => ( - - - :{port.port} - - {port.processName ?? ''} - - - )) - )} -
-
-
+ void startRunAction(worktreeId, action, config)} + onStop={(action) => void stopRunAction(worktreeId, action.id)} + onShowOutput={focusRunTab} + onOpenPorts={openPortsPanel} + /> ) } diff --git a/src/renderer/src/components/run-actions/RunActionButtonView.fixture.tsx b/src/renderer/src/components/run-actions/RunActionButtonView.fixture.tsx new file mode 100644 index 00000000000..187884695f1 --- /dev/null +++ b/src/renderer/src/components/run-actions/RunActionButtonView.fixture.tsx @@ -0,0 +1,117 @@ +import { defineFixture } from '../../component-fixtures/component-fixture' +import type { RunActionRun } from '@/lib/run-actions/run-actions-store' +import type { OrcaRunAction } from '../../../../shared/orca-yaml-hook-types' +import type { WorkspacePort } from '../../../../shared/workspace-ports' +import { RunActionButtonView, type RunActionButtonViewProps } from './RunActionButtonView' + +const noop = (): void => {} + +const web: OrcaRunAction = { + id: 'web', + name: 'Web', + command: 'pnpm dev --port $ORCA_PORT', + default: true +} +const worker: OrcaRunAction = { id: 'worker', name: 'Worker', command: 'pnpm worker:dev' } +const tests: OrcaRunAction = { id: 'test', name: 'Test', command: 'pnpm test:watch' } + +function run(action: OrcaRunAction, patch: Partial): RunActionRun { + return { + worktreeId: 'repo::/w', + repoId: 'repo', + actionId: action.id, + actionName: action.name, + tabId: `tab-${action.id}`, + status: 'running', + exitCode: null, + startedAt: 0, + ...patch + } +} + +const port = (value: number, processName: string): WorkspacePort => ({ + kind: 'workspace', + id: `p-${value}`, + bindHost: '127.0.0.1', + connectHost: 'localhost', + port: value, + processName, + protocol: 'http', + owner: { worktreeId: 'repo::/w', confidence: 'cwd' } +}) + +const base: RunActionButtonViewProps = { + actions: [web, worker, tests], + primary: web, + runs: {}, + ports: [], + shortcut: '⌥⌘↩', + onRun: noop, + onStop: noop, + onShowOutput: noop, + onOpenPorts: noop +} + +function Bar({ children }: { children: React.ReactNode }): React.JSX.Element { + return ( +
+ {children} +
+ ) +} + +function openMenu(frame: HTMLElement): void { + const trigger = frame.querySelector('button[aria-label="Run actions"]') + trigger?.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, button: 0 })) +} + +export const idle = defineFixture({ + width: 420, + render: () => ( + + + + ) +}) + +export const idleMenu = defineFixture({ ...idle, width: 420, interact: openMenu }) + +const runningProps: RunActionButtonViewProps = { + ...base, + runs: { web: run(web, {}), test: run(tests, { status: 'failed', exitCode: 1 }) }, + ports: [port(21230, 'node'), port(21231, 'node')] +} + +export const running = defineFixture({ + width: 420, + render: () => ( + + + + ) +}) + +export const runningMenu = defineFixture({ ...running, interact: openMenu }) + +export const stoppingMenu = defineFixture({ + width: 420, + render: () => ( + + + + ), + interact: openMenu +}) + +export const singleLegacyAction = defineFixture({ + width: 420, + render: () => ( + + + + ) +}) diff --git a/src/renderer/src/components/run-actions/RunActionButtonView.tsx b/src/renderer/src/components/run-actions/RunActionButtonView.tsx new file mode 100644 index 00000000000..1c391f20b3d --- /dev/null +++ b/src/renderer/src/components/run-actions/RunActionButtonView.tsx @@ -0,0 +1,225 @@ +import React from 'react' +import { ChevronDown, Play, Plug, RotateCw, Square, SquareTerminal } from 'lucide-react' +import { useTranslate } from '@/i18n/use-translate' +import { cn } from '@/lib/utils' +import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuLabel, + DropdownMenuSeparator, + DropdownMenuTrigger +} from '@/components/ui/dropdown-menu' +import type { RunActionRun, RunActionStatus } from '@/lib/run-actions/run-actions-store' +import type { OrcaRunAction } from '../../../../shared/orca-yaml-hook-types' +import type { WorkspacePort } from '../../../../shared/workspace-ports' + +export function isRunLive(status: RunActionStatus | undefined): boolean { + return status === 'running' || status === 'stopping' +} + +function StatusDot({ status }: { status: RunActionStatus | undefined }): React.JSX.Element | null { + if (!status) { + return null + } + return ( + + ) +} + +function useStatusLabel(): (run: RunActionRun | undefined) => string { + const t = useTranslate() + return (run) => { + switch (run?.status) { + case 'running': + return t('runActions.status.running', 'Running') + case 'stopping': + return t('runActions.status.stopping', 'Stopping…') + case 'stopped': + return t('runActions.status.stopped', 'Stopped') + case 'exited': + return t('runActions.status.exited', 'Exited') + case 'failed': + return run.exitCode === null + ? t('runActions.status.failed', 'Failed') + : t('runActions.status.failedWithCode', 'Failed (exit {{code}})', { code: run.exitCode }) + default: + return t('runActions.status.notRunning', 'Not running') + } + } +} + +export type RunActionButtonViewProps = { + actions: readonly OrcaRunAction[] + primary: OrcaRunAction + runs: Readonly> + ports: readonly WorkspacePort[] + shortcut: string | null + onRun: (action: OrcaRunAction) => void + onStop: (action: OrcaRunAction) => void + onShowOutput: (run: RunActionRun) => void + onOpenPorts: () => void +} + +function ActionRow({ + action, + run, + onRun, + onStop, + onShowOutput +}: { + action: OrcaRunAction + run: RunActionRun | undefined +} & Pick): React.JSX.Element { + const t = useTranslate() + const statusLabel = useStatusLabel() + const live = isRunLive(run?.status) + return ( + <> + + + {action.name} + {statusLabel(run)} + +
+ {action.command} +
+ {live ? ( + <> + onRun(action)}> + + {t('runActions.restart', 'Restart')} + + onStop(action)}> + + {t('runActions.stop', 'Stop')} + + + ) : ( + onRun(action)}> + + {run ? t('runActions.runAgain', 'Run again') : t('runActions.run', 'Run')} + + )} + {run ? ( + onShowOutput(run)}> + + {t('runActions.showOutput', 'Show output')} + + ) : null} + + ) +} + +/** + * Titlebar control for run actions. The main button starts the primary action (or shows its + * terminal while it runs); the menu holds every action, Stop/Restart, and the ports it opened. + */ +export function RunActionButtonView({ + actions, + primary, + runs, + ports, + shortcut, + onRun, + onStop, + onShowOutput, + onOpenPorts +}: RunActionButtonViewProps): React.JSX.Element { + const t = useTranslate() + const primaryRun = runs[primary.id] + const primaryLive = isRunLive(primaryRun?.status) + const anyLive = Object.values(runs).some((run) => isRunLive(run.status)) + const primaryLabel = primaryLive + ? t('runActions.showRunning', 'Show {{name}}', { name: primary.name }) + : t('runActions.runNamed', 'Run {{name}}', { name: primary.name }) + const onPrimaryClick = (): void => { + if (primaryRun && primaryLive) { + onShowOutput(primaryRun) + } else { + onRun(primary) + } + } + + return ( +
+ + + + + + {shortcut ? `${primaryLabel} (${shortcut})` : primaryLabel} + + + + + + + + {actions.map((action, index) => ( + + {index > 0 ? : null} + + + ))} + + + {t('runActions.ports', 'Ports')} + + {ports.length === 0 ? ( +
+ {t('runActions.noPorts', 'No servers listening in this workspace yet.')} +
+ ) : ( + ports.slice(0, 6).map((port) => ( + + + :{port.port} + + {port.processName ?? ''} + + + )) + )} +
+
+
+ ) +}