feat: add xstate state management for user state
Introduce xstate v5 and @xstate/react to manage user authentication and profile state. Implement userMachine with actors for initialization, fetching, and logout operations, along with actions for updating user data and clearing state.
This commit is contained in:
@@ -16,9 +16,9 @@
|
||||
* - `recordXxx(today)` 写入 todayString,后续 `canXxx` 在同一天返回 `false`。
|
||||
*/
|
||||
|
||||
import { LocalStorage } from "../local_storage";
|
||||
import { Result, type Result as ResultT } from "@/utils/result";
|
||||
import { StorageKeys } from "../storage_keys";
|
||||
import { LocalStorage } from "@/utils/local-storage";
|
||||
|
||||
export class AppStorage {
|
||||
private static _ls: LocalStorage | null = null;
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
* - `clearAuthData` 顺序清理:loginToken → guestToken → refreshToken,任一失败立即返回
|
||||
*/
|
||||
|
||||
import { LocalStorage } from "../local_storage";
|
||||
import { Result, type Result as ResultT } from "@/utils/result";
|
||||
import { StorageKeys } from "../storage_keys";
|
||||
import type { IAuthStorage } from "./iauth_storage";
|
||||
import { LocalStorage } from "@/utils/local-storage";
|
||||
|
||||
export class AuthStorage implements IAuthStorage {
|
||||
private readonly ls: LocalStorage;
|
||||
|
||||
@@ -18,13 +18,13 @@
|
||||
import {
|
||||
GuestChatQuotaSchema,
|
||||
type GuestChatQuotaData,
|
||||
} from "@/data/services/schemas/chat/guest_chat_quota";
|
||||
} from "@/data/schemas/chat/guest_chat_quota";
|
||||
import { z } from "zod";
|
||||
|
||||
import { LocalStorage } from "../local_storage";
|
||||
import { type Result as ResultT } from "@/utils/result";
|
||||
import { StorageKeys } from "../storage_keys";
|
||||
import type { IChatStorage } from "./ichat_storage";
|
||||
import { LocalStorage } from "@/utils/local-storage";
|
||||
|
||||
export class ChatStorage implements IChatStorage {
|
||||
private readonly ls: LocalStorage;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
*/
|
||||
|
||||
import type { Result } from "@/utils/result";
|
||||
import type { GuestChatQuotaData } from "@/data/services/schemas/chat/guest_chat_quota";
|
||||
import type { GuestChatQuotaData } from "@/data/schemas/chat/guest_chat_quota";
|
||||
|
||||
export interface IChatStorage {
|
||||
getGuestDailyChatQuota(): Promise<Result<GuestChatQuotaData | null>>;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
*/
|
||||
|
||||
import type { Result } from "@/utils/result";
|
||||
import type { UserData } from "@/data/services/schemas/user/user";
|
||||
import type { UserData } from "@/data/schemas/user/user";
|
||||
|
||||
export interface IUserStorage {
|
||||
getUser(): Promise<Result<UserData | null>>;
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
* - 单例挂在 class 静态字段,HMR 复用安全
|
||||
*/
|
||||
|
||||
import { UserSchema, type UserData } from "@/data/services/schemas/user/user";
|
||||
import { LocalStorage } from "../local_storage";
|
||||
import { UserSchema, type UserData } from "@/data/schemas/user/user";
|
||||
import { type Result as ResultT } from "@/utils/result";
|
||||
import { StorageKeys } from "../storage_keys";
|
||||
import type { IUserStorage } from "./iuser_storage";
|
||||
import { LocalStorage } from "@/utils/local-storage";
|
||||
|
||||
export class UserStorage implements IUserStorage {
|
||||
private readonly ls: LocalStorage;
|
||||
|
||||
Reference in New Issue
Block a user