import type { Store } from '@reduxjs/toolkit'; import type Bottle from 'bottlejs'; import type { FC, ReactNode } from 'react'; import { useEffect, useRef, useState } from 'react'; import { Provider } from 'react-redux'; import type { ShlinkApiClient } from './api-contract'; import { FeaturesProvider, useFeatures } from './utils/features'; import type { SemVer } from './utils/helpers/version'; import { RoutesPrefixProvider } from './utils/routesPrefix'; import type { Settings } from './utils/settings'; import { SettingsProvider } from './utils/settings'; type ShlinkWebComponentProps = { serverVersion: SemVer; apiClient: ShlinkApiClient; routesPrefix?: string; settings?: Settings; createNotFound?: (nonPrefixedHomePath: string) => ReactNode; }; // FIXME // This allows to track the reference to be resolved by the container, but it's hacky and relies on not more than one // ShlinkWebComponent rendered at the same time let apiClientRef: ShlinkApiClient; export const createShlinkWebComponent = ( bottle: Bottle, ): FC => ({ serverVersion, apiClient, settings, routesPrefix = '', createNotFound }) => { const features = useFeatures(serverVersion); const mainContent = useRef(); const [theStore, setStore] = useState(); useEffect(() => { apiClientRef = apiClient; bottle.value('apiClientFactory', () => apiClientRef); // It's important to not try to resolve services before the API client has been registered, as many other services // depend on it const { container } = bottle; const { Main, store, loadMercureInfo } = container; mainContent.current =
; setStore(store); // Load mercure info store.dispatch(loadMercureInfo(settings)); }, [apiClient]); return !theStore ? <> : ( {mainContent.current} ); };