From 21fa361e491d7d31d7ee121e60a05ede33f31980 Mon Sep 17 00:00:00 2001 From: JSR Date: Wed, 18 Mar 2026 18:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BE=A7=E6=A0=8F=E9=A6=96?= =?UTF-8?q?=E6=AC=A1=E6=89=93=E5=BC=80=E6=A0=87=E7=AD=BE=E9=A1=B5=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=BB=B6=E8=BF=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ContentArea.tsx | 65 +++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/src/components/ContentArea.tsx b/src/components/ContentArea.tsx index b968003..e8d39bc 100644 --- a/src/components/ContentArea.tsx +++ b/src/components/ContentArea.tsx @@ -1,26 +1,43 @@ -import React, { Suspense, lazy } from "react" +import React, { Suspense, lazy, useEffect } from "react" import { Layout, Space, Button, Tag, Spin } from "antd" import { Routes, Route, Navigate } from "react-router-dom" import { useTranslation } from "react-i18next" import { WindowControls } from "./WindowControls" -const Home = lazy(() => import("./Home").then((m) => ({ default: m.Home }))) -const StudentManager = lazy(() => - import("./StudentManager").then((m) => ({ default: m.StudentManager })) -) -const Settings = lazy(() => import("./Settings").then((m) => ({ default: m.Settings }))) -const ReasonManager = lazy(() => - import("./ReasonManager").then((m) => ({ default: m.ReasonManager })) -) -const ScoreManager = lazy(() => import("./ScoreManager").then((m) => ({ default: m.ScoreManager }))) -const Leaderboard = lazy(() => import("./Leaderboard").then((m) => ({ default: m.Leaderboard }))) +const loadHome = () => import("./Home") +const loadStudentManager = () => import("./StudentManager") +const loadSettings = () => import("./Settings") +const loadReasonManager = () => import("./ReasonManager") +const loadScoreManager = () => import("./ScoreManager") +const loadLeaderboard = () => import("./Leaderboard") +const loadSettlementHistory = () => import("./SettlementHistory") +const loadAutoScoreManager = () => import("./AutoScoreManager") + +const Home = lazy(() => loadHome().then((m) => ({ default: m.Home }))) +const StudentManager = lazy(() => loadStudentManager().then((m) => ({ default: m.StudentManager }))) +const Settings = lazy(() => loadSettings().then((m) => ({ default: m.Settings }))) +const ReasonManager = lazy(() => loadReasonManager().then((m) => ({ default: m.ReasonManager }))) +const ScoreManager = lazy(() => loadScoreManager().then((m) => ({ default: m.ScoreManager }))) +const Leaderboard = lazy(() => loadLeaderboard().then((m) => ({ default: m.Leaderboard }))) const SettlementHistory = lazy(() => - import("./SettlementHistory").then((m) => ({ default: m.SettlementHistory })) + loadSettlementHistory().then((m) => ({ default: m.SettlementHistory })) ) const AutoScoreManager = lazy(() => - import("./AutoScoreManager").then((m) => ({ default: m.AutoScoreManager })) + loadAutoScoreManager().then((m) => ({ default: m.AutoScoreManager })) ) +const warmupRouteChunks = () => + Promise.allSettled([ + loadHome(), + loadStudentManager(), + loadSettings(), + loadReasonManager(), + loadScoreManager(), + loadLeaderboard(), + loadSettlementHistory(), + loadAutoScoreManager(), + ]) + const { Content } = Layout interface ContentAreaProps { @@ -37,6 +54,28 @@ export function ContentArea({ onLogout, }: ContentAreaProps): React.JSX.Element { const { t } = useTranslation() + + useEffect(() => { + let cancelled = false + let timer: ReturnType | undefined + + const runWarmup = () => { + if (cancelled) return + warmupRouteChunks().catch(() => void 0) + } + + if ("requestIdleCallback" in window) { + ;(window as any).requestIdleCallback(runWarmup, { timeout: 1500 }) + } else { + timer = setTimeout(runWarmup, 300) + } + + return () => { + cancelled = true + if (timer) clearTimeout(timer) + } + }, []) + const permissionTag = (