perf(*): 创作工作台 - 优化首页按需加载与画布交互并修复历史迁移冲突

This commit is contained in:
ddCat
2026-09-05 13:24:10 +08:00
parent 165b4443ab
commit 58f01d1eb6
107 changed files with 3025 additions and 1089 deletions

View File

@@ -1,8 +1,7 @@
import type { ReactNode } from "react";
import { useEffect } from "react";
import { applyUserSession } from "@/lib/user-session";
import { getAuthSession } from "@/services/api/auth";
import { getAuthSession, type AuthSessionPayload } from "@/services/api/auth";
import { FullScreenLoader } from "@/components/ui/aceternity/full-screen-loader";
import { preloadWorkspaceRoute } from "@/lib/workspace-route-modules";
import { useUserStore } from "@/stores/use-user-store";
@@ -12,14 +11,21 @@ export function AuthSessionHydrator({ children }: { children: ReactNode }) {
useEffect(() => {
let cancelled = false;
// 登录态与当前工作区 chunk 并行恢复,避免进入应用后再出现一次页面级等待。
preloadWorkspaceRoute(window.location.pathname);
getAuthSession()
.then(async (payload) => {
if (!cancelled) await applyUserSession(payload);
if (cancelled) return;
if (!payload.user) {
applyAnonymousSession(payload);
return;
}
// 账号数据、画布和素材持久化只属于已登录工作区,登录页不下载这些模块。
const { applyUserSession } = await import("@/lib/user-session");
if (cancelled) return;
await applyUserSession(payload);
preloadWorkspaceRoute(window.location.pathname);
})
.catch(async () => {
if (!cancelled) await applyUserSession({ user: null, logicalModels: [] });
.catch(() => {
if (!cancelled) applyAnonymousSession({ user: null, logicalModels: [] });
});
return () => {
cancelled = true;
@@ -28,3 +34,12 @@ export function AuthSessionHydrator({ children }: { children: ReactNode }) {
return hydrated ? children : <FullScreenLoader />;
}
function applyAnonymousSession(payload: AuthSessionPayload) {
const store = useUserStore.getState();
store.clearSession();
store.setRuntimeLimits(payload.runtimeLimits);
store.setDrawingEngine(payload.drawingEngine);
store.setFeatures(payload.features);
store.setHydrated(true);
}