Files
OpenFlare/frontend/app/(main)/config-versions/components/cleanup-dialog.tsx
T
ryan 5a8722ff07 feat(frontend): 控制台 next-intl 中英双语
接入无 URL 前缀的 zh-CN/en,顶栏与外观设置可切换语言;选择写入 cookie 后刷新生效。
2026-08-16 16:49:16 +08:00

109 lines
2.9 KiB
TypeScript

'use client';
import { useEffect } from 'react';
import { zodResolver } from '@hookform/resolvers/zod';
import { Loader2 } from 'lucide-react';
import { useTranslations } from 'next-intl';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
function buildCleanupSchema(t: (key: string) => string) {
return z.object({
keepCount: z.number().int().min(3, t('keepMin')),
});
}
type CleanupFormValues = z.infer<ReturnType<typeof buildCleanupSchema>>;
interface CleanupDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onConfirm: (keepCount: number) => void;
loading: boolean;
}
export function CleanupDialog({
open,
onOpenChange,
onConfirm,
loading,
}: CleanupDialogProps) {
const t = useTranslations('configVersions');
const tc = useTranslations('common');
const form = useForm<CleanupFormValues>({
resolver: zodResolver(buildCleanupSchema(t)),
defaultValues: { keepCount: 10 },
});
useEffect(() => {
if (open) {
form.reset({ keepCount: 10 });
}
}, [form, open]);
const handleConfirm = form.handleSubmit((values) => {
onConfirm(values.keepCount);
});
return (
<AlertDialog open={open} onOpenChange={onOpenChange}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t('cleanupTitle')}</AlertDialogTitle>
<AlertDialogDescription>{t('cleanupDesc')}</AlertDialogDescription>
</AlertDialogHeader>
<div className='space-y-2'>
<Label htmlFor='keepCount'>{t('keepCount')}</Label>
<Input
id='keepCount'
type='number'
min={3}
disabled={loading}
{...form.register('keepCount', { valueAsNumber: true })}
/>
<p className='text-xs text-muted-foreground'>
{t('keepHint')}
</p>
{form.formState.errors.keepCount ? (
<p className='text-xs text-destructive'>
{form.formState.errors.keepCount.message}
</p>
) : null}
</div>
<AlertDialogFooter>
<AlertDialogCancel disabled={loading}>{tc('cancel')}</AlertDialogCancel>
<Button
variant='destructive'
onClick={() => void handleConfirm()}
disabled={loading}
>
{loading ? (
<>
<Loader2 className='size-4 animate-spin mr-1' />
{t('cleaning')}
</>
) : (
t('confirmCleanup')
)}
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}