Skip to content

Commit a47d81e

Browse files
authored
fix(ui): derive cron timezone suggestions (#115973)
1 parent 34b72d1 commit a47d81e

6 files changed

Lines changed: 94 additions & 24 deletions

File tree

‎ui/src/e2e/cron-filters.e2e.test.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describeControlUiE2e("Control UI cron mocked Gateway E2E", () => {
139139
await server?.close();
140140
});
141141

142-
it("suggests Asia/Shanghai in the rendered cron timezone selector", async () => {
142+
it("suggests browser-supported timezones without restricting free-form input", async () => {
143143
const context = await browser.newContext({
144144
locale: "en-US",
145145
serviceWorkers: "block",
@@ -163,11 +163,18 @@ describeControlUiE2e("Control UI cron mocked Gateway E2E", () => {
163163
const timezone = page.locator("#cron-cron-tz");
164164
await timezone.waitFor({ state: "visible" });
165165
expect(await timezone.getAttribute("list")).toBe("cron-tz-suggestions");
166-
expect(
167-
await page
168-
.locator("#cron-tz-suggestions option")
169-
.evaluateAll((options) => options.map((option) => option.getAttribute("value"))),
170-
).toContain("Asia/Shanghai");
166+
const browserTimezone = await page.evaluate(
167+
() => Intl.DateTimeFormat().resolvedOptions().timeZone,
168+
);
169+
const timezoneOptions = await page
170+
.locator("#cron-tz-suggestions option")
171+
.evaluateAll((options) => options.map((option) => option.getAttribute("value")));
172+
expect(timezoneOptions).toContain(browserTimezone);
173+
expect(timezoneOptions).toContain("UTC");
174+
expect(timezoneOptions.length).toBeGreaterThan(100);
175+
176+
await timezone.fill("Etc/GMT+3");
177+
expect(await timezone.inputValue()).toBe("Etc/GMT+3");
171178
} finally {
172179
await context.close();
173180
}

‎ui/src/i18n/locales/en.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5507,7 +5507,8 @@ export const en: TranslationMap = {
55075507
summaryCronTz: "Cron schedule {expr} ({tz})",
55085508
timezoneOptional: "Timezone",
55095509
timezonePlaceholder: "America/Los_Angeles",
5510-
timezoneHelp: "Optional. Any valid IANA timezone.",
5510+
timezoneHelp:
5511+
"Optional. Leave blank to use the Gateway host timezone, or enter any valid IANA timezone.",
55115512
runsIn: "Runs in",
55125513
mainSession: "Main session",
55135514
isolatedSession: "Isolated session",

‎ui/src/pages/cron/cron-page.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ import {
3939
} from "../../lib/sessions/route-navigation.ts";
4040
import { OpenClawLightDomElement } from "../../lit/openclaw-element.ts";
4141
import { SubscriptionsController } from "../../lit/subscriptions-controller.ts";
42-
import {
43-
buildCronSuggestions,
44-
THINKING_SUGGESTIONS,
45-
TIMEZONE_SUGGESTIONS,
46-
} from "./form-suggestions.ts";
42+
import { buildCronSuggestions, THINKING_SUGGESTIONS } from "./form-suggestions.ts";
4743
import { renderCron, type CronDetailTab, type CronListTab } from "./view.ts";
4844

4945
class CronPage extends OpenClawLightDomElement {
@@ -423,7 +419,7 @@ class CronPage extends OpenClawLightDomElement {
423419
agentSuggestions: suggestions.agentSuggestions,
424420
modelSuggestions: suggestions.modelSuggestions,
425421
thinkingSuggestions: THINKING_SUGGESTIONS,
426-
timezoneSuggestions: TIMEZONE_SUGGESTIONS,
422+
timezoneSuggestions: suggestions.timezoneSuggestions,
427423
deliveryToSuggestions: suggestions.deliveryToSuggestions,
428424
accountSuggestions: suggestions.accountTargets,
429425
onListTabChange: (tab) => {

‎ui/src/pages/cron/form-suggestions.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,9 @@ import {
88
type CronState,
99
} from "../../lib/cron/index.ts";
1010
import { sortUniqueStrings } from "../../lib/string-coerce.ts";
11+
import { resolveCronTimezoneSuggestions } from "./timezone-suggestions.ts";
1112

1213
export const THINKING_SUGGESTIONS = ["off", "minimal", "low", "medium", "high"];
13-
export const TIMEZONE_SUGGESTIONS = [
14-
"UTC",
15-
"America/Los_Angeles",
16-
"America/Denver",
17-
"America/Chicago",
18-
"America/New_York",
19-
"Europe/London",
20-
"Europe/Berlin",
21-
"Asia/Shanghai",
22-
"Asia/Tokyo",
23-
];
2414

2515
function unique(values: string[]): string[] {
2616
return sortUniqueStrings(values.map((value) => value.trim()).filter(Boolean));
@@ -74,6 +64,7 @@ export function buildCronSuggestions(params: {
7464
return {
7565
agentSuggestions,
7666
modelSuggestions,
67+
timezoneSuggestions: resolveCronTimezoneSuggestions(params.cron.cronJobs),
7768
accountTargets,
7869
deliveryToSuggestions:
7970
params.cron.cronForm.deliveryMode === "webhook"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { CronJob } from "../../api/types.ts";
3+
import { resolveCronTimezoneSuggestions } from "./timezone-suggestions.ts";
4+
5+
function cronJob(timezone: string): CronJob {
6+
return {
7+
schedule: { kind: "cron", expr: "0 9 * * *", tz: timezone },
8+
} as CronJob;
9+
}
10+
11+
describe("resolveCronTimezoneSuggestions", () => {
12+
it("prioritizes relevant zones before the browser catalog", () => {
13+
expect(
14+
resolveCronTimezoneSuggestions(
15+
[cronJob("Pacific/Chatham"), cronJob("UTC"), cronJob(" Pacific/Chatham ")],
16+
"Asia/Singapore",
17+
["Europe/London", "America/New_York", "Asia/Singapore"],
18+
),
19+
).toEqual(["Asia/Singapore", "UTC", "Pacific/Chatham", "America/New_York", "Europe/London"]);
20+
});
21+
22+
it("keeps useful suggestions when the browser catalog is unavailable", () => {
23+
expect(resolveCronTimezoneSuggestions([cronJob("America/St_Johns")], "", [])).toEqual([
24+
"UTC",
25+
"America/St_Johns",
26+
]);
27+
});
28+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { CronJob } from "../../api/types.ts";
2+
import { sortUniqueStrings } from "../../lib/string-coerce.ts";
3+
4+
function resolveBrowserTimezone(): string {
5+
try {
6+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
7+
} catch {
8+
return "";
9+
}
10+
}
11+
12+
function resolveSupportedTimezones(): string[] {
13+
try {
14+
return Intl.supportedValuesOf?.("timeZone") ?? [];
15+
} catch {
16+
return [];
17+
}
18+
}
19+
20+
function uniqueInOrder(values: string[]): string[] {
21+
const seen = new Set<string>();
22+
return values
23+
.map((value) => value.trim())
24+
.filter((value) => {
25+
if (!value || seen.has(value)) {
26+
return false;
27+
}
28+
seen.add(value);
29+
return true;
30+
});
31+
}
32+
33+
export function resolveCronTimezoneSuggestions(
34+
cronJobs: CronJob[],
35+
browserTimezone = resolveBrowserTimezone(),
36+
supportedTimezones = resolveSupportedTimezones(),
37+
): string[] {
38+
const configuredTimezones = cronJobs.map((job) =>
39+
job.schedule.kind === "cron" && typeof job.schedule.tz === "string" ? job.schedule.tz : "",
40+
);
41+
return uniqueInOrder([
42+
browserTimezone,
43+
"UTC",
44+
...configuredTimezones,
45+
...sortUniqueStrings(supportedTimezones.map((value) => value.trim()).filter(Boolean)),
46+
]);
47+
}

0 commit comments

Comments
 (0)