From fd2d7fff17425d5af5117eedc7afd0a2cafc3b90 Mon Sep 17 00:00:00 2001 From: Gigiaj Date: Fri, 20 Jun 2025 16:15:20 -0500 Subject: [PATCH] Swap to load from the latest active session in the matrixSession array and retain fallback behavior for old sessions --- src/app/state/sessions.ts | 43 +++++++-------------------------------- 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/src/app/state/sessions.ts b/src/app/state/sessions.ts index 85bcd10e..98a2a700 100644 --- a/src/app/state/sessions.ts +++ b/src/app/state/sessions.ts @@ -1,4 +1,3 @@ -import { atom } from 'jotai'; import { atomWithLocalStorage, getLocalStorageItem, @@ -71,22 +70,19 @@ export const getSessionStoreName = (session: Session): SessionStoreName => { }; export const MATRIX_SESSIONS_KEY = 'matrixSessions'; -const baseSessionsAtom = atomWithLocalStorage( +export const sessionsAtom = atomWithLocalStorage( MATRIX_SESSIONS_KEY, (key) => { - const defaultSessions: Sessions = []; - const sessions = getLocalStorageItem(key, defaultSessions); - - // Before multi account support session was stored - // as multiple item in local storage. - // So we need these migration code. const fallbackSession = getFallbackSession(); if (fallbackSession) { + console.warn('Migrating from a fallback session...'); + const newSessions: Sessions = [fallbackSession]; + setLocalStorageItem(key, newSessions); removeFallbackSession(); - sessions.push(fallbackSession); - setLocalStorageItem(key, sessions); + return newSessions; } - return sessions; + + return getLocalStorageItem(key, []); }, (key, value) => { setLocalStorageItem(key, value); @@ -102,28 +98,3 @@ export type SessionsAction = type: 'DELETE'; session: Session; }; - -export const sessionsAtom = atom( - (get) => get(baseSessionsAtom), - (get, set, action) => { - if (action.type === 'PUT') { - const sessions = [...get(baseSessionsAtom)]; - const sessionIndex = sessions.findIndex( - (session) => session.userId === action.session.userId - ); - if (sessionIndex === -1) { - sessions.push(action.session); - } else { - 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); - } - } -);