From 6394c8ba7bdbc0f856e67a55bc9156fdfc93b0d3 Mon Sep 17 00:00:00 2001 From: chenhang Date: Thu, 11 Jun 2026 19:10:01 +0800 Subject: [PATCH] refactor(chat): remove legacy screen visibility events and use absolute state refs - Remove ChatScreenVisible and ChatScreenInvisible events from chat state machine - Remove legacy ChatInit event handler - Migrate state transitions to use absolute references (#chat.guestSession, #chat.userSession, #chat.idle) - Adjust legal text wording for Cozsweet privacy agreement --- src/app/auth/components/auth-legal-text.tsx | 4 ++-- src/app/chat/components/chat-screen.tsx | 2 -- src/stores/chat/chat-events.ts | 2 -- src/stores/chat/chat-machine.ts | 23 ++++++++------------- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/app/auth/components/auth-legal-text.tsx b/src/app/auth/components/auth-legal-text.tsx index 6fbe17c3..43dac5c8 100644 --- a/src/app/auth/components/auth-legal-text.tsx +++ b/src/app/auth/components/auth-legal-text.tsx @@ -32,14 +32,14 @@ export function AuthLegalText() { {checked ? : null}

- I agree to the{" "} + I agree to the Cozsweet {" "} - Cozsweet Privacy Agreement + Privacy Agreement {" "} and{" "} { - chatDispatch({ type: "ChatScreenVisible" }); dispatchAuthLifecycle(authState.loginStatus, chatDispatch); return () => { - chatDispatch({ type: "ChatScreenInvisible" }); }; // 注:mount useEffect 故意**不**依赖 loginStatus —— 首次 mount 时机是固定的 // ("用户进 /chat 那一刻"),loginStatus 由 splash/auth-screen 提前 settle。 diff --git a/src/stores/chat/chat-events.ts b/src/stores/chat/chat-events.ts index acbd55cb..6caf7e0f 100644 --- a/src/stores/chat/chat-events.ts +++ b/src/stores/chat/chat-events.ts @@ -18,8 +18,6 @@ export type ChatEvent = // 内部 UI 事件 | { type: "ChatInit" } - | { type: "ChatScreenVisible" } - | { type: "ChatScreenInvisible" } // 鉴权生命周期(**用户**显式触发登录后由 chat-screen 派) | { type: "ChatGuestLogin" } | { type: "ChatNonGuestLogin"; token: string } diff --git a/src/stores/chat/chat-machine.ts b/src/stores/chat/chat-machine.ts index bd2a2c94..a45037b9 100644 --- a/src/stores/chat/chat-machine.ts +++ b/src/stores/chat/chat-machine.ts @@ -297,10 +297,8 @@ export const chatMachine = setup({ // ───────────────────────────────────────────────────────────── idle: { on: { - ChatGuestLogin: "guestSession", - ChatNonGuestLogin: "userSession", - // 兼容旧事件(**无**资源 —— 旧 chat-screen 还在派 ChatInit) - ChatInit: "guestSession", + ChatGuestLogin: "#chat.guestSession", + ChatNonGuestLogin: "#chat.userSession", ChatLoadMoreHistory: "loadingMoreHistory", // 旧逻辑保留(翻历史用) }, }, @@ -345,10 +343,9 @@ export const chatMachine = setup({ actions: "appendUserImage", }, ChatLoadMoreHistory: "loadingMore", - ChatScreenInvisible: "idle", // 屏不可见 → idle - ChatLogout: "idle", // 登出 → idle + ChatLogout: "#chat.idle", // 登出 → idle // 跨态(游客 → email) - ChatNonGuestLogin: "userSession", + ChatNonGuestLogin: "#chat.userSession", ChatAISentenceReceived: { actions: "appendOrUpdateAISentence", }, @@ -426,10 +423,9 @@ export const chatMachine = setup({ actions: "appendUserImage", }, ChatLoadMoreHistory: "loadingMore", - ChatScreenInvisible: "idle", // 屏不可见 → idle(cleanup WS via parent state exit) - ChatLogout: "idle", // 登出 → idle(cleanup WS) + ChatLogout: "#chat.idle", // 登出 → idle(cleanup WS) // 跨态(email → 游客) - ChatGuestLogin: "guestSession", + ChatGuestLogin: "#chat.guestSession", ChatAISentenceReceived: { actions: "appendOrUpdateAISentence", }, @@ -501,10 +497,9 @@ export const chatMachine = setup({ actions: "appendUserImage", }, ChatLoadMoreHistory: "loadingMoreHistory", - ChatScreenInvisible: "idle", - ChatLogout: "idle", - ChatGuestLogin: "guestSession", - ChatNonGuestLogin: "userSession", + ChatLogout: "#chat.idle", + ChatGuestLogin: "#chat.guestSession", + ChatNonGuestLogin: "#chat.userSession", ChatAISentenceReceived: { actions: "appendOrUpdateAISentence", },