|
- // 用于新建镜像
- export const mirrorNameKey = 'mirror-name';
- // 模型部署服务
- export const serviceInfoKey = 'service-info';
- // 模型部署服务版本
- export const serviceVersionInfoKey = 'service-version-info';
- // 编辑器 url
- export const editorUrlKey = 'editor-url';
- // 数据集、模型资源
- export const resourceItemKey = 'resource-item';
-
- /**
- * Retrieves an item from session storage by key.
- *
- * If `isObject` is true, the function attempts to parse the stored value as JSON.
- * If parsing fails, the function returns undefined.
- *
- * @param {string} key - The key of the item to retrieve
- * @param {boolean} [isObject=false] - Whether to parse the stored value as JSON
- * @return {any} The retrieved item, or undefined if not found or parsing fails
- */
- export const getSessionStorageItem = (key: string, isObject: boolean = false): any => {
- const jsonStr = sessionStorage.getItem(key);
- if (!isObject) {
- return jsonStr;
- }
- if (jsonStr) {
- try {
- return JSON.parse(jsonStr);
- } catch (error) {
- return undefined;
- }
- }
- return undefined;
- };
-
- /**
- * Sets an item in session storage by key.
- *
- * If `isObject` is true, the function stringifies the state as JSON before storing.
- *
- * @param {string} key - The key of the item to set
- * @param {any} [state] - The value of the item to set
- * @param {boolean} [isObject=false] - Whether to stringify the state as JSON
- */
- export const setSessionStorageItem = (key: string, state?: any, isObject: boolean = false) => {
- if (state) {
- sessionStorage.setItem(key, isObject ? JSON.stringify(state) : state);
- }
- };
-
- /**
- * Removes an item from session storage by key.
- *
- * @param {string} key - The key of the item to remove
- */
- export const removeSessionStorageItem = (key: string) => {
- sessionStorage.removeItem(key);
- };
-
- /**
- * Retrieves an item from session storage by key and then removes it.
- *
- * This function is useful for passing data from one page to another.
- *
- * @param {string} key - The key of the item to retrieve
- * @param {boolean} [isObject=false] - Whether to parse the stored value as JSON
- * @return {any} The retrieved item, or undefined if not found or parsing fails
- */
- export const getSessionItemThenRemove = (key: string, isObject: boolean = false): any => {
- const res = getSessionStorageItem(key, isObject);
- sessionStorage.removeItem(key);
- return res;
- };
|