All files / apps/web/src main.tsx

0% Statements 0/121
0% Branches 0/1
0% Functions 0/1
0% Lines 0/121

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                                                                                                                                                                                                                                                   
import { Auth0Provider, type Auth0ProviderOptions } from '@auth0/auth0-react';
import { QueryClientProvider } from '@tanstack/react-query';
import { Fragment } from 'react';
import { createRoot } from 'react-dom/client';
import { createBrowserRouter, createRoutesFromElements, Route, RouterProvider } from 'react-router-dom';

import {
  ColorSchemePreferenceContext,
  ColorSchemePreferenceProvider,
  DesignSystemProvider,
} from '@allshares/studio-design-system';
import { DEFAULT_THEME } from '@allshares/studio-design-system/themes/default';
import { routes } from '@amalia/core/routes';
import { ConnectorsOAuthCallbackView } from '@amalia/data-capture/connectors/views/oauth-callback';
import { LinkAdapter } from '@amalia/design-system/ext';
import { DateFnsLocaleContext } from '@amalia/ext/date-fns/components';
import { IFrameAuthentication, InitiatedSSOAuthentication } from '@amalia/kernel/auth/components';
import { config } from '@amalia/kernel/config/client';
import { queryClient } from '@amalia/kernel/config/react-query';
import { UnexpectedErrorPage } from '@amalia/kernel/error/components';

import { App } from './App';
import { AppProviders } from './AppProviders';
import { WebLocalizationProvider } from './components/WebLocalizationProvider';

/**
 * Cannot turn off the error messages from Emotion, so we filter them out.
 * https://github.com/emotion-js/emotion/issues/1105#issuecomment-1885044724
 */
// eslint-disable-next-line no-console
const consoleError = console.error;
// eslint-disable-next-line no-console
console.error = function filterEmotionErrors(msg: unknown, ...args: unknown[]) {
  if (msg && typeof msg === 'string' && msg.includes('server-side rendering')) {
    return;
  }

  consoleError(msg, ...args);
};

const auth0AuthorizationParams: Auth0ProviderOptions['authorizationParams'] = {
  audience: config.auth0.audience,
  scope: config.auth0.scope,
  redirect_uri: config.auth0.redirectUri,
};

// Removed due to LS-88 -- to reaccess later
// const onRedirectCallback = (appState?: { returnTo?: string } | null) => {
//   const target = appState?.returnTo;
//
//   if (target) {
//     globalThis.location.href = target;
//   }
// };

const router = createBrowserRouter(
  createRoutesFromElements(
    <Fragment>
      {/* Unauthenticated routes */}
      <Route
        element={<IFrameAuthentication />}
        errorElement={<UnexpectedErrorPage />}
        path={routes.AUTH_IFRAME}
      />
      <Route
        element={<InitiatedSSOAuthentication />}
        errorElement={<UnexpectedErrorPage />}
        path={routes.AUTH_SSO_INITIATED}
      />
      <Route
        element={<ConnectorsOAuthCallbackView />}
        path={routes.AUTH_CONNECTOR_CALLBACK}
      />

      {/* Authenticated routes */}
      <Route
        errorElement={<UnexpectedErrorPage />}
        path="*"
        element={
          <AppProviders>
            <App />
          </AppProviders>
        }
      />
    </Fragment>,
  ),
);

createRoot(document.getElementById('root')!).render(
  <Auth0Provider
    useRefreshTokens
    useRefreshTokensFallback
    authorizationParams={auth0AuthorizationParams}
    clientId={config.auth0.clientId}
    domain={config.auth0.domain}
    useFormData={false}
  >
    <QueryClientProvider client={queryClient}>
      <WebLocalizationProvider>
        <DateFnsLocaleContext.Consumer>
          {(dateFnsLocale) => (
            <ColorSchemePreferenceProvider>
              <ColorSchemePreferenceContext.Consumer>
                {({ colorSchemePreference }) => (
                  <DesignSystemProvider
                    colorSchemePreference={colorSchemePreference}
                    dateFnsLocale={dateFnsLocale}
                    linkComponent={LinkAdapter}
                    theme={DEFAULT_THEME}
                  >
                    <RouterProvider router={router} />
                  </DesignSystemProvider>
                )}
              </ColorSchemePreferenceContext.Consumer>
            </ColorSchemePreferenceProvider>
          )}
        </DateFnsLocaleContext.Consumer>
      </WebLocalizationProvider>
    </QueryClientProvider>
  </Auth0Provider>,
);