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, CUSTOM_INTERVAL, RETENTION_RANGE, } from '../../../helpers/constants'; import { trimLinesAndRemoveEmpty } from '../../../helpers/helpers'; import '../FormButton.css'; import { Checkbox } from '../../ui/Controls/Checkbox'; 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; }; type Props = { initialValues: Partial; processing: boolean; processingReset: boolean; onSubmit: (values: FormValues) => void; onReset: () => void; }; export const Form = ({ initialValues, processing, processingReset, onSubmit, onReset }: Props) => { const { t } = useTranslation(); const { register, handleSubmit, watch, setValue, control, formState: { isSubmitting }, } = useForm({ mode: 'onChange', defaultValues: { enabled: initialValues.enabled || false, interval: initialValues.interval || DAY, customInterval: initialValues.customInterval || null, ignored: initialValues.ignored || '', }, }); 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 handleIgnoredBlur = (e: React.FocusEvent) => { const trimmed = trimLinesAndRemoveEmpty(e.target.value); setValue('ignored', trimmed); }; const disableSubmit = isSubmitting || processing || (intervalValue === RETENTION_CUSTOM && !customIntervalValue); return (
} />
statistics_retention
statistics_retention_desc
{!STATS_INTERVALS_DAYS.includes(intervalValue) && (
{i18next.t('custom_retention_input')}
{/* */} { setValue('customInterval', parseInt(e.target.value, 10)); }} />
)} {STATS_INTERVALS_DAYS.map((interval) => ( // ))}
ignore_domains_title
ignore_domains_desc_stats
{/* */}