diff --git a/src/app/sidebar/sidebar-screen.tsx b/src/app/sidebar/sidebar-screen.tsx index f468710e..702faae7 100644 --- a/src/app/sidebar/sidebar-screen.tsx +++ b/src/app/sidebar/sidebar-screen.tsx @@ -1,6 +1,5 @@ "use client"; -import { useEffect } from "react"; import { LogOut } from "lucide-react"; import { signOut } from "next-auth/react"; import { useRouter } from "next/navigation"; @@ -30,18 +29,6 @@ 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 eb52db55..79af0332 100644 --- a/src/stores/user/__tests__/user-machine.test.ts +++ b/src/stores/user/__tests__/user-machine.test.ts @@ -101,10 +101,11 @@ describe("userMachine", () => { actor.stop(); }); - it("updates user profile synchronously", () => { + it("updates user profile fields synchronously", () => { const actor = createActor(createTestUserMachine()).start(); - actor.send({ type: "UserUpdate", user: makeUser({ username: "After" }) }); + actor.send({ type: "UserUpdate", user: makeUser({ username: "Before" }) }); + actor.send({ type: "UserUpdateUsername", 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 8f0c1f60..86f420eb 100644 --- a/src/stores/user/user-events.ts +++ b/src/stores/user/user-events.ts @@ -7,5 +7,8 @@ export type UserEvent = | { type: "UserInit" } | { type: "UserFetch" } | { type: "UserUpdate"; user: UserView } + | { type: "UserUpdateUsername"; username: string } | { type: "UserClearLocal" } - | { type: "UserLogout" }; + | { type: "UserLogout" } + | { type: "UserDeleteChatHistory" } + | { type: "UserDeleteAccount" }; diff --git a/src/stores/user/user-machine.ts b/src/stores/user/user-machine.ts index eff3353d..588f096c 100644 --- a/src/stores/user/user-machine.ts +++ b/src/stores/user/user-machine.ts @@ -40,6 +40,13 @@ 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, @@ -59,10 +66,15 @@ export const userMachine = setup({ UserUpdate: { actions: "updateUser", }, + UserUpdateUsername: { + actions: "updateUsername", + }, UserClearLocal: { actions: "clearUser", }, UserLogout: "loggingOut", + UserDeleteChatHistory: {}, + UserDeleteAccount: {}, }, },