Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions apps/web/src/components/chat/ThreadAutomationsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import {
type ScheduledTask,
type ThreadId,
} from "@t3tools/contracts";
import type { TimestampFormat } from "@t3tools/contracts/settings";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";

import { ThreadDetailsSection } from "./ThreadDetailsSection";
import { useClientSettings } from "../../hooks/useSettings";
import { cn } from "../../lib/utils";
import { ScheduledRelativeLabel } from "../settings/ScheduledTasksSettings";
import { scheduleLabel } from "../settings/scheduledTasksSettings.logic";
Expand All @@ -43,12 +45,13 @@ const STATUS_DOT_CLASS: Record<ScheduledTask["lastRunStatus"], string> = {

function taskScheduleLine(
task: ScheduledTask,
timestampFormat: TimestampFormat,
timeZone: string | undefined,
resolve: MessageResolver,
) {
return (
<>
{scheduleLabel(task.schedule, timeZone, task.nextRunAt, resolve)}
{scheduleLabel(task.schedule, timestampFormat, timeZone, task.nextRunAt, resolve)}
{task.blockedByPolicy !== undefined ? (
` · ${task.blockedByPolicy}`
) : task.enabled && task.nextRunAt !== null ? (
Expand Down Expand Up @@ -87,6 +90,7 @@ export function ThreadAutomationsPanel(props: {
const runTaskNow = useAtomCommand(serverEnvironment.runScheduledTaskNow, {
label: "thread automation run now",
});
const timestampFormat = useClientSettings((settings) => settings.timestampFormat);
const navigate = useNavigate();
const gt = useGT();
const m = useMessages();
Expand Down Expand Up @@ -198,13 +202,13 @@ export function ThreadAutomationsPanel(props: {
{task.title}
</span>
<p className="truncate text-2xs text-muted-foreground">
{taskScheduleLine(task, tasksQuery.data?.timeZone, m)}
{taskScheduleLine(task, timestampFormat, tasksQuery.data?.timeZone, m)}
</p>
</TooltipTrigger>
<TooltipPopup side="left">
<p className="font-medium">{task.title}</p>
<p className="text-muted-foreground">
{taskScheduleLine(task, tasksQuery.data?.timeZone, m)}
{taskScheduleLine(task, timestampFormat, tasksQuery.data?.timeZone, m)}
</p>
</TooltipPopup>
</Tooltip>
Expand Down
13 changes: 11 additions & 2 deletions apps/web/src/components/settings/ScheduledTasksSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { msg, T, useGT, useLocale, useMessages, Var } from "gt-react";
import { m, type MessageResolver } from "../../i18n/messages";

import { formatRelativeTime } from "../../timestampFormat";
import { useEnvironmentSettings } from "../../hooks/useSettings";
import { useClientSettings, useEnvironmentSettings } from "../../hooks/useSettings";
import { getCustomModelOptionsByInstance } from "../../modelSelection";
import { providerInstanceEntriesFor } from "../../providerInstances";
import { usePrimaryCloudLinkState } from "../../cloud/cloudLinkState";
Expand Down Expand Up @@ -795,6 +795,7 @@ function ScheduledTaskRow({
const canOperate = useAtomValue(
serverEnvironment.upsertScheduledTask.permissionAtom(environmentId),
);
const timestampFormat = useClientSettings((settings) => settings.timestampFormat);
const [busy, setBusy] = useState(false);
const [deliveriesOpen, setDeliveriesOpen] = useState(false);
const isWebhook = task.schedule.type === "webhook";
Expand Down Expand Up @@ -841,7 +842,15 @@ function ScheduledTaskRow({
status={
<div className="flex flex-wrap items-center gap-2">
<span>
{scheduleLabel(task.schedule, timeZone, task.nextRunAt, messages, locale)} ·{" "}
{scheduleLabel(
task.schedule,
timestampFormat,
timeZone,
task.nextRunAt,
messages,
locale,
)}{" "}
·{" "}
{task.blockedByPolicy !== undefined ? (
task.blockedByPolicy
) : !task.enabled ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,49 @@ it("rejects dedupe templates without a request placeholder", () => {
expect(scheduleFromDraft({ ...draft, coalesceKey: "{{body.bad" })).toBeNull();
});

// The host locale's clock for a wall time, so these pass in any locale (digits, day period, zero padding).
function clock(hour12: boolean, hour: number, minute: number) {
return new Intl.DateTimeFormat(undefined, {
hour: "numeric",
minute: "2-digit",
hour12,
timeZone: "UTC",
}).format(Date.UTC(2000, 0, 1, hour, minute));
}

it("appends the server zone only to fixed-time schedule labels", () => {
expect(scheduleLabel({ type: "fixed_time", timeOfDay: "16:31" }, "Etc/UTC")).toBe(
"Daily at 16:31 UTC",
expect(scheduleLabel({ type: "fixed_time", timeOfDay: "16:31" }, "24-hour", "Etc/UTC")).toBe(
`Daily at ${clock(false, 16, 31)} UTC`,
);
expect(scheduleLabel({ type: "fixed_time", timeOfDay: "09:00" }, "24-hour")).toBe(
`Daily at ${clock(false, 9, 0)}`,
);
expect(scheduleLabel({ type: "interval", everyMs: 60_000 }, "24-hour", "Etc/UTC")).toBe(
"Every 1 min",
);
expect(scheduleLabel({ type: "fixed_time", timeOfDay: "09:00" })).toBe("Daily at 09:00");
expect(scheduleLabel({ type: "interval", everyMs: 60_000 }, "Etc/UTC")).toBe("Every 1 min");
expect(scheduleLabel({ type: "webhook", signature: null }, "Etc/UTC")).toBe("On webhook");
expect(scheduleLabel({ type: "webhook", signature: null }, "24-hour", "Etc/UTC")).toBe(
"On webhook",
);
});

it("shows schedule times in the Time format setting", () => {
const daily = { type: "fixed_time", timeOfDay: "17:30" } as const;
expect(scheduleLabel(daily, "24-hour")).toBe(`Daily at ${clock(false, 17, 30)}`);
expect(scheduleLabel(daily, "12-hour")).toBe(`Daily at ${clock(true, 17, 30)}`);
const midnight = scheduleLabel({ type: "fixed_time", timeOfDay: "0:05" }, "24-hour");
expect(midnight).toBe(`Daily at ${clock(false, 0, 5)}`);
// An h24 clock would print midnight as 24:05 in both the label and the helper.
expect(midnight).not.toContain("24");

// A local wall time, so the label reads 17:30 in any host zone.
const runAt = new Date(2026, 10, 20, 17, 30);
const once = { type: "once", runAt: runAt.toISOString() } as const;
for (const hour12 of [false, true]) {
const dateTime = new Intl.DateTimeFormat(undefined, {
dateStyle: "medium",
timeStyle: "short",
hour12,
}).format(runAt);
expect(scheduleLabel(once, hour12 ? "12-hour" : "24-hour")).toBe(`Once, ${dateTime}`);
}
});
14 changes: 7 additions & 7 deletions apps/web/src/components/settings/scheduledTasksSettings.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
type WebhookFilterDraft,
} from "@t3tools/client-runtime/scheduled-task-webhook";
import { scheduleTimeZoneLabel } from "@t3tools/client-runtime/scheduled-task-time-zone";
import type { TimestampFormat } from "@t3tools/contracts/settings";
import { msg } from "gt-react";

import {
Expand All @@ -32,6 +33,7 @@ import {
} from "@t3tools/shared/projectSettings";
import { getActiveLocale, m, type MessageResolver } from "../../i18n/messages";
import type { ProviderInstanceEntry } from "../../providerInstances";
import { formatDateTimeTimestamp, formatTimeOfDay } from "../../timestampFormat";

import type { ResolvedSettingsScope } from "./settingsScope";

Expand All @@ -43,21 +45,19 @@ export function weekdayShortLabel(day: number, locale: string = getActiveLocale(
);
}

const ONCE_FORMATTER = new Intl.DateTimeFormat(undefined, {
dateStyle: "medium",
timeStyle: "short",
});

export function scheduleLabel(
schedule: ScheduledTaskSchedule,
timestampFormat: TimestampFormat,
timeZone?: string,
runAt?: string | null,
resolve: MessageResolver = m,
locale: string = getActiveLocale(),
): string {
if (schedule.type === "webhook") return resolve(msg("On webhook"));
if (schedule.type === "once") {
return resolve(msg("Once, {time}", { time: ONCE_FORMATTER.format(new Date(schedule.runAt)) }));
return resolve(
msg("Once, {time}", { time: formatDateTimeTimestamp(schedule.runAt, timestampFormat) }),
);
}
if (schedule.type === "interval") {
const minutes = schedule.everyMs / 60_000;
Expand All @@ -66,7 +66,7 @@ export function scheduleLabel(
: resolve(msg("Every {seconds} sec", { seconds: Math.round(schedule.everyMs / 1000) }));
}
const weekdays = schedule.weekdays ?? [];
const time = schedule.timeOfDay;
const time = formatTimeOfDay(schedule.timeOfDay, timestampFormat);
const zone = scheduleTimeZoneLabel(timeZone, runAt);
if (weekdays.length === 0) {
return zone === undefined
Expand Down
27 changes: 27 additions & 0 deletions apps/web/src/timestampFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ export function formatShortTimestamp(isoDate: string, timestampFormat: Timestamp
return formatTime(date, timestampFormat);
}

const dateTimeFormatterCache = new Map<TimestampFormat, Intl.DateTimeFormat>();

export function formatDateTimeTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {
const date = parseTimestampDate(isoDate);
if (!date) return "";
let formatter = dateTimeFormatterCache.get(timestampFormat);
if (!formatter) {
formatter = new Intl.DateTimeFormat(timestampLocale, {
dateStyle: "medium",
timeStyle: "short",
...(timestampFormat === "locale" ? {} : { hour12: timestampFormat === "12-hour" }),
});
dateTimeFormatterCache.set(timestampFormat, formatter);
}
return formatter.format(date);
}

/** A wall-clock `H:MM` or `HH:MM` that names no date or zone, such as a daily schedule's time. */
export function formatTimeOfDay(timeOfDay: string, timestampFormat: TimestampFormat): string {
const match = /^(\d{1,2}):(\d{2})$/.exec(timeOfDay);
if (!match) return timeOfDay;
// Default-zone formatters run in UTC, so the host zone cannot shift the wall time.
return getTimestampFormatter(timestampFormat, false).format(
Date.UTC(2000, 0, 1, Number(match[1]), Number(match[2])),
);
}

const numericDateOptions: Intl.DateTimeFormatOptions = { month: "numeric", day: "numeric" };
const numericDateWithYearOptions: Intl.DateTimeFormatOptions = {
...numericDateOptions,
Expand Down
Loading