@etsoo/materialui
Version:
TypeScript Material-UI Implementation
43 lines (42 loc) • 1.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.listCacheKeyGenerator = listCacheKeyGenerator;
exports.useListCacheInitLoad = useListCacheInitLoad;
const react_1 = require("@etsoo/react");
const react_2 = __importDefault(require("react"));
const GridUtils_1 = require("../GridUtils");
const shared_1 = require("@etsoo/shared");
function listCacheKeyGenerator(cacheKey) {
return `${cacheKey}-list-scroll`;
}
function useListCacheInitLoad(cacheKey, cacheMinutes) {
// Reference
const ref = react_2.default.useRef(null);
// Search data
const searchData = (0, react_1.useSearchParamsWithCache)(cacheKey);
// Avoid repeatedly load from cache
if (ref.current || !cacheKey)
return undefined;
// Cache data
const cacheData = GridUtils_1.GridUtils.getCacheData(cacheKey, cacheMinutes);
if (cacheData) {
const { rows, state } = cacheData;
GridUtils_1.GridUtils.mergeSearchData(state, searchData);
// Update flag value
ref.current = true;
return (ref) => {
// Scroll position
const scrollData = sessionStorage.getItem(listCacheKeyGenerator(cacheKey));
if (scrollData) {
const data = JSON.parse(scrollData);
shared_1.ExtendUtils.waitFor(() => ref.scrollToRow({ index: data.startIndex }), 100);
}
// Return cached rows and state
return [rows, state];
};
}
return undefined;
}