Don't throw error when localStorage is null (#2505)

This commit is contained in:
Lipis 2020-12-11 18:13:13 +02:00 committed by GitHub
parent a4e1f2c5c1
commit 7c7fb4903b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 10 deletions

View File

@ -99,19 +99,31 @@ export const importFromLocalStorage = () => {
};
export const getElementsStorageSize = () => {
const elements = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS);
const elementsSize = elements ? JSON.stringify(elements).length : 0;
return elementsSize;
try {
const elements = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS);
const elementsSize = elements ? JSON.stringify(elements).length : 0;
return elementsSize;
} catch (error) {
console.error(error);
return 0;
}
};
export const getTotalStorageSize = () => {
const appState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
const collab = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
const library = localStorage.getItem(APP_STORAGE_KEYS.LOCAL_STORAGE_LIBRARY);
try {
const appState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
const collab = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
const library = localStorage.getItem(
APP_STORAGE_KEYS.LOCAL_STORAGE_LIBRARY,
);
const appStateSize = appState ? JSON.stringify(appState).length : 0;
const collabSize = collab ? JSON.stringify(collab).length : 0;
const librarySize = library ? JSON.stringify(library).length : 0;
const appStateSize = appState ? JSON.stringify(appState).length : 0;
const collabSize = collab ? JSON.stringify(collab).length : 0;
const librarySize = library ? JSON.stringify(library).length : 0;
return appStateSize + collabSize + librarySize + getElementsStorageSize();
return appStateSize + collabSize + librarySize + getElementsStorageSize();
} catch (error) {
console.error(error);
return 0;
}
};