diff --git a/package-lock.json b/package-lock.json index 668f3c236..828730244 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,7 @@ "@redhat-cloud-services/frontend-components": "^4.2.11", "@redhat-cloud-services/frontend-components-notifications": "^4.1.0", "@redhat-cloud-services/frontend-components-pdf-generator": "4.0.5", - "@redhat-cloud-services/frontend-components-utilities": "^4.0.13", + "@redhat-cloud-services/frontend-components-utilities": "^4.0.14", "@redhat-cloud-services/host-inventory-client": "1.2.0", "@redhat-cloud-services/rbac-client": "1.2.0", "@scalprum/core": "^0.7.0", @@ -4665,9 +4665,9 @@ } }, "node_modules/@redhat-cloud-services/frontend-components-utilities": { - "version": "4.0.13", - "resolved": "https://registry.npmjs.org/@redhat-cloud-services/frontend-components-utilities/-/frontend-components-utilities-4.0.13.tgz", - "integrity": "sha512-UBvfUlrf0IVPRcqhipRXGBNIuMP1+CG4FRNyGKCb8PuZM/iPX951JmEbelgBAyEwKGffnEPojP0A+9Xu6iFlJg==", + "version": "4.0.14", + "resolved": "https://registry.npmjs.org/@redhat-cloud-services/frontend-components-utilities/-/frontend-components-utilities-4.0.14.tgz", + "integrity": "sha512-Xf3zvEZ9nPZJ24qlDYtIkmFW/zsWwQHzU4rfaftNDol7mP+X21PunW3pMb9YSCDzRiqKiwWDUjtgiVjiuNTb3w==", "dependencies": { "@redhat-cloud-services/rbac-client": "^1.0.111 || 2.x", "@redhat-cloud-services/types": "^1.0.9", diff --git a/package.json b/package.json index 1aed8ac5a..9e7200761 100644 --- a/package.json +++ b/package.json @@ -144,7 +144,7 @@ "@redhat-cloud-services/frontend-components": "^4.2.11", "@redhat-cloud-services/frontend-components-notifications": "^4.1.0", "@redhat-cloud-services/frontend-components-pdf-generator": "4.0.5", - "@redhat-cloud-services/frontend-components-utilities": "^4.0.13", + "@redhat-cloud-services/frontend-components-utilities": "^4.0.14", "@redhat-cloud-services/host-inventory-client": "1.2.0", "@redhat-cloud-services/rbac-client": "1.2.0", "@scalprum/core": "^0.7.0", diff --git a/src/bootstrap.tsx b/src/bootstrap.tsx index 17393c2c5..6a3a06281 100644 --- a/src/bootstrap.tsx +++ b/src/bootstrap.tsx @@ -16,9 +16,9 @@ import chromeStore from './state/chromeStore'; import { GenerateId } from '@patternfly/react-core/dist/dynamic/helpers/GenerateId/GenerateId'; import { isPreviewAtom } from './state/atoms/releaseAtom'; import AppPlaceholder from './components/AppPlaceholder'; -import useAsyncLoader from './hooks/useAsyncLoader'; import { ChromeUserConfig, initChromeUserConfig } from './utils/initUserConfig'; import ChromeAuthContext from './auth/ChromeAuthContext'; +import useSuspenseLoader from '@redhat-cloud-services/frontend-components-utilities/useSuspenseLoader/useSuspenseLoader'; const isITLessEnv = ITLess(); const language: keyof typeof messages = 'en'; @@ -68,7 +68,7 @@ const ConfigLoader = () => { function initFail() { initPreview(false); } - const { loader } = useAsyncLoader(initChromeUserConfig, initSuccess, initFail); + const { loader } = useSuspenseLoader(initChromeUserConfig, initSuccess, initFail); const [cookieElement, setCookieElement] = useState(null); return ( }> diff --git a/src/hooks/useAsyncLoader.ts b/src/hooks/useAsyncLoader.ts deleted file mode 100644 index b2d210a0e..000000000 --- a/src/hooks/useAsyncLoader.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { useRef } from 'react'; - -function useAsyncLoader, T extends Array>( - asyncMethod: (...args: T) => Promise, - afterResolve?: (result: R) => void, - afterReject?: (error: any) => void -) { - const storage = useRef<{ resolved: boolean; rejected: boolean; promise?: Promise; result?: R }>({ - resolved: false, - rejected: false, - promise: undefined, - result: undefined, - }); - - return { - loader: (...args: Parameters) => { - if (storage.current.rejected) return; - - if (storage.current.resolved) return storage.current.result; - - if (storage.current.promise) throw storage.current.promise; - - storage.current.promise = asyncMethod(...args) - .then((res) => { - storage.current.promise = undefined; - storage.current.resolved = true; - storage.current.result = res; - afterResolve?.(res); - return res; - }) - .catch((error) => { - storage.current.promise = undefined; - storage.current.rejected = true; - afterReject?.(error); - return error; - }); - - throw storage.current.promise; - }, - }; -} - -export default useAsyncLoader;