refactor(table): 移除本地存储清理逻辑并改用sessionStorage存储表格尺寸
- 删除了定期清理本地存储相关代码 - 去除了dayjs依赖和相关时间判断 - 将表格尺寸的存储由localStorage改为sessionStorage,提高会话隔离性2.0
parent
a6e2d1f7e4
commit
843648c69b
|
|
@ -1,5 +1,4 @@
|
||||||
import { ProTable } from "@ant-design/pro-components";
|
import { ProTable } from "@ant-design/pro-components";
|
||||||
import dayjs from "dayjs";
|
|
||||||
import { useEffect, useMemo, useState } from "react";
|
import { useEffect, useMemo, useState } from "react";
|
||||||
import { throttle } from "throttle-debounce";
|
import { throttle } from "throttle-debounce";
|
||||||
import { useAntdResizableHeader } from "use-antd-resizable-header";
|
import { useAntdResizableHeader } from "use-antd-resizable-header";
|
||||||
|
|
@ -7,38 +6,6 @@ import { getDataType, getIndexColumn } from "../../utils";
|
||||||
import "use-antd-resizable-header/dist/style.css";
|
import "use-antd-resizable-header/dist/style.css";
|
||||||
import "./index.less";
|
import "./index.less";
|
||||||
|
|
||||||
const CLEANUP_INTERVAL_DAYS = 10; // 清理间隔天数
|
|
||||||
const LAST_CLEANUP_KEY = "tableLocalStorageLastCleanup"; // 最后清理时间存储key
|
|
||||||
|
|
||||||
// 清理本地存储中特定后缀的key
|
|
||||||
function cleanupTableLocalStorage() {
|
|
||||||
const now = dayjs();
|
|
||||||
const lastCleanupStr = localStorage.getItem(LAST_CLEANUP_KEY);
|
|
||||||
const lastCleanup = lastCleanupStr ? dayjs(lastCleanupStr) : null;
|
|
||||||
|
|
||||||
// 如果没有上次清理时间或距离上次清理已超过10天
|
|
||||||
if (!lastCleanup || now.diff(lastCleanup, "day") >= CLEANUP_INTERVAL_DAYS) {
|
|
||||||
// 查找并清理符合条件的key
|
|
||||||
const keysToRemove = [];
|
|
||||||
for (let i = 0; i < localStorage.length; i++) {
|
|
||||||
const key = localStorage.key(i);
|
|
||||||
if (key && (key.endsWith("#columnState")
|
|
||||||
|| key.endsWith("#size")
|
|
||||||
|| key.endsWith("#resizable"))) {
|
|
||||||
keysToRemove.push(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除匹配的key
|
|
||||||
keysToRemove.forEach((key) => {
|
|
||||||
localStorage.removeItem(key);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 更新最后清理时间
|
|
||||||
localStorage.setItem(LAST_CLEANUP_KEY, now.toISOString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPersistenceKey(index) {
|
function getPersistenceKey(index) {
|
||||||
const basename = process.env?.app?.basename;
|
const basename = process.env?.app?.basename;
|
||||||
const pathname = decodeURIComponent(window.location.pathname);
|
const pathname = decodeURIComponent(window.location.pathname);
|
||||||
|
|
@ -52,11 +19,11 @@ function getPersistenceKey(index) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTableSize(index) {
|
function getTableSize(index) {
|
||||||
return window.localStorage.getItem(getPersistenceKey(index).size);
|
return window.sessionStorage.getItem(getPersistenceKey(index).size);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setTableSize(value, index) {
|
function setTableSize(value, index) {
|
||||||
return window.localStorage.setItem(getPersistenceKey(index).size, value);
|
return window.sessionStorage.setItem(getPersistenceKey(index).size, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function Table(props) {
|
function Table(props) {
|
||||||
|
|
@ -146,8 +113,6 @@ function Table(props) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanupTableLocalStorage();
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (mutationObserver && resizeObserver && enabledResizer) {
|
if (mutationObserver && resizeObserver && enabledResizer) {
|
||||||
resizeObserver.unobserve(document.body);
|
resizeObserver.unobserve(document.body);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue