Skip to content
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
Render cancel
Changes behavior of other buttons too
  • Loading branch information
presleyp committed May 25, 2022
commit 5b5219e131e467900fa9c34ba1cf6bd8c3daabbd
3 changes: 3 additions & 0 deletions site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface WorkspaceProps {
handleStop: () => void
handleRetry: () => void
handleUpdate: () => void
handleCancel: () => void
workspace: TypesGen.Workspace
resources?: TypesGen.WorkspaceResource[]
getResourcesError?: Error
Expand All @@ -30,6 +31,7 @@ export const Workspace: React.FC<WorkspaceProps> = ({
handleStop,
handleRetry,
handleUpdate,
handleCancel,
workspace,
resources,
getResourcesError,
Expand Down Expand Up @@ -57,6 +59,7 @@ export const Workspace: React.FC<WorkspaceProps> = ({
handleStop={handleStop}
handleRetry={handleRetry}
handleUpdate={handleUpdate}
handleCancel={handleCancel}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import React from "react"

export interface WorkspaceActionButtonProps {
label: string
loadingLabel: string
isLoading: boolean
loadingLabel?: string
isLoading?: boolean
icon: JSX.Element
onClick: () => void
className?: string
Expand Down
47 changes: 34 additions & 13 deletions site/src/components/WorkspaceActions/WorkspaceActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CloudDownloadIcon from "@material-ui/icons/CloudDownload"
import PlayArrowRoundedIcon from "@material-ui/icons/PlayArrowRounded"
import ReplayIcon from "@material-ui/icons/Replay"
import StopIcon from "@material-ui/icons/Stop"
import CancelIcon from "@material-ui/icons/Cancel"
import React from "react"
import { Link as RouterLink } from "react-router-dom"
import { Workspace } from "../../api/typesGenerated"
Expand All @@ -18,6 +19,7 @@ export const Language = {
start: "Start workspace",
starting: "Starting workspace",
retry: "Retry",
cancel: "Cancel action",
update: "Update workspace",
}

Expand All @@ -28,12 +30,27 @@ export const Language = {
const canAcceptJobs = (workspaceStatus: WorkspaceStatus) =>
["started", "stopped", "deleted", "error", "canceled"].includes(workspaceStatus)

/**
* Jobs that are in progress (queued or pending) can be canceled.
* @param workspaceStatus WorkspaceStatus
* @returns boolean
*/
const canCancelJobs = (workspaceStatus: WorkspaceStatus) =>
["starting", "stopping", "deleting"].includes(workspaceStatus)

const canStart = (workspaceStatus: WorkspaceStatus) =>
["stopped", "canceled", "error"].includes(workspaceStatus)

const canStop = (workspaceStatus: WorkspaceStatus) =>
["started", "canceled", "error"].includes(workspaceStatus)

export interface WorkspaceActionsProps {
workspace: Workspace
handleStart: () => void
handleStop: () => void
handleRetry: () => void
handleUpdate: () => void
handleCancel: () => void
}

export const WorkspaceActions: React.FC<WorkspaceActionsProps> = ({
Expand All @@ -42,6 +59,7 @@ export const WorkspaceActions: React.FC<WorkspaceActionsProps> = ({
handleStop,
handleRetry,
handleUpdate,
handleCancel
}) => {
const styles = useStyles()
const workspaceStatus = getWorkspaceStatus(workspace.latest_build)
Expand All @@ -51,7 +69,17 @@ export const WorkspaceActions: React.FC<WorkspaceActionsProps> = ({
<Link underline="none" component={RouterLink} to="edit">
<Button variant="outlined">Settings</Button>
</Link>
{(workspaceStatus === "started" || workspaceStatus === "stopping") && (
{canStart(workspaceStatus) && (
<WorkspaceActionButton
className={styles.actionButton}
icon={<PlayArrowRoundedIcon />}
onClick={handleStart}
label={Language.start}
loadingLabel={Language.starting}
isLoading={workspaceStatus === "starting"}
/>
)}
{canStop(workspaceStatus) && (
<WorkspaceActionButton
className={styles.actionButton}
icon={<StopIcon />}
Expand All @@ -61,21 +89,14 @@ export const WorkspaceActions: React.FC<WorkspaceActionsProps> = ({
isLoading={workspaceStatus === "stopping"}
/>
)}
{(workspaceStatus === "stopped" || workspaceStatus === "starting") && (
{canCancelJobs(workspaceStatus) &&
<WorkspaceActionButton
className={styles.actionButton}
icon={<PlayArrowRoundedIcon />}
onClick={handleStart}
label={Language.start}
loadingLabel={Language.starting}
isLoading={workspaceStatus === "starting"}
icon={<CancelIcon />}
onClick={handleCancel}
label={Language.cancel}
/>
)}
{workspaceStatus === "error" && (
<Button className={styles.actionButton} startIcon={<ReplayIcon />} onClick={handleRetry}>
{Language.retry}
</Button>
)}
}
{workspace.outdated && canAcceptJobs(workspaceStatus) && (
<Button className={styles.actionButton} startIcon={<CloudDownloadIcon />} onClick={handleUpdate}>
{Language.update}
Expand Down
1 change: 1 addition & 0 deletions site/src/pages/WorkspacePage/WorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const WorkspacePage: React.FC = () => {
handleStop={() => workspaceSend("STOP")}
handleRetry={() => workspaceSend("RETRY")}
handleUpdate={() => workspaceSend("UPDATE")}
handleCancel={() => workspaceSend("CANCEL")}
resources={resources}
getResourcesError={getResourcesError instanceof Error ? getResourcesError : undefined}
builds={builds}
Expand Down
4 changes: 2 additions & 2 deletions site/src/util/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export const DisplayStatusLanguage = {
stopped: "Stopped",
deleting: "Deleting",
deleted: "Deleted",
canceling: "Canceling",
canceled: "Canceled",
canceling: "Canceling action",
canceled: "Canceled action",
failed: "Failed",
queued: "Queued",
}
Expand Down
3 changes: 2 additions & 1 deletion site/src/xServices/workspace/workspaceXService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ContextExclusionPlugin } from "webpack"
import { assign, createMachine, send } from "xstate"
import { pure } from "xstate/lib/actions"
import * as API from "../../api/api"
Expand Down Expand Up @@ -42,6 +41,7 @@ export type WorkspaceEvent =
| { type: "STOP" }
| { type: "RETRY" }
| { type: "UPDATE" }
| { type: "CANCEL" }
| { type: "LOAD_MORE_BUILDS" }
| { type: "REFRESH_TIMELINE" }

Expand Down Expand Up @@ -138,6 +138,7 @@ export const workspaceMachine = createMachine(
STOP: "requestingStop",
RETRY: [{ cond: "triedToStart", target: "requestingStart" }, { target: "requestingStop" }],
UPDATE: "refreshingTemplate",
CANCEL: "requestingCancel"
},
},
requestingStart: {
Expand Down