2021-02-16 06:17:23 +00:00
|
|
|
import { Variant } from "components/ads/common";
|
|
|
|
|
import { Toaster } from "components/ads/Toast";
|
|
|
|
|
import {
|
|
|
|
|
LOCAL_STORAGE_QUOTA_EXCEEDED_MESSAGE,
|
|
|
|
|
LOCAL_STORAGE_NO_SPACE_LEFT_ON_DEVICE_MESSAGE,
|
2021-03-13 14:24:45 +00:00
|
|
|
createMessage,
|
2021-02-16 06:17:23 +00:00
|
|
|
} from "constants/messages";
|
|
|
|
|
|
|
|
|
|
const getLocalStorage = () => {
|
|
|
|
|
const storage = window.localStorage;
|
|
|
|
|
|
|
|
|
|
const handleError = (e: Error) => {
|
|
|
|
|
let message;
|
|
|
|
|
if (e.name === "QuotaExceededError") {
|
|
|
|
|
message = LOCAL_STORAGE_QUOTA_EXCEEDED_MESSAGE;
|
|
|
|
|
} else if (e.name === "NS_ERROR_FILE_NO_DEVICE_SPACE") {
|
|
|
|
|
message = LOCAL_STORAGE_NO_SPACE_LEFT_ON_DEVICE_MESSAGE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
|
Toaster.show({
|
2021-03-13 14:24:45 +00:00
|
|
|
text: createMessage(message),
|
2021-02-16 06:17:23 +00:00
|
|
|
variant: Variant.danger,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getItem = (key: string): string | null => {
|
|
|
|
|
try {
|
|
|
|
|
return storage.getItem(key);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
handleError(e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setItem = (key: string, value: string) => {
|
|
|
|
|
try {
|
|
|
|
|
storage.setItem(key, value);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
handleError(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const removeItem = (key: string) => {
|
|
|
|
|
try {
|
|
|
|
|
storage.removeItem(key);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
handleError(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-24 05:09:47 +00:00
|
|
|
const clear = () => {
|
|
|
|
|
try {
|
|
|
|
|
storage.clear();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
handleError(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-16 06:17:23 +00:00
|
|
|
const isSupported = () => !!window.localStorage;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
getItem,
|
|
|
|
|
setItem,
|
|
|
|
|
removeItem,
|
|
|
|
|
isSupported,
|
2021-03-24 05:09:47 +00:00
|
|
|
clear,
|
2021-02-16 06:17:23 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const localStorage = getLocalStorage();
|
|
|
|
|
|
|
|
|
|
export default localStorage;
|