2021-11-05 05:49:19 +00:00
|
|
|
import * as log from "loglevel";
|
2021-02-16 06:17:23 +00:00
|
|
|
import {
|
|
|
|
|
LOCAL_STORAGE_QUOTA_EXCEEDED_MESSAGE,
|
|
|
|
|
LOCAL_STORAGE_NO_SPACE_LEFT_ON_DEVICE_MESSAGE,
|
2021-04-26 09:18:39 +00:00
|
|
|
LOCAL_STORAGE_NOT_SUPPORTED_APP_MIGHT_NOT_WORK_AS_EXPECTED,
|
2021-03-13 14:24:45 +00:00
|
|
|
createMessage,
|
2024-08-06 14:52:22 +00:00
|
|
|
} from "ee/constants/messages";
|
2024-08-09 14:20:29 +00:00
|
|
|
import { toast } from "@appsmith/ads";
|
2021-02-16 06:17:23 +00:00
|
|
|
|
2022-11-02 09:09:59 +00:00
|
|
|
export const LOCAL_STORAGE_KEYS = {
|
|
|
|
|
CANVAS_CARDS_STATE: "CANVAS_CARDS_STATE",
|
2024-03-12 09:43:52 +00:00
|
|
|
SPLITPANE_ANNOUNCEMENT: "SPLITPANE_ANNOUNCEMENT",
|
2022-11-02 09:09:59 +00:00
|
|
|
};
|
|
|
|
|
|
2021-04-26 09:18:39 +00:00
|
|
|
class LocalStorageNotSupportedError extends Error {
|
|
|
|
|
name: string;
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.name = "LOCAL_STORAGE_NOT_SUPPORTED";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
class WebStorage {
|
|
|
|
|
private storage: Storage;
|
|
|
|
|
private _isSupported: boolean;
|
2021-02-16 06:17:23 +00:00
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
constructor(storage: Storage) {
|
|
|
|
|
this.storage = storage;
|
|
|
|
|
|
|
|
|
|
this._isSupported = this.isSupported();
|
|
|
|
|
}
|
2021-04-26 09:18:39 +00:00
|
|
|
// ref: https://github.com/Modernizr/Modernizr/blob/94592f279a410436530c7c06acc42a6e90c20150/feature-detects/storage/localstorage.js
|
2022-09-02 13:15:48 +00:00
|
|
|
isSupported = () => {
|
2021-04-26 09:18:39 +00:00
|
|
|
try {
|
2022-09-02 13:15:48 +00:00
|
|
|
this.storage.setItem("test", "testA");
|
|
|
|
|
this.storage.removeItem("test");
|
2021-04-26 09:18:39 +00:00
|
|
|
return true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
getItem = (key: string): string | null => {
|
|
|
|
|
try {
|
|
|
|
|
if (!this._isSupported) {
|
|
|
|
|
throw new LocalStorageNotSupportedError();
|
|
|
|
|
}
|
|
|
|
|
return this.storage.getItem(key);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.handleError(error as Error);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2021-04-26 09:18:39 +00:00
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
handleError = (e: Error) => {
|
2021-02-16 06:17:23 +00:00
|
|
|
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;
|
2021-04-26 09:18:39 +00:00
|
|
|
} else if (e.name === "LOCAL_STORAGE_NOT_SUPPORTED") {
|
|
|
|
|
// Fail silently
|
2021-11-05 05:49:19 +00:00
|
|
|
log.error(
|
2021-04-26 09:18:39 +00:00
|
|
|
createMessage(
|
|
|
|
|
LOCAL_STORAGE_NOT_SUPPORTED_APP_MIGHT_NOT_WORK_AS_EXPECTED,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return;
|
2021-02-16 06:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message) {
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show(createMessage(message), {
|
|
|
|
|
kind: "error",
|
2021-02-16 06:17:23 +00:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
setItem = (key: string, value: string) => {
|
2021-02-16 06:17:23 +00:00
|
|
|
try {
|
2022-09-02 13:15:48 +00:00
|
|
|
if (!this._isSupported) {
|
2021-04-26 09:18:39 +00:00
|
|
|
throw new LocalStorageNotSupportedError();
|
|
|
|
|
}
|
2022-09-02 13:15:48 +00:00
|
|
|
this.storage.setItem(key, value);
|
2022-06-21 13:57:34 +00:00
|
|
|
} catch (error) {
|
2022-09-02 13:15:48 +00:00
|
|
|
this.handleError(error as Error);
|
2021-02-16 06:17:23 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
removeItem = (key: string) => {
|
2021-02-16 06:17:23 +00:00
|
|
|
try {
|
2022-09-02 13:15:48 +00:00
|
|
|
if (!this._isSupported) {
|
2021-04-26 09:18:39 +00:00
|
|
|
throw new LocalStorageNotSupportedError();
|
|
|
|
|
}
|
2022-09-02 13:15:48 +00:00
|
|
|
this.storage.removeItem(key);
|
2022-06-21 13:57:34 +00:00
|
|
|
} catch (error) {
|
2022-09-02 13:15:48 +00:00
|
|
|
this.handleError(error as Error);
|
2021-02-16 06:17:23 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
clear = () => {
|
2021-02-16 06:17:23 +00:00
|
|
|
try {
|
2022-09-02 13:15:48 +00:00
|
|
|
if (!this._isSupported) {
|
2021-04-26 09:18:39 +00:00
|
|
|
throw new LocalStorageNotSupportedError();
|
|
|
|
|
}
|
2022-09-02 13:15:48 +00:00
|
|
|
this.storage.clear();
|
2022-06-21 13:57:34 +00:00
|
|
|
} catch (error) {
|
2022-09-02 13:15:48 +00:00
|
|
|
this.handleError(error as Error);
|
2021-02-16 06:17:23 +00:00
|
|
|
}
|
|
|
|
|
};
|
2022-09-02 13:15:48 +00:00
|
|
|
}
|
2021-02-16 06:17:23 +00:00
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
export class LocalStorage extends WebStorage {
|
|
|
|
|
constructor() {
|
|
|
|
|
super(window.localStorage);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-24 05:09:47 +00:00
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
class SessionStorage extends WebStorage {
|
|
|
|
|
constructor() {
|
|
|
|
|
super(window.sessionStorage);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-16 06:17:23 +00:00
|
|
|
|
2022-09-02 13:15:48 +00:00
|
|
|
const localStorage = new LocalStorage();
|
|
|
|
|
export const sessionStorage = new SessionStorage();
|
2021-02-16 06:17:23 +00:00
|
|
|
|
|
|
|
|
export default localStorage;
|