import { AppError } from '../monitoring/errorTypes'; /** * Async operation state */ export interface AsyncState { /** Operation result data */ data: T | null; /** Error if operation failed */ error: AppError | null; /** Whether operation is in progress */ isLoading: boolean; /** Whether operation has errored */ isError: boolean; /** Whether operation completed successfully */ isSuccess: boolean; /** Number of retry attempts made */ retryCount: number; } /** * Recovery options for async operations */ export interface RecoveryOptions { /** Maximum retry attempts */ maxRetries?: number; /** Initial retry delay in ms */ retryDelay?: number; /** Whether to auto-retry on failure */ autoRetry?: boolean; /** Whether to execute on mount */ executeOnMount?: boolean; /** Callback on error */ onError?: (error: AppError) => void; /** Callback on success */ onSuccess?: (data: T) => void; /** Callback on each retry */ onRetry?: (attempt: number) => void; } /** * Async operation with recovery controls */ export interface AsyncWithRecovery extends AsyncState { /** Execute the async operation */ execute: () => Promise; /** Retry the operation */ retry: () => Promise; /** Reset state */ reset: () => void; } /** * Hook for async operations with automatic error recovery * * @example * ```tsx * function UserProfile({ userId }: { userId: string }) { * const { * data: user, * isLoading, * isError, * error, * execute, * retry * } = useAsyncWithRecovery( * () => fetchUser(userId), * { * executeOnMount: true, * maxRetries: 3, * autoRetry: true, * onError: (err) => toast.error(err.message) * } * ); * * if (isLoading) return ; * if (isError) return ; * return ; * } * ``` */ export declare function useAsyncWithRecovery(asyncFn: () => Promise, options?: RecoveryOptions): AsyncWithRecovery; /** * Result of network-aware operation hook */ export interface NetworkAwareOperationResult { /** Execute the operation */ execute: () => Promise; /** Current online status */ isOnline: boolean; /** Whether waiting for network */ isWaiting: boolean; /** Error if any */ error: AppError | null; } /** * Hook for network-aware operations that wait for connectivity * * @example * ```tsx * function SyncButton() { * const { execute, isOnline, isWaiting } = useNetworkAwareOperation( * () => syncData(), * { requiresNetwork: true, waitForOnline: true } * ); * * return ( * * ); * } * ``` */ export declare function useNetworkAwareOperation(operation: () => Promise, options?: { /** Whether operation requires network */ requiresNetwork?: boolean; /** Whether to wait for online status */ waitForOnline?: boolean; /** Timeout for waiting (ms) */ onlineTimeout?: number; }): NetworkAwareOperationResult; /** * Result of optimistic update hook */ export interface OptimisticUpdateResult { /** Current value (optimistic or confirmed) */ value: T; /** Perform optimistic update */ update: (optimisticValue: T, updateData: U) => Promise; /** Whether update is pending */ isPending: boolean; /** Error if update failed */ error: AppError | null; } /** * Hook for optimistic updates with automatic rollback on failure * * @example * ```tsx * function LikeButton({ post }: { post: Post }) { * const { value: likes, update, isPending } = useOptimisticUpdate( * post.likes, * (newLikes) => updatePostLikes(post.id, newLikes), * { onRollback: () => toast.error('Failed to like') } * ); * * const handleLike = () => { * update(likes + 1, likes + 1); * }; * * return ( * * ); * } * ``` */ export declare function useOptimisticUpdate(currentValue: T, updateFn: (update: U) => Promise, options?: { /** Callback when rollback occurs */ onRollback?: (original: T, error: AppError) => void; /** Callback on successful update */ onSuccess?: (newValue: T) => void; }): OptimisticUpdateResult; /** * Result of safe callback hook */ export interface SafeCallbackResult { /** Execute callback with error handling */ execute: (...args: Args) => Promise; /** Error if callback failed */ error: AppError | null; /** Clear error state */ clearError: () => void; } /** * Hook that wraps a callback with error handling * * @example * ```tsx * function DeleteButton({ onDelete }: { onDelete: () => Promise }) { * const { execute, error, clearError } = useSafeCallback(onDelete, { * onError: (err) => console.error('Delete failed:', err), * reportError: true * }); * * return ( * <> * * {error &&

Error: {error.message}

} * * ); * } * ``` */ export declare function useSafeCallback(callback: (...args: Args) => Return | Promise, options?: { /** Callback on error */ onError?: (error: AppError) => void; /** Fallback return value on error */ fallbackValue?: Return; /** Whether to report errors */ reportError?: boolean; }): SafeCallbackResult; /** * Toast error entry */ export interface ToastEntry { id: string; error: AppError; } /** * Result of error toast hook */ export interface ErrorToastResult { /** Current toast entries */ toasts: ToastEntry[]; /** Show error toast */ showError: (error: unknown) => void; /** Dismiss specific toast */ dismissToast: (id: string) => void; /** Clear all toasts */ clearAll: () => void; } /** * Hook for managing error toasts with auto-dismiss * * @example * ```tsx * function App() { * const { toasts, showError, dismissToast } = useErrorToast({ * duration: 5000, * maxToasts: 3 * }); * * return ( * <> * * {toasts.map(t => ( * dismissToast(t.id)} /> * ))} * * {// Pass showError to child components //} * * ); * } * ``` */ export declare function useErrorToast(options?: { /** Auto-dismiss duration in ms */ duration?: number; /** Maximum toasts to show */ maxToasts?: number; }): ErrorToastResult; /** * Recovery state for component error handling */ export interface RecoveryState { /** Current error if any */ error: AppError | null; /** Number of recovery attempts */ recoveryAttempts: number; /** Whether currently recovering */ isRecovering: boolean; /** Set error state */ setError: (error: unknown) => void; /** Clear error state */ clearError: () => void; /** Attempt recovery */ attemptRecovery: (recoveryFn: () => Promise) => Promise; /** Reset all state */ reset: () => void; } /** * Hook for managing component-level error recovery state * * @example * ```tsx * function DataFetcher() { * const recovery = useRecoveryState({ maxAttempts: 3 }); * * useEffect(() => { * fetchData().catch(recovery.setError); * }, []); * * if (recovery.error) { * return ( * recovery.attemptRecovery(() => fetchData())} * isRetrying={recovery.isRecovering} * /> * ); * } * * return ; * } * ``` */ export declare function useRecoveryState(options?: { /** Maximum recovery attempts */ maxAttempts?: number; /** Callback when max attempts reached */ onMaxAttemptsReached?: (error: AppError) => void; }): RecoveryState; /** * Contextual error information for better UX */ export interface ErrorContext { /** User action that triggered the error */ action: string; /** Resource being accessed */ resource?: string; /** Feature name */ feature?: string; } /** * Hook for managing contextual error information * * @example * ```tsx * function UserEditor() { * const errorContext = useErrorContext({ * action: 'update profile', * resource: 'user settings', * feature: 'Profile' * }); * * const handleSave = async () => { * try { * await saveProfile(); * } catch (err) { * // Error will have context attached * errorContext.reportError(err); * } * }; * * return
; * } * ``` */ export declare function useErrorContext(context: ErrorContext): { context: ErrorContext; reportError: (error: unknown) => void; createContextualError: (error: unknown) => AppError; };