File size: 2,054 Bytes
0b85fad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from "react";
import { BACKEND_URL, APP_CONFIG } from "../config/constants";

interface BackendHealthCheckProps {
  backendUrl?: string;
  checkInterval?: number;
}

export function BackendHealthCheck({ 
  checkInterval = APP_CONFIG.HEALTH_CHECK_INTERVAL
}: BackendHealthCheckProps) {
  const [backendStatus, setBackendStatus] = useState<'loading' | 'online' | 'offline'>('loading');
  const healthCheckUrl = `${BACKEND_URL}/api/health`;

  useEffect(() => {
    const checkBackendHealth = async () => {
      try {
        const response = await fetch(healthCheckUrl);
        if (response.ok) {
          const data = await response.json();
          if (data.status === 'ok') {
            setBackendStatus('online');
          } else {
            setBackendStatus('offline');
          }
        } else {
          setBackendStatus('offline');
        }
      } catch (error) {
        setBackendStatus('offline');
      }
    };

    checkBackendHealth();
    
    // Check health at specified interval
    const interval = setInterval(checkBackendHealth, checkInterval);
    
    return () => clearInterval(interval);
  }, [healthCheckUrl, checkInterval]);

  const getStatusColor = () => {
    switch (backendStatus) {
      case 'online':
        return 'bg-green-500';
      case 'offline':
        return 'bg-red-500';
      case 'loading':
        return 'bg-yellow-500';
      default:
        return 'bg-gray-500';
    }
  };

  const getStatusText = () => {
    switch (backendStatus) {
      case 'online':
        return 'Backend Online';
      case 'offline':
        return 'Backend Offline';
      case 'loading':
        return 'Checking Backend...';
      default:
        return 'Unknown Status';
    }
  };

  return (
    <div className="absolute top-4 right-4 flex items-center space-x-2 bg-gray-800 rounded-lg px-3 py-2">
      <div className={`w-3 h-3 rounded-full ${getStatusColor()} animate-pulse`}></div>
      <span className="text-sm font-medium">{getStatusText()}</span>
    </div>
  );
}