import type { SelectOption } from '@rocket.chat/fuselage'; import { InputBox, Field, MultiSelect, FieldGroup, Box, Select, FieldLabel, FieldRow, Callout } from '@rocket.chat/fuselage'; import type { TranslationKey } from '@rocket.chat/ui-contexts'; import { useId, useMemo } from 'react'; import { useFormContext, Controller, useFieldArray } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { useTimezoneNameList } from '../../../hooks/useTimezoneNameList'; import { BusinessHoursMultiple } from '../additionalForms'; import { defaultWorkHours, DAYS_OF_WEEK } from './mapBusinessHoursForm'; type mappedDayTime = { day: string; start: { time: string; }; finish: { time: string; }; open: boolean; }; export type BusinessHoursFormData = { name: string; timezoneName: string; daysOpen: string[]; daysTime: mappedDayTime[]; departmentsToApplyBusinessHour: string; active: boolean; departments: { value: string; label: string; }[]; }; // TODO: replace `Select` in favor `SelectFiltered` // TODO: add time validation for start and finish not be equal on UI // TODO: add time validation for start not be higher than finish on UI export type BusinessHoursFormProps = { type?: 'default' | 'custom' }; const BusinessHoursForm = ({ type }: BusinessHoursFormProps) => { const { t } = useTranslation(); const timeZones = useTimezoneNameList(); const timeZonesOptions: SelectOption[] = useMemo(() => timeZones.map((name) => [name, t(name as TranslationKey)]), [t, timeZones]); const daysOptions: SelectOption[] = useMemo(() => DAYS_OF_WEEK.map((day) => [day, t(day as TranslationKey)]), [t]); const { watch, control } = useFormContext(); const { daysTime } = watch(); const { fields: daysTimeFields, replace } = useFieldArray({ control, name: 'daysTime' }); const timezoneField = useId(); const daysOpenField = useId(); const daysTimeField = useId(); const handleChangeDaysTime = (values: string[]) => { const newValues = values .map((item) => daysTime.find(({ day }) => day === item) || defaultWorkHours(true).find(({ day }) => day === item)) .filter((item): item is mappedDayTime => Boolean(item)); replace(newValues); }; return ( {type === 'custom' && } {t('Timezone')}