@etsoo/materialui
Version:
TypeScript Material-UI Implementation
36 lines (35 loc) • 1.27 kB
JavaScript
import { useSearchParamsWithCache } from "@etsoo/react";
import React from "react";
import { GridUtils } from "../GridUtils";
import { ExtendUtils } from "@etsoo/shared";
export function listCacheKeyGenerator(cacheKey) {
return `${cacheKey}-list-scroll`;
}
export function useListCacheInitLoad(cacheKey, cacheMinutes) {
// Reference
const ref = React.useRef(null);
// Search data
const searchData = useSearchParamsWithCache(cacheKey);
// Avoid repeatedly load from cache
if (ref.current || !cacheKey)
return undefined;
// Cache data
const cacheData = GridUtils.getCacheData(cacheKey, cacheMinutes);
if (cacheData) {
const { rows, state } = cacheData;
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);
ExtendUtils.waitFor(() => ref.scrollToRow({ index: data.startIndex }), 100);
}
// Return cached rows and state
return [rows, state];
};
}
return undefined;
}