File size: 1,882 Bytes
d97b8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from 'react';
import { Loader2, RefreshCw, AlertCircle } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';

interface LoadingStateProps {
  isLoading?: boolean;
  error?: Error | null;
  onRetry?: () => void;
  loadingText?: string;
  errorText?: string;
  children?: React.ReactNode;
  className?: string;
}

const LoadingState: React.FC<LoadingStateProps> = ({

  isLoading = false,

  error = null,

  onRetry,

  loadingText = 'Loading...',

  errorText = 'Failed to load data',

  children,

  className = ''

}) => {
  if (error) {
    return (
      <Card className={`w-full ${className}`}>

        <CardContent className="flex flex-col items-center justify-center py-8 space-y-4">

          <AlertCircle className="h-8 w-8 text-destructive" />

          <div className="text-center space-y-2">

            <p className="text-sm font-medium text-destructive">

              {errorText}

            </p>

            <p className="text-xs text-muted-foreground">

              {error.message}

            </p>

          </div>

          {onRetry && (

            <Button onClick={onRetry} variant="outline" size="sm">

              <RefreshCw className="h-4 w-4 mr-2" />

              Try Again

            </Button>

          )}

        </CardContent>

      </Card>
    );
  }

  if (isLoading) {
    return (
      <Card className={`w-full ${className}`}>

        <CardContent className="flex flex-col items-center justify-center py-8 space-y-4">

          <Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />

          <p className="text-sm text-muted-foreground">

            {loadingText}

          </p>

        </CardContent>

      </Card>
    );
  }

  return <>{children}</>;
};

export default LoadingState;