@etsoo/materialui
Version:
TypeScript Material-UI Implementation
46 lines (45 loc) • 1.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.gridCacheKeyGenerator = gridCacheKeyGenerator;
exports.useGridCacheInitLoad = useGridCacheInitLoad;
const react_1 = require("@etsoo/react");
const react_2 = __importDefault(require("react"));
const GridUtils_1 = require("../GridUtils");
const shared_1 = require("@etsoo/shared");
function gridCacheKeyGenerator(cacheKey) {
return `${cacheKey}-grid-scroll`;
}
function useGridCacheInitLoad(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(gridCacheKeyGenerator(cacheKey));
if (scrollData) {
const data = JSON.parse(scrollData);
shared_1.ExtendUtils.waitFor(() => ref.scrollToCell({
rowIndex: data.rowStartIndex,
columnIndex: data.columnStartIndex
}), 100);
}
// Return cached rows and state
return [rows, state];
};
}
return undefined;
}