File size: 6,747 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useEffect, useState } from 'react';
import { AlertCircle, X, RefreshCw, WifiOff } from 'lucide-react';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { checkApiConnection } from '@/lib/api';
import { API_BASE_URL } from '@/config';
import apiClient from '@/lib/api/axios-instance';

const ApiConnectionAlert = () => {
  const [isApiAvailable, setIsApiAvailable] = useState<boolean | null>(null);
  const [checking, setChecking] = useState(false);
  const [isVisible, setIsVisible] = useState(true);
  const [retryCount, setRetryCount] = useState(0);
  const [fadeOut, setFadeOut] = useState(false);

  const checkConnection = async () => {
    setChecking(true);
    try {
      console.log('Checking API connection...');
      const isAvailable = await checkApiConnection();
      console.log('API connection check result:', isAvailable);
      setIsApiAvailable(isAvailable);
      
      if (isAvailable) {
        // If connection is restored after previous failures, show success briefly then hide
        if (retryCount > 0) {
          setTimeout(() => {
            setFadeOut(true);
            setTimeout(() => setIsVisible(false), 300);
          }, 3000);
        }
      } else {
        setRetryCount(prev => prev + 1);
      }
    } catch (error) {
      console.error('Error checking API connection:', error);
      setIsApiAvailable(false);
      setRetryCount(prev => prev + 1);
    } finally {
      setChecking(false);
    }
  };

  useEffect(() => {
    checkConnection();
    
    // Set up automatic retry every 30 seconds if API is down
    const intervalId = setInterval(() => {
      if (isApiAvailable === false) {
        checkConnection();
      }
    }, 30000);
    
    return () => clearInterval(intervalId);
  }, [isApiAvailable]);

  const closeAlert = () => {
    setFadeOut(true);
    setTimeout(() => setIsVisible(false), 300);
  };

  if (isApiAvailable === true && !isVisible) {
    return null;
  }

  return (
    <>

      {(isApiAvailable === false || (isApiAvailable === true && retryCount > 0)) && isVisible && (

        <div 

          className={`fixed bottom-4 right-4 z-50 max-w-md w-full transition-all duration-300 ${

            fadeOut ? 'opacity-0 translate-y-5' : 'opacity-100 translate-y-0'

          }`}

        >

          <Alert

            variant={isApiAvailable === true ? "default" : "destructive"}

            className={`

              relative shadow-lg border-2 

              ${isApiAvailable === true 

                ? "bg-green-50 border-green-200 dark:bg-green-900/20 dark:border-green-800" 

                : "bg-red-50 border-red-200 dark:bg-red-900/20 dark:border-red-800"}

            `}

          >

            <Button

              variant="ghost"

              size="icon"

              className="absolute top-2 right-2 h-6 w-6 rounded-full p-0"

              onClick={closeAlert}

            >

              <X className="h-4 w-4" />

            </Button>

            

            {isApiAvailable === true ? (

              <div className="flex items-start">

                <div className="rounded-full bg-green-100 dark:bg-green-800 p-2 mr-3">

                  <RefreshCw className="h-4 w-4 text-green-600 dark:text-green-200" />

                </div>

                <div>

                  <AlertTitle className="text-green-700 dark:text-green-300">

                    API Connection Restored

                  </AlertTitle>

                  <AlertDescription className="text-green-600 dark:text-green-400">

                    <p className="mb-2">

                      Successfully connected to the API server. You can continue using the application.

                    </p>

                  </AlertDescription>

                </div>

              </div>

            ) : (

              <div className="flex items-start">

                <div className="rounded-full bg-red-100 dark:bg-red-800 p-2 mr-3">

                  <WifiOff className="h-4 w-4 text-red-600 dark:text-red-200" />

                </div>

                <div>

                  <AlertTitle className="text-red-700 dark:text-red-300">

                    API Connection Error

                  </AlertTitle>

                  <AlertDescription className="text-red-600 dark:text-red-400">

                    <p className="mb-3">

                      Cannot connect to the API server at <code className="px-1 py-0.5 bg-red-100 dark:bg-red-900/30 rounded">{API_BASE_URL}</code>.

                      Some features may not work correctly.

                    </p>

                    

                    <div className="mb-3 space-y-1 pl-5">

                      <p className="font-medium text-sm">Troubleshooting steps:</p>

                      <ul className="list-disc pl-4 text-sm space-y-1">

                        <li>Ensure the API server is running</li>

                        <li>Check your network connection</li>

                        <li>Verify that ports are not blocked by a firewall</li>

                        <li>If you're a developer, check for CORS issues</li>

                      </ul>

                    </div>

                    

                    <div className="flex justify-end gap-2 mt-2">

                      <Button 

                        variant="outline" 

                        size="sm"

                        onClick={closeAlert}

                        className="border-red-200 hover:bg-red-100 hover:text-red-800"

                      >

                        Dismiss

                      </Button>

                      <Button 

                        size="sm"

                        onClick={checkConnection} 

                        disabled={checking}

                        className="bg-red-600 hover:bg-red-700 text-white"

                      >

                        {checking ? (

                          <>

                            <RefreshCw className="h-3 w-3 mr-1 animate-spin" />

                            Checking...

                          </>

                        ) : (

                          <>

                            <RefreshCw className="h-3 w-3 mr-1" />

                            Retry Connection

                          </>

                        )}

                      </Button>

                    </div>

                  </AlertDescription>

                </div>

              </div>

            )}

          </Alert>

        </div>

      )}

    </>
  );
};

export default ApiConnectionAlert;