import type React from "react"; import { EmptyState } from "./EmptyState"; import { ErrorState } from "./ErrorState"; import { Skeleton } from "./Skeleton"; export interface AsyncBoundaryProps { loading: boolean; error: Error | null; data: unknown | null; empty?: boolean; emptyText?: string; fallback?: React.ReactNode; onRetry?: () => void; children: React.ReactNode; } export function AsyncBoundary({ loading, error, data, empty = false, emptyText = "No data available", fallback, onRetry, children, }: AsyncBoundaryProps) { // If there's an error and no stale data, render ErrorState if (error && data === null) { return ; } // If it's loading and there is no stale data, render Loading state / Skeleton if (loading && data === null) { if (fallback) { return <>{fallback}; } return (
); } // If there is data but it's empty, render EmptyState if (!loading && (empty || data === null)) { return ; } // Render children (stale data is kept visible even if loading is true in background) return <>{children}; }