Skip to content
Open
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
15 changes: 11 additions & 4 deletions apps/web/src/components/chat/ThreadAutomationsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,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 @@ -38,10 +40,14 @@ const STATUS_DOT_CLASS: Record<ScheduledTask["lastRunStatus"], string> = {
failed: "bg-destructive",
};

function taskScheduleLine(task: ScheduledTask, timeZone: string | undefined) {
function taskScheduleLine(
task: ScheduledTask,
timestampFormat: TimestampFormat,
timeZone: string | undefined,
) {
return (
<>
{scheduleLabel(task.schedule, timeZone, task.nextRunAt)}
{scheduleLabel(task.schedule, timestampFormat, timeZone, task.nextRunAt)}
{task.blockedByPolicy !== undefined ? (
` · ${task.blockedByPolicy}`
) : task.enabled && task.nextRunAt !== null ? (
Expand Down Expand Up @@ -74,6 +80,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 [busyTaskId, setBusyTaskId] = useState<string | null>(null);

Expand Down Expand Up @@ -181,13 +188,13 @@ export function ThreadAutomationsPanel(props: {
{task.title}
</span>
<p className="truncate text-2xs text-muted-foreground">
{taskScheduleLine(task, tasksQuery.data?.timeZone)}
{taskScheduleLine(task, timestampFormat, tasksQuery.data?.timeZone)}
</p>
</TooltipTrigger>
<TooltipPopup side="left">
<p className="font-medium">{task.title}</p>
<p className="text-muted-foreground">
{taskScheduleLine(task, tasksQuery.data?.timeZone)}
{taskScheduleLine(task, timestampFormat, tasksQuery.data?.timeZone)}
</p>
</TooltipPopup>
</Tooltip>
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/settings/ScheduledTasksSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import {
} from "@t3tools/client-runtime/state/runtime";

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 @@ -739,6 +739,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 @@ -785,7 +786,7 @@ function ScheduledTaskRow({
status={
<div className="flex flex-wrap items-center gap-2">
<span>
{scheduleLabel(task.schedule, timeZone, task.nextRunAt)} ·{" "}
{scheduleLabel(task.schedule, timestampFormat, timeZone, task.nextRunAt)} ·{" "}
{task.blockedByPolicy !== undefined ? (
task.blockedByPolicy
) : !task.enabled ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,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}`);
}
});
15 changes: 8 additions & 7 deletions apps/web/src/components/settings/scheduledTasksSettings.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ 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 {
resolveProjectSettings,
type LegacyProjectSettingsFields,
} from "@t3tools/shared/projectSettings";
import type { ProviderInstanceEntry } from "../../providerInstances";
import { formatDateTimeTimestamp, formatTimeOfDay } from "../../timestampFormat";

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

export const WEEKDAY_LABELS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] as const;

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

export function scheduleLabel(
schedule: ScheduledTaskSchedule,
timestampFormat: TimestampFormat,
timeZone?: string,
runAt?: string | null,
): string {
if (schedule.type === "webhook") return "On webhook";
if (schedule.type === "once") return `Once, ${ONCE_FORMATTER.format(new Date(schedule.runAt))}`;
if (schedule.type === "once") {
return `Once, ${formatDateTimeTimestamp(schedule.runAt, timestampFormat)}`;
}
if (schedule.type === "interval") {
const minutes = schedule.everyMs / 60_000;
return Number.isInteger(minutes)
Expand All @@ -61,7 +61,8 @@ export function scheduleLabel(
? "Weekdays"
: weekdays.map((day) => WEEKDAY_LABELS[day]).join(", ");
const zone = scheduleTimeZoneLabel(timeZone, runAt);
return `${days} at ${schedule.timeOfDay}${zone === undefined ? "" : ` ${zone}`}`;
const time = formatTimeOfDay(schedule.timeOfDay, timestampFormat);
return `${days} at ${time}${zone === undefined ? "" : ` ${zone}`}`;
}

export function matchesScheduledTaskScope(
Expand Down
28 changes: 28 additions & 0 deletions apps/web/src/timestampFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@ export function formatShortTimestamp(isoDate: string, timestampFormat: Timestamp
return getTimestampFormatter(timestampFormat, false).format(date);
}

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

/** A date and time, such as "Oct 20, 2026, 5:30 PM". */
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;
// Formatted in UTC so the host zone cannot shift the wall time.
return getTimestampFormatter(timestampFormat, false, "UTC").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