import * as React from "react"; import lazyWithRetry from "~/utils/lazyWithRetry"; export interface LazyComponent> { Component: React.LazyExoticComponent; preload: () => Promise<{ default: T }>; } interface LazyLoadOptions { retries?: number; interval?: number; /** If provided, picks this named export from the module instead of `default`. */ exportName?: string; } /** * Creates a lazy-loaded component with preloading capability and automatic retries on failure. * Supports both default and named exports. * * @param factory A function that returns a promise of a module. * @param options Optional configuration for retry behavior and export name. * @returns An object containing the lazy Component and a preload function. * * @example * ```typescript * // Default export * const MyComponent = createLazyComponent(() => import('./MyComponent')); * * // Named export * const MyComponent = createLazyComponent(() => import('./MyComponent'), { * exportName: 'MyComponent', * }); * ``` */ export function createLazyComponent>( factory: () => Promise>, options: LazyLoadOptions = {} ): LazyComponent { const { retries, interval, exportName } = options; const wrappedFactory = exportName ? () => factory().then((m) => ({ default: m[exportName], })) : (factory as () => Promise<{ default: T }>); return { Component: lazyWithRetry(wrappedFactory, retries, interval), preload: wrappedFactory, }; }