import React from 'react'; import { View, Text, TextInput, Pressable, StyleSheet, Platform } from 'react-native'; import DateTimePicker from '@react-native-community/datetimepicker'; import { ChevronDown } from '../icons'; import { format, parseISO } from 'date-fns'; import type { RecurrenceRule } from '../../api/types'; import { radius, spacing, typography, type ThemePalette } from '../../theme/tokens'; import { useColors } from '../../theme/colors'; import { Button } from '..'; import { useCalendarLocale } from '../../lib/calendar-locale'; import { EDITOR_FREQUENCIES, UNIT_LABEL_KEYS, WEEKDAYS, buildRecurrenceSummary, buildRuleFromEditorValue, capitalize, editorValueFromRule, monthName, nthLabel, weekdayName, type EditorFrequency, type EndsMode, type MonthlyMode, type RecurrenceEditorValue, } from '../../lib/recurrence'; interface RecurrenceEditorProps { rule: RecurrenceRule | null; eventStart: Date; onSave: (rule: RecurrenceRule) => void; onCancel: () => void; } // Small popover-style select, matching the EventModal field pattern. function InlineSelect({ value, options, onChange, flex, }: { value: T; options: { value: T; label: string }[]; onChange: (value: T) => void; flex?: boolean; }) { const c = useColors(); const styles = React.useMemo(() => makeStyles(c), [c]); const [open, setOpen] = React.useState(false); const selected = options.find((o) => o.value === value); return ( setOpen((v) => !v)}> {selected?.label ?? String(value)} {open && ( {options.map((opt) => ( { onChange(opt.value); setOpen(false); }} > {opt.label} ))} )} ); } function NumberInput({ value, min, max, onChange, accessibilityLabel, }: { value: number; min: number; max: number; onChange: (value: number) => void; accessibilityLabel?: string; }) { const c = useColors(); const styles = React.useMemo(() => makeStyles(c), [c]); const [text, setText] = React.useState(String(value)); React.useEffect(() => setText(String(value)), [value]); return ( { const n = parseInt(text, 10); const clamped = Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : min; setText(String(clamped)); onChange(clamped); }} keyboardType="number-pad" style={styles.numberInput} accessibilityLabel={accessibilityLabel} /> ); } export function RecurrenceEditor({ rule, eventStart, onSave, onCancel }: RecurrenceEditorProps) { const c = useColors(); const styles = React.useMemo(() => makeStyles(c), [c]); const { locale, t } = useCalendarLocale(); const [value, setValue] = React.useState(() => editorValueFromRule(rule, eventStart), ); const [showUntilPicker, setShowUntilPicker] = React.useState(false); const update = (patch: Partial) => setValue((prev) => ({ ...prev, ...patch })); const toggleWeekDay = (day: string) => { setValue((prev) => ({ ...prev, weekDays: prev.weekDays.includes(day) ? prev.weekDays.length > 1 ? prev.weekDays.filter((d) => d !== day) : prev.weekDays : [...prev.weekDays, day], })); }; const builtRule = buildRuleFromEditorValue(value, eventStart); const summary = buildRecurrenceSummary(builtRule, t, locale); return ( {t('calendar.recurrence.editor_every', 'Every')} update({ interval })} accessibilityLabel={t('calendar.recurrence.editor_every', 'Every')} /> flex value={value.frequency} options={EDITOR_FREQUENCIES.map((f) => ({ value: f, label: t(UNIT_LABEL_KEYS[f][0], UNIT_LABEL_KEYS[f][1]) }))} onChange={(frequency) => update({ frequency })} /> {value.frequency === 'weekly' && ( {WEEKDAYS.map((day) => { const active = value.weekDays.includes(day); return ( toggleWeekDay(day)} style={[styles.weekdayChip, active && styles.weekdayChipActive]} accessibilityRole="checkbox" accessibilityLabel={weekdayName(day, 'long', locale)} accessibilityState={{ checked: active }} > {weekdayName(day, 'short', locale)} ); })} )} {value.frequency === 'yearly' && ( {capitalize(t('calendar.recurrence.editor_in', 'in'))} flex value={value.month} options={Array.from({ length: 12 }, (_, i) => i + 1).map((m) => ({ value: m, label: capitalize(monthName(m, locale)), }))} onChange={(month) => update({ month })} /> )} {(value.frequency === 'monthly' || value.frequency === 'yearly') && ( value={value.monthlyMode} options={[ { value: 'day', label: capitalize(t('calendar.recurrence.editor_on_day', 'on day')) }, { value: 'nth', label: capitalize(t('calendar.recurrence.editor_on_the', 'on the')) }, ]} onChange={(monthlyMode) => update({ monthlyMode })} /> {value.monthlyMode === 'day' ? ( update({ monthDay })} accessibilityLabel={t('calendar.recurrence.editor_on_day', 'on day')} /> ) : ( <> flex value={value.nth} options={[1, 2, 3, 4, -1].map((n) => ({ value: n, label: capitalize(nthLabel(n, t)) }))} onChange={(nth) => update({ nth })} /> flex value={value.nthDay} options={WEEKDAYS.map((d) => ({ value: d, label: capitalize(weekdayName(d, 'long', locale)) }))} onChange={(nthDay) => update({ nthDay })} /> )} )} {t('calendar.recurrence.editor_ends', 'Ends')} flex={value.endsMode === 'never'} value={value.endsMode} options={[ { value: 'never', label: t('calendar.recurrence.editor_never', 'Never') }, { value: 'on', label: t('calendar.recurrence.editor_ends_on', 'On') }, { value: 'after', label: t('calendar.recurrence.editor_ends_after', 'After') }, ]} onChange={(endsMode) => update({ endsMode })} /> {value.endsMode === 'on' && ( setShowUntilPicker(true)}> {format(parseISO(value.untilDate), 'MMM d, yyyy', { locale })} )} {value.endsMode === 'after' && ( <> update({ count })} accessibilityLabel={t('calendar.recurrence.editor_ends_after', 'After')} /> {t('calendar.recurrence.editor_occurrences_count', '{count, plural, one {occurrence} other {occurrences}}', { count: value.count })} )} {summary} {showUntilPicker && ( { setShowUntilPicker(false); if (d) update({ untilDate: format(d, 'yyyy-MM-dd') }); }} /> )} ); } function makeStyles(c: ThemePalette) { return StyleSheet.create({ container: { borderWidth: 1, borderColor: c.border, borderRadius: radius.sm, backgroundColor: c.surface, padding: spacing.md, gap: spacing.sm, }, row: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm }, rowLabel: { ...typography.body, color: c.textSecondary }, muted: { ...typography.caption, color: c.textMuted, flexShrink: 1 }, numberInput: { minWidth: 52, paddingHorizontal: spacing.sm, paddingVertical: spacing.xs, borderRadius: radius.xs, borderWidth: 1, borderColor: c.border, backgroundColor: c.background, color: c.text, ...typography.body, textAlign: 'center', }, selectFlex: { flex: 1, minWidth: 0 }, selectButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', gap: spacing.xs, paddingHorizontal: spacing.sm, paddingVertical: spacing.xs, minHeight: 36, borderRadius: radius.xs, borderWidth: 1, borderColor: c.border, backgroundColor: c.background, }, selectText: { ...typography.body, color: c.text, flexShrink: 1 }, popover: { marginTop: spacing.xs, borderWidth: 1, borderColor: c.border, borderRadius: radius.xs, backgroundColor: c.background, overflow: 'hidden', }, popoverRow: { paddingHorizontal: spacing.md, paddingVertical: spacing.sm, borderBottomWidth: 1, borderBottomColor: c.borderLight, }, popoverRowActive: { backgroundColor: c.primaryBg }, popoverRowText: { ...typography.body, color: c.text }, weekdayRow: { flexDirection: 'row', gap: 4 }, weekdayChip: { flex: 1, minWidth: 0, paddingVertical: spacing.xs, borderRadius: radius.xs, borderWidth: 1, borderColor: c.border, alignItems: 'center', }, weekdayChipActive: { borderColor: c.primary, backgroundColor: c.primaryBg, }, weekdayChipText: { ...typography.caption, color: c.textMuted }, weekdayChipTextActive: { color: c.primary }, footer: { borderTopWidth: 1, borderTopColor: c.border, paddingTop: spacing.sm, gap: spacing.sm, }, summary: { ...typography.caption, color: c.textMuted }, footerButtons: { flexDirection: 'row', justifyContent: 'flex-end', gap: spacing.sm }, }); }