import React, { useEffect } from 'react'; import { Trans, useTranslation } from 'react-i18next'; import i18next from 'i18next'; import { Controller, useForm } from 'react-hook-form'; import { STATS_INTERVALS_DAYS, DAY, RETENTION_CUSTOM, RETENTION_RANGE } from '../../../helpers/constants'; import '../FormButton.css'; import { Checkbox } from '../../ui/Controls/Checkbox'; import { Input } from '../../ui/Controls/Input'; import { toNumber } from '../../../helpers/form'; import { Textarea } from '../../ui/Controls/Textarea'; const getIntervalTitle = (interval: any) => { switch (interval) { case RETENTION_CUSTOM: return i18next.t('settings_custom'); case DAY: return i18next.t('interval_24_hour'); default: return i18next.t('interval_days', { count: interval / DAY }); } }; export type FormValues = { enabled: boolean; interval: number; customInterval?: number | null; ignored: string; }; const defaultFormValues = { enabled: false, interval: DAY, customInterval: null, ignored: '', }; type Props = { initialValues: FormValues; processing: boolean; processingReset: boolean; onSubmit: (values: FormValues) => void; onReset: () => void; }; export const Form = ({ initialValues, processing, processingReset, onSubmit, onReset }: Props) => { const { t } = useTranslation(); const { handleSubmit, watch, setValue, control, formState: { isSubmitting }, } = useForm({ mode: 'onBlur', defaultValues: { ...defaultFormValues, ...initialValues, }, }); const intervalValue = watch('interval'); const customIntervalValue = watch('customInterval'); useEffect(() => { if (STATS_INTERVALS_DAYS.includes(intervalValue)) { setValue('customInterval', null); } }, [intervalValue]); const onSubmitForm = (data: FormValues) => { onSubmit(data); }; const disableSubmit = isSubmitting || processing || (intervalValue === RETENTION_CUSTOM && !customIntervalValue); return (
( )} />
statistics_retention
statistics_retention_desc
{!STATS_INTERVALS_DAYS.includes(intervalValue) && (
{i18next.t('custom_retention_input')}
( { const { value } = e.target; field.onChange(toNumber(value)); }} /> )} />
)} {STATS_INTERVALS_DAYS.map((interval) => ( ))}
ignore_domains_title
ignore_domains_desc_stats
(