always open last used account on reloads

This commit is contained in:
Ajay Bura 2026-08-21 11:33:20 +05:30
commit 94d05c72c3
4 changed files with 46 additions and 3 deletions

View file

@ -0,0 +1,13 @@
import { useEffect } from 'react';
export const useDocumentVisibilityChange = (onChange: (visible: boolean) => void) => {
useEffect(() => {
const handleChange = () => {
onChange(document.visibilityState === 'visible');
};
document.addEventListener('visibilitychange', handleChange);
return () => {
document.removeEventListener('visibilitychange', handleChange);
};
}, [onChange]);
};

View file

@ -1,4 +1,4 @@
import React, { useMemo, useRef } from 'react';
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { Avatar, Box, Button, config, Icon, Icons, Text } from 'folds';
import { Link, Navigate, Outlet, useNavigate } from 'react-router-dom';
import { useAtom, useAtomValue } from 'jotai';
@ -23,12 +23,13 @@ import {
getAfterLoginRedirectPath,
setAfterLoginRedirectPath,
} from './afterLoginRedirectPath';
import { Session } from '../../client/sessions';
import { Session, setLastActiveUserId } from '../../client/sessions';
import { OrDivider } from '../components/OrDivider';
import { getAuthJSONRequest } from '../../client/request';
import { useUserProfileRequest } from '../hooks/useUserProfile';
import { mxcToHttp } from '../../client/mxcToHttp';
import { useSessionRequest } from '../hooks/sw/useSessionRequest';
import { useDocumentVisibilityChange } from '../hooks/useDocumentVisibilityChange';
function AccountCard({ session }: { session: Session }) {
const { userId } = session;
@ -176,6 +177,23 @@ export function ClientAccountSelector() {
useSessionRequest(session ?? sessions[0]);
useDocumentVisibilityChange(
useCallback(
(visible) => {
if (visible && session) {
setLastActiveUserId(session.userId);
}
},
[session]
)
);
useEffect(() => {
if (session) {
setLastActiveUserId(session.userId);
}
}, [session]);
if (!session) {
return <AccountSelector />;
}

View file

@ -3,6 +3,7 @@ import { atomWithLocalStorage } from './utils/atomWithLocalStorage';
import {
CLIENT_SESSIONS_STORE_KEY,
getFallbackSession,
getLastActiveUserId,
removeFallbackSession,
Session,
Sessions,
@ -66,4 +67,4 @@ export const sessionsAtom = atom<Sessions, [SessionsAction], undefined>(
}
);
export const sessionUserIdAtom = atom<string | undefined>(undefined);
export const sessionUserIdAtom = atom<string | undefined>(getLastActiveUserId());

View file

@ -2,6 +2,8 @@ import { getLocalStorageItem, setLocalStorageItem } from './localStorage';
export const CLIENT_SESSIONS_STORE_KEY = 'client-sessions';
const LAST_ACTIVE_USERID = 'last-active-userId';
export type Session = {
baseUrl: string;
userId: string;
@ -95,3 +97,12 @@ export const removeSession = (session: Session): void => {
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;
};