import React, { useEffect } from 'react'; import { Trans, useTranslation } from 'react-i18next'; import i18next from 'i18next'; import { Controller, useForm } from 'react-hook-form'; import { trimLinesAndRemoveEmpty } from '../../../helpers/helpers'; import { QUERY_LOG_INTERVALS_DAYS, HOUR, DAY, RETENTION_CUSTOM, RETENTION_RANGE, CUSTOM_INTERVAL, } from '../../../helpers/constants'; import '../FormButton.css'; import { Checkbox } from '../../ui/Controls/Checkbox'; const getIntervalTitle = (interval: number) => { switch (interval) { case RETENTION_CUSTOM: return i18next.t('settings_custom'); case 6 * HOUR: return i18next.t('interval_6_hour'); case DAY: return i18next.t('interval_24_hour'); default: return i18next.t('interval_days', { count: interval / DAY }); } }; export type FormValues = { enabled: boolean; anonymize_client_ip: 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, anonymize_client_ip: initialValues.anonymize_client_ip || false, interval: initialValues.interval || DAY, customInterval: initialValues.customInterval || null, ignored: initialValues.ignored || '', }, }); const intervalValue = watch('interval'); const customIntervalValue = watch('customInterval'); useEffect(() => { if (QUERY_LOG_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 (
( onChange(value)} disabled={processing} /> )} />
( onChange(value)} disabled={processing} /> )} />
query_log_retention
{!QUERY_LOG_INTERVALS_DAYS.includes(intervalValue) && (
{i18next.t('custom_rotation_input')}
{ setValue('customInterval', parseInt(e.target.value, 10)); }} />
)} {QUERY_LOG_INTERVALS_DAYS.map((interval) => ( ))}
ignore_domains_desc_query