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
WorkspaceScheduleForm: refactor ttlShutdownAt
  • Loading branch information
johnstcn committed Jun 9, 2022
commit 0278a93acb9c6d0816c813be284ebdd4d1c0a09e
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,7 @@ describe("ttlShutdownAt", () => {
it.each<[dayjs.Dayjs, Workspace, string, number, string]>([
[
dayjs("2022-05-17T18:09:00Z"),
{
...Mocks.MockWorkspace,
latest_build: {
...Mocks.MockWorkspaceBuild,
transition: "stop",
},
},
Mocks.MockStoppedWorkspace,
"America/Chicago",
1,
"Your workspace will automatically shut down after this amount of time has elapsed.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as Yup from "yup"
import { FieldErrors } from "../../api/errors"
import { Workspace } from "../../api/typesGenerated"
import { getFormHelpers } from "../../util/formUtils"
import { isWorkspaceOn } from "../../util/workspace"
import { FormFooter } from "../FormFooter/FormFooter"
import { FullPageForm } from "../FullPageForm/FullPageForm"
import { Stack } from "../Stack/Stack"
Expand Down Expand Up @@ -278,22 +279,20 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
)
}

const ttlShutdownAt = (now: dayjs.Dayjs, workspace: Workspace, tz: string, newTTL: number): string => {
if (workspace.latest_build.transition !== "start") {
export const ttlShutdownAt = (now: dayjs.Dayjs, workspace: Workspace, tz: string, newTTL: number): string => {
const newDeadline = dayjs(workspace.latest_build.updated_at).add(newTTL, "hour")
if (!isWorkspaceOn(workspace)) {
return Language.ttlHelperText
}
if (newTTL === 0) {
} else if (newTTL === 0) {
return Language.ttlCausesNoShutdownHelperText
}
const newDeadline = dayjs(workspace.latest_build.updated_at).add(newTTL, "hour")
if (newDeadline.isBefore(now)) {
} else if (newDeadline.isBefore(now)) {
return `⚠️ ${Language.ttlCausesShutdownHelperText} ${Language.ttlCausesShutdownImmediately} ⚠️`
}
if (newDeadline.isBefore(now.add(30, "minute"))) {
} else if (newDeadline.isBefore(now.add(30, "minute"))) {
return `⚠️ ${Language.ttlCausesShutdownHelperText} ${Language.ttlCausesShutdownSoon} ⚠️`
} else {
const newDeadlineString = newDeadline.tz(tz).format("hh:mm A z")
return `${Language.ttlCausesShutdownHelperText} ${Language.ttlCausesShutdownAt} ${newDeadlineString}.`
}
const newDeadlineString = newDeadline.tz(tz).format("hh:mm A z")
return `${Language.ttlCausesShutdownHelperText} ${Language.ttlCausesShutdownAt} ${newDeadlineString}.`
}

const useStyles = makeStyles({
Expand Down