mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-13 02:26:36 +08:00
feat(frontend): 控制台 next-intl 中英双语
接入无 URL 前缀的 zh-CN/en,顶栏与外观设置可切换语言;选择写入 cookie 后刷新生效。
This commit is contained in:
@@ -47,6 +47,8 @@ import {
|
||||
} from '@/lib/openflare/origin-error-page-templates';
|
||||
import { OptionService } from '@/lib/services/openflare';
|
||||
|
||||
import { useTranslations } from 'next-intl';
|
||||
|
||||
import {
|
||||
invalidateResponseQueries,
|
||||
KEY_HTML,
|
||||
@@ -56,6 +58,8 @@ import {
|
||||
} from '../../components/shared';
|
||||
|
||||
export default function ErrorPageEditPage() {
|
||||
const t = useTranslations('responses');
|
||||
const tc = useTranslations('common');
|
||||
const { user, loading: authLoading } = useAuth();
|
||||
const queryClient = useQueryClient();
|
||||
const [html, setHtml] = useState('');
|
||||
@@ -79,33 +83,33 @@ export default function ErrorPageEditPage() {
|
||||
mutationFn: async () => {
|
||||
const bytes = new TextEncoder().encode(html).length;
|
||||
if (bytes > ORIGIN_ERROR_PAGE_HTML_MAX_BYTES) {
|
||||
throw new Error('HTML 大小超出限制(最大 256KB)');
|
||||
throw new Error(t('htmlTooLarge'));
|
||||
}
|
||||
await OptionService.updateBatch([{ key: KEY_HTML, value: html }]);
|
||||
},
|
||||
onSuccess: async () => {
|
||||
toast.success('源站错误页 HTML 已保存,请前往版本发布使配置生效');
|
||||
toast.success(t('errorHtmlSaved'));
|
||||
await invalidateResponseQueries(queryClient);
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(error instanceof Error ? error.message : '保存失败');
|
||||
toast.error(error instanceof Error ? error.message : t('saveFailed'));
|
||||
},
|
||||
});
|
||||
|
||||
const loadSelectedTemplate = () => {
|
||||
const tmpl = getOriginErrorPageTemplate(templateId);
|
||||
if (!tmpl) {
|
||||
toast.error('未找到所选模板');
|
||||
toast.error(t('templateNotFound'));
|
||||
return;
|
||||
}
|
||||
setHtml(tmpl.html);
|
||||
toast.success(`已加载模板「${tmpl.name}」到编辑器`);
|
||||
toast.success(t('templateLoaded', { name: tmpl.name }));
|
||||
};
|
||||
|
||||
const restoreDefault = () => {
|
||||
setHtml('');
|
||||
setRestoreOpen(false);
|
||||
toast.success('已清空为使用服务端内置默认模板(需保存后生效)');
|
||||
toast.success(t('errorRestored'));
|
||||
};
|
||||
|
||||
if (authLoading) {
|
||||
@@ -113,7 +117,7 @@ export default function ErrorPageEditPage() {
|
||||
<div className='w-full py-6 px-1'>
|
||||
<LoadingStateWithBorder
|
||||
icon={FileCode2}
|
||||
description='加载权限信息...'
|
||||
description={t('loadingPermission')}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
@@ -124,8 +128,8 @@ export default function ErrorPageEditPage() {
|
||||
<div className='w-full py-6 px-1'>
|
||||
<EmptyStateWithBorder
|
||||
icon={FileCode2}
|
||||
title='权限不足'
|
||||
description='只有管理员可以编辑错误页 HTML。'
|
||||
title={t('forbidden')}
|
||||
description={t('forbiddenEditError')}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
@@ -136,7 +140,7 @@ export default function ErrorPageEditPage() {
|
||||
<div className='w-full py-6 px-1'>
|
||||
<LoadingStateWithBorder
|
||||
icon={FileCode2}
|
||||
description='加载错误页配置...'
|
||||
description={t('loadingErrorConfig')}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
@@ -149,7 +153,7 @@ export default function ErrorPageEditPage() {
|
||||
message={
|
||||
optionsQuery.error instanceof Error
|
||||
? optionsQuery.error.message
|
||||
: '加载失败'
|
||||
: t('loadFailed')
|
||||
}
|
||||
onRetry={() => void optionsQuery.refetch()}
|
||||
/>
|
||||
@@ -162,7 +166,7 @@ export default function ErrorPageEditPage() {
|
||||
<div className='flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between'>
|
||||
<div className='flex items-center gap-3'>
|
||||
<Button variant='outline' size='icon' className='h-8 w-8' asChild>
|
||||
<Link href='/responses?tab=error' aria-label='返回错误页'>
|
||||
<Link href='/responses?tab=error' aria-label={t('backToError')}>
|
||||
<ArrowLeft className='size-4' />
|
||||
</Link>
|
||||
</Button>
|
||||
@@ -170,7 +174,7 @@ export default function ErrorPageEditPage() {
|
||||
<FileCode2 className='size-5 text-primary' />
|
||||
<div>
|
||||
<h1 className='text-2xl font-semibold tracking-tight'>
|
||||
编辑错误页 HTML
|
||||
{t('editErrorHtml')}
|
||||
</h1>
|
||||
</div>
|
||||
</div>
|
||||
@@ -185,11 +189,11 @@ export default function ErrorPageEditPage() {
|
||||
<>
|
||||
<div className='flex items-center gap-1.5'>
|
||||
<span className='text-[11px] text-muted-foreground hidden sm:inline'>
|
||||
内置模板
|
||||
{t('builtinTemplates')}
|
||||
</span>
|
||||
<Select value={templateId} onValueChange={setTemplateId}>
|
||||
<SelectTrigger className='h-7 w-[160px] text-[11px] bg-background'>
|
||||
<SelectValue placeholder='选择模板' />
|
||||
<SelectValue placeholder={t('selectTemplate')} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{ORIGIN_ERROR_PAGE_TEMPLATES.map((tmpl) => (
|
||||
@@ -211,7 +215,7 @@ export default function ErrorPageEditPage() {
|
||||
onClick={loadSelectedTemplate}
|
||||
>
|
||||
<Sparkles className='size-3.5' />
|
||||
加载模板
|
||||
{t('loadTemplate')}
|
||||
</Button>
|
||||
</div>
|
||||
<Button
|
||||
@@ -222,7 +226,7 @@ export default function ErrorPageEditPage() {
|
||||
onClick={() => setRestoreOpen(true)}
|
||||
>
|
||||
<RotateCcw className='size-3.5' />
|
||||
恢复默认
|
||||
{t('restoreDefault')}
|
||||
</Button>
|
||||
<Button
|
||||
size='sm'
|
||||
@@ -235,7 +239,7 @@ export default function ErrorPageEditPage() {
|
||||
) : (
|
||||
<Save className='size-3' />
|
||||
)}
|
||||
保存
|
||||
{tc('save')}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
@@ -244,15 +248,15 @@ export default function ErrorPageEditPage() {
|
||||
<AlertDialog open={restoreOpen} onOpenChange={setRestoreOpen}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>恢复默认 HTML?</AlertDialogTitle>
|
||||
<AlertDialogTitle>{t('restoreTitle')}</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
将清空编辑器内容,保存后使用服务端内置默认模板。此操作不会自动保存。
|
||||
{t('restoreErrorDesc')}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>取消</AlertDialogCancel>
|
||||
<AlertDialogCancel>{tc('cancel')}</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={restoreDefault}>
|
||||
确认恢复
|
||||
{t('confirmRestore')}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
|
||||
Reference in New Issue
Block a user