|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { format } from 'date-fns' |
| 4 | +import { ChipDatePicker, ChipModalField, Switch } from '@/components/emcn' |
| 5 | +import type { Recurrence } from '@/app/workspace/[workspaceId]/scheduled-tasks/utils/recurrence' |
| 6 | + |
| 7 | +const WEEKDAY_PRESET = [1, 2, 3, 4, 5] |
| 8 | +/** Seed count when the user first chooses "ends after N runs". */ |
| 9 | +const DEFAULT_END_AFTER_COUNT = 10 |
| 10 | +/** Cadence a task falls back to when the user first flips on recurrence. */ |
| 11 | +const DEFAULT_RECURRING_FREQUENCY = 'daily' |
| 12 | + |
| 13 | +/** The frequency presets the dropdown authors, keyed by a synthetic option value. */ |
| 14 | +type FrequencyOption = 'daily' | 'weekly' | 'weekdays' | 'monthly' | 'custom' |
| 15 | + |
| 16 | +function isWeekdayPreset(weekdays: number[]): boolean { |
| 17 | + return ( |
| 18 | + weekdays.length === WEEKDAY_PRESET.length && WEEKDAY_PRESET.every((d) => weekdays.includes(d)) |
| 19 | + ) |
| 20 | +} |
| 21 | + |
| 22 | +/** Collapses a recurring recurrence into the single dropdown value that represents it. */ |
| 23 | +function frequencyOptionFor(recurrence: Recurrence): FrequencyOption { |
| 24 | + if (recurrence.frequency === 'weekly') |
| 25 | + return isWeekdayPreset(recurrence.weekdays) ? 'weekdays' : 'weekly' |
| 26 | + if (recurrence.frequency === 'once') return DEFAULT_RECURRING_FREQUENCY |
| 27 | + return recurrence.frequency |
| 28 | +} |
| 29 | + |
| 30 | +interface RecurrenceSectionProps { |
| 31 | + recurrence: Recurrence |
| 32 | + onChange: (recurrence: Recurrence) => void |
| 33 | + /** The launch day, so weekly/monthly labels name the weekday and day-of-month. */ |
| 34 | + launchDate: string |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * The repeat + end controls for a scheduled task, rendered as a body section |
| 39 | + * below the prompt: a "Recurring" {@link Switch} that toggles a one-time launch |
| 40 | + * into a repeat, and — once on — the frequency preset and how it ends (never, on |
| 41 | + * a date, or after N runs). |
| 42 | + * |
| 43 | + * Composed as a sibling between the prompt body and footer; it owns its own |
| 44 | + * leading separator and mirrors {@link ChipModalBody}'s spacing |
| 45 | + * (`gap-4 px-2 pt-4 pb-4.5`) so every {@link ChipModalField} lands at the same |
| 46 | + * effective `px-4` as the modal header/footer — no changes to the `ChipModal` |
| 47 | + * primitives. |
| 48 | + */ |
| 49 | +export function RecurrenceSection({ recurrence, onChange, launchDate }: RecurrenceSectionProps) { |
| 50 | + const launch = new Date(`${launchDate}T00:00`) |
| 51 | + const isRecurring = recurrence.frequency !== 'once' |
| 52 | + |
| 53 | + const frequencyOptions = [ |
| 54 | + { value: 'daily', label: 'Daily' }, |
| 55 | + { value: 'weekly', label: `Weekly on ${format(launch, 'EEE')}` }, |
| 56 | + { value: 'weekdays', label: 'Weekdays' }, |
| 57 | + { value: 'monthly', label: `Monthly on the ${format(launch, 'do')}` }, |
| 58 | + ...(recurrence.frequency === 'custom' ? [{ value: 'custom', label: 'Custom' }] : []), |
| 59 | + ] |
| 60 | + |
| 61 | + /** |
| 62 | + * Flips the one-time launch into a repeat and back. Toggling off keeps the |
| 63 | + * recurrence shape (cadence, end, and a passed-through `custom` cron) on the |
| 64 | + * object and only sets `frequency: 'once'` — the wire ignores everything but |
| 65 | + * `frequency` for a one-time task — so toggling back on restores `custom` |
| 66 | + * rather than silently rewriting a conversationally-authored cron to `daily`. |
| 67 | + */ |
| 68 | + const handleRecurringToggle = (checked: boolean) => { |
| 69 | + if (!checked) { |
| 70 | + onChange({ ...recurrence, frequency: 'once' }) |
| 71 | + return |
| 72 | + } |
| 73 | + onChange({ |
| 74 | + ...recurrence, |
| 75 | + frequency: recurrence.cron ? 'custom' : DEFAULT_RECURRING_FREQUENCY, |
| 76 | + weekdays: [], |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + const handleFrequencyChange = (value: string) => { |
| 81 | + const option = value as FrequencyOption |
| 82 | + switch (option) { |
| 83 | + case 'daily': |
| 84 | + onChange({ ...recurrence, frequency: 'daily', weekdays: [] }) |
| 85 | + return |
| 86 | + case 'weekly': |
| 87 | + onChange({ ...recurrence, frequency: 'weekly', weekdays: [launch.getDay()] }) |
| 88 | + return |
| 89 | + case 'weekdays': |
| 90 | + onChange({ ...recurrence, frequency: 'weekly', weekdays: [...WEEKDAY_PRESET] }) |
| 91 | + return |
| 92 | + case 'monthly': |
| 93 | + onChange({ ...recurrence, frequency: 'monthly', weekdays: [] }) |
| 94 | + return |
| 95 | + case 'custom': |
| 96 | + onChange({ ...recurrence, frequency: 'custom' }) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const handleEndChange = (value: string) => { |
| 101 | + if (value === 'never') onChange({ ...recurrence, end: { type: 'never' } }) |
| 102 | + else if (value === 'on') |
| 103 | + onChange({ ...recurrence, end: { type: 'on', date: format(launch, 'yyyy-MM-dd') } }) |
| 104 | + else { |
| 105 | + const count = recurrence.end.type === 'after' ? recurrence.end.count : DEFAULT_END_AFTER_COUNT |
| 106 | + onChange({ ...recurrence, end: { type: 'after', count } }) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return ( |
| 111 | + <div className='flex flex-col'> |
| 112 | + <div className='h-px bg-[var(--border)]' /> |
| 113 | + <div className='flex flex-col gap-4 px-2 pt-4 pb-4.5'> |
| 114 | + <ChipModalField type='custom' title='Recurring'> |
| 115 | + <Switch checked={isRecurring} onCheckedChange={handleRecurringToggle} /> |
| 116 | + </ChipModalField> |
| 117 | + |
| 118 | + {isRecurring && ( |
| 119 | + <> |
| 120 | + <ChipModalField |
| 121 | + type='dropdown' |
| 122 | + title='Frequency' |
| 123 | + value={frequencyOptionFor(recurrence)} |
| 124 | + options={frequencyOptions} |
| 125 | + onChange={handleFrequencyChange} |
| 126 | + /> |
| 127 | + |
| 128 | + <ChipModalField |
| 129 | + type='dropdown' |
| 130 | + title='Ends' |
| 131 | + value={recurrence.end.type} |
| 132 | + options={[ |
| 133 | + { value: 'never', label: 'No end' }, |
| 134 | + { value: 'on', label: 'Ends on' }, |
| 135 | + { value: 'after', label: 'Ends after' }, |
| 136 | + ]} |
| 137 | + onChange={handleEndChange} |
| 138 | + /> |
| 139 | + |
| 140 | + {recurrence.end.type === 'on' && ( |
| 141 | + <ChipModalField type='custom' title='End date'> |
| 142 | + <ChipDatePicker |
| 143 | + value={recurrence.end.date} |
| 144 | + onChange={(date) => onChange({ ...recurrence, end: { type: 'on', date } })} |
| 145 | + fullWidth |
| 146 | + /> |
| 147 | + </ChipModalField> |
| 148 | + )} |
| 149 | + |
| 150 | + {recurrence.end.type === 'after' && ( |
| 151 | + <ChipModalField |
| 152 | + type='input' |
| 153 | + title='Number of runs' |
| 154 | + value={String(recurrence.end.count)} |
| 155 | + onChange={(value) => { |
| 156 | + const count = Math.max(1, Math.floor(Number(value) || 1)) |
| 157 | + onChange({ ...recurrence, end: { type: 'after', count } }) |
| 158 | + }} |
| 159 | + /> |
| 160 | + )} |
| 161 | + </> |
| 162 | + )} |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + ) |
| 166 | +} |
0 commit comments