fix logout fails by twice close machine calls
This commit is contained in:
parent
a32b92c3c8
commit
308b7db854
5 changed files with 29 additions and 39 deletions
|
|
@ -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 <AccountSelector />;
|
||||
|
|
|
|||
|
|
@ -32,15 +32,10 @@ const baseSessionsAtom = atomWithLocalStorage<Sessions>(
|
|||
}
|
||||
);
|
||||
|
||||
export type SessionsAction =
|
||||
| {
|
||||
type: 'PUT';
|
||||
session: Session;
|
||||
}
|
||||
| {
|
||||
type: 'DELETE';
|
||||
session: Session;
|
||||
};
|
||||
export type SessionsAction = {
|
||||
type: 'PUT';
|
||||
session: Session;
|
||||
};
|
||||
|
||||
export const sessionsAtom = atom<Sessions, [SessionsAction], undefined>(
|
||||
(get) => get(baseSessionsAtom),
|
||||
|
|
@ -56,13 +51,6 @@ export const sessionsAtom = atom<Sessions, [SessionsAction], undefined>(
|
|||
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);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ export const initializeCrypto = async (
|
|||
const machine = new Machine(olmMachine, outgoingSender, outgoingRequestRetrier);
|
||||
|
||||
const deleteCryptoStorage = async () => {
|
||||
closeMachine();
|
||||
await deleteDB(getCryptoDBName(storeName));
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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<string | undefined>(LAST_ACTIVE_USERID, undefined);
|
||||
return userId;
|
||||
};
|
||||
|
||||
export const userHasSession = (userId: string): boolean => {
|
||||
const sessions = getLocalStorageItem<Sessions>(CLIENT_SESSIONS_STORE_KEY, []);
|
||||
|
||||
|
|
@ -93,16 +102,9 @@ export const userHasSession = (userId: string): boolean => {
|
|||
export const removeSession = (session: Session): void => {
|
||||
const sessions = getLocalStorageItem<Sessions>(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<string | undefined>(LAST_ACTIVE_USERID, undefined);
|
||||
return userId;
|
||||
setLastActiveUserId(undefined);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -157,7 +157,6 @@ export const createStorage = async (
|
|||
};
|
||||
|
||||
const deleteStorage = async () => {
|
||||
disconnectStorage();
|
||||
await deleteDB(dbName);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue