Skip to content

Commit a1d4ed8

Browse files
committed
refactor: remove enums from workspace actions
1 parent 26740cf commit a1d4ed8

File tree

2 files changed

+59
-66
lines changed

2 files changed

+59
-66
lines changed

site/src/pages/WorkspacePage/WorkspaceActions/WorkspaceActions.tsx

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import {
1111
UpdateButton,
1212
ActivateButton,
1313
} from "./Buttons";
14-
import {
15-
ButtonMapping,
16-
ButtonTypesEnum,
17-
actionsByWorkspaceStatus,
18-
} from "./constants";
14+
import { ButtonMapping, actionsByWorkspaceStatus } from "./constants";
1915

2016
import Divider from "@mui/material/Divider";
2117
import DuplicateIcon from "@mui/icons-material/FileCopyOutlined";
@@ -76,42 +72,32 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
7672
const { duplicateWorkspace, isDuplicationReady } =
7773
useWorkspaceDuplication(workspace);
7874

79-
// A mapping of button type to the corresponding React component
75+
// A mapping of button type to their corresponding React components
8076
const buttonMapping: ButtonMapping = {
81-
[ButtonTypesEnum.update]: <UpdateButton handleAction={handleUpdate} />,
82-
[ButtonTypesEnum.updating]: (
83-
<UpdateButton loading handleAction={handleUpdate} />
84-
),
85-
[ButtonTypesEnum.start]: (
86-
<StartButton workspace={workspace} handleAction={handleStart} />
87-
),
88-
[ButtonTypesEnum.starting]: (
77+
update: <UpdateButton handleAction={handleUpdate} />,
78+
updating: <UpdateButton loading handleAction={handleUpdate} />,
79+
start: <StartButton workspace={workspace} handleAction={handleStart} />,
80+
starting: (
8981
<StartButton loading workspace={workspace} handleAction={handleStart} />
9082
),
91-
[ButtonTypesEnum.stop]: <StopButton handleAction={handleStop} />,
92-
[ButtonTypesEnum.stopping]: (
93-
<StopButton loading handleAction={handleStop} />
94-
),
95-
[ButtonTypesEnum.restart]: (
83+
stop: <StopButton handleAction={handleStop} />,
84+
stopping: <StopButton loading handleAction={handleStop} />,
85+
restart: (
9686
<RestartButton workspace={workspace} handleAction={handleRestart} />
9787
),
98-
[ButtonTypesEnum.restarting]: (
88+
restarting: (
9989
<RestartButton
10090
loading
10191
workspace={workspace}
10292
handleAction={handleRestart}
10393
/>
10494
),
105-
[ButtonTypesEnum.deleting]: <ActionLoadingButton label="Deleting" />,
106-
[ButtonTypesEnum.canceling]: <DisabledButton label="Canceling..." />,
107-
[ButtonTypesEnum.deleted]: <DisabledButton label="Deleted" />,
108-
[ButtonTypesEnum.pending]: <ActionLoadingButton label="Pending..." />,
109-
[ButtonTypesEnum.activate]: (
110-
<ActivateButton handleAction={handleDormantActivate} />
111-
),
112-
[ButtonTypesEnum.activating]: (
113-
<ActivateButton loading handleAction={handleDormantActivate} />
114-
),
95+
deleting: <ActionLoadingButton label="Deleting" />,
96+
canceling: <DisabledButton label="Canceling..." />,
97+
deleted: <DisabledButton label="Deleted" />,
98+
pending: <ActionLoadingButton label="Pending..." />,
99+
activate: <ActivateButton handleAction={handleDormantActivate} />,
100+
activating: <ActivateButton loading handleAction={handleDormantActivate} />,
115101
};
116102

117103
return (
@@ -120,18 +106,17 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
120106
data-testid="workspace-actions"
121107
>
122108
{canBeUpdated &&
123-
(isUpdating
124-
? buttonMapping[ButtonTypesEnum.updating]
125-
: buttonMapping[ButtonTypesEnum.update])}
109+
(isUpdating ? buttonMapping.updating : buttonMapping.update)}
126110

127-
{isRestarting && buttonMapping[ButtonTypesEnum.restarting]}
111+
{isRestarting && buttonMapping.restarting}
128112

129113
{!isRestarting &&
130114
actionsByStatus.map((action) => (
131115
<Fragment key={action}>{buttonMapping[action]}</Fragment>
132116
))}
133117

134118
{canCancel && <CancelButton handleAction={handleCancel} />}
119+
135120
<MoreMenu>
136121
<MoreMenuTrigger>
137122
<ThreeDotsButton

site/src/pages/WorkspacePage/WorkspaceActions/constants.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,39 @@ import { Workspace, WorkspaceStatus } from "api/typesGenerated";
22
import { ReactNode } from "react";
33
import { workspaceUpdatePolicy } from "utils/workspace";
44

5-
// the button types we have
6-
export enum ButtonTypesEnum {
7-
start = "start",
8-
starting = "starting",
9-
stop = "stop",
10-
stopping = "stopping",
11-
restart = "restart",
12-
restarting = "restarting",
13-
deleting = "deleting",
14-
update = "update",
15-
updating = "updating",
16-
activate = "activate",
17-
activating = "activating",
18-
// disabled buttons
19-
canceling = "canceling",
20-
deleted = "deleted",
21-
pending = "pending",
22-
}
5+
/**
6+
* An iterable of all button types supported by the workspace actions UI
7+
*/
8+
const buttonTypes = [
9+
"start",
10+
"starting",
11+
"stop",
12+
"stopping",
13+
"restart",
14+
"restarting",
15+
"deleting",
16+
"update",
17+
"updating",
18+
"activate",
19+
"activating",
20+
21+
// These are buttons that should be with disabled UI elements
22+
"canceling",
23+
"deleted",
24+
"pending",
25+
] as const;
26+
27+
/**
28+
* A button type supported by the workspace actions UI
29+
*/
30+
export type ButtonType = (typeof buttonTypes)[number];
2331

2432
export type ButtonMapping = {
25-
[key in ButtonTypesEnum]: ReactNode;
33+
[key in ButtonType]: ReactNode;
2634
};
2735

2836
interface WorkspaceAbilities {
29-
actions: ButtonTypesEnum[];
37+
actions: ButtonType[];
3038
canCancel: boolean;
3139
canAcceptJobs: boolean;
3240
}
@@ -38,7 +46,7 @@ export const actionsByWorkspaceStatus = (
3846
): WorkspaceAbilities => {
3947
if (workspace.dormant_at) {
4048
return {
41-
actions: [ButtonTypesEnum.activate],
49+
actions: ["activate"],
4250
canCancel: false,
4351
canAcceptJobs: false,
4452
};
@@ -49,7 +57,7 @@ export const actionsByWorkspaceStatus = (
4957
) {
5058
if (status === "running") {
5159
return {
52-
actions: [ButtonTypesEnum.stop],
60+
actions: ["stop"],
5361
canCancel: false,
5462
canAcceptJobs: true,
5563
};
@@ -67,56 +75,56 @@ export const actionsByWorkspaceStatus = (
6775

6876
const statusToActions: Record<WorkspaceStatus, WorkspaceAbilities> = {
6977
starting: {
70-
actions: [ButtonTypesEnum.starting],
78+
actions: ["starting"],
7179
canCancel: true,
7280
canAcceptJobs: false,
7381
},
7482
running: {
75-
actions: [ButtonTypesEnum.stop, ButtonTypesEnum.restart],
83+
actions: ["stop", "restart"],
7684
canCancel: false,
7785
canAcceptJobs: true,
7886
},
7987
stopping: {
80-
actions: [ButtonTypesEnum.stopping],
88+
actions: ["stopping"],
8189
canCancel: true,
8290
canAcceptJobs: false,
8391
},
8492
stopped: {
85-
actions: [ButtonTypesEnum.start],
93+
actions: ["start"],
8694
canCancel: false,
8795
canAcceptJobs: true,
8896
},
8997
canceled: {
90-
actions: [ButtonTypesEnum.start, ButtonTypesEnum.stop],
98+
actions: ["start", "stop"],
9199
canCancel: false,
92100
canAcceptJobs: true,
93101
},
94102
// in the case of an error
95103
failed: {
96-
actions: [ButtonTypesEnum.start, ButtonTypesEnum.stop],
104+
actions: ["start", "stop"],
97105
canCancel: false,
98106
canAcceptJobs: true,
99107
},
100108
/**
101109
* disabled states
102110
*/
103111
canceling: {
104-
actions: [ButtonTypesEnum.canceling],
112+
actions: ["canceling"],
105113
canCancel: false,
106114
canAcceptJobs: false,
107115
},
108116
deleting: {
109-
actions: [ButtonTypesEnum.deleting],
117+
actions: ["deleting"],
110118
canCancel: true,
111119
canAcceptJobs: false,
112120
},
113121
deleted: {
114-
actions: [ButtonTypesEnum.deleted],
122+
actions: ["deleted"],
115123
canCancel: false,
116124
canAcceptJobs: false,
117125
},
118126
pending: {
119-
actions: [ButtonTypesEnum.pending],
127+
actions: ["pending"],
120128
canCancel: false,
121129
canAcceptJobs: false,
122130
},

0 commit comments

Comments
 (0)