package.cjs.react-utils.usePersistedQuery.usePersistedQuery.js.map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
{"version":3,"file":"usePersistedQuery.js","names":["_react","require","_reactQuery","_ramda","_constants","_general","ownKeys","e","r","t","Object","keys","getOwnPropertySymbols","o","filter","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","arguments","length","forEach","_defineProperty2","getOwnPropertyDescriptors","defineProperties","defineProperty","localStorageQueryCache","set","key","data","cache","getAll","newCache","modifiedAt","Date","now","setToLocalStorage","QUERY_CACHE_NAME_SPACE","getFromLocalStorage","get","isOutdated","localCache","staleTime","isNil","usePersistedQuery","queryKey","fetch","options","queryResult","useQuery","queryFn","useEffect","isSuccess","isFreshLoading","isLoading","getCache","_default","exports"],"sources":["../../../../src/react-utils/usePersistedQuery/usePersistedQuery.js"],"sourcesContent":["import { useEffect } from \"react\";\n\nimport { useQuery } from \"@tanstack/react-query\";\nimport { isNil } from \"ramda\";\nimport { QUERY_CACHE_NAME_SPACE } from \"react-utils/constants\";\nimport { getFromLocalStorage, setToLocalStorage } from \"utils/general\";\n\nconst localStorageQueryCache = {\n set: (key, data) => {\n const cache = localStorageQueryCache.getAll();\n const newCache = { ...cache, [key]: { data, modifiedAt: Date.now() } };\n setToLocalStorage(QUERY_CACHE_NAME_SPACE, newCache);\n },\n getAll: () => {\n const cache = getFromLocalStorage(QUERY_CACHE_NAME_SPACE);\n if (!cache) return {};\n\n return cache;\n },\n get: key => localStorageQueryCache.getAll()[key],\n};\n\nconst isOutdated = (localCache, staleTime) => {\n if (isNil(localCache)) return true;\n\n if (isNil(staleTime)) return false;\n\n return localCache.modifiedAt <= Date.now() - staleTime;\n};\n\nconst usePersistedQuery = (queryKey, fetch, options) => {\n const queryResult = useQuery({\n queryKey,\n queryFn: fetch,\n ...options,\n });\n const localCache = localStorageQueryCache.get(queryKey);\n\n useEffect(() => {\n if (!queryResult.isSuccess) return;\n localStorageQueryCache.set(queryKey, queryResult.data);\n }, [queryKey, queryResult.data, queryResult.isSuccess]);\n\n if (isOutdated(localCache, options?.staleTime)) {\n return { ...queryResult, isFreshLoading: queryResult.isLoading };\n }\n\n return {\n ...queryResult,\n data: queryResult.data || localCache.data,\n isFreshLoading: false,\n };\n};\n\nusePersistedQuery.getCache = localStorageQueryCache.get;\n\nexport default usePersistedQuery;\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,WAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AAAuE,SAAAK,QAAAC,CAAA,EAAAC,CAAA,QAAAC,CAAA,GAAAC,MAAA,CAAAC,IAAA,CAAAJ,CAAA,OAAAG,MAAA,CAAAE,qBAAA,QAAAC,CAAA,GAAAH,MAAA,CAAAE,qBAAA,CAAAL,CAAA,GAAAC,CAAA,KAAAK,CAAA,GAAAA,CAAA,CAAAC,MAAA,WAAAN,CAAA,WAAAE,MAAA,CAAAK,wBAAA,CAAAR,CAAA,EAAAC,CAAA,EAAAQ,UAAA,OAAAP,CAAA,CAAAQ,IAAA,CAAAC,KAAA,CAAAT,CAAA,EAAAI,CAAA,YAAAJ,CAAA;AAAA,SAAAU,cAAAZ,CAAA,aAAAC,CAAA,MAAAA,CAAA,GAAAY,SAAA,CAAAC,MAAA,EAAAb,CAAA,UAAAC,CAAA,WAAAW,SAAA,CAAAZ,CAAA,IAAAY,SAAA,CAAAZ,CAAA,QAAAA,CAAA,OAAAF,OAAA,CAAAI,MAAA,CAAAD,CAAA,OAAAa,OAAA,WAAAd,CAAA,QAAAe,gBAAA,aAAAhB,CAAA,EAAAC,CAAA,EAAAC,CAAA,CAAAD,CAAA,SAAAE,MAAA,CAAAc,yBAAA,GAAAd,MAAA,CA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