import Box from '@mui/material/Box'; import IconButton from '@mui/material/IconButton'; import { styled } from '@mui/material/styles'; import Typography from '@mui/material/Typography'; import { useAtomValue } from 'jotai'; import React from 'react'; import { localeString } from '../localization/strings'; import { addMonths, clampDayRuns, computeRunDays, computeRunsOnDay, formatDayKey, formatRelative, formatTime, localeToBcp47, monthKeyInTz, } from '../nextRuns'; import { cronExpState } from '../selector'; import { cronValidationErrorMessageState, localeState } from '../store'; // The calendar window: the current month plus the next eleven (a full year // ahead). Users can page across these twelve months (never before the current // one). `MONTH_SPAN` is the count of months SHOWN; `WINDOW_MONTHS` is how far // ahead we enumerate runs. const MONTH_SPAN = 12; // Enumerate every run from now to the end of the last shown month so each firing // day is marked — not a fixed-size prefix. computeRunsUntil caps the list, so a // once-a-minute cron can't blow up; days past the cap simply go unmarked. const WINDOW_MONTHS = MONTH_SPAN; // Relative-time labels re-render on this cadence (the per-day list). Occurrences // are NOT recomputed here (see the compute effect). const TICK_MS = 30_000; const Panel = styled(Box)(({ theme }) => ({ padding: '18px 20px', backgroundColor: theme.palette.action.hover, })); const Header = styled(Box)({ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 12, }); const PanelLabel = styled(Typography)(({ theme }) => ({ fontSize: 11, letterSpacing: '0.07em', textTransform: 'uppercase', fontWeight: 600, color: theme.palette.text.secondary, })); const MonthNav = styled(Box)({ display: 'flex', alignItems: 'center', gap: 2 }); const MonthLabel = styled(Typography)({ fontSize: 12.5, fontWeight: 600, minWidth: 96, textAlign: 'center', }); // Roomy hit area for the month arrows — the glyph is small, so padding does the // work of making the whole ~32px square clickable. const NavButton = styled(IconButton)({ padding: 8, fontSize: 16, lineHeight: 1 }); const Grid = styled('div')({ display: 'grid', gridTemplateColumns: 'repeat(7, 1fr)', // Generous gap so circular day cells breathe and a selected ring never bleeds // into the neighbouring cell. gap: 6, }); const Weekday = styled('div')(({ theme }) => ({ fontSize: 10, fontWeight: 600, textAlign: 'center', color: theme.palette.text.secondary, paddingBottom: 2, })); // A day cell is a real button so it's keyboard-operable: days with runs are // filled with the accent color, the selected day gets a ring, and clicking any // day (run or not) filters the list below to it. const Day = styled('button')(({ theme }) => ({ appearance: 'none', border: 0, background: 'transparent', font: 'inherit', padding: 0, aspectRatio: '1 / 1', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 12, borderRadius: '50%', color: theme.palette.text.primary, cursor: 'pointer', '&:hover': { backgroundColor: theme.palette.action.selected }, '&[data-blank="true"]': { visibility: 'hidden', cursor: 'default' }, '&[data-has-runs="true"]': { backgroundColor: theme.palette.primary.main, color: theme.palette.primary.contrastText, fontWeight: 600, }, '&[data-has-runs="true"]:hover': { backgroundColor: theme.palette.primary.dark }, // Selection is a ring sitting OUTSIDE the circle, with a gap (the panel // background shows through `outlineOffset`) so it reads even on a filled // highlighted day, and the grid gap keeps it clear of the neighbouring cell. '&[data-selected="true"]': { outline: `2px solid ${theme.palette.primary.main}`, outlineOffset: 2, }, '&:focus-visible': { outline: `2px solid ${theme.palette.primary.main}`, outlineOffset: 2, }, })); const DayHeading = styled(Typography)(({ theme }) => ({ marginTop: 14, fontSize: 12, fontWeight: 600, color: theme.palette.text.secondary, })); // A busy day (e.g. an every-few-minutes cron) can have dozens of runs; cap the // visible list at MAX_VISIBLE_RUNS rows and scroll the rest. Each row is a fixed // RUN_ROW_HEIGHT (border-box) so the cap is an exact pixel height. const MAX_VISIBLE_RUNS = 5; const RUN_ROW_HEIGHT = 36; const RunList = styled('ul')({ listStyle: 'none', margin: '8px 0 0', padding: 0, maxHeight: MAX_VISIBLE_RUNS * RUN_ROW_HEIGHT, overflowY: 'auto', }); const RunRow = styled('li')(({ theme }) => ({ boxSizing: 'border-box', height: RUN_ROW_HEIGHT, display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12, fontSize: 13.5, borderTop: `1px solid ${theme.palette.divider}`, '&:first-of-type': { borderTop: 0 }, })); // Divider standing in for the collapsed middle of a dense day (first 10 + last // 10 shown). Non-interactive, centred, muted — visually distinct from a RunRow. const GapRow = styled('li')(({ theme }) => ({ boxSizing: 'border-box', height: RUN_ROW_HEIGHT, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 12, color: theme.palette.text.secondary, borderTop: `1px solid ${theme.palette.divider}`, })); const When = styled('span')({ fontWeight: 500 }); const Relative = styled('span')(({ theme }) => ({ color: theme.palette.text.secondary, fontSize: 12.5, whiteSpace: 'nowrap', })); const Message = styled(Typography)(({ theme }) => ({ color: theme.palette.text.secondary, fontSize: 13.5, })); interface NextRunsProps { timezone?: string; } // Cache Intl.DateTimeFormat instances by locale + options. monthLabel runs on // every render (per visible month) and weekdayInitials per locale, so reusing // the formatter avoids reallocating it each time. const dateTimeFormatCache = new Map(); function dateTimeFormat(locale: string, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat { const key = `${locale}|${JSON.stringify(options)}`; let fmt = dateTimeFormatCache.get(key); if (!fmt) { // This IS the hoist the rule wants: built once per locale+options key and // cached (mirrors the cache in nextRuns.ts). // react-doctor-disable-next-line react-doctor/js-hoist-intl fmt = new Intl.DateTimeFormat(locale, options); dateTimeFormatCache.set(key, fmt); } return fmt; } // Sunday-first narrow weekday initials for `localeTag` (Jan 1 2023 is a Sunday). function weekdayInitials(localeTag: string): string[] { const fmt = dateTimeFormat(localeTag, { weekday: 'narrow', timeZone: 'UTC' }); return Array.from({ length: 7 }, (_, i) => fmt.format(new Date(Date.UTC(2023, 0, 1 + i)))); } function monthLabel(ym: string, localeTag: string): string { const [year, month] = ym.split('-').map(Number); return dateTimeFormat(localeTag, { month: 'long', year: 'numeric', timeZone: 'UTC', }).format(new Date(Date.UTC(year, month - 1, 1))); } export default function NextRuns({ timezone }: NextRunsProps) { const cronExp = useAtomValue(cronExpState); const validationError = useAtomValue(cronValidationErrorMessageState); const locale = useAtomValue(localeState); const localeTag = localeToBcp47(locale.cronDescriptionText); // Ticking wall-clock for relative labels only — kept out of the occurrence // computation so we don't re-parse the cron every 30s. const [now, setNow] = React.useState(() => new Date()); React.useEffect(() => { const id = setInterval(() => setNow(new Date()), TICK_MS); return () => clearInterval(id); }, []); // The calendar days (ISO `YYYY-MM-DD`, ascending) that fire inside the visible // window, plus the `anchor` they were computed from (so the month strip aligns // with the same "current month"). Only DAY-level marking is computed here — not // every occurrence — so a dense cron (e.g. every minute) can't blow up or get // capped into dropping whole months off the far end. Recomputed only when the // cron / validity / timezone change; gated behind the library's validator and // guarded inside computeRunDays. The horizon is the start of the month AFTER // the last shown one, with a one-day buffer so a timezone behind UTC doesn't // clip the last day's late runs. const [{ days, anchor }, setData] = React.useState<{ days: string[]; anchor: Date }>(() => ({ days: [], anchor: new Date(), })); React.useEffect(() => { const at = new Date(); if (validationError.length > 0) { // Not derivable during render: the firing-day set comes from an async // cron-parser computation (dynamically imported, off the main entry) with // a cancellation guard. The effect is the correct tool here; the empty // branch just short-circuits the same state the async path writes. // react-doctor-disable-next-line react-doctor/no-adjust-state-on-prop-change setData({ days: [], anchor: at }); return; } let cancelled = false; const startYm = monthKeyInTz(at, timezone); const horizonYm = addMonths(startYm, WINDOW_MONTHS); const [hy, hm] = horizonYm.split('-').map(Number); const horizon = new Date(Date.UTC(hy, hm - 1, 2)); computeRunDays(cronExp, horizon, { timezone, anchor: at }).then((next) => { if (!cancelled) { setData({ days: next, anchor: at }); } }); return () => { cancelled = true; }; }, [cronExp, validationError, timezone]); const daySet = React.useMemo(() => new Set(days), [days]); // The three months on offer, current first — a fixed strip regardless of where // the runs fall. const months = React.useMemo(() => { const start = monthKeyInTz(anchor, timezone); return Array.from({ length: MONTH_SPAN }, (_, i) => addMonths(start, i)); }, [anchor, timezone]); const weekdays = React.useMemo(() => weekdayInitials(localeTag), [localeTag]); // On a fresh schedule, jump to the month of the soonest run and pre-select that // day so the list isn't empty; if nothing runs in the window, fall back to the // current month with no selection. This is a state-reset-on-prop-change: do it // during render (the React-recommended pattern) keyed off the firing-day set, // so the reset only fires when `days` itself changes (new cron / tz) — not on // every render that recreates `months`, and without the extra commit a useEffect // would force. const [cursor, setCursor] = React.useState(0); const [selectedDay, setSelectedDay] = React.useState(null); // Track the last firing-day set we reacted to. A ref (not state) because it's // only compared during render, never rendered — the setCursor/setSelectedDay // calls below already drive the re-render. const prevDays = React.useRef(days); if (prevDays.current !== days) { prevDays.current = days; if (days.length === 0) { setCursor(0); setSelectedDay(null); } else { const firstDay = days[0]; setSelectedDay(firstDay); const idx = months.indexOf(firstDay.slice(0, 7)); setCursor(idx >= 0 ? idx : 0); } } // The actual run times for the SELECTED day only — computed on demand (a cheap // single-day window) rather than up front for every day in the window. Tagged // with the day they belong to so a slow async result never paints onto a // different day the user has since clicked. A day that isn't in `daySet` has no // runs by definition, so we resolve it to `[]` synchronously (no parser hop, // no stale-list flash); only firing days take the async path. const [selectedRuns, setSelectedRuns] = React.useState<{ day: string; runs: Date[] } | null>( null, ); React.useEffect(() => { if (!selectedDay || validationError.length > 0) { // Not derivable during render: a firing day's run times come from an async // cron-parser computation (see below) with a cancellation guard. These two // early branches just short-circuit the same state the async path writes // (no selection / a non-firing day → no runs), so the effect stays the // single owner of `selectedRuns`. // react-doctor-disable-next-line react-doctor/no-adjust-state-on-prop-change setSelectedRuns(null); return; } if (!daySet.has(selectedDay)) { // react-doctor-disable-next-line react-doctor/no-adjust-state-on-prop-change setSelectedRuns({ day: selectedDay, runs: [] }); return; } let cancelled = false; // Anchor to the same "now" the day-marking used so today's already-elapsed // runs are dropped from the list (and the list stays consistent with which // days are marked as firing). Future days are unaffected. computeRunsOnDay(cronExp, selectedDay, { timezone, anchor }).then((next) => { if (!cancelled) { setSelectedRuns({ day: selectedDay, runs: next }); } }); return () => { cancelled = true; }; }, [cronExp, validationError, timezone, selectedDay, daySet, anchor]); const safeCursor = Math.min(cursor, months.length - 1); // Only trust the resolved runs once they match the currently selected day; a // firing day that's still computing reads `undefined` (heading only, no flash). const selectedDayRuns = selectedRuns && selectedRuns.day === selectedDay ? selectedRuns.runs : undefined; return (
{localeString(locale, 'nextRunsLabel')} setCursor((c) => Math.max(c - 1, 0))} > ‹ {monthLabel(months[safeCursor], localeTag)} = months.length - 1} onClick={() => setCursor((c) => Math.min(c + 1, months.length - 1))} > ›
{validationError.length > 0 ? ( {localeString(locale, 'invalidScheduleText')} ) : ( <> {days.length === 0 ? ( {localeString(locale, 'noUpcomingRunsText')} ) : ( selectedDay && ( <> {formatDayKey(selectedDay, localeTag)} {selectedDayRuns === undefined ? null : selectedDayRuns.length === 0 ? ( {localeString(locale, 'noRunsOnDayText')} ) : ( (() => { // Cap a dense day to its first 10 + last 10 runs, collapsing // the middle into a "{count} more runs" divider. const { head, tail, hidden } = clampDayRuns(selectedDayRuns); const renderRow = (date: Date) => ( {formatTime(date, localeTag, timezone)} {formatRelative(date, now, localeTag)} ); return ( {head.map(renderRow)} {hidden > 0 && ( )} {tail.map(renderRow)} ); })() )} ) )} )}
); } interface MonthGridProps { ym: string; weekdays: string[]; daySet: Set; localeTag: string; selectedDay: string | null; onSelect: (day: string) => void; } function MonthGrid({ ym, weekdays, daySet, localeTag, selectedDay, onSelect }: MonthGridProps) { const [year, month] = ym.split('-').map(Number); const monthIdx = month - 1; // Weekday/day counts are properties of the abstract calendar month, so they're // computed in UTC (timezone-independent); the run buckets were already keyed in // the target timezone. const firstWeekday = new Date(Date.UTC(year, monthIdx, 1)).getUTCDay(); const daysInMonth = new Date(Date.UTC(year, monthIdx + 1, 0)).getUTCDate(); const cells: React.ReactNode[] = []; for (let i = 0; i < firstWeekday; i++) { cells.push(