Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
97 changes: 93 additions & 4 deletions ui/desktop/src/components/schedule/CronPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ const i18n = defineMessages({
day: { id: 'cronPicker.day', defaultMessage: 'Day' },
week: { id: 'cronPicker.week', defaultMessage: 'Week' },
month: { id: 'cronPicker.month', defaultMessage: 'Month' },
quarter: { id: 'cronPicker.quarter', defaultMessage: 'Quarter' },
year: { id: 'cronPicker.year', defaultMessage: 'Year' },
inMonth: { id: 'cronPicker.inMonth', defaultMessage: 'in' },
startingMonth: { id: 'cronPicker.startingMonth', defaultMessage: 'starting month' },
january: { id: 'cronPicker.january', defaultMessage: 'January' },
february: { id: 'cronPicker.february', defaultMessage: 'February' },
march: { id: 'cronPicker.march', defaultMessage: 'March' },
Expand All @@ -39,7 +41,26 @@ const i18n = defineMessages({
atSecond: { id: 'cronPicker.atSecond', defaultMessage: 'at second' },
});

type Period = 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
type Period = 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';

const quarterMonthsByStartMonth: Record<string, string> = {
'1': '1,4,7,10',
'2': '2,5,8,11',
'3': '3,6,9,12',
};

const quarterDayLimitByStartMonth: Record<string, number> = {
'1': 30,
'2': 28,
'3': 30,
};

const getQuarterStartMonth = (month: string): string | null => {
const entry = Object.entries(quarterMonthsByStartMonth).find(
([, quarterMonths]) => quarterMonths === month
);
return entry?.[0] ?? null;
};

type ParsedCron = {
period: Period;
Expand Down Expand Up @@ -76,6 +97,12 @@ const parseCron = (cron: string): ParsedCron => {

const [second, minute, hour, dayOfMonth, month, dayOfWeek] = parts;

if (dayOfMonth !== '*') {
const quarterStartMonth = getQuarterStartMonth(month);
if (quarterStartMonth) {
return { period: 'quarter', second, minute, hour, dayOfMonth, month, dayOfWeek };
}
}
if (month !== '*' && dayOfMonth !== '*') {
return { period: 'year', second, minute, hour, dayOfMonth, month, dayOfWeek };
}
Expand Down Expand Up @@ -124,6 +151,7 @@ export const CronPicker: React.FC<CronPickerProps> = ({ schedule, onChange, isVa
const [dayOfWeek, setDayOfWeek] = useState('1');
const [dayOfMonth, setDayOfMonth] = useState('1');
const [month, setMonth] = useState('1');
const [quarterStartMonth, setQuarterStartMonth] = useState('1');
const [readableCron, setReadableCron] = useState('');

useEffect(() => {
Expand All @@ -138,10 +166,24 @@ export const CronPicker: React.FC<CronPickerProps> = ({ schedule, onChange, isVa
setDayOfWeek(parsed.dayOfWeek === '*' ? '1' : parsed.dayOfWeek);
setDayOfMonth(parsed.dayOfMonth === '*' ? '1' : parsed.dayOfMonth);
setMonth(parsed.month === '*' ? '1' : parsed.month);
setQuarterStartMonth(getQuarterStartMonth(parsed.month) ?? '1');
}, [schedule]);

const maxDayOfMonth = period === 'quarter' ? quarterDayLimitByStartMonth[quarterStartMonth] : 31;

useEffect(() => {
const parsedDay = parseInt(dayOfMonth, 10);
if (!Number.isNaN(parsedDay) && parsedDay > maxDayOfMonth) {
setDayOfMonth(maxDayOfMonth.toString());
Comment on lines +138 to +139

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid rewriting existing quarterly schedules on load

When an existing cron like 0 0 14 31 1,4,7,10 * is loaded, the new quarter parsing path sets period='quarter' and this effect immediately clamps dayOfMonth from 31 to 30, so saving the modal without intentional schedule edits mutates the original cron. This is a regression for pre-existing schedules that legitimately used day 31 with quarterly month lists and now get silently downgraded to day 30.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed by using custom cron expression on ui

}
}, [dayOfMonth, maxDayOfMonth]);

useEffect(() => {
const hour24 = to24Hour(hour12, isPM);
const clampedDayOfMonth = Math.min(
Math.max(parseInt(dayOfMonth, 10) || 1, 1),
maxDayOfMonth
).toString();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep quarter day input and emitted cron in sync

For quarter schedules, the emitted cron uses clampedDayOfMonth, but the component only writes back to state when the value is above maxDayOfMonth, not when it is empty or below 1. As a result, entering 0 (or clearing the field) can leave the UI showing an invalid day while onChange emits day 1, so users may save a schedule different from what the form displays.

Useful? React with 👍 / 👎.

let cron: string;

switch (period) {
Expand All @@ -160,6 +202,9 @@ export const CronPicker: React.FC<CronPickerProps> = ({ schedule, onChange, isVa
case 'month':
cron = `${second} ${minute} ${hour24} ${dayOfMonth} * *`;
break;
case 'quarter':
cron = `${second} ${minute} ${hour24} ${clampedDayOfMonth} ${quarterMonthsByStartMonth[quarterStartMonth]} *`;
break;
case 'year':
cron = `${second} ${minute} ${hour24} ${dayOfMonth} ${month} *`;
break;
Expand All @@ -178,7 +223,18 @@ export const CronPicker: React.FC<CronPickerProps> = ({ schedule, onChange, isVa
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [period, second, minute, hour12, isPM, dayOfWeek, dayOfMonth, month]);
}, [
period,
second,
minute,
hour12,
isPM,
dayOfWeek,
dayOfMonth,
month,
quarterStartMonth,
maxDayOfMonth,
]);

const selectClassName = 'px-2 py-1 border rounded bg-white dark:bg-gray-800 dark:border-gray-600';

Expand All @@ -196,11 +252,40 @@ export const CronPicker: React.FC<CronPickerProps> = ({ schedule, onChange, isVa
<option value="day">{intl.formatMessage(i18n.day)}</option>
<option value="week">{intl.formatMessage(i18n.week)}</option>
<option value="month">{intl.formatMessage(i18n.month)}</option>
<option value="quarter">{intl.formatMessage(i18n.quarter)}</option>
<option value="year">{intl.formatMessage(i18n.year)}</option>
</select>
</div>

<div className="space-y-3">
{period === 'quarter' && (
<div className="flex flex-wrap items-center gap-2">
<div className="flex items-center gap-2">
<span className="text-sm">{intl.formatMessage(i18n.startingMonth)}</span>
<select
value={quarterStartMonth}
onChange={(e) => setQuarterStartMonth(e.target.value)}
className={selectClassName}
>
<option value="1">{intl.formatMessage(i18n.january)}</option>
<option value="2">{intl.formatMessage(i18n.february)}</option>
<option value="3">{intl.formatMessage(i18n.march)}</option>
</select>
</div>
<div className="flex items-center gap-2">
<span className="text-sm">{intl.formatMessage(i18n.onDay)}</span>
<input
type="number"
min="1"
max={maxDayOfMonth}
value={dayOfMonth}
onChange={(e) => setDayOfMonth(e.target.value)}
className="w-16 px-2 py-1 border rounded"
/>
</div>
</div>
)}

{period === 'year' && (
<div className="flex items-center gap-2">
<span className="text-sm">{intl.formatMessage(i18n.inMonth)}</span>
Expand Down Expand Up @@ -231,7 +316,7 @@ export const CronPicker: React.FC<CronPickerProps> = ({ schedule, onChange, isVa
<input
type="number"
min="1"
max="31"
max={maxDayOfMonth}
value={dayOfMonth}
onChange={(e) => setDayOfMonth(e.target.value)}
className="w-16 px-2 py-1 border rounded"
Expand All @@ -258,7 +343,11 @@ export const CronPicker: React.FC<CronPickerProps> = ({ schedule, onChange, isVa
</div>
)}

{(period === 'day' || period === 'week' || period === 'month' || period === 'year') && (
{(period === 'day' ||
period === 'week' ||
period === 'month' ||
period === 'quarter' ||
period === 'year') && (
<div className="flex items-center gap-2">
<span className="text-sm">{intl.formatMessage(i18n.at)}</span>
<input
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render, screen, type RenderOptions, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import { IntlTestWrapper } from '../../../i18n/test-utils';
import { CronPicker } from '../CronPicker';

const renderWithIntl = (ui: React.ReactElement, options?: RenderOptions) =>
render(ui, { wrapper: IntlTestWrapper, ...options });

const getLastCron = (onChange: ReturnType<typeof vi.fn>) => {
const calls = onChange.mock.calls;
return calls[calls.length - 1]?.[0];
};

describe('CronPicker', () => {
it('generates quarterly cron expressions from the quarter preset', async () => {
const user = userEvent.setup();
const onChange = vi.fn();

renderWithIntl(<CronPicker schedule={null} onChange={onChange} isValid={vi.fn()} />);

await user.selectOptions(screen.getAllByRole('combobox')[0], 'quarter');

await waitFor(() => {
expect(getLastCron(onChange)).toBe('0 0 14 1 1,4,7,10 *');
});

const dayInput = screen.getAllByRole('spinbutton')[0];
await user.clear(dayInput);
await user.type(dayInput, '31');
await user.selectOptions(screen.getAllByRole('combobox')[1], '2');

await waitFor(() => {
expect(dayInput).toHaveValue(28);
expect(getLastCron(onChange)).toBe('0 0 14 28 2,5,8,11 *');
});
});
});
6 changes: 6 additions & 0 deletions ui/desktop/src/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,18 @@
"cronPicker.onDay": {
"defaultMessage": "on day"
},
"cronPicker.quarter": {
"defaultMessage": "Quarter"
},
"cronPicker.saturday": {
"defaultMessage": "Saturday"
},
"cronPicker.september": {
"defaultMessage": "September"
},
"cronPicker.startingMonth": {
"defaultMessage": "starting month"
},
"cronPicker.sunday": {
"defaultMessage": "Sunday"
},
Expand Down
Loading