import { useEffect } from 'react' import * as z from 'zod' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { useTranslation } from 'react-i18next' import { Button } from '@/components/ui/button' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Input } from '@/components/ui/input' import { Switch } from '@/components/ui/switch' import { SettingsSection } from '../components/settings-section' import { useUpdateOption } from '../hooks/use-update-option' const botProtectionSchema = z.object({ TurnstileCheckEnabled: z.boolean(), TurnstileSiteKey: z.string().optional(), TurnstileSecretKey: z.string().optional(), }) type BotProtectionFormValues = z.infer type BotProtectionSectionProps = { defaultValues: BotProtectionFormValues } export function BotProtectionSection({ defaultValues, }: BotProtectionSectionProps) { const { t } = useTranslation() const updateOption = useUpdateOption() const form = useForm({ resolver: zodResolver(botProtectionSchema), defaultValues, }) useEffect(() => { form.reset(defaultValues) }, [defaultValues, form]) const onSubmit = async (data: BotProtectionFormValues) => { const updates = Object.entries(data).filter( ([key, value]) => value !== defaultValues[key as keyof BotProtectionFormValues] ) for (const [key, value] of updates) { await updateOption.mutateAsync({ key, value: value ?? '' }) } } return (
(
{t('Enable Turnstile')} {t( 'Protect login and registration with Cloudflare Turnstile' )}
)} /> ( {t('Site Key')} )} /> ( {t('Secret Key')} )} />
) }