export type ConnectionSpeed = 'slow' | 'medium' | 'fast'; export interface SmartLoaderOptions { /** Auto-detect slow connections and adjust behavior */ adaptToConnection?: boolean; /** Show estimated progress based on historical data */ estimateProgress?: boolean; /** Automatically retry on failure */ autoRetry?: boolean; /** Maximum retry attempts */ maxRetries?: number; /** Use intelligent delay based on connection speed */ intelligentDelay?: boolean; /** Callback when loading starts */ onLoadingStart?: () => void; /** Callback when loading completes */ onLoadingComplete?: () => void; /** Callback on retry */ onRetry?: (attempt: number) => void; } export interface UseSmartLoaderReturn { /** Current loading state */ loading: boolean; /** Progress percentage (0-100) */ progress: number; /** Estimated time remaining in seconds */ estimatedTimeRemaining: number; /** Detected connection speed */ connectionSpeed: ConnectionSpeed; /** Whether loader should be shown (accounts for intelligent delay) */ shouldShowLoader: boolean; /** Current retry attempt */ retryAttempt: number; /** Start loading */ startLoading: () => void; /** Stop loading */ stopLoading: (success?: boolean) => void; /** Update progress manually */ updateProgress: (value: number) => void; /** Retry the operation */ retry: () => void; } /** * useSmartLoader - Intelligent loader with adaptive UX * * Combines multiple smart loading features: connection detection, progress estimation, * intelligent delays, and automatic retry logic for optimal user experience. * * @param options - Configuration options * @returns Smart loading state and controls * * @example * ```tsx * function SmartDataLoader() { * const { * loading, * progress, * estimatedTimeRemaining, * connectionSpeed, * shouldShowLoader, * startLoading, * stopLoading, * updateProgress, * } = useSmartLoader({ * adaptToConnection: true, * estimateProgress: true, * autoRetry: true, * maxRetries: 3, * intelligentDelay: true, * }); * * const loadData = async () => { * startLoading(); * try { * const data = await fetchData((progress) => { * updateProgress(progress); * }); * stopLoading(true); * } catch (error) { * stopLoading(false); * } * }; * * return ( * <> * {shouldShowLoader && ( *
Connection: {connectionSpeed}
*ETA: {estimatedTimeRemaining}s
*