All files / core/blong-browser/src/context BlongContext.tsx

80.76% Statements 42/52
68.96% Branches 20/29
86.66% Functions 13/15
85.1% Lines 40/47

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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287                                                                                                                                                                                34x       1118x 1118x 1118x                                   388x                               34x                                 4x         4x                           390x   61x   61x 61x 61x 61x 61x 61x 61x 61x 55x 55x   4x               4x       4x 4x 4x         4x             4x                           391x   391x   391x             391x           390x 390x                       390x                                     390x   61x 61x 61x 61x     390x                    
/**
 * BlongContext — the central provider that wires together all blong-browser
 * infrastructure: handler proxy, schema registry, QueryClient, etc.
 */
import type {IHandlerProxy, ILogger, IMeta} from '@feasibleone/blong';
import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
import {createContext, use, useMemo, type ReactNode} from 'react';
import {blongEvents} from '../lib/eventBus.js';
import {schemaRegistry as defaultSchemaRegistry} from '../schema/registry.js';
import {useAppStore, type TranslationDict} from '../state/appStore.js';
import type {IBlongError} from '../types/action.js';
import type {ISchemaRegistry} from '../types/schema.js';
 
/** Portal-specific UI configuration accessed from IHandlerProxy.config.portal */
export interface IBlongPortalConfig {
    /** URL to fetch the OpenAPI schema document from (default: '/openapi.json') */
    schemaUrl?: string;
    /** Application base URL */
    baseUrl?: string;
    /** Enable debug mode */
    debug?: boolean;
    /**
     * TEST-ONLY: when `true`, the wrapped handler proxy is exposed as the
     * `window.__blongHandler` global so Playwright / E2E tests can invoke
     * server methods directly from the page.  Exposing the handler in a real
     * deployment would let any script call privileged methods, so this must
     * only ever be enabled by a test intent (e.g. the `playwright` intent) —
     * never in production.
     */
    testHook?: boolean;
    /**
     * Login route — when set, the global error dialog shows a "Login" button
     * that navigates here. Typically used when a dropdown / load error indicates
     * an expired session (401 / unauthenticated).
     */
    loginRoute?: string;
    /** Login screen configuration (self-registration / social login). */
    login?: {
        /**
         * Action dispatched as `component/${registerPage}` when the Register
         * button is clicked on the Login screen.
         */
        registerPage?: string;
    };
    /** Google OAuth configuration for the "Continue with Google" flow. */
    google?: {
        /** Authorization server base URL (real Google, or the local mock). */
        baseUrl?: string;
        clientId?: string;
        redirectUri?: string;
        scope?: string;
    };
    /**
     * Per-language translation dictionaries (English key → translated string).
     * When provided, the app registers them and `setLanguage(language)` swaps
     * the active table to the matching language's dictionary — so the UI locale
     * follows the user's preferred language returned at login.
     */
    translations?: Record<string, TranslationDict>;
    /**
     * UI languages offered by the menubar language switcher (ad-hoc switching).
     * Each entry is `{value, label}` where `value` matches a translation-
     * dictionary key.  When omitted, the switcher derives the list from the
     * keys of `translations` (and hides when fewer than two are available).
     */
    languages?: Array<{value: string; label: string}>;
}
 
type AnyHandlerProxy = IHandlerProxy<Record<string, unknown>>;
 
/** Context value shape */
export interface IBlongContextValue {
    /** Handler proxy — call methods via handler.subjectObjectPredicate(params, $meta) */
    handler: AnyHandlerProxy['handler'];
    /** Runtime configuration — portal UI settings in config.portal */
    config: Record<string, unknown> & {portal?: IBlongPortalConfig};
    /** Library functions from the handler group */
    lib: AnyHandlerProxy['lib'];
    /** Typed errors from the realm's error layer */
    errors: AnyHandlerProxy['errors'];
    /** Schema registry for OpenAPI schema resolution */
    schemaRegistry: ISchemaRegistry;
    /** TanStack Query client (created internally, exposed for advanced use) */
    queryClient: QueryClient;
    /** Logger instance */
    log?: ILogger;
}
 
const BlongContext = createContext<IBlongContextValue | null>(null);
 
/** Hook to access the blong context value — throws if used outside provider */
export function useBlong(): IBlongContextValue {
    const ctx = use(BlongContext);
    Iif (!ctx) throw new Error('[blong-browser] useBlong must be used inside <BlongProvider>');
    return ctx;
}
 
export interface IBlongProviderProps {
    /**
     * Handler proxy injected by the browser platform (from ready.ts).
     * config.portal may contain schemaUrl, baseUrl, debug, loginRoute.
     */
    handlerProxy: IHandlerProxy<{portal?: IBlongPortalConfig} & Record<string, unknown>>;
    /** Logger instance */
    log?: ILogger;
    /** Custom schema registry (default: singleton) */
    schemaRegistry?: ISchemaRegistry;
    children: ReactNode;
}
 
/** Creates a single QueryClient per provider instance */
function createQueryClient() {
    return new QueryClient({
        defaultOptions: {
            queries: {
                staleTime: 5 * 60 * 1000, // 5 minutes
                retry: 1,
                refetchOnWindowFocus: false,
            },
        },
    });
}
 
/**
 * Runtime error types that indicate an expired/invalid session.  The MLE
 * codec already attempts a token renewal; when that fails the
 * remaining 401 surfaces here and the user is asked to log in again.
 */
const AUTH_ERROR_TYPES = new Set([
    'identity.unauthenticated',
    'identity.invalidCredentials',
    'identity.sessionExpired',
    'rpc.jwtInvalid',
    'rpc.refreshFailed',
    'gateway.jwtMissingHeader',
    'login.refreshTokenExpired',
    'login.sessionNotFound',
    'login.sessionRevoked',
    'login.sessionInactive',
    'login.sessionExpired',
    'login.invalidCookie',
]);
 
/** Whether an error indicates the session is no longer usable (needs re-login). */
function isAuthError(error: unknown): boolean {
    const e = error as {
        type?: string;
        statusCode?: number;
        res?: {statusCode?: number};
    };
    return (
        e?.res?.statusCode === 401 ||
        e?.statusCode === 401 ||
        AUTH_ERROR_TYPES.has(e?.type ?? '')
    );
}
 
/**
 * Wrap the handler proxy so all method calls emit action events and surface
 * non-validation errors via the global error dialog automatically.
 * Field-validation errors are intentionally left for the form to handle.
 * The error is always re-thrown so callers can still react if needed.
 */
function wrapHandlerProxy(handler: AnyHandlerProxy['handler']): AnyHandlerProxy['handler'] {
    return new Proxy(handler, {
        get(target, prop: string | symbol) {
            Iif (typeof prop === 'symbol')
                return (target as Record<symbol, unknown>)[prop as symbol];
            const fn = (target as Record<string, unknown>)[prop as string];
            Iif (typeof fn !== 'function') return fn;
            const method = String(prop);
            return async (...args: unknown[]) => {
                const [params] = args;
                blongEvents.emit('action:before', {method, params});
                try {
                    const result = await (fn as (...a: unknown[]) => Promise<unknown>)(...args);
                    blongEvents.emit('action:success', {method, params, result});
                    return result;
                } catch (err) {
                    blongEvents.emit('action:error', {method, params, error: err});
                    // Name the failed call in the console. A popup that says only
                    // "Not Found" does not say what was not found, and neither does
                    // the dialog's own log line — the method is the context that
                    // turns that into a one-second answer. This is also the only
                    // record of a validation failure, which shows no popup at all.
                    // Parameter *keys* are logged, not values: a login call carries
                    // credentials as values.
                    console.error(`[blong] ${method} failed`, {
                        params: Object.keys((params as object | undefined) ?? {}),
                        error: err,
                    });
                    const blongErr = err as Partial<IBlongError>;
                    Eif (!blongErr.validation?.length) {
                        Iif (isAuthError(err)) {
                            // Token renewal already failed in the codec — ask the
                            // user to log in again via the popup (no auto-retry).
                            useAppStore.getState().setLoginPrompt(true);
                        } else {
                            useAppStore.getState().showError({
                                type: blongErr.type ?? 'error',
                                message: blongErr.message ?? 'An unexpected error occurred.',
                                print: blongErr.print,
                            } as IBlongError);
                        }
                    }
                    throw err;
                }
            };
        },
    }) as AnyHandlerProxy['handler'];
}
 
/** Provider component — wrap your application root with this */
export function BlongProvider({
    handlerProxy,
    schemaRegistry = defaultSchemaRegistry,
    log,
    children,
}: IBlongProviderProps) {
    const queryClient = useMemo(() => createQueryClient(), []);
 
    const {handler, config, lib, errors} = handlerProxy;
 
    const wrappedHandler = useMemo(() => wrapHandlerProxy(handler), [handler]);
 
    // TEST-ONLY test hook: expose the wrapped handler (`__blongHandler`) and the
    // app store (`__blongStore`) on `window` for Playwright / E2E tests.  Gated
    // behind `config.portal.testHook` — a flag that is ONLY set by a test intent
    // (e.g. `playwright`), never in production, so a real deployment never
    // exposes the handler proxy or the store to page scripts.
    Iif (typeof window !== 'undefined' && (config as {portal?: IBlongPortalConfig}).portal?.testHook) {
        const win = window as unknown as Record<string, unknown>;
        win.__blongHandler = wrappedHandler;
        win.__blongStore = useAppStore;
    }
 
    const value = useMemo<IBlongContextValue>(
        () => ({
            handler: wrappedHandler,
            config: config as Record<string, unknown> & {portal?: IBlongPortalConfig},
            lib,
            errors,
            schemaRegistry,
            queryClient,
            log,
        }),
        [wrappedHandler, config, lib, errors, schemaRegistry, queryClient, log],
    );
 
    return (
        <BlongContext value={value}>
            <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
        </BlongContext>
    );
}
 
/**
 * Create a minimal IHandlerProxy from a dispatch function.
 * Useful in tests and Storybook where the full runtime is not available.
 */
export function makeHandlerProxy(
    dispatch: (
        method: string,
        params?: Record<string, unknown>,
        $meta?: IMeta,
    ) => Promise<unknown>,
    config: {portal?: IBlongPortalConfig} & Record<string, unknown> = {},
): IHandlerProxy<{portal?: IBlongPortalConfig} & Record<string, unknown>> {
    const handler = new Proxy({} as Record<string, unknown>, {
        get(_, prop: string | symbol) {
            Iif (typeof prop === 'symbol') return undefined;
            const method = String(prop);
            return (params: Record<string, unknown>, $meta?: IMeta) =>
                dispatch(method, params, $meta);
        },
    });
    return {
        config,
        handler,
        lib: {} as AnyHandlerProxy['lib'],
        errors: {},
        utBus: {info: () => ({encrypt: {}, sign: {}})},
        gateway: {config: () => ({public: {sign: {}, encrypt: {}}})},
        apiSchema: {},
    } as unknown as IHandlerProxy<{portal?: IBlongPortalConfig} & Record<string, unknown>>;
}