From e040595ba19b050044229561396ab2c389bd5035 Mon Sep 17 00:00:00 2001 From: Robbs Luo Date: Wed, 2 Dec 2026 22:18:04 +0800 Subject: [PATCH] =?UTF-8?q?fix=20COD-41=EF=BC=9A=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E4=B8=8E=E5=85=A8=E5=B1=80=E6=A1=86=E6=9E=B6=E8=8B=B1=E6=96=87?= =?UTF-8?q?=E6=96=87=E6=A1=88=E8=A1=A5=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/App.tsx | 16 ++++-- web/src/components/ErrorBoundary.tsx | 76 +++++++++++++++------------- web/src/components/PageSkeleton.tsx | 4 +- web/src/components/Pagination.tsx | 16 +++--- web/src/components/ThemeToggle.tsx | 4 +- web/src/components/Toast.tsx | 4 +- web/src/components/UserMenu.tsx | 18 +++++-- web/src/i18n/__tests__/i18n.test.tsx | 5 ++ web/src/i18n/messages.ts | 48 ++++++++++++++++++ web/src/ui/index.tsx | 18 ++++--- 10 files changed, 146 insertions(+), 63 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index 9a32f0b0..8eea83fd 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -8,6 +8,7 @@ import { ChangePasswordDialog } from "./components/ChangePasswordDialog"; import { FirstRunWizard } from "./components/FirstRunWizard"; import { ActivatePage } from "./pages/ActivatePage"; import { LoginPage } from "./pages/LoginPage"; +import { useI18n } from "./i18n"; // 路由级懒加载:recharts / 大页面不进首屏 chunk(900KB 单 chunk → 拆包) const CredentialsPage = lazy(() => import("./pages/CredentialsPage").then(m => ({ default: m.CredentialsPage }))); @@ -24,9 +25,14 @@ const LogsPage = lazy(() => import("./pages/LogsPage").then(m => ({ default: m.L const UpstreamStatusPage = lazy(() => import("./pages/UpstreamStatusPage").then(m => ({ default: m.UpstreamStatusPage }))); const BillingPage = lazy(() => import("./pages/BillingPage").then(m => ({ default: m.BillingPage }))); -const LOADING = ( -
载入中…
-); +function Loading() { + const { t } = useI18n(); + return ( +
+ {t("common.loading")} +
+ ); +} /** * admin-only 页面的路由守卫:非 admin 直接送回首页。 @@ -68,7 +74,7 @@ export function App() { }, [queryClient]); if (session.isLoading) { - return LOADING; + return ; } if (!session.data) { @@ -91,7 +97,7 @@ export function App() { return ( <> - + }> } /> }> diff --git a/web/src/components/ErrorBoundary.tsx b/web/src/components/ErrorBoundary.tsx index 5b6a2006..771b0856 100644 --- a/web/src/components/ErrorBoundary.tsx +++ b/web/src/components/ErrorBoundary.tsx @@ -1,10 +1,49 @@ import { Component, type ErrorInfo, type ReactNode } from "react"; import { RotateCcw, TriangleAlert } from "lucide-react"; import { Button } from "./ui/button"; +import { useI18n } from "../i18n"; type Props = { children: ReactNode }; type State = { error: Error | null }; +/** 失败面板(函数组件以便读 i18n;仍由上方 class 边界捕获)。 */ +function ErrorFallback({ error }: { error: Error }) { + const { t } = useI18n(); + return ( +
+
+
+ + {t("common.errorBoundaryTitle")} +
+

{t("common.errorBoundaryDesc")}

+
+          {error.message || String(error)}
+        
+
+ + +
+
+
+ ); +} + /** * 渲染错误兜底:任何页面组件抛错时给一页可读的失败面板,而不是白屏。 * @@ -30,41 +69,6 @@ export class ErrorBoundary extends Component { render(): ReactNode { const { error } = this.state; if (!error) return this.props.children; - return ( -
-
-
- - 页面出错了 -
-

- 这个页面渲染时遇到异常,其他功能不受影响。可以重载页面重试;若反复出现, - 请把下面的错误信息与浏览器控制台日志一并反馈。 -

-
-            {error.message || String(error)}
-          
-
- - -
-
-
- ); + return ; } } diff --git a/web/src/components/PageSkeleton.tsx b/web/src/components/PageSkeleton.tsx index 4ebbc4e7..64f998dc 100644 --- a/web/src/components/PageSkeleton.tsx +++ b/web/src/components/PageSkeleton.tsx @@ -1,5 +1,6 @@ import { Skeleton } from "../ui"; import { cn } from "@/lib/utils"; +import { useI18n } from "../i18n"; /** * 页面加载骨架:替代纯文字「载入中…」,让布局在数据到达前就稳定, @@ -16,9 +17,10 @@ export function PageSkeleton({ rows?: number; className?: string; }) { + const { t } = useI18n(); return (
- 载入中… + {t("common.loading")} {/* 页头占位 */}
diff --git a/web/src/components/Pagination.tsx b/web/src/components/Pagination.tsx index 15be346d..d27f8726 100644 --- a/web/src/components/Pagination.tsx +++ b/web/src/components/Pagination.tsx @@ -1,6 +1,7 @@ import { ChevronLeft, ChevronRight } from "lucide-react"; import { Button, Select } from "../ui"; import { DEFAULT_PAGE_SIZE, PAGE_SIZE_OPTIONS } from "../lib/constants"; +import { useI18n } from "../i18n"; /** * 全站统一的分页控件。 @@ -50,6 +51,7 @@ export function Pagination({ busy?: boolean; className?: string; }) { + const { t } = useI18n(); // 页数优先用显式 pageCount;否则由 total 现算;都没有 = 未知(游标模式) const resolvedCount = pageCount ?? (total !== null && total !== undefined @@ -58,8 +60,10 @@ export function Pagination({ const next = hasNext ?? (resolvedCount !== null ? page < resolvedCount - 1 : false); const info = resolvedCount !== null - ? `第 ${page + 1} / ${resolvedCount} 页` - : `第 ${page + 1} 页`; + ? t("pagination.pageInfo") + .replace("{page}", String(page + 1)) + .replace("{total}", String(resolvedCount)) + : t("pagination.pageInfoUnknown").replace("{page}", String(page + 1)); return (
{info} {onPageSizeChange && ( @@ -93,7 +97,7 @@ export function Pagination({ onClick={() => onPageChange(Math.max(0, page - 1))} > - 上一页 + {t("pagination.prev")}
diff --git a/web/src/components/ThemeToggle.tsx b/web/src/components/ThemeToggle.tsx index 486e658b..929e1824 100644 --- a/web/src/components/ThemeToggle.tsx +++ b/web/src/components/ThemeToggle.tsx @@ -1,9 +1,11 @@ import { useState } from "react"; import { Moon, Sun } from "lucide-react"; import { Button } from "../ui"; +import { useI18n } from "../i18n"; /** 明暗主题切换:切换 .dark 并持久化到 localStorage(首次跟随系统)。 */ export function ThemeToggle() { + const { t } = useI18n(); const [dark, setDark] = useState(() => document.documentElement.classList.contains("dark"), ); @@ -20,7 +22,7 @@ export function ThemeToggle() { }; return ( - ); diff --git a/web/src/components/Toast.tsx b/web/src/components/Toast.tsx index 9d951611..d048f520 100644 --- a/web/src/components/Toast.tsx +++ b/web/src/components/Toast.tsx @@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef, useState } from "react"; import type { ReactNode } from "react"; import { CircleAlert, CircleCheck, Info, TriangleAlert, X } from "lucide-react"; import { cn } from "@/lib/utils"; +import { useI18n } from "../i18n"; /** * 浮层轻提示(toast):操作结果不再以插入式横幅撑开页面,改为顶部居中浮动、 @@ -126,6 +127,7 @@ function ToastCard({ toast: ToastItem; onDismiss: (id: number) => void; }) { + const { t } = useI18n(); const duration = toast.duration ?? DEFAULT_DURATION; const Icon = ICON[toast.tone]; @@ -150,7 +152,7 @@ function ToastCard({
{toast.content}
) : undefined }