2018-10-22 08:04:19 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
// Copy-paste from mytrezor (old wallet)
|
|
|
|
// https://github.com/satoshilabs/mytrezor/blob/develop/app/scripts/storage/BackendLocalStorage.js
|
|
|
|
export const getStoragePath = (): string => {
|
|
|
|
const regexHash = /^([^#]*)#.*$/;
|
|
|
|
const path = window.location.href.replace(regexHash, '$1');
|
|
|
|
const regexStart = /^[^:]*:\/\/[^/]*\//;
|
|
|
|
return path.replace(regexStart, '/');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const STORAGE_PATH: string = getStoragePath();
|
|
|
|
|
|
|
|
type StorageType = 'local' | 'session';
|
|
|
|
|
|
|
|
export const get: <T>(type: StorageType, key: string) => T | null = (type, key) => {
|
|
|
|
const storage = type === 'local' ? window.localStorage : window.sessionStorage;
|
|
|
|
try {
|
|
|
|
return storage.getItem(key);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(`Get ${type} storage: ${error}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const set = (type: StorageType, key: string, value: string | number | boolean): void => {
|
|
|
|
const storage = type === 'local' ? window.localStorage : window.sessionStorage;
|
|
|
|
try {
|
|
|
|
storage.setItem(key, value);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(`Save ${type} storage: ${error}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = (type: StorageType, key: string): void => {
|
|
|
|
const storage = type === 'local' ? window.localStorage : window.sessionStorage;
|
|
|
|
try {
|
|
|
|
storage.removeItem(key);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(`Remove ${type} storage: ${error}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const clearAll = (type: ?StorageType): void => {
|
|
|
|
let clearLocal: boolean = true;
|
|
|
|
let clearSession: boolean = true;
|
|
|
|
if (typeof type === 'string') {
|
|
|
|
clearLocal = type === 'local';
|
|
|
|
clearSession = !clearLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (clearLocal) {
|
2019-03-04 12:33:02 +00:00
|
|
|
Object.keys(window.localStorage).forEach(
|
|
|
|
key => key.indexOf(STORAGE_PATH) >= 0 && window.localStorage.removeItem(key)
|
|
|
|
);
|
2018-10-22 08:04:19 +00:00
|
|
|
}
|
|
|
|
if (clearSession) {
|
2019-03-04 12:33:02 +00:00
|
|
|
Object.keys(window.sessionStorage).forEach(
|
|
|
|
key => key.indexOf(STORAGE_PATH) >= 0 && window.sessionStorage.removeItem(key)
|
|
|
|
);
|
2018-10-22 08:04:19 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Clearing sessionStorage error: ${error}`);
|
|
|
|
}
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|