Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 6x 7x 7x 7x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 2x 2x 2x 2x 7x | import { LOCAL_STORAGE_MOCK_TOKEN_KEY } from '@amalia/kernel/auth/components';
import { type UserContract } from '@amalia/tenants/users/types';
export const LOCAL_STORAGE_COMPANY_ID_KEY = 'companyId';
export const LOCAL_STORAGE_USER_ID_KEY = 'userId';
const LOCAL_STORAGE_VALUES_TO_PRESERVE = [JSON.stringify(['color-scheme-preference'])];
// Add HubSportConversations type to window
declare global {
var HubSpotConversations:
| {
clear: (options: { resetWidget: boolean }) => void;
}
| undefined;
}
export type WithMaybeCompanyId<T extends { company?: { id?: string } }, U = T['company']> = Omit<
T,
'company' | 'companyId'
> & {
company?: U;
companyId?: string;
};
/**
* On load, check if local storage has the same companyId that current user.
* If that's not the case, clear local storage data and register new companyId
*/
export const checkLocalStorageOnLoad = ({
company,
companyId,
id: currentUserId,
}: WithMaybeCompanyId<UserContract>) => {
// Do not clear storage in Cypress tests, or else the robot won't be able to authenticate.
if (localStorage.getItem(LOCAL_STORAGE_MOCK_TOKEN_KEY)) {
return;
}
const currentUserCompanyId = companyId ?? company?.id;
sessionStorage.clear();
if (currentUserCompanyId && localStorage.getItem(LOCAL_STORAGE_COMPANY_ID_KEY) !== currentUserCompanyId) {
// Save values to restore later
const values: Record<string, string> = {};
LOCAL_STORAGE_VALUES_TO_PRESERVE.forEach((v) => {
const value = localStorage.getItem(v);
if (value !== null) {
values[v] = value;
}
});
localStorage.clear();
// Restore values
Object.entries(values).forEach(([k, v]) => {
localStorage.setItem(k, v);
});
localStorage.setItem(LOCAL_STORAGE_COMPANY_ID_KEY, currentUserCompanyId);
}
if (currentUserId && localStorage.getItem(LOCAL_STORAGE_USER_ID_KEY) !== currentUserId) {
// Try to clear the Hubspot tokens from local storage
globalThis.HubSpotConversations?.clear({ resetWidget: true });
localStorage.setItem(LOCAL_STORAGE_USER_ID_KEY, currentUserId);
}
};
|