31 lines
732 B
JavaScript
31 lines
732 B
JavaScript
export const USER_INFO_UPDATED_EVENT = 'user-info-updated';
|
|
|
|
function getCachedUserInfo() {
|
|
try {
|
|
const raw = window.localStorage.getItem('userInfo');
|
|
return raw ? JSON.parse(raw) : {};
|
|
} catch (error) {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
export function updateCachedUserInfo(changes) {
|
|
if (typeof window === 'undefined' || !window.localStorage) {
|
|
return {};
|
|
}
|
|
|
|
const nextUserInfo = {
|
|
...getCachedUserInfo(),
|
|
...(changes && typeof changes === 'object' ? changes : {})
|
|
};
|
|
|
|
try {
|
|
window.localStorage.setItem('userInfo', JSON.stringify(nextUserInfo));
|
|
} catch (error) {
|
|
void error;
|
|
}
|
|
|
|
window.dispatchEvent(new CustomEvent(USER_INFO_UPDATED_EVENT, { detail: nextUserInfo }));
|
|
return nextUserInfo;
|
|
}
|