Files
new-api/web/default/src/features/system-settings/auth/bot-protection-section.tsx
T
t0ng7u d146e45e2f ⚖️ chore(web/default): add reusable copyright header tooling
Add a Bun script to apply and normalize AGPL copyright headers across the default frontend source files.

The script keeps headers idempotent, upgrades existing headers to the 2023-2026 QuantumNous range, and is exposed through `bun run copyright` for future maintenance.
2026-05-09 11:35:07 +08:00

160 lines
4.8 KiB
TypeScript
Vendored

/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
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<typeof botProtectionSchema>
type BotProtectionSectionProps = {
defaultValues: BotProtectionFormValues
}
export function BotProtectionSection({
defaultValues,
}: BotProtectionSectionProps) {
const { t } = useTranslation()
const updateOption = useUpdateOption()
const form = useForm<BotProtectionFormValues>({
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 (
<SettingsSection
title={t('Bot Protection')}
description={t(
'Protect login and registration with Cloudflare Turnstile'
)}
>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className='space-y-6'
autoComplete='off'
>
<FormField
control={form.control}
name='TurnstileCheckEnabled'
render={({ field }) => (
<FormItem className='flex flex-row items-center justify-between rounded-lg border p-4'>
<div className='space-y-0.5'>
<FormLabel className='text-base'>
{t('Enable Turnstile')}
</FormLabel>
<FormDescription>
{t(
'Protect login and registration with Cloudflare Turnstile'
)}
</FormDescription>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name='TurnstileSiteKey'
render={({ field }) => (
<FormItem>
<FormLabel>{t('Site Key')}</FormLabel>
<FormControl>
<Input
placeholder={t('Your Turnstile site key')}
autoComplete='off'
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name='TurnstileSecretKey'
render={({ field }) => (
<FormItem>
<FormLabel>{t('Secret Key')}</FormLabel>
<FormControl>
<Input
type='password'
placeholder={t('Your Turnstile secret key')}
autoComplete='new-password'
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type='submit' disabled={updateOption.isPending}>
{updateOption.isPending ? t('Saving...') : t('Save Changes')}
</Button>
</form>
</Form>
</SettingsSection>
)
}