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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 6x 8x 28x 8x 8x 4x 4x 4x 4x 4x 4x 4x 8x 8x 4x 4x 4x 8x 8x 8x 4x 4x 8x 8x 8x 8x 8x 8x 4x 4x 4x | /**
* App — top-level blong-browser application component.
*
* Provides the single composition root for the portal UI. Wraps the Portal
* shell with BlongProvider so all child components have access to the handler
* proxy, the schema registry, and TanStack Query.
*
* This component is the canonical reuse point:
* - `portalReady` renders it into the DOM via ReactDOM.createRoot
* - Storybook decorators mount it with a mock handler proxy
* - Unit tests can render it with a spy handler proxy
*
* Props mirror IPortalProps so callers can customise the shell (logo, etc.)
* while the provider wiring is always handled here.
*/
import {ConfirmDialog, ConfirmPopup, PrimeReactProvider} from '../../primereact/index.js';
import './App.css';
import type {IHandlerProxy, ILogger} from '@feasibleone/blong';
import React from 'react';
import {BlongProvider, useBlong, type IBlongPortalConfig} from '../../context/BlongContext.js';
import {useAppStore} from '../../state/appStore.js';
import {type IPortalConfig} from '../../storybook.js';
import {ErrorDialog} from '../Error/Error.js';
import {ActionHint} from '../Hint/Hint.js';
import {AccountMenu} from '../AccountMenu/AccountMenu.js';
import {LanguageSwitcher} from '../LanguageSwitcher/LanguageSwitcher.js';
import {Login} from '../Login/Login.js';
import {LoginPopup} from '../LoginPopup/LoginPopup.js';
import {OAuthCallback} from '../OAuthCallback/OAuthCallback.js';
import {Portal, type IPortalProps} from '../Portal/Portal.js';
import {Theme, type IThemeConfig} from '../Theme/Theme.js';
import {ThemeSwitcher} from '../ThemeSwitcher/ThemeSwitcher.js';
import {bgLocale} from '../../primereact/locales.js';
const DEFAULT_THEME: IThemeConfig = {
type: 'compact',
palette: 'dark',
// Bundled PrimeReact locales — registered up front so `setLanguage`
// (e.g. from a user's preferred language at login) never crashes the
// Theme with an unknown locale. Only activated when that language is set.
languages: {bg: bgLocale},
};
export interface IAppProps extends IPortalProps {
/**
* Handler proxy injected by the browser platform.
* config.portal may contain schemaUrl, baseUrl, debug, loginRoute.
*/
handlerProxy: IHandlerProxy<{portal?: IBlongPortalConfig} & Record<string, unknown>>;
/** Logger instance */
log?: ILogger;
/** PrimeReact theme configuration (defaults to compact / dark palette) */
theme?: IThemeConfig;
/**
* When provided, renders in place of the Portal shell.
* Useful for Storybook decorators and unit tests that need provider
* context (BlongProvider + Theme) without the full portal UI.
*/
children?: React.ReactNode;
/**
* Optional component rendered instead of the portal when the user is not
* authenticated (i.e. `auth.isAuthenticated` is false).
* The component may call useBlong() to access the handler proxy.
*/
loginComponent?: React.ComponentType;
}
function AppShell({
loginComponent: LoginComponent,
children,
portalProps,
}: {
loginComponent?: React.ComponentType;
children?: React.ReactNode;
portalProps: IPortalProps;
}) {
const {handler, config} = useBlong();
const isAuthenticated = useAppStore(s => s.auth.isAuthenticated);
// Boot-time session restore: exchange the restore cookie for fresh tokens
// so a reload with a live session skips the login screen. `restored`
// gates the first paint to avoid a login-screen flash while the request
// round-trips.
const [restored, setRestored] = React.useState(false);
React.useEffect(() => {
let cancelled = false;
(async () => {
try {
await handler.authSessionGet({}, {});
} catch {
// Ignore — stays logged out.
} finally {
Eif (!cancelled) setRestored(true);
}
})();
return () => {
cancelled = true;
};
}, [handler]);
// OAuth callback handling runs once — after a successful exchange the
// callback URL is stripped and the App must drop this screen (the session
// token is already in the store by then).
const [oauthHandled, setOauthHandled] = React.useState(false);
React.useEffect(() => {
Eif (isAuthenticated) {
(handler.portalConfigGet({}, {}) as Promise<IPortalConfig>)?.then(config => {
useAppStore.getState().setPortalConfig(config);
});
}
}, [handler, isAuthenticated]);
const loginHandler = React.useCallback(
async ({username, password}: {username: string; password: string}) => {
await handler.authLogin({username, password}, {});
},
[handler],
);
const appConfig = config as {
portal?: IBlongPortalConfig;
login?: {registerPage?: string};
google?: {baseUrl?: string; clientId?: string; redirectUri?: string; scope?: string};
};
// Register the app's per-language translation dictionaries so the UI can
// apply the user's preferred language (returned at login) to the locale.
React.useEffect(() => {
const dicts = appConfig.portal?.translations;
Iif (dicts && Object.keys(dicts).length > 0) {
useAppStore.getState().setTranslationsByLanguage(dicts);
}
}, [appConfig.portal?.translations]);
const registerPage = appConfig.login?.registerPage;
const googleEnabled = Boolean(appConfig.google?.baseUrl && appConfig.google?.clientId);
const onGoogle = React.useCallback(() => {
void handler.authGoogleRedirect({}, {});
}, [handler]);
const onExchange = React.useCallback(
async ({code, state, redirectUri}: {code: string; state?: string; redirectUri?: string}) => {
await handler.authGoogleLogin({code, state, redirectUri}, {});
},
[handler],
);
// OAuth callback route — the Google (or mock) redirect target. The app may
// be served under a base path (e.g. `/s/`), so match the suffix.
Iif (
typeof window !== 'undefined' &&
!oauthHandled &&
window.location.pathname.endsWith('/oauth/callback')
) {
return <OAuthCallback onExchange={onExchange} onSuccess={() => setOauthHandled(true)} />;
}
// Wait for the boot-time session restore before painting — avoids flashing
// the login screen when a live session is restored from the cookie.
if (!restored) return null;
Iif (!isAuthenticated) {
return LoginComponent ? (
<LoginComponent />
) : (
<Login
onLogin={loginHandler}
logoIcon="pi pi-globe"
title="Blong Portal"
registerPage={registerPage}
googleLogin={googleEnabled ? {onGoogle} : undefined}
// titleComponent={}
// orgComponent={}
/>
);
}
return children ?? (
<Portal
{...portalProps}
menubarEnd={
<>
<ThemeSwitcher />
<LanguageSwitcher />
<AccountMenu />
</>
}
/>
);
}
export function App({
handlerProxy,
theme = DEFAULT_THEME,
children,
loginComponent,
log,
...portalProps
}: IAppProps) {
return (
<BlongProvider
handlerProxy={handlerProxy}
log={log}
>
<PrimeReactProvider>
<Theme theme={theme}>
<AppShell
loginComponent={loginComponent}
portalProps={portalProps}
>
{children}
</AppShell>
<LoginPopup />
<ErrorDialog />
<ConfirmDialog />
<ConfirmPopup />
<ActionHint />
</Theme>
</PrimeReactProvider>
</BlongProvider>
);
}
|