diff --git a/src/app/pages/AccountSelector.tsx b/src/app/pages/AccountSelector.tsx index f7361ecc..b3c1dfe2 100644 --- a/src/app/pages/AccountSelector.tsx +++ b/src/app/pages/AccountSelector.tsx @@ -30,6 +30,7 @@ import { useUserProfileRequest } from '../hooks/useUserProfile'; import { mxcToHttp } from '../../client/mxcToHttp'; import { useSessionRequest } from '../hooks/sw/useSessionRequest'; import { useDocumentVisibilityChange } from '../hooks/useDocumentVisibilityChange'; +import { useDocumentFocusChange } from '../hooks/useDocumentFocusChange'; function AccountCard({ session }: { session: Session }) { const { userId } = session; @@ -185,22 +186,23 @@ export function ClientAccountSelector() { useSessionRequest(session ?? sessions[0]); - useDocumentVisibilityChange( - useCallback( - (visible) => { - if (visible && session) { - setLastActiveUserId(session.userId); - } - }, - [session] - ) + const handleVisibility = useCallback( + (visible: boolean) => { + if (visible && userId) { + setLastActiveUserId(userId); + } + }, + [userId] ); + useDocumentVisibilityChange(handleVisibility); + useDocumentFocusChange(handleVisibility); + useEffect(() => { - if (session) { - setLastActiveUserId(session.userId); + if (userId) { + setLastActiveUserId(userId); } - }, [session]); + }, [userId]); if (!session) { return ; diff --git a/src/app/state/sessions.ts b/src/app/state/sessions.ts index b12c4587..695e3acf 100644 --- a/src/app/state/sessions.ts +++ b/src/app/state/sessions.ts @@ -32,15 +32,10 @@ const baseSessionsAtom = atomWithLocalStorage( } ); -export type SessionsAction = - | { - type: 'PUT'; - session: Session; - } - | { - type: 'DELETE'; - session: Session; - }; +export type SessionsAction = { + type: 'PUT'; + session: Session; +}; export const sessionsAtom = atom( (get) => get(baseSessionsAtom), @@ -56,13 +51,6 @@ export const sessionsAtom = atom( sessions.splice(sessionIndex, 1, action.session); } set(baseSessionsAtom, sessions); - return; - } - if (action.type === 'DELETE') { - const sessions = get(baseSessionsAtom).filter( - (session) => session.userId !== action.session.userId - ); - set(baseSessionsAtom, sessions); } } ); diff --git a/src/client/crypto.ts b/src/client/crypto.ts index 7102b494..6953413f 100644 --- a/src/client/crypto.ts +++ b/src/client/crypto.ts @@ -39,7 +39,6 @@ export const initializeCrypto = async ( const machine = new Machine(olmMachine, outgoingSender, outgoingRequestRetrier); const deleteCryptoStorage = async () => { - closeMachine(); await deleteDB(getCryptoDBName(storeName)); }; diff --git a/src/client/sessions.ts b/src/client/sessions.ts index e57a110d..69672831 100644 --- a/src/client/sessions.ts +++ b/src/client/sessions.ts @@ -84,6 +84,15 @@ export const getSessionStoreName = (session: Session): SessionStoreName => { }; }; +export const setLastActiveUserId = (userId: string | undefined) => { + setLocalStorageItem(LAST_ACTIVE_USERID, userId); +}; + +export const getLastActiveUserId = (): string | undefined => { + const userId = getLocalStorageItem(LAST_ACTIVE_USERID, undefined); + return userId; +}; + export const userHasSession = (userId: string): boolean => { const sessions = getLocalStorageItem(CLIENT_SESSIONS_STORE_KEY, []); @@ -93,16 +102,9 @@ export const userHasSession = (userId: string): boolean => { export const removeSession = (session: Session): void => { const sessions = getLocalStorageItem(CLIENT_SESSIONS_STORE_KEY, []); - const newSessions = sessions.filter((s) => s.userId === session.userId); + const newSessions = sessions.filter((s) => s.userId !== session.userId); setLocalStorageItem(CLIENT_SESSIONS_STORE_KEY, newSessions); -}; -export const setLastActiveUserId = (userId: string) => { - setLocalStorageItem(LAST_ACTIVE_USERID, userId); -}; - -export const getLastActiveUserId = (): string | undefined => { - const userId = getLocalStorageItem(LAST_ACTIVE_USERID, undefined); - return userId; + setLastActiveUserId(undefined); }; diff --git a/src/client/storage/index.ts b/src/client/storage/index.ts index 6ca0f8a7..b69edc6e 100644 --- a/src/client/storage/index.ts +++ b/src/client/storage/index.ts @@ -157,7 +157,6 @@ export const createStorage = async ( }; const deleteStorage = async () => { - disconnectStorage(); await deleteDB(dbName); };