Skip to content

Commit ac6cb26

Browse files
authored
feat: ws schedule timezone select (#2032)
Resolves: #1959 Summary: The package tzdata is used to create a meaningful select-list for timezone in the workspace schedule form. Impact: Improved UX. Furthermore, we guess your timezone if the form is being initialized from scratch.
1 parent 2b12bee commit ac6cb26

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

site/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"react-router-dom": "6.3.0",
4949
"sourcemapped-stacktrace": "1.1.11",
5050
"swr": "1.2.2",
51+
"tzdata": "1.0.30",
5152
"uuid": "8.3.2",
5253
"xstate": "4.32.1",
5354
"xterm": "4.18.0",

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Language, validationSchema, WorkspaceScheduleFormValues } from "./WorkspaceScheduleForm"
2+
import { zones } from "./zones"
23

34
const valid: WorkspaceScheduleFormValues = {
45
sunday: false,
@@ -127,6 +128,15 @@ describe("validationSchema", () => {
127128
expect(validate).toThrowError(Language.errorTimezone)
128129
})
129130

131+
it.each<[string]>(zones.map((zone) => [zone]))(`validation passes for tz=%p`, (zone) => {
132+
const values: WorkspaceScheduleFormValues = {
133+
...valid,
134+
timezone: zone,
135+
}
136+
const validate = () => validationSchema.validateSync(values)
137+
expect(validate).not.toThrow()
138+
})
139+
130140
it("allows a ttl of 7 days", () => {
131141
const values: WorkspaceScheduleFormValues = {
132142
...valid,

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import FormControlLabel from "@material-ui/core/FormControlLabel"
44
import FormGroup from "@material-ui/core/FormGroup"
55
import FormHelperText from "@material-ui/core/FormHelperText"
66
import FormLabel from "@material-ui/core/FormLabel"
7-
import Link from "@material-ui/core/Link"
7+
import MenuItem from "@material-ui/core/MenuItem"
88
import makeStyles from "@material-ui/core/styles/makeStyles"
99
import TextField from "@material-ui/core/TextField"
1010
import dayjs from "dayjs"
@@ -18,6 +18,7 @@ import { getFormHelpers } from "../../util/formUtils"
1818
import { FormFooter } from "../FormFooter/FormFooter"
1919
import { FullPageForm } from "../FullPageForm/FullPageForm"
2020
import { Stack } from "../Stack/Stack"
21+
import { zones } from "./zones"
2122

2223
// REMARK: timezone plugin depends on UTC
2324
//
@@ -203,21 +204,20 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
203204
/>
204205

205206
<TextField
206-
{...formHelpers(
207-
"timezone",
208-
<>
209-
Timezone must be a valid name from the{" "}
210-
<Link href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List" target="_blank">
211-
timezone database
212-
</Link>
213-
</>,
214-
)}
207+
{...formHelpers("timezone")}
215208
disabled={isLoading}
216209
InputLabelProps={{
217210
shrink: true,
218211
}}
219212
label={Language.timezoneLabel}
220-
/>
213+
select
214+
>
215+
{zones.map((zone) => (
216+
<MenuItem key={zone} value={zone}>
217+
{zone}
218+
</MenuItem>
219+
))}
220+
</TextField>
221221

222222
<FormControl component="fieldset" error={Boolean(form.errors.monday)}>
223223
<FormLabel className={styles.daysOfWeekLabel} component="legend">
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import tzData from "tzdata"
2+
3+
export const zones: string[] = Object.keys(tzData.zones)

site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export const formValuesToTTLRequest = (values: WorkspaceScheduleFormValues): Typ
9292
}
9393
}
9494

95-
export const workspaceToInitialValues = (workspace: TypesGen.Workspace): WorkspaceScheduleFormValues => {
95+
export const workspaceToInitialValues = (
96+
workspace: TypesGen.Workspace,
97+
defaultTimeZone = "",
98+
): WorkspaceScheduleFormValues => {
9699
const schedule = workspace.autostart_schedule
97100
const ttlHours = workspace.ttl_ms ? Math.round(workspace.ttl_ms / (1000 * 60 * 60)) : 0
98101

@@ -106,12 +109,12 @@ export const workspaceToInitialValues = (workspace: TypesGen.Workspace): Workspa
106109
friday: false,
107110
saturday: false,
108111
startTime: "",
109-
timezone: "",
112+
timezone: defaultTimeZone,
110113
ttl: ttlHours,
111114
}
112115
}
113116

114-
const timezone = extractTimezone(schedule, dayjs.tz.guess())
117+
const timezone = extractTimezone(schedule, defaultTimeZone)
115118

116119
const expression = cronParser.parseExpression(stripTimezone(schedule))
117120

@@ -162,7 +165,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
162165
return (
163166
<WorkspaceScheduleForm
164167
fieldErrors={formErrors}
165-
initialValues={workspaceToInitialValues(workspace)}
168+
initialValues={workspaceToInitialValues(workspace, dayjs.tz.guess())}
166169
isLoading={scheduleState.tags.has("loading")}
167170
onCancel={() => {
168171
navigate(`/workspaces/${workspaceId}`)

site/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13270,6 +13270,11 @@ typescript@4.6.4:
1327013270
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9"
1327113271
integrity sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg==
1327213272

13273+
tzdata@1.0.30:
13274+
version "1.0.30"
13275+
resolved "https://registry.yarnpkg.com/tzdata/-/tzdata-1.0.30.tgz#d9d5a4b4b5e1ed95f6255f98c0564c4256316f52"
13276+
integrity sha512-/0yogZsIRUVhGIEGZahL+Nnl9gpMD6jtQ9MlVtPVofFwhaqa+cFTgRy1desTAKqdmIJjS6CL+i6F/mnetrLaxw==
13277+
1327313278
uglify-js@^3.1.4:
1327413279
version "3.15.1"
1327513280
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.15.1.tgz#9403dc6fa5695a6172a91bc983ea39f0f7c9086d"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy