PromucFlow_constructor/app/client/src/utils/localStorage.tsx
Arsalan Yaldram f58451aa5f
feat: upgrade to create react app 5 (#14000)
* Updated Typescript types.

* Typefixes after merge with release.

* chore: GenericApiResponse Removed alltogether.

* chore: resolved ApiResponse unknown errors removed PageListPayload.

* Added shouldBeDefined.

* fix: Resolved type errors.

* fix: Typescript upgrade to 4.5 and type fixes.

* feat: upgrade to cra 5

* feat: uncomment service worker registeration

* force secure websocket protocol

* jest test fixes

* fix: react function lint rule removed

* fix: klona test case.

* fix: typescirpt issues resolved

* fix: timeout for colorpicker test and change env.

* feat: update client-build.yml file

* fix: remove brotliplugin use compression plugin

* fix: build config fixed

* fix: upgrade webpack plugin

* fix: add branchbutton test to todo.

* fix: remove branch button test.

* fix: Add tailwind theme values, fix cypress tests

* fix: Typescript type fixes.

* feat: run jest tests in silent mode

* fix: cypress rgb values add branchbutton jest test

* fix: review comments, fixes for error.message

* fix: increase cache size for the workbox

* fix: remove OrgApi.ts file

* fix: cypress.json file remove credentials

* fix: downgrade react and react-dom packages

Co-authored-by: rahulramesha <rahul@appsmith.com>
2022-06-21 19:27:34 +05:30

118 lines
2.7 KiB
TypeScript

import { Variant } from "components/ads/common";
import { Toaster } from "components/ads/Toast";
import * as log from "loglevel";
import {
LOCAL_STORAGE_QUOTA_EXCEEDED_MESSAGE,
LOCAL_STORAGE_NO_SPACE_LEFT_ON_DEVICE_MESSAGE,
LOCAL_STORAGE_NOT_SUPPORTED_APP_MIGHT_NOT_WORK_AS_EXPECTED,
createMessage,
} from "@appsmith/constants/messages";
class LocalStorageNotSupportedError extends Error {
name: string;
constructor() {
super();
this.name = "LOCAL_STORAGE_NOT_SUPPORTED";
}
}
export const getLocalStorage = () => {
const storage = window.localStorage;
// ref: https://github.com/Modernizr/Modernizr/blob/94592f279a410436530c7c06acc42a6e90c20150/feature-detects/storage/localstorage.js
const isSupported = () => {
try {
storage.setItem("test", "testA");
storage.removeItem("test");
return true;
} catch (e) {
return false;
}
};
const _isSupported = isSupported();
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;
} else if (e.name === "LOCAL_STORAGE_NOT_SUPPORTED") {
// Fail silently
log.error(
createMessage(
LOCAL_STORAGE_NOT_SUPPORTED_APP_MIGHT_NOT_WORK_AS_EXPECTED,
),
);
return;
}
if (message) {
Toaster.show({
text: createMessage(message),
variant: Variant.danger,
});
} else {
throw e;
}
};
const getItem = (key: string): string | null => {
try {
if (!_isSupported) {
throw new LocalStorageNotSupportedError();
}
return storage.getItem(key);
} catch (error) {
handleError(error as Error);
}
return null;
};
const setItem = (key: string, value: string) => {
try {
if (!_isSupported) {
throw new LocalStorageNotSupportedError();
}
storage.setItem(key, value);
} catch (error) {
handleError(error as Error);
}
};
const removeItem = (key: string) => {
try {
if (!_isSupported) {
throw new LocalStorageNotSupportedError();
}
storage.removeItem(key);
} catch (error) {
handleError(error as Error);
}
};
const clear = () => {
try {
if (!_isSupported) {
throw new LocalStorageNotSupportedError();
}
storage.clear();
} catch (error) {
handleError(error as Error);
}
};
return {
getItem,
setItem,
removeItem,
isSupported,
clear,
};
};
const localStorage = getLocalStorage();
export default localStorage;