UNPKG

@etsoo/materialui

Version:

TypeScript Material-UI Implementation

39 lines (38 loc) 1.37 kB
import { useSearchParamsWithCache } from "@etsoo/react"; import React from "react"; import { GridUtils } from "../GridUtils"; import { ExtendUtils } from "@etsoo/shared"; export function gridCacheKeyGenerator(cacheKey) { return `${cacheKey}-grid-scroll`; } export function useGridCacheInitLoad(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(gridCacheKeyGenerator(cacheKey)); if (scrollData) { const data = JSON.parse(scrollData); ExtendUtils.waitFor(() => ref.scrollToCell({ rowIndex: data.rowStartIndex, columnIndex: data.columnStartIndex }), 100); } // Return cached rows and state return [rows, state]; }; } return undefined; }