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","object","enumerableOnly","keys","Object","getOwnPropertySymbols","symbols","filter","sym","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","key","_defineProperty2","getOwnPropertyDescriptors","defineProperties","defineProperty","localStorageQueryCache","set","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,MAAA,EAAAC,cAAA,QAAAC,IAAA,GAAAC,MAAA,CAAAD,IAAA,CAAAF,MAAA,OAAAG,MAAA,CAAAC,qBAAA,QAAAC,OAAA,GAAAF,MAAA,CAAAC,qBAAA,CAAAJ,MAAA,GAAAC,cAAA,KAAAI,OAAA,GAAAA,OAAA,CAAAC,MAAA,WAAAC,GAAA,WAAAJ,MAAA,CAAAK,wBAAA,CAAAR,MAAA,EAAAO,GAAA,EAAAE,UAAA,OAAAP,IAAA,CAAAQ,IAAA,CAAAC,KAAA,CAAAT,IAAA,EAAAG,OAAA,YAAAH,IAAA;AAAA,SAAAU,cAAAC,MAAA,aAAAC,CAAA,MAAAA,CAAA,GAAAC,SAAA,CAAAC,MAAA,EAAAF,CAAA,UAAAG,MAAA,WAAAF,SAAA,CAAAD,CAAA,IAAAC,SAAA,CAAAD,CAAA,QAAAA,CAAA,OAAAf,OAAA,CAAAI,MAAA,CAAAc,MAAA,OAAAC,OAAA,WAAAC,GAAA,QAAAC,gBAAA,aAAAP,MAAA,EAAAM,GAAA,EAAAF,MAAA,CAAAE,GAAA,SAAAhB,MAAA,CAAAkB,yBAAA,G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