From 6e811b0e095100a2bd20702f557505fd85b8b3b2 Mon Sep 17 00:00:00 2001 From: chenhang Date: Tue, 30 Jun 2026 11:36:52 +0800 Subject: [PATCH] fix(user): refresh profile on sidebar open --- src/app/sidebar/sidebar-screen.tsx | 13 +++++++++++++ src/stores/user/__tests__/user-machine.test.ts | 5 ++--- src/stores/user/user-events.ts | 5 +---- src/stores/user/user-machine.ts | 12 ------------ 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/app/sidebar/sidebar-screen.tsx b/src/app/sidebar/sidebar-screen.tsx index 702faae7..f468710e 100644 --- a/src/app/sidebar/sidebar-screen.tsx +++ b/src/app/sidebar/sidebar-screen.tsx @@ -1,5 +1,6 @@ "use client"; +import { useEffect } from "react"; import { LogOut } from "lucide-react"; import { signOut } from "next-auth/react"; import { useRouter } from "next/navigation"; @@ -29,6 +30,18 @@ export function SidebarScreen() { const auth = useAuthState(); const authDispatch = useAuthDispatch(); + useEffect(() => { + if (!auth.hasInitialized || auth.isLoading) return; + if (auth.loginStatus === "notLoggedIn") return; + + userDispatch({ type: "UserFetch" }); + }, [ + auth.hasInitialized, + auth.isLoading, + auth.loginStatus, + userDispatch, + ]); + const handleLogoutClick = () => { userDispatch({ type: "UserClearLocal" }); authDispatch({ type: "AuthLogoutSubmitted" }); diff --git a/src/stores/user/__tests__/user-machine.test.ts b/src/stores/user/__tests__/user-machine.test.ts index 79af0332..eb52db55 100644 --- a/src/stores/user/__tests__/user-machine.test.ts +++ b/src/stores/user/__tests__/user-machine.test.ts @@ -101,11 +101,10 @@ describe("userMachine", () => { actor.stop(); }); - it("updates user profile fields synchronously", () => { + it("updates user profile synchronously", () => { const actor = createActor(createTestUserMachine()).start(); - actor.send({ type: "UserUpdate", user: makeUser({ username: "Before" }) }); - actor.send({ type: "UserUpdateUsername", username: "After" }); + actor.send({ type: "UserUpdate", user: makeUser({ username: "After" }) }); expect(actor.getSnapshot().context.currentUser?.username).toBe("After"); diff --git a/src/stores/user/user-events.ts b/src/stores/user/user-events.ts index 86f420eb..8f0c1f60 100644 --- a/src/stores/user/user-events.ts +++ b/src/stores/user/user-events.ts @@ -7,8 +7,5 @@ export type UserEvent = | { type: "UserInit" } | { type: "UserFetch" } | { type: "UserUpdate"; user: UserView } - | { type: "UserUpdateUsername"; username: string } | { type: "UserClearLocal" } - | { type: "UserLogout" } - | { type: "UserDeleteChatHistory" } - | { type: "UserDeleteAccount" }; + | { type: "UserLogout" }; diff --git a/src/stores/user/user-machine.ts b/src/stores/user/user-machine.ts index 588f096c..eff3353d 100644 --- a/src/stores/user/user-machine.ts +++ b/src/stores/user/user-machine.ts @@ -40,13 +40,6 @@ export const userMachine = setup({ }; }), - updateUsername: assign(({ context, event }) => { - if (event.type !== "UserUpdateUsername" || !context.currentUser) return {}; - return { - currentUser: { ...context.currentUser, username: event.username }, - }; - }), - clearUser: assign(() => ({ currentUser: null, avatarUrl: null, @@ -66,15 +59,10 @@ export const userMachine = setup({ UserUpdate: { actions: "updateUser", }, - UserUpdateUsername: { - actions: "updateUsername", - }, UserClearLocal: { actions: "clearUser", }, UserLogout: "loggingOut", - UserDeleteChatHistory: {}, - UserDeleteAccount: {}, }, },