From 4dc2ee0cabddf5d654e23237c76cd2450f224649 Mon Sep 17 00:00:00 2001 From: monoid Date: Thu, 9 Oct 2025 17:46:59 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20useEffect=EC=99=80=20useState=EB=A5=BC?= =?UTF-8?q?=20=ED=99=9C=EC=9A=A9=ED=95=98=EC=97=AC=20useLogin=20=ED=9B=85?= =?UTF-8?q?=20=EA=B0=9C=EC=84=A0=20=EB=B0=8F=20=EB=AC=B4=ED=95=9C=20?= =?UTF-8?q?=EB=A3=A8=ED=94=84=20=EB=B0=A9=EC=A7=80=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/client/src/state/user.ts | 37 ++++++++++--------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/packages/client/src/state/user.ts b/packages/client/src/state/user.ts index 62a3871..07be4d0 100644 --- a/packages/client/src/state/user.ts +++ b/packages/client/src/state/user.ts @@ -7,7 +7,7 @@ import { refreshService, resetPasswordService, } from "./api.ts"; -import { useSyncExternalStore } from "react"; +import { useEffect, useState } from "react"; let localObj: LoginResponse | null = null; @@ -135,32 +135,19 @@ export const doResetPassword = async (username: string, oldpassword: string, new } }; -let imutableSnapshot: ReturnType = getUserSessions(); -const getSnapshot = () => { - const snap = getUserSessions(); - // to avoid infinite loop, do not update the snapshot if it hasn't changed - if (JSON.stringify(snap) === JSON.stringify(imutableSnapshot)) { - return imutableSnapshot; - } - return snap; -} -const subscribe = (onChange: () => void) => { - console.log("Auth hook subscribed"); - const listener = () => { - console.log("Auth state changed, updating hook"); - imutableSnapshot = getSnapshot(); - onChange(); - }; - window.addEventListener("auth", listener); - return () => window.removeEventListener("auth", listener); -} export function useLogin() { - const hook = useSyncExternalStore( - subscribe, - getUserSessions, - ); - return hook; + const [user, setUser] = useState(getUserSessions()); + useEffect(() => { + const listener = (_e: AuthEvent) => { + setUser(getUserSessions()); + }; + window.addEventListener("auth", listener); + return () => { + window.removeEventListener("auth", listener); + }; + }, []); + return user; }