import { Checkbox, cn, Input, Radio, Select, Text } from "@cloudflare/kumo"; import { Info } from "@phosphor-icons/react"; import { useId } from "react"; import { useTimeOfDayDraft } from "./DateTimeField.tsx"; import { InsetPanel } from "./InsetPanel.tsx"; import { RECURRENCE_PRESETS, type RecurrenceFrequency, type RecurrenceOrdinal, type RecurrencePreset, type RecurrenceProblem, type RecurrenceValue, type RecurrenceWeekday, RRULE_WEEKDAYS, } from "./recurrence.ts"; import { hourCycleOf, type WeekDay, weekdayNames, weekdayOrder } from "./schedule-model.ts"; export interface RecurrenceEditorLabels { /** The preset group's legend ("Repeats"). */ legend: string; /** Preset names ("Every hour", "Every day", "Weekdays", "Every week", "Every month", "Once", "Custom"). */ presets: Record; /** One line under each preset card (optional per preset). */ presetHints?: Partial>; /** The time of day ("At"); "hourly" asks for the minute ("Minutes past the hour"). */ time: string; minute: string; /** Weekly: "On". */ weekday: string; /** Monthly: "Day of the month", each day ("12th", "On the 12th") and the last day ("Last day"). */ monthDay: string; monthDayOption: (day: number) => string; lastDay: string; /** Custom: "Every", the unit ("days", "weeks", "months" by count), "On these days". */ interval: string; frequency: string; frequencies: Record string>; weekdays: string; /** Custom monthly: "On day 12" / "On the second Tuesday". */ monthlyOn: string; monthlyDay: string; monthlyWeekday: string; ordinal: string; ordinals: Record<`${RecurrenceOrdinal}`, string>; /** End: "Ends", "Never", "On a date", "After a number of runs", the date and the count. */ end: string; ends: { never: string; on: string; after: string }; endDate: string; endCount: string; /** Under the time when it is on the hour: "Pick a minute such as :07 so many sites do not all run at :00." */ avoidOnTheHour: string; /** A time that cannot be read. */ invalidTime: string; /** The sentence panel's name ("Summary"). */ summary: string; } export interface RecurrenceEditorProps { value: RecurrenceValue; onChange: (next: RecurrenceValue) => void; locale: string; /** Custom weekday checkboxes start here (Monday in Turkish). */ weekStartsOn: WeekDay; labels: RecurrenceEditorLabels; /** * The rule in words, from the screen's own templates ("Weekdays at 09:07", "Her ayın ikinci * salısı 09:07"): no cron-to-text library (RFC-0008 §15.8). Spoken politely when it changes. */ sentence?: string | undefined; /** Messages for problems to show (`recurrenceProblems`), already worded. */ problems?: Partial>; /** Presets offered, in order. Default: hourly, daily, weekdays, weekly, monthly, once, custom. */ presets?: readonly RecurrencePreset[]; hourCycle?: "h23" | "h12"; disabled?: boolean; className?: string; } const ORDINALS: readonly RecurrenceOrdinal[] = [1, 2, 3, 4, -1]; const MONTH_DAYS = [...Array.from({ length: 31 }, (_, i) => i + 1), -1]; /** * How often an automation runs (RFC-0008 §9.3 D3, §15.4): the presets as `Radio * appearance="card"` (hourly, daily, weekdays, weekly, monthly, once, custom), then only the * controls the choice needs — the time (24-hour in Turkish; minutes for hourly), the weekday, the * day of the month, or for custom: every N days/weeks/months, weekday `Checkbox`es in * `weekStartsOn` order, monthly "on day 12" / "on the second Tuesday" / the last day, and the end * (never / on a date / after N runs). A hint asks for a minute off the hour. It emits a * structured `RecurrenceValue` (`recurrenceRule` turns it into the RRULE subset) and shows the * screen's sentence for it in an `InsetPanel`. Weekday names come from `Intl` in `locale`. */ export function RecurrenceEditor({ value, onChange, locale, weekStartsOn, labels, sentence, problems = {}, presets = RECURRENCE_PRESETS, hourCycle, disabled, className, }: RecurrenceEditorProps) { const set = (patch: Partial) => onChange({ ...value, ...patch }); const cycle = hourCycle ?? hourCycleOf(locale); const time = useTimeOfDayDraft( value.time, (t) => { if (t) set({ time: t }); }, locale, cycle, ); const id = useId(); const longNames = weekdayNames(locale, "long"); const shortNames = weekdayNames(locale, "short"); const order = weekdayOrder(weekStartsOn); const weekdayItems = order.map((d) => ({ value: RRULE_WEEKDAYS[d] as RecurrenceWeekday, label: longNames[d] as string, })); const minute = Number(value.time.slice(3, 5)); const timeError = problems.time ?? (time.unreadable ? labels.invalidTime : undefined); const custom = value.preset === "custom"; const timeField = value.preset === "once" ? null : value.preset === "hourly" ? ( { const n = Math.min(59, Math.max(0, Math.trunc(Number(v) || 0))); set({ time: `${value.time.slice(0, 2)}:${String(n).padStart(2, "0")}` }); }} disabled={disabled} className="w-full" data-recurrence="minute" /> ) : ( ); return (
set({ preset: v as RecurrencePreset })} disabled={disabled} className="grid gap-2 @md:grid-cols-2" > {presets.map((p) => ( ))} {value.preset !== "once" && (
{timeField} {value.preset === "weekly" && ( set({ monthDay: Number(v) })} items={MONTH_DAYS.map((d) => ({ value: String(d), label: d === -1 ? labels.lastDay : labels.monthDayOption(d), }))} disabled={disabled} className="w-full" /> )} {custom && (
set({ interval: Math.min(999, Math.max(1, Math.trunc(Number(v) || 1))) }) } error={problems.interval} disabled={disabled} className="w-full" data-recurrence="interval" /> set({ monthDay: Number(v) })} items={MONTH_DAYS.map((d) => ({ value: String(d), label: d === -1 ? labels.lastDay : labels.monthDayOption(d), }))} disabled={disabled} className="w-full" /> ) : ( <> set({ weekdays: [String(v) as RecurrenceWeekday] })} items={weekdayItems} disabled={disabled} className="w-full" /> )}
)} {custom && (
set({ end: v === "on" ? { kind: "on", date: value.end.kind === "on" ? value.end.date : "" } : v === "after" ? { kind: "after", count: value.end.kind === "after" ? value.end.count : 10 } : { kind: "never" }, }) } disabled={disabled} > {value.end.kind === "on" && ( set({ end: { kind: "on", date: v } })} error={problems.until} disabled={disabled} className="w-full" data-recurrence="until" /> )} {value.end.kind === "after" && ( set({ end: { kind: "after", count: Math.max(1, Math.trunc(Number(v) || 1)) } }) } error={problems.count} disabled={disabled} className="w-full" data-recurrence="count" /> )}
)} {sentence && ( {sentence} )}
); }