Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Change workspace version using the UI #5158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 24, 2022
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
13 changes: 13 additions & 0 deletions site/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const WorkspaceBuildPage = lazy(
() => import("./pages/WorkspaceBuildPage/WorkspaceBuildPage"),
)
const WorkspacePage = lazy(() => import("./pages/WorkspacePage/WorkspacePage"))
const WorkspaceChangeVersionPage = lazy(
() => import("./pages/WorkspaceChangeVersionPage/WorkspaceChangeVersionPage"),
)
const WorkspaceSchedulePage = lazy(
() => import("./pages/WorkspaceSchedulePage/WorkspaceSchedulePage"),
)
Expand Down Expand Up @@ -360,6 +363,7 @@ export const AppRouter: FC = () => {
</AuthAndFrame>
}
/>

<Route
path="schedule"
element={
Expand All @@ -386,6 +390,15 @@ export const AppRouter: FC = () => {
</AuthAndFrame>
}
/>

<Route
path="change-version"
element={
<RequireAuth>
<WorkspaceChangeVersionPage />
</RequireAuth>
}
/>
</Route>
</Route>

Expand Down
18 changes: 18 additions & 0 deletions site/src/components/DropdownButton/ActionCtas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Button from "@material-ui/core/Button"
import { makeStyles } from "@material-ui/core/styles"
import BlockIcon from "@material-ui/icons/Block"
import CloudQueueIcon from "@material-ui/icons/CloudQueue"
import UpdateOutlined from "@material-ui/icons/UpdateOutlined"
import CropSquareIcon from "@material-ui/icons/CropSquare"
import DeleteOutlineIcon from "@material-ui/icons/DeleteOutline"
import PlayCircleOutlineIcon from "@material-ui/icons/PlayCircleOutline"
Expand Down Expand Up @@ -33,6 +34,23 @@ export const UpdateButton: FC<React.PropsWithChildren<WorkspaceAction>> = ({
)
}

export const ChangeVersionButton: FC<
React.PropsWithChildren<WorkspaceAction>
> = ({ handleAction }) => {
const styles = useStyles()
const { t } = useTranslation("workspacePage")

return (
<Button
className={styles.actionButton}
startIcon={<UpdateOutlined />}
onClick={handleAction}
>
{t("actionButton.changeVersion")}
</Button>
)
}

export const StartButton: FC<React.PropsWithChildren<WorkspaceAction>> = ({
handleAction,
}) => {
Expand Down
3 changes: 3 additions & 0 deletions site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface WorkspaceProps {
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
handleChangeVersion: () => void
isUpdating: boolean
workspace: TypesGen.Workspace
resources?: TypesGen.WorkspaceResource[]
Expand All @@ -68,6 +69,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
handleDelete,
handleUpdate,
handleCancel,
handleChangeVersion,
workspace,
isUpdating,
resources,
Expand Down Expand Up @@ -143,6 +145,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
handleDelete={handleDelete}
handleUpdate={handleUpdate}
handleCancel={handleCancel}
handleChangeVersion={handleChangeVersion}
isUpdating={isUpdating}
/>
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const renderComponent = async (props: Partial<WorkspaceActionsProps> = {}) => {
handleDelete={jest.fn()}
handleUpdate={jest.fn()}
handleCancel={jest.fn()}
handleChangeVersion={jest.fn()}
isUpdating={false}
/>,
)
Expand All @@ -35,6 +36,7 @@ const renderAndClick = async (props: Partial<WorkspaceActionsProps> = {}) => {
handleDelete={jest.fn()}
handleUpdate={jest.fn()}
handleCancel={jest.fn()}
handleChangeVersion={jest.fn()}
isUpdating={false}
/>,
)
Expand Down
6 changes: 6 additions & 0 deletions site/src/components/WorkspaceActions/WorkspaceActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next"
import { WorkspaceStatus } from "../../api/typesGenerated"
import {
ActionLoadingButton,
ChangeVersionButton,
DeleteButton,
DisabledButton,
StartButton,
Expand All @@ -20,6 +21,7 @@ export interface WorkspaceActionsProps {
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
handleChangeVersion: () => void
isUpdating: boolean
children?: ReactNode
}
Expand All @@ -32,6 +34,7 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
handleDelete,
handleUpdate,
handleCancel,
handleChangeVersion,
isUpdating,
}) => {
const { t } = useTranslation("workspacePage")
Expand All @@ -45,6 +48,9 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
[ButtonTypesEnum.updating]: (
<ActionLoadingButton label={t("actionButton.updating")} />
),
[ButtonTypesEnum.changeVersion]: (
<ChangeVersionButton handleAction={handleChangeVersion} />
),
[ButtonTypesEnum.start]: <StartButton handleAction={handleStart} />,
[ButtonTypesEnum.starting]: (
<ActionLoadingButton label={t("actionButton.starting")} />
Expand Down
20 changes: 17 additions & 3 deletions site/src/components/WorkspaceActions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum ButtonTypesEnum {
deleting = "deleting",
update = "update",
updating = "updating",
changeVersion = "changeVersion",
// disabled buttons
canceling = "canceling",
deleted = "deleted",
Expand All @@ -34,7 +35,11 @@ export const statusToAbilities: Record<WorkspaceStatus, WorkspaceAbilities> = {
canAcceptJobs: false,
},
running: {
actions: [ButtonTypesEnum.stop, ButtonTypesEnum.delete],
actions: [
ButtonTypesEnum.stop,
ButtonTypesEnum.changeVersion,
ButtonTypesEnum.delete,
],
canCancel: false,
canAcceptJobs: true,
},
Expand All @@ -44,22 +49,31 @@ export const statusToAbilities: Record<WorkspaceStatus, WorkspaceAbilities> = {
canAcceptJobs: false,
},
stopped: {
actions: [ButtonTypesEnum.start, ButtonTypesEnum.delete],
actions: [
ButtonTypesEnum.start,
ButtonTypesEnum.changeVersion,
ButtonTypesEnum.delete,
],
canCancel: false,
canAcceptJobs: true,
},
canceled: {
actions: [
ButtonTypesEnum.start,
ButtonTypesEnum.stop,
ButtonTypesEnum.changeVersion,
ButtonTypesEnum.delete,
],
canCancel: false,
canAcceptJobs: true,
},
// in the case of an error
failed: {
actions: [ButtonTypesEnum.start, ButtonTypesEnum.delete],
actions: [
ButtonTypesEnum.start,
ButtonTypesEnum.changeVersion,
ButtonTypesEnum.delete,
],
canCancel: false,
canAcceptJobs: true,
},
Expand Down
2 changes: 2 additions & 0 deletions site/src/i18n/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import workspacesPage from "./workspacesPage.json"
import usersPage from "./usersPage.json"
import templateVersionPage from "./templateVersionPage.json"
import loginPage from "./loginPage.json"
import workspaceChangeVersionPage from "./workspaceChangeVersionPage.json"

export const en = {
common,
Expand All @@ -24,4 +25,5 @@ export const en = {
usersPage,
templateVersionPage,
loginPage,
workspaceChangeVersionPage,
}
9 changes: 9 additions & 0 deletions site/src/i18n/en/workspaceChangeVersionPage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"title": "",
"labels": {
"workspaceVersion": "Workspace version",
"submit": "Update version",
"createdBy": "Created by",
"active": "Active"
}
}
3 changes: 2 additions & 1 deletion site/src/i18n/en/workspacePage.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"updating": "Updating",
"starting": "Starting...",
"stopping": "Stopping...",
"deleting": "Deleting..."
"deleting": "Deleting...",
"changeVersion": "Change version"
},
"disabledButton": {
"canceling": "Canceling",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { makeStyles } from "@material-ui/core/styles"
import TextField from "@material-ui/core/TextField"
import Autocomplete from "@material-ui/lab/Autocomplete"
import { Template, TemplateVersion, Workspace } from "api/typesGenerated"
import { FormFooter } from "components/FormFooter/FormFooter"
import { Pill } from "components/Pill/Pill"
import { Stack } from "components/Stack/Stack"
import { useFormik } from "formik"
import { FC } from "react"
import { useTranslation } from "react-i18next"
import { createDayString } from "util/createDayString"
import * as Yup from "yup"

const validationSchema = Yup.object({
versionId: Yup.string().required(),
})

export const WorkspaceChangeVersionForm: FC<{
isLoading: boolean
workspace: Workspace
template: Template
versions: TemplateVersion[]
onSubmit: (versionId: string) => void
onCancel: () => void
}> = ({ isLoading, workspace, template, versions, onSubmit, onCancel }) => {
const styles = useStyles()
const { t } = useTranslation("workspaceChangeVersionPage")
const formik = useFormik({
initialValues: {
versionId: workspace.latest_build.template_version_id,
},
validationSchema,
onSubmit: ({ versionId }) => onSubmit(versionId),
})
const autocompleteValue = versions.find(
(version) => version.id === formik.values.versionId,
)

return (
<form onSubmit={formik.handleSubmit}>
<Stack direction="column" spacing={3}>
<Stack
direction="row"
spacing={2}
className={styles.workspace}
alignItems="center"
>
<div className={styles.workspaceIcon}>
<img src={workspace.template_icon} alt="" />
</div>
<Stack direction="column" spacing={0.5}>
<span className={styles.workspaceName}>{workspace.name}</span>

<span className={styles.workspaceDescription}>
{workspace.template_display_name.length > 0
? workspace.template_display_name
: workspace.template_name}
</span>
</Stack>
</Stack>

<Autocomplete
id="workspaceVersion"
disableClearable
options={versions.slice().reverse()}
value={autocompleteValue}
onChange={async (_event, value) => {
if (value) {
await formik.setFieldValue("versionId", value.id)
}
}}
renderInput={(params) => (
<TextField
{...params}
label={t("labels.workspaceVersion")}
variant="outlined"
fullWidth
/>
)}
getOptionLabel={(version: TemplateVersion) => version.name}
renderOption={(version: TemplateVersion) => (
<div className={styles.menuItem}>
<div>
<div>{version.name}</div>
<div className={styles.versionDescription}>
{t("labels.createdBy")} {version.created_by.username}{" "}
{createDayString(version.created_at)}
</div>
</div>

{template.active_version_id === version.id && (
<Pill
type="success"
text={t("labels.active")}
className={styles.activePill}
/>
)}
</div>
)}
/>
</Stack>

<FormFooter
onCancel={onCancel}
isLoading={isLoading}
submitLabel={t("labels.submit")}
/>
</form>
)
}

const useStyles = makeStyles((theme) => ({
workspace: {
padding: theme.spacing(2.5, 3),
borderRadius: theme.shape.borderRadius,
backgroundColor: theme.palette.background.paper,
border: `1px solid ${theme.palette.divider}`,
},

workspaceName: {
fontSize: theme.spacing(2),
},

workspaceDescription: {
fontSize: theme.spacing(1.75),
color: theme.palette.text.secondary,
},

workspaceIcon: {
width: theme.spacing(5),
lineHeight: 1,

"& img": {
width: "100%",
},
},

menuItem: {
paddingTop: theme.spacing(1),
paddingBottom: theme.spacing(1),
position: "relative",
width: "100%",
},

versionDescription: {
fontSize: theme.spacing(1.5),
color: theme.palette.text.secondary,
},

activePill: {
position: "absolute",
top: theme.spacing(2),
right: theme.spacing(2),
},
}))
Loading