1 import { useMemo } from 'react';
3 import { c } from 'ttag';
5 import { WEEKLY_TYPE } from '@proton/shared/lib/calendar/constants';
6 import type { WeekStartsOn } from '@proton/shared/lib/date-fns-utc/interface';
7 import { getFormattedWeekdays } from '@proton/shared/lib/date/date';
8 import { dateLocale } from '@proton/shared/lib/i18n';
9 import type { DateTimeModel, FrequencyModel } from '@proton/shared/lib/interfaces/calendar';
10 import clsx from '@proton/utils/clsx';
12 import DayCheckbox from '../inputs/DayCheckbox';
14 const DAYS = Array.from({ length: 7 }, (a, i) => i);
17 frequencyModel: FrequencyModel;
19 weekStartsOn: WeekStartsOn;
20 onChange: (value: FrequencyModel) => void;
21 displayStacked?: boolean;
23 const RepeatOnRow = ({ frequencyModel, start, weekStartsOn, onChange, displayStacked = false }: Props) => {
24 const [weekdaysLong, weekdaysAbbreviations] = useMemo(() => {
25 return ['cccc', 'cccccc'].map((format) => getFormattedWeekdays(format, { locale: dateLocale }));
27 const currentDay = start.date.getDay();
29 const handleToggleDay = (day: number) => {
30 if (day === currentDay) {
31 // do not allow to toggle current day
34 const selectedDays = frequencyModel.weekly.days || [currentDay];
35 if (selectedDays.includes(day)) {
36 const newDays = selectedDays.filter((selectedDay) => selectedDay !== day);
37 return onChange({ ...frequencyModel, weekly: { type: WEEKLY_TYPE.ON_DAYS, days: newDays } });
39 onChange({ ...frequencyModel, weekly: { type: WEEKLY_TYPE.ON_DAYS, days: selectedDays.concat(day).sort() } });
43 <div className={clsx('mb-2 ml-0', !displayStacked && 'md:ml-2')}>
44 <label className={clsx(displayStacked && 'text-semibold')} id="label-event-weekly-repeat">{c('Label')
45 .t`Repeat on`}</label>
46 <div className="flex gap-2">
47 {DAYS.map((dayIndex) => {
48 const day = (dayIndex + weekStartsOn) % 7;
49 const dayLong = weekdaysLong[day];
50 const dayAbbreviation = weekdaysAbbreviations[day];
51 const checked = frequencyModel.weekly.days.includes(day);
57 dayAbbreviation={dayAbbreviation}
58 dayLong={day === currentDay ? c('Tooltip').t`Change start date to unselect` : dayLong}
59 onChange={() => handleToggleDay(day)}
60 aria-describedby="label-event-weekly-repeat"
61 disabled={day === currentDay}
70 export default RepeatOnRow;