Skip to content

Commit f7e9ba9

Browse files
committed
Refactor duplicate and clean up code
1 parent 2f3cce0 commit f7e9ba9

File tree

5 files changed

+84
-158
lines changed

5 files changed

+84
-158
lines changed

site/src/modules/workspaces/WorkspaceMoreActions/WorkspaceMoreActions.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,18 @@ import { Link as RouterLink } from "react-router-dom";
2525
import { ChangeWorkspaceVersionDialog } from "./ChangeWorkspaceVersionDialog";
2626
import { WorkspaceDeleteDialog } from "./WorkspaceDeleteDialog";
2727
import type { WorkspacePermissions } from "../permissions";
28+
import { useWorkspaceDuplication } from "pages/CreateWorkspacePage/useWorkspaceDuplication";
2829

2930
type WorkspaceMoreActionsProps = {
3031
workspace: Workspace;
31-
isDuplicationReady: boolean;
3232
permissions: WorkspacePermissions;
3333
disabled?: boolean;
34-
onDuplicate: () => void;
3534
};
3635

3736
export const WorkspaceMoreActions: FC<WorkspaceMoreActionsProps> = ({
3837
workspace,
3938
disabled,
4039
permissions,
41-
isDuplicationReady,
42-
onDuplicate,
4340
}) => {
4441
const queryClient = useQueryClient();
4542

@@ -58,6 +55,10 @@ export const WorkspaceMoreActions: FC<WorkspaceMoreActionsProps> = ({
5855
deleteWorkspace(workspace, queryClient),
5956
);
6057

58+
// Duplicate
59+
const { duplicateWorkspace, isDuplicationReady } =
60+
useWorkspaceDuplication(workspace);
61+
6162
return (
6263
<>
6364
<DropdownMenu>
@@ -94,7 +95,7 @@ export const WorkspaceMoreActions: FC<WorkspaceMoreActionsProps> = ({
9495
)}
9596

9697
<DropdownMenuItem
97-
onClick={onDuplicate}
98+
onClick={duplicateWorkspace}
9899
disabled={!isDuplicationReady}
99100
>
100101
<CopyIcon />

site/src/pages/WorkspacePage/Workspace.tsx

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,33 @@ import type { WorkspacePermissions } from "../../modules/workspaces/permissions"
2828
import { resourceOptionValue, useResourcesNav } from "./useResourcesNav";
2929

3030
export interface WorkspaceProps {
31-
handleStart: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
32-
handleStop: () => void;
33-
handleRestart: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
34-
handleUpdate: () => void;
35-
handleCancel: () => void;
36-
handleSettings: () => void;
37-
handleDormantActivate: () => void;
38-
handleToggleFavorite: () => void;
31+
workspace: TypesGen.Workspace;
32+
template: TypesGen.Template;
33+
permissions: WorkspacePermissions;
3934
isUpdating: boolean;
4035
isRestarting: boolean;
41-
workspace: TypesGen.Workspace;
4236
hideSSHButton?: boolean;
4337
hideVSCodeDesktopButton?: boolean;
4438
buildInfo?: TypesGen.BuildInfoResponse;
4539
sshPrefix?: string;
46-
template: TypesGen.Template;
47-
canDebugMode: boolean;
48-
handleRetry: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
49-
handleDebug: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
5040
buildLogs?: TypesGen.ProvisionerJobLog[];
5141
latestVersion?: TypesGen.TemplateVersion;
52-
permissions: WorkspacePermissions;
5342
timings?: TypesGen.WorkspaceBuildTimings;
43+
handleStart: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
44+
handleStop: () => void;
45+
handleRestart: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
46+
handleUpdate: () => void;
47+
handleCancel: () => void;
48+
handleDormantActivate: () => void;
49+
handleToggleFavorite: () => void;
50+
handleRetry: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
51+
handleDebug: (buildParameters?: TypesGen.WorkspaceBuildParameter[]) => void;
5452
}
5553

5654
/**
5755
* Workspace is the top-level component for viewing an individual workspace
5856
*/
5957
export const Workspace: FC<WorkspaceProps> = ({
60-
handleStart,
61-
handleStop,
62-
handleRestart,
63-
handleUpdate,
64-
handleCancel,
65-
handleSettings,
66-
handleDormantActivate,
67-
handleToggleFavorite,
6858
workspace,
6959
isUpdating,
7060
isRestarting,
@@ -73,13 +63,19 @@ export const Workspace: FC<WorkspaceProps> = ({
7363
buildInfo,
7464
sshPrefix,
7565
template,
76-
canDebugMode,
77-
handleRetry,
78-
handleDebug,
7966
buildLogs,
8067
latestVersion,
8168
permissions,
8269
timings,
70+
handleStart,
71+
handleStop,
72+
handleRestart,
73+
handleUpdate,
74+
handleCancel,
75+
handleDormantActivate,
76+
handleToggleFavorite,
77+
handleRetry,
78+
handleDebug,
8379
}) => {
8480
const navigate = useNavigate();
8581
const theme = useTheme();
@@ -136,23 +132,20 @@ export const Workspace: FC<WorkspaceProps> = ({
136132
>
137133
<WorkspaceTopbar
138134
workspace={workspace}
135+
template={template}
136+
permissions={permissions}
137+
latestVersion={latestVersion}
138+
isUpdating={isUpdating}
139+
isRestarting={isRestarting}
139140
handleStart={handleStart}
140141
handleStop={handleStop}
141142
handleRestart={handleRestart}
142143
handleUpdate={handleUpdate}
143144
handleCancel={handleCancel}
144-
handleSettings={handleSettings}
145145
handleRetry={handleRetry}
146146
handleDebug={handleDebug}
147147
handleDormantActivate={handleDormantActivate}
148148
handleToggleFavorite={handleToggleFavorite}
149-
canDebugMode={canDebugMode}
150-
isUpdating={isUpdating}
151-
isRestarting={isRestarting}
152-
canUpdateWorkspace={permissions.updateWorkspace}
153-
template={template}
154-
permissions={permissions}
155-
latestVersion={latestVersion}
156149
/>
157150

158151
<div

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

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
import DownloadOutlined from "@mui/icons-material/DownloadOutlined";
2-
import DuplicateIcon from "@mui/icons-material/FileCopyOutlined";
3-
import SettingsIcon from "@mui/icons-material/SettingsOutlined";
41
import type { Workspace, WorkspaceBuildParameter } from "api/typesGenerated";
5-
import { Button } from "components/Button/Button";
6-
import {
7-
DropdownMenu,
8-
DropdownMenuContent,
9-
DropdownMenuItem,
10-
DropdownMenuSeparator,
11-
DropdownMenuTrigger,
12-
} from "components/DropdownMenu/DropdownMenu";
132
import { useAuthenticated } from "hooks/useAuthenticated";
14-
import { EllipsisVertical } from "lucide-react";
153
import {
164
type ActionType,
175
abilitiesByWorkspaceStatus,
186
} from "modules/workspaces/actions";
19-
import { useWorkspaceDuplication } from "pages/CreateWorkspacePage/useWorkspaceDuplication";
20-
import { type FC, Fragment, type ReactNode, useState } from "react";
7+
import { type FC, Fragment, type ReactNode } from "react";
218
import { mustUpdateWorkspace } from "utils/workspace";
229
import {
2310
ActivateButton,
@@ -33,56 +20,57 @@ import {
3320
} from "./Buttons";
3421
import { DebugButton } from "./DebugButton";
3522
import { RetryButton } from "./RetryButton";
23+
import { WorkspaceMoreActions } from "modules/workspaces/WorkspaceMoreActions/WorkspaceMoreActions";
24+
import type { WorkspacePermissions } from "modules/workspaces/permissions";
3625

3726
export interface WorkspaceActionsProps {
3827
workspace: Workspace;
28+
isUpdating: boolean;
29+
isRestarting: boolean;
30+
permissions: WorkspacePermissions;
3931
handleToggleFavorite: () => void;
4032
handleStart: (buildParameters?: WorkspaceBuildParameter[]) => void;
4133
handleStop: () => void;
4234
handleRestart: (buildParameters?: WorkspaceBuildParameter[]) => void;
4335
handleUpdate: () => void;
4436
handleCancel: () => void;
45-
handleSettings: () => void;
4637
handleRetry: (buildParameters?: WorkspaceBuildParameter[]) => void;
4738
handleDebug: (buildParameters?: WorkspaceBuildParameter[]) => void;
4839
handleDormantActivate: () => void;
49-
isUpdating: boolean;
50-
isRestarting: boolean;
51-
children?: ReactNode;
52-
canChangeVersions: boolean;
53-
canDebug: boolean;
5440
}
5541

5642
export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
5743
workspace,
44+
isUpdating,
45+
isRestarting,
46+
permissions,
5847
handleToggleFavorite,
5948
handleStart,
6049
handleStop,
6150
handleRestart,
6251
handleUpdate,
6352
handleCancel,
64-
handleSettings,
6553
handleRetry,
6654
handleDebug,
6755
handleDormantActivate,
68-
isUpdating,
69-
isRestarting,
70-
canChangeVersions,
71-
canDebug,
7256
}) => {
73-
const { duplicateWorkspace, isDuplicationReady } =
74-
useWorkspaceDuplication(workspace);
75-
7657
const { user } = useAuthenticated();
7758
const isOwner =
7859
user.roles.find((role) => role.name === "owner") !== undefined;
7960
const { actions, canCancel, canAcceptJobs } = abilitiesByWorkspaceStatus(
8061
workspace,
81-
{ canDebug, isOwner },
62+
{ canDebug: permissions.deploymentConfig, isOwner },
8263
);
8364

84-
const mustUpdate = mustUpdateWorkspace(workspace, canChangeVersions);
85-
const tooltipText = getTooltipText(workspace, mustUpdate, canChangeVersions);
65+
const mustUpdate = mustUpdateWorkspace(
66+
workspace,
67+
permissions.updateWorkspaceVersion,
68+
);
69+
const tooltipText = getTooltipText(
70+
workspace,
71+
mustUpdate,
72+
permissions.updateWorkspaceVersion,
73+
);
8674

8775
// A mapping of button type to the corresponding React component
8876
const buttonMapping: Record<ActionType, ReactNode> = {
@@ -170,36 +158,11 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
170158
onToggle={handleToggleFavorite}
171159
/>
172160

173-
<DropdownMenu>
174-
<DropdownMenuTrigger asChild>
175-
<Button
176-
size="icon-lg"
177-
variant="subtle"
178-
aria-label="Workspace actions"
179-
data-testid="workspace-options-button"
180-
aria-controls="workspace-options"
181-
disabled={!canAcceptJobs}
182-
>
183-
<EllipsisVertical aria-hidden="true" />
184-
<span className="sr-only">Workspace actions</span>
185-
</Button>
186-
</DropdownMenuTrigger>
187-
188-
<DropdownMenuContent id="workspace-options" align="end">
189-
<DropdownMenuItem onClick={handleSettings}>
190-
<SettingsIcon />
191-
Settings
192-
</DropdownMenuItem>
193-
194-
<DropdownMenuItem
195-
onClick={duplicateWorkspace}
196-
disabled={!isDuplicationReady}
197-
>
198-
<DuplicateIcon />
199-
Duplicate&hellip;
200-
</DropdownMenuItem>
201-
</DropdownMenuContent>
202-
</DropdownMenu>
161+
<WorkspaceMoreActions
162+
workspace={workspace}
163+
permissions={permissions}
164+
disabled={!canAcceptJobs}
165+
/>
203166
</div>
204167
);
205168
};

0 commit comments

Comments
 (0)