refactor: scope providers by route and use XState selectors

This commit is contained in:
2026-07-13 19:07:53 +08:00
parent f9b5f22ee9
commit 37ae152abb
24 changed files with 512 additions and 336 deletions
+13 -2
View File
@@ -9,13 +9,24 @@
* - guest / email / OAuth → 重新 hydrate user store
*/
import { useEffect, useRef } from "react";
import { shallowEqual } from "@xstate/react";
import type { LoginStatus } from "@/data/dto/auth";
import { useAuthState } from "@/stores/auth/auth-context";
import {
selectAuthIsLoading,
useAuthSelector,
} from "@/stores/auth/auth-context";
import { useUserDispatch } from "@/stores/user/user-context";
export function UserAuthSync() {
const { hasInitialized, isLoading, loginStatus } = useAuthState();
const { hasInitialized, isLoading, loginStatus } = useAuthSelector(
(state) => ({
hasInitialized: state.context.hasInitialized,
isLoading: selectAuthIsLoading(state),
loginStatus: state.context.loginStatus,
}),
shallowEqual,
);
const userDispatch = useUserDispatch();
const prevLoginStatusRef = useRef<LoginStatus | null>(null);