Skip to content

Commit

Permalink
Remove suspense boundary from token refresh.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Sep 25, 2024
1 parent 24d28fb commit 10bd78a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
46 changes: 11 additions & 35 deletions src/bootstrap.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { Suspense, useContext, useEffect } from 'react';
import React, { useEffect } from 'react';
import { createRoot } from 'react-dom/client';
import { Provider } from 'react-redux';
import { IntlProvider, ReactIntlErrorCode } from 'react-intl';
import { Provider as JotaiProvider, useSetAtom } from 'jotai';
import { Provider as JotaiProvider } from 'jotai';

import { spinUpStore } from './redux/redux-config';
import RootApp from './components/RootApp';
Expand All @@ -12,13 +12,9 @@ import OIDCProvider from './auth/OIDCConnector/OIDCProvider';
import messages from './locales/data.json';
import ErrorBoundary from './components/ErrorComponents/ErrorBoundary';
import chromeStore from './state/chromeStore';
import { GenerateId } from '@patternfly/react-core/dist/dynamic/helpers/GenerateId/GenerateId';
import { isPreviewAtom } from './state/atoms/releaseAtom';
import { GenerateId } from '@patternfly/react-core/dist/dynamic/helpers/GenerateId/GenerateId';;

Check failure on line 15 in src/bootstrap.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
import AppPlaceholder from './components/AppPlaceholder';
import { ChromeUserConfig, initChromeUserConfig } from './utils/initUserConfig';
import ChromeAuthContext from './auth/ChromeAuthContext';
import useSuspenseLoader from '@redhat-cloud-services/frontend-components-utilities/useSuspenseLoader/useSuspenseLoader';
import { userConfigAtom } from './state/atoms/userConfigAtom';
import useSessionConfig from './hooks/useSessionConfig';

const isITLessEnv = ITLess();
const language: keyof typeof messages = 'en';
Expand All @@ -39,36 +35,16 @@ const useInitializeAnalytics = () => {
}, []);
};

const App = ({ initApp }: { initApp: (...args: Parameters<typeof initChromeUserConfig>) => ChromeUserConfig | undefined }) => {
const { getUser, token } = useContext(ChromeAuthContext);
// triggers suspense based async call to block rendering until the async call is resolved
// TODO: Most of async init should be moved to this method
initApp({
getUser,
token,
});
const App = () => {
const loaded = useSessionConfig();

useInitializeAnalytics();

return <RootApp />;
};

const ConfigLoader = () => {
const initPreview = useSetAtom(isPreviewAtom);
const setUserConfig = useSetAtom(userConfigAtom);
function initSuccess(userConfig: ChromeUserConfig) {
initPreview(userConfig.data.uiPreview);
setUserConfig(userConfig);
}
function initFail() {
initPreview(false);
if (!loaded) {
return <AppPlaceholder />;
}
const { loader } = useSuspenseLoader(initChromeUserConfig, initSuccess, initFail);
return (
<Suspense fallback={<AppPlaceholder />}>
<App initApp={loader} />
</Suspense>
);

return <RootApp />;
};

const entry = document.getElementById('chrome-entry');
Expand All @@ -92,7 +68,7 @@ if (entry) {
>
<ErrorBoundary>
<AuthProvider>
<ConfigLoader />
<App />
</AuthProvider>
</ErrorBoundary>
</IntlProvider>
Expand Down
43 changes: 43 additions & 0 deletions src/hooks/useSessionConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useContext, useEffect, useState } from 'react';
import { useSetAtom } from 'jotai';
import { ChromeUserConfig, initChromeUserConfig } from '../utils/initUserConfig';
import { isPreviewAtom } from '../state/atoms/releaseAtom';
import { userConfigAtom } from '../state/atoms/userConfigAtom';
import ChromeAuthContext from '../auth/ChromeAuthContext';

const useSessionConfig = () => {
const [configLoaded, setConfigLoaded] = useState(false);
const { getUser, token } = useContext(ChromeAuthContext);
const initPreview = useSetAtom(isPreviewAtom);
const setUserConfig = useSetAtom(userConfigAtom);
function initSuccess(userConfig: ChromeUserConfig) {
if (!configLoaded) {
// do not re-initialize preview if already loaded
// it can "restart" active user sessions. Because toggling preview re-rendes the UI modules, it can cause issues
initPreview(userConfig.data.uiPreview);
}
setUserConfig(userConfig);
}
function initFail() {
initPreview(false);
}

async function initConfig() {
try {
const config = await initChromeUserConfig({ getUser, token });
initSuccess(config);
setConfigLoaded(true);
} catch (error) {
initFail();
throw error;
}
}

useEffect(() => {
initConfig();
}, [getUser, token]);

return configLoaded;
};

export default useSessionConfig;

0 comments on commit 10bd78a

Please sign in to comment.