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

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

+1
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

+10
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

+11-11
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">
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

+7-4
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

+5
Original file line numberDiff line numberDiff line change
@@ -13270,6 +13270,11 @@ [email protected]:
1327013270
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9"
1327113271
integrity sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg==
1327213272

13273+
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)