1 import type { PersistedSessionWithLocalID } from '@proton/shared/lib/authentication/SessionInterface';
2 import { getPersistedSessions } from '@proton/shared/lib/authentication/persistedSessionStorage';
4 import { LAST_ACTIVE_PING } from '../store/_user/useActivePing';
5 import { sendErrorReport } from './errorHandling';
6 import { EnrichedError } from './errorHandling/EnrichedError';
8 const getLastActiveUserId = () => {
9 const storageKeys = Object.keys(localStorage);
10 let lastActiveUserId = '';
13 for (const k of storageKeys) {
14 if (k.startsWith(LAST_ACTIVE_PING)) {
15 const data = JSON.parse(localStorage[k]);
16 const lastPing = Number(data.value);
17 if (lastAccess < lastPing) {
18 lastAccess = lastPing;
19 lastActiveUserId = k.substring(k.indexOf(LAST_ACTIVE_PING) + LAST_ACTIVE_PING.length + 1);
23 return lastActiveUserId || null;
26 export const getLastActivePersistedUserSession = () => {
28 // Last Active Persisted Session in any apps
29 let persistedSession: PersistedSessionWithLocalID | null = null;
30 const lastActiveUserId = getLastActiveUserId();
31 const persistedSessions = getPersistedSessions();
32 const lastPersitedSessionFromPing = persistedSessions.find(
33 (persistedSession) => persistedSession.UserID === lastActiveUserId
35 if (lastPersitedSessionFromPing) {
36 return lastPersitedSessionFromPing;
38 for (const data of getPersistedSessions()) {
39 if (persistedSession === null || data.persistedAt > persistedSession.persistedAt) {
40 persistedSession = data;
43 return persistedSession;
46 new EnrichedError('Failed to parse JSON from localStorage', {