Skip to content
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
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"oidc-client-ts": "^3.1.0",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-error-boundary": "^5.0.0",
"react-hook-form": "^7.54.2",
"react-i18next": "^15.4.1",
"react-oidc-context": "^3.2.0",
Expand Down
6 changes: 0 additions & 6 deletions public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@
"EditMembers": {
"addButton": "Add"
},
"FrontendConfigContext": {
"errorMessage": "useFrontendConfig must be used within a FrontendConfigProvider"
},
"ControlPlaneListView": {
"projectHeader": "Project:"
},
Expand All @@ -141,9 +138,6 @@
"App": {
"loading": "Loading..."
},
"main": {
"failedMessage": "Failed to load frontend configuration"
},
"Providers": {
"headerProviders": "Providers",
"tableHeaderVersion": "Version",
Expand Down
26 changes: 26 additions & 0 deletions src/context/AuthProviderOnboarding.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ReactNode, use } from 'react';
import { AuthProvider, AuthProviderProps } from 'react-oidc-context';
import { useFrontendConfig } from './FrontendConfigContext.tsx';
import { LoadCrateKubeConfig } from '../lib/oidc/crate.ts';

interface AuthProviderOnboardingProps {
children?: ReactNode;
}

// Promise needs to be cached
// https://react.dev/blog/2024/12/05/react-19#use-does-not-support-promises-created-in-render
const fetchAuthPromiseCache = new Map<string, Promise<AuthProviderProps>>();

export function AuthProviderOnboarding({
children,
}: AuthProviderOnboardingProps) {
const { backendUrl } = useFrontendConfig();

const fetchAuthConfigPromise =
fetchAuthPromiseCache.get(backendUrl) ?? LoadCrateKubeConfig(backendUrl);
fetchAuthPromiseCache.set(backendUrl, fetchAuthConfigPromise);

const authConfig = use(fetchAuthConfigPromise);

return <AuthProvider {...authConfig}>{children}</AuthProvider>;
}
63 changes: 30 additions & 33 deletions src/context/FrontendConfigContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FC, ReactNode, createContext, useContext } from 'react';
import { ReactNode, createContext, use } from 'react';
import { DocLinkCreator } from '../lib/shared/links';
import { useTranslation } from 'react-i18next';

export enum Landscape {
Live = 'LIVE',
Expand All @@ -9,49 +8,47 @@ export enum Landscape {
Development = 'DEV',
}

export interface FrontendConfig {
interface FrontendConfigContextProps {
backendUrl: string;
landscape?: Landscape;
documentationBaseUrl: string;
}

export interface FrontendConfigProviderProps extends FrontendConfig {
links: DocLinkCreator;
}

const FrontendConfigContext = createContext<FrontendConfigProviderProps | null>(
const FrontendConfigContext = createContext<FrontendConfigContextProps | null>(
null,
);

export const useFrontendConfig = () => {
const c = useContext(FrontendConfigContext);
const { t } = useTranslation();

if (!c) {
throw new Error(t('FrontendConfigContext.errorMessage'));
}
return c;
};
const fetchPromise = fetch('/frontend-config.json').then((res) => res.json());

export const FrontendConfigProvider: FC<{
interface FrontendConfigProviderProps {
children: ReactNode;
config: FrontendConfig;
}> = ({ children, config }) => {
}

export function FrontendConfigProvider({
children,
}: FrontendConfigProviderProps) {
const config = use(fetchPromise);
const docLinks = new DocLinkCreator(config.documentationBaseUrl);
const value: FrontendConfigContextProps = {
links: docLinks,
backendUrl: config.backendUrl,
landscape: config.landscape,
documentationBaseUrl: config.documentationBaseUrl,
};

return (
<FrontendConfigContext.Provider
value={{
links: docLinks,
backendUrl: config.backendUrl,
landscape: config.landscape,
documentationBaseUrl: config.documentationBaseUrl,
}}
>
{children}
</FrontendConfigContext.Provider>
<FrontendConfigContext value={value}>{children}</FrontendConfigContext>
);
};

export async function LoadFrontendConfig(): Promise<FrontendConfig> {
return fetch('/frontend-config.json').then((res) => res.json());
}

export const useFrontendConfig = () => {
const context = use(FrontendConfigContext);

if (!context) {
throw new Error(
'useFrontendConfig must be used within a FrontendConfigProvider.',
);
}
return context;
};
2 changes: 1 addition & 1 deletion src/lib/oidc/crate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GetAuthPropsForCurrentContext } from './shared.ts';

export function LoadCrateKubeConfig(
backendUrl: string,
): Promise<AuthProviderProps | void> {
): Promise<AuthProviderProps> {
const uri = backendUrl + '/.well-known/openmcp/kubeconfig';

return fetch(uri)
Expand Down
55 changes: 24 additions & 31 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import React, { Suspense } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';
import { ThemeProvider } from '@ui5/webcomponents-react';
import { AuthProvider } from 'react-oidc-context';
import { LoadCrateKubeConfig } from './lib/oidc/crate.ts';
import { BusyIndicator, ThemeProvider } from '@ui5/webcomponents-react';
import { SWRConfig } from 'swr';
import { ToastProvider } from './context/ToastContext.tsx';
import { CopyButtonProvider } from './context/CopyButtonContext.tsx';
import {
FrontendConfigProvider,
LoadFrontendConfig,
} from './context/FrontendConfigContext.tsx';
import { FrontendConfigProvider } from './context/FrontendConfigContext.tsx';
import '@ui5/webcomponents-react/dist/Assets'; //used for loading themes
import { DarkModeSystemSwitcher } from './components/Core/DarkModeSystemSwitcher.tsx';
import '.././i18n.ts';
import './utils/i18n/timeAgo';
import { useTranslation } from 'react-i18next';
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
import IllustratedError from './components/Shared/IllustratedError.tsx';
import { AuthProviderOnboarding } from './context/AuthProviderOnboarding.tsx';

(async () => {
try {
const frontendConfig = await LoadFrontendConfig();
const authconfig = await LoadCrateKubeConfig(frontendConfig.backendUrl);
const ErrorFallback = ({ error }: FallbackProps) => {
return <IllustratedError error={error} />;
};

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<FrontendConfigProvider config={frontendConfig}>
<AuthProvider key={'crate'} {...authconfig}>
const rootElement = document.getElementById('root');
const root = createRoot(rootElement!);

root.render(
<React.StrictMode>
<ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => {}}>
<Suspense fallback={<BusyIndicator active />}>
<FrontendConfigProvider>
<AuthProviderOnboarding>
<ThemeProvider>
<ToastProvider>
<CopyButtonProvider>
Expand All @@ -41,17 +42,9 @@ import { useTranslation } from 'react-i18next';
</CopyButtonProvider>
</ToastProvider>
</ThemeProvider>
</AuthProvider>
</AuthProviderOnboarding>
</FrontendConfigProvider>
</React.StrictMode>,
);
} catch (e) {
const { t } = useTranslation();
console.error('failed to load frontend configuration or kubeconfig', e);
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<div>{t('main.failedMessage')}=</div>
</React.StrictMode>,
);
}
})();
</Suspense>
</ErrorBoundary>
</React.StrictMode>,
);