import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { GenericModal } from "../ui/GenericModal"; import { MenuItem, TextField, Typography, Box } from "@mui/material"; import { Dropdown } from "../ui/DropdownMui"; import Button from "@mui/material/Button"; import { validMomentTimezones } from '../../constants/const'; import { useGlobalInfoStore } from '../../context/globalInfo'; import { getSchedule, deleteSchedule } from '../../api/storage'; interface ScheduleSettingsProps { isOpen: boolean; handleStart: (settings: ScheduleSettings) => Promise; handleClose: () => void; initialSettings?: ScheduleSettings | null; } export interface ScheduleSettings { runEvery: number; runEveryUnit: string; startFrom: string; dayOfMonth?: string; atTimeStart?: string; atTimeEnd?: string; timezone: string; } export const ScheduleSettingsModal = ({ isOpen, handleStart, handleClose, initialSettings }: ScheduleSettingsProps) => { const { t } = useTranslation(); const [schedule, setSchedule] = useState(null); const [settings, setSettings] = useState({ runEvery: 1, runEveryUnit: 'HOURS', startFrom: 'MONDAY', dayOfMonth: '1', atTimeStart: '00:00', atTimeEnd: '01:00', timezone: 'UTC' }); useEffect(() => { if (initialSettings) { setSettings(initialSettings); } }, [initialSettings]); const handleChange = (field: keyof ScheduleSettings, value: string | number | boolean) => { setSettings(prev => ({ ...prev, [field]: value })); }; const textStyle = { width: '150px', height: '52px', marginRight: '10px', }; const dropDownStyle = { marginTop: '2px', width: '150px', height: '59px', marginRight: '10px', }; const units = [ 'MINUTES', 'HOURS', 'DAYS', 'WEEKS', 'MONTHS' ]; const days = [ 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY' ]; const { recordingId, notify } = useGlobalInfoStore(); const deleteRobotSchedule = () => { if (recordingId) { deleteSchedule(recordingId); setSchedule(null); notify('success', t('Schedule deleted successfully')); } else { console.error('No recording id provided'); } setSettings({ runEvery: 1, runEveryUnit: 'HOURS', startFrom: 'MONDAY', dayOfMonth: '', atTimeStart: '00:00', atTimeEnd: '01:00', timezone: 'UTC' }); }; const getRobotSchedule = async () => { if (recordingId) { const scheduleData = await getSchedule(recordingId); setSchedule(scheduleData); } else { console.error('No recording id provided'); } } useEffect(() => { if (isOpen) { const fetchSchedule = async () => { await getRobotSchedule(); }; fetchSchedule(); } }, [isOpen]); const getDayOrdinal = (day: string | undefined) => { if (!day) return ''; const lastDigit = day.slice(-1); const lastTwoDigits = day.slice(-2); // Special cases for 11, 12, 13 if (['11', '12', '13'].includes(lastTwoDigits)) { return t('schedule_settings.labels.on_day.th'); } // Other cases switch (lastDigit) { case '1': return t('schedule_settings.labels.on_day.st'); case '2': return t('schedule_settings.labels.on_day.nd'); case '3': return t('schedule_settings.labels.on_day.rd'); default: return t('schedule_settings.labels.on_day.th'); } }; return ( *': { marginBottom: '20px' }, }}> {t('schedule_settings.title')} <> {schedule !== null ? ( <> {t('schedule_settings.run_every')}: {schedule.runEvery} {schedule.runEveryUnit.toLowerCase()} {['MONTHS', 'WEEKS'].includes(settings.runEveryUnit) ? t('schedule_settings.start_from') : t('schedule_settings.start_from')}: {schedule.startFrom.charAt(0).toUpperCase() + schedule.startFrom.slice(1).toLowerCase()} {schedule.runEveryUnit === 'MONTHS' && ( {t('schedule_settings.on_day')}: {schedule.dayOfMonth}{getDayOrdinal(schedule.dayOfMonth)} of the month )} {t('schedule_settings.at_around')}: {schedule.atTimeStart}, {schedule.timezone} {t('schedule_settings.timezone')} ) : ( <> {t('schedule_settings.labels.run_once_every')} handleChange('runEvery', parseInt(e.target.value))} sx={textStyle} inputProps={{ min: 1 }} /> handleChange('runEveryUnit', e.target.value)} sx={dropDownStyle} > {units.map((unit) => ( {unit.charAt(0).toUpperCase() + unit.slice(1).toLowerCase()} ))} {['MONTHS', 'WEEKS'].includes(settings.runEveryUnit) ? t('schedule_settings.labels.start_from_label') : t('schedule_settings.labels.start_from_label')} handleChange('startFrom', e.target.value)} sx={dropDownStyle} > {days.map((day) => ( {day.charAt(0).toUpperCase() + day.slice(1).toLowerCase()} ))} {settings.runEveryUnit === 'MONTHS' && ( {t('schedule_settings.labels.on_day_of_month')} handleChange('dayOfMonth', e.target.value)} sx={textStyle} inputProps={{ min: 1, max: 31 }} /> )} {['MINUTES', 'HOURS'].includes(settings.runEveryUnit) ? ( {t('schedule_settings.labels.in_between')} handleChange('atTimeStart', e.target.value)} sx={textStyle} /> handleChange('atTimeEnd', e.target.value)} sx={textStyle} /> ) : ( {t('schedule_settings.at_around')} handleChange('atTimeStart', e.target.value)} sx={textStyle} /> )} {t('schedule_settings.timezone')} handleChange('timezone', e.target.value)} sx={dropDownStyle} > {validMomentTimezones.map((tz) => ( {tz.charAt(0).toUpperCase() + tz.slice(1).toLowerCase()} ))} )} ); }; const modalStyle = { top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: '40%', backgroundColor: 'background.paper', p: 4, height: 'fit-content', display: 'block', padding: '20px', };