refactor(stores): split state and event unions into separate files

Extract `<Name>Context` interface + `initialContext` const into
`<name>-context.ts` and the event union into `<name>-events.ts` for
all four state machines (auth, chat, sidebar, user) under src/stores/.

- `*-machine.ts` keeps helpers, actors, actions, and the
  `setup().createMachine()` body; re-exports the types/initial value
  to preserve its public API.
- `*-types.ts` re-exports from the new files for backward compatibility
  (sidebar keeps enum re-exports, chat keeps GuestChatQuota).
- `index.ts` barrels updated to re-export the new files.
- Removed unused model imports (AuthMode, AuthPanelMode) from
  auth-machine.ts; kept LoginType, UiMessage, UserView where still
  used by actors/helpers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
kanban
2026-06-10 18:21:28 +08:00
committed by chenhang
parent 0593920318
commit 744e23fc29
20 changed files with 233 additions and 171 deletions
+3
View File
@@ -5,7 +5,10 @@
* - 业务类(Dart 风格 context/reducer/side-effects)已合并到 user-machine.ts
* - 类型由 user-types.ts 重新导出
* - React 入口为 user-context.tsxuseMachine 包装)
* - Context 形状与初始值位于 user-context.ts
* - 事件联合位于 user-events.ts
*/
export * from "./user-context";
export * from "./user-events";
export * from "./user-types";
export { userMachine } from "./user-machine";
+20
View File
@@ -0,0 +1,20 @@
/**
* User 状态机:Context 形状 + 初始值
*/
import type { UserView } from "@/models/user/user";
export interface UserContext {
currentUser: UserView | null;
pronouns: string;
coinBalance: number;
isLoading: boolean;
avatarUrl: string | null;
}
export const initialContext: UserContext = {
currentUser: null,
pronouns: "He",
coinBalance: 45,
isLoading: false,
avatarUrl: null,
};
+14
View File
@@ -0,0 +1,14 @@
/**
* User 状态机:事件联合
*/
import type { UserView } from "@/models/user/user";
export type UserEvent =
| { type: "UserInit" }
| { type: "UserFetch" }
| { type: "UserUpdate"; user: UserView }
| { type: "UserUpdateUsername"; username: string }
| { type: "UserUpdatePronouns"; pronouns: string }
| { type: "UserLogout" }
| { type: "UserDeleteChatHistory" }
| { type: "UserDeleteAccount" };
+6 -30
View File
@@ -17,37 +17,13 @@ import { authRepository } from "@/data/repositories/auth_repository";
import { UserStorage } from "@/data/storage/user/user_storage";
import { Result } from "@/utils/result";
// ============================================================
// Context
// ============================================================
export interface UserContext {
currentUser: UserView | null;
pronouns: string;
coinBalance: number;
isLoading: boolean;
avatarUrl: string | null;
}
import { UserContext, initialContext } from "./user-context";
import type { UserEvent } from "./user-events";
const initialContext: UserContext = {
currentUser: null,
pronouns: "He",
coinBalance: 45,
isLoading: false,
avatarUrl: null,
};
// ============================================================
// Events
// ============================================================
export type UserEvent =
| { type: "UserInit" }
| { type: "UserFetch" }
| { type: "UserUpdate"; user: UserView }
| { type: "UserUpdateUsername"; username: string }
| { type: "UserUpdatePronouns"; pronouns: string }
| { type: "UserLogout" }
| { type: "UserDeleteChatHistory" }
| { type: "UserDeleteAccount" };
// 重新导出 Context / Event 类型,保持 machine 文件的公共 API 不变
export type { UserContext } from "./user-context";
export { initialContext } from "./user-context";
export type { UserEvent } from "./user-events";
// ============================================================
// Helpers
+3 -2
View File
@@ -3,7 +3,8 @@
*
* 原始文件定义了 UserState 接口、UserEvent 联合、initialUserState。
* 状态机重构后:
* - `UserEvent` 联合由 `user-machine.ts` 重新导出(保留类型)
* - `UserEvent` 联合由 `user-events.ts` 导出
* - Context 形状与初始值由 `user-context.ts` 导出
* - `UserState` 由 `user-context.tsx` 导出(保持原 API 兼容)
*/
export type { UserEvent } from "./user-machine";
export type { UserEvent } from "./user-events";