All files / src/contexts useConfig.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 2/2
100% Lines 5/5

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                                                361x   361x 2x           359x                             115x    
import { useContext } from 'react';
import type { AppConfig } from '../config/runtime-config';
import { ConfigContext } from './createConfigContext';
 
/**
 * Hook to access runtime configuration
 *
 * Use this hook in any component that needs access to environment-specific
 * configuration like API endpoints, authentication settings, or feature flags.
 *
 * @returns Runtime configuration
 * @throws Error if used outside ConfigProvider
 *
 * @example
 * ```tsx
 * function MyComponent() {
 *   const config = useConfig();
 *   const apiUrl = config.apiBaseUrl;
 *   const isAnalyticsEnabled = config.features.analytics;
 *   // ...
 * }
 * ```
 */
export function useConfig(): AppConfig {
  const config = useContext(ConfigContext);
 
  if (!config) {
    throw new Error(
      'useConfig must be used within ConfigProvider. ' +
        'Ensure that your component is wrapped in <ConfigProvider>.'
    );
  }
 
  return config;
}
 
/**
 * Hook to access runtime configuration WITHOUT throwing when it is not yet loaded.
 *
 * Since the config gate was decoupled (the app shell now renders before
 * `GET /api/v1/config` resolves), components that may render during that brief window
 * — notably `useTurnstile`, which sits on the eager public homepage path via the
 * newsletter widget — must tolerate a null config and re-render when it arrives.
 * Treat `null` as "config not ready yet" (e.g. feature flags default to off).
 *
 * @returns Runtime configuration, or `null` if it has not loaded yet.
 */
export function useOptionalConfig(): AppConfig | null {
  return useContext(ConfigContext);
}