feat(storage): run startup persistence migrations

This commit is contained in:
2026-07-10 17:42:06 +08:00
parent b05adb1bb2
commit 8b40720821
7 changed files with 230 additions and 45 deletions
+22 -3
View File
@@ -5,11 +5,12 @@
* 派发时机:仅 mount 时一次(`useEffect` deps = `[dispatch]`,永远不重跑)。
*
* 与 <OAuthSessionSync /> 平级:
* - AuthStatusChecker: 启动时一次性 init恢复登录态、绑定 ASID/PSID、尝试 PSID 直登
* - AuthStatusChecker: 启动时先跑 storage migration,再恢复登录态、绑定 ASID/PSID、尝试 PSID 直登
* - OAuthSessionSync: 持续监听 NextAuth session"别人在写"
*/
import { useEffect } from "react";
import { runStorageMigrations } from "@/data/storage";
import { useAuthDispatch } from "./auth-context";
import { Logger } from "@/utils";
@@ -19,8 +20,26 @@ export function AuthStatusChecker() {
const dispatch = useAuthDispatch();
useEffect(() => {
log.debug("[auth-init] mount → dispatch AuthInit");
dispatch({ type: "AuthInit" });
let cancelled = false;
async function runStartupAuthInit() {
const migrationResult = await runStorageMigrations();
if (!migrationResult.success) {
log.warn(
{ err: migrationResult.error.message },
"[auth-init] storage migration failed; continuing AuthInit",
);
}
if (cancelled) return;
log.debug("[auth-init] mount → dispatch AuthInit");
dispatch({ type: "AuthInit" });
}
void runStartupAuthInit();
return () => {
cancelled = true;
};
}, [dispatch]);
// 无 UI