Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove suspense boundary from token refresh. #2945

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 10 additions & 34 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 @@ -13,12 +13,8 @@ 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 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;
Loading