Skip to content

fix(UI): workspace restart button stops build before starting a new one #7301

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 7 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add restarting btn
  • Loading branch information
Kira-Pilot committed Apr 26, 2023
commit 2e2b89da760287733648dcd4a10f4c07ff23504c
5 changes: 4 additions & 1 deletion site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ export interface WorkspaceProps {
}
handleStart: () => void
handleStop: () => void
handleRestart: any
handleRestart: () => void
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
handleSettings: () => void
handleChangeVersion: () => void
isUpdating: boolean
isRestarting: boolean
workspace: TypesGen.Workspace
resources?: TypesGen.WorkspaceResource[]
builds?: TypesGen.WorkspaceBuild[]
Expand Down Expand Up @@ -81,6 +82,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
handleChangeVersion,
workspace,
isUpdating,
isRestarting,
resources,
builds,
canUpdateWorkspace,
Expand Down Expand Up @@ -142,6 +144,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
handleChangeVersion={handleChangeVersion}
canChangeVersions={canChangeVersions}
isUpdating={isUpdating}
isRestarting={isRestarting}
/>
</Stack>
}
Expand Down
6 changes: 3 additions & 3 deletions site/src/components/WorkspaceActions/Buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { LoadingButton } from "components/LoadingButton/LoadingButton"
import { FC, PropsWithChildren } from "react"
import { useTranslation } from "react-i18next"
import { makeStyles } from "@material-ui/core/styles"
// import { UseMutateFunction } from "@tanstack/react-query"
// import { WorkspaceBuild } from "api/typesGenerated"

interface WorkspaceAction {
handleAction: () => void
Expand Down Expand Up @@ -69,7 +67,9 @@ export const StopButton: FC<PropsWithChildren<WorkspaceAction>> = ({
)
}

export const RestartButton: FC<PropsWithChildren<any>> = ({ handleAction }) => {
export const RestartButton: FC<PropsWithChildren<WorkspaceAction>> = ({
handleAction,
}) => {
const { t } = useTranslation("workspacePage")
const styles = useStyles()

Expand Down
18 changes: 14 additions & 4 deletions site/src/components/WorkspaceActions/WorkspaceActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ export interface WorkspaceActionsProps {
isOutdated: boolean
handleStart: () => void
handleStop: () => void
handleRestart: any
handleRestart: () => void
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
handleSettings: () => void
handleChangeVersion: () => void
isUpdating: boolean
isRestarting: boolean
children?: ReactNode
canChangeVersions: boolean
}
Expand All @@ -52,6 +53,7 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
handleSettings,
handleChangeVersion,
isUpdating,
isRestarting,
canChangeVersions,
}) => {
const styles = useStyles()
Expand Down Expand Up @@ -95,6 +97,12 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
/>
),
[ButtonTypesEnum.restart]: <RestartButton handleAction={handleRestart} />,
[ButtonTypesEnum.restarting]: (
<ActionLoadingButton
label="Restarting"
key={ButtonTypesEnum.restarting}
/>
),
[ButtonTypesEnum.deleting]: (
<ActionLoadingButton
label={t("actionButton.deleting")}
Expand Down Expand Up @@ -133,9 +141,11 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
(isUpdating
? buttonMapping[ButtonTypesEnum.updating]
: buttonMapping[ButtonTypesEnum.update])}
{actionsByStatus.map((action) => (
<span key={action}>{buttonMapping[action]}</span>
))}
{isRestarting && buttonMapping[ButtonTypesEnum.restarting]}
{!isRestarting &&
actionsByStatus.map((action) => (
<span key={action}>{buttonMapping[action]}</span>
))}
{canCancel && <CancelButton handleAction={handleCancel} />}
<div>
<Button
Expand Down
1 change: 1 addition & 0 deletions site/src/components/WorkspaceActions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum ButtonTypesEnum {
stop = "stop",
stopping = "stopping",
restart = "restart",
restarting = "restarting",
deleting = "deleting",
update = "update",
updating = "updating",
Expand Down
8 changes: 7 additions & 1 deletion site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ export const WorkspaceReadyPage = ({
Error | unknown | undefined
>(undefined)

const { mutate: restartWorkspace } = useRestartWorkspace(setRestartBuildError)
const [isRestarting, setIsRestarting] = useState<boolean>(false)

const { mutate: restartWorkspace } = useRestartWorkspace(
setRestartBuildError,
setIsRestarting,
)

// keep banner machine in sync with workspace
useEffect(() => {
Expand Down Expand Up @@ -127,6 +132,7 @@ export const WorkspaceReadyPage = ({
),
}}
isUpdating={workspaceState.matches("ready.build.requestingUpdate")}
isRestarting={isRestarting}
workspace={workspace}
handleStart={() => workspaceSend({ type: "START" })}
handleStop={() => workspaceSend({ type: "STOP" })}
Expand Down
18 changes: 12 additions & 6 deletions site/src/pages/WorkspacePage/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { delay } from "utils/delay"
import { ProvisionerJob, WorkspaceBuild } from "api/typesGenerated"
import { useMutation } from "@tanstack/react-query"

export function waitForStop(build: WorkspaceBuild) {
export function waitForBuild(build: WorkspaceBuild) {
return new Promise((res, reject) => {
void (async () => {
let latestJobInfo: ProvisionerJob | undefined = undefined
Expand Down Expand Up @@ -40,22 +40,28 @@ export function waitForStop(build: WorkspaceBuild) {
}

export const useRestartWorkspace = (
setRestartBuildError: (arg: Error | unknown | undefined) => void,
setBuildError: (arg: Error | unknown | undefined) => void,
setLoading: (arg: boolean) => void,
) => {
return useMutation({
mutationFn: stopWorkspace,
onSuccess: async (data) => {
onMutate: () => setLoading(true),
onSuccess: async (data: WorkspaceBuild) => {
try {
await waitForStop(data)
await startWorkspace({
await waitForBuild(data)
const startBuild = await startWorkspace({
workspaceId: data.workspace_id,
templateVersionId: data.template_version_id,
})
await waitForBuild(startBuild)

setLoading(false)
} catch (error) {
if ((error as WorkspaceBuild).status === "canceled") {
return
}
setRestartBuildError(error)
setBuildError(error)
setLoading(false)
}
},
})
Expand Down