|
1 | | -import { |
2 | | - DropdownMenu, |
3 | | - DropdownMenuContent, |
4 | | - DropdownMenuTrigger, |
5 | | - Icon, |
6 | | - IconButton, |
7 | | - MenuItem, |
8 | | - MenuSeparator, |
9 | | -} from "@repo/ui"; |
| 1 | +import { Icon, MenuItem, MenuSeparator, type CodiconName } from "@repo/ui"; |
| 2 | + |
| 3 | +import { RowActions } from "./RowActions"; |
10 | 4 |
|
11 | 5 | import type { Workspace } from "@repo/shared"; |
| 6 | +import type { WorkspaceStatus } from "coder/site/src/api/typesGenerated"; |
12 | 7 |
|
13 | 8 | import type { WorkspaceCommands } from "./WorkspacesPanel"; |
14 | 9 |
|
| 10 | +type LifecycleAction = "Start" | "Stop" | "Restart" | "Retry"; |
| 11 | + |
| 12 | +const LIFECYCLE_ICONS: Readonly<Record<LifecycleAction, CodiconName>> = { |
| 13 | + Start: "play", |
| 14 | + Stop: "primitive-square", |
| 15 | + Restart: "debug-restart", |
| 16 | + Retry: "debug-restart", |
| 17 | +}; |
| 18 | + |
| 19 | +function lifecycleActions(status: WorkspaceStatus): readonly LifecycleAction[] { |
| 20 | + switch (status) { |
| 21 | + case "running": |
| 22 | + return ["Stop", "Restart"]; |
| 23 | + case "stopped": |
| 24 | + return ["Start"]; |
| 25 | + case "failed": |
| 26 | + case "canceled": |
| 27 | + return ["Retry"]; |
| 28 | + default: |
| 29 | + return []; |
| 30 | + } |
| 31 | +} |
| 32 | + |
15 | 33 | export function WorkspaceActions({ |
16 | | - workspaceId, |
17 | | - name, |
18 | | - agentId, |
19 | | - status, |
| 34 | + workspace, |
20 | 35 | api, |
21 | 36 | }: { |
22 | | - workspaceId: string; |
23 | | - name: string; |
24 | | - agentId?: string; |
25 | | - status?: Workspace["latest_build"]["status"]; |
26 | | - api: Pick<WorkspaceCommands, "openWorkspace" | "viewInDashboard">; |
| 37 | + workspace: Workspace; |
| 38 | + api: WorkspaceCommands; |
27 | 39 | }) { |
28 | | - const lifecycleActions = |
29 | | - status === "running" |
30 | | - ? ["Stop", "Restart"] |
31 | | - : status === "stopped" |
32 | | - ? ["Start"] |
33 | | - : status === "failed" || status === "canceled" |
34 | | - ? ["Retry"] |
35 | | - : []; |
| 40 | + const workspaceId = workspace.id; |
| 41 | + const actions = lifecycleActions(workspace.latest_build.status); |
36 | 42 | return ( |
37 | | - <> |
38 | | - <IconButton |
39 | | - icon="play" |
40 | | - label={`Open ${name} in VS Code`} |
41 | | - tooltip="Open in VS Code" |
42 | | - onClick={() => |
43 | | - api.openWorkspace({ workspaceId, ...(agentId ? { agentId } : {}) }) |
44 | | - } |
45 | | - /> |
46 | | - <IconButton |
47 | | - icon="link-external" |
48 | | - label={`View ${name} in dashboard`} |
49 | | - tooltip="View in Dashboard" |
50 | | - onClick={() => api.viewInDashboard({ workspaceId, page: "workspace" })} |
51 | | - /> |
52 | | - {!agentId && ( |
53 | | - <DropdownMenu> |
54 | | - <DropdownMenuTrigger |
55 | | - render={ |
56 | | - <IconButton |
57 | | - icon="more" |
58 | | - label={`More actions for ${name}`} |
59 | | - tooltip="More Actions" |
60 | | - /> |
61 | | - } |
62 | | - /> |
63 | | - <DropdownMenuContent |
64 | | - align="end" |
65 | | - aria-description="Disabled actions are not supported by this panel." |
66 | | - > |
67 | | - {lifecycleActions.map((label) => ( |
68 | | - <MenuItem key={label} disabled> |
69 | | - <Icon |
70 | | - name={ |
71 | | - label === "Start" |
72 | | - ? "play" |
73 | | - : label === "Stop" |
74 | | - ? "primitive-square" |
75 | | - : "debug-restart" |
76 | | - } |
77 | | - /> |
78 | | - {label} |
79 | | - </MenuItem> |
80 | | - ))} |
81 | | - {lifecycleActions.length > 0 && <MenuSeparator />} |
82 | | - <MenuItem disabled> |
83 | | - <Icon name="share" /> |
84 | | - Share |
85 | | - </MenuItem> |
86 | | - <MenuItem |
87 | | - onClick={() => |
88 | | - api.viewInDashboard({ workspaceId, page: "settings" }) |
89 | | - } |
90 | | - > |
91 | | - <Icon name="gear" /> |
92 | | - Settings |
93 | | - </MenuItem> |
94 | | - <MenuSeparator /> |
95 | | - <MenuItem disabled> |
96 | | - <Icon name="pulse" /> |
97 | | - Ping |
98 | | - </MenuItem> |
99 | | - <MenuItem disabled> |
100 | | - <Icon name="dashboard" /> |
101 | | - Speed Test |
102 | | - </MenuItem> |
103 | | - <MenuItem disabled> |
104 | | - <Icon name="archive" /> |
105 | | - Support Bundle |
106 | | - </MenuItem> |
107 | | - <MenuSeparator /> |
108 | | - <MenuItem disabled className="workspace-delete"> |
109 | | - <Icon name="trash" /> |
110 | | - Delete... |
111 | | - </MenuItem> |
112 | | - </DropdownMenuContent> |
113 | | - </DropdownMenu> |
114 | | - )} |
115 | | - </> |
| 43 | + <RowActions |
| 44 | + name={workspace.name || "Unknown"} |
| 45 | + target={{ workspaceId }} |
| 46 | + api={api} |
| 47 | + menuDescription="Disabled actions are not supported by this panel." |
| 48 | + > |
| 49 | + {actions.map((action) => ( |
| 50 | + <MenuItem key={action} disabled> |
| 51 | + <Icon name={LIFECYCLE_ICONS[action]} /> |
| 52 | + {action} |
| 53 | + </MenuItem> |
| 54 | + ))} |
| 55 | + {actions.length > 0 && <MenuSeparator />} |
| 56 | + <MenuItem disabled> |
| 57 | + <Icon name="share" /> |
| 58 | + Share |
| 59 | + </MenuItem> |
| 60 | + <MenuItem |
| 61 | + onClick={() => api.viewInDashboard({ workspaceId, page: "settings" })} |
| 62 | + > |
| 63 | + <Icon name="gear" /> |
| 64 | + Settings |
| 65 | + </MenuItem> |
| 66 | + <MenuSeparator /> |
| 67 | + <MenuItem disabled className="workspace-delete"> |
| 68 | + <Icon name="trash" /> |
| 69 | + Delete... |
| 70 | + </MenuItem> |
| 71 | + </RowActions> |
116 | 72 | ); |
117 | 73 | } |
0 commit comments