All files / src/components/features/admin/monitoring/LiveIndicator index.tsx

97.67% Statements 168/172
92.3% Branches 24/26
100% Functions 3/3
97.67% Lines 168/172

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x         1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 20x 16x 16x 16x 16x 16x 16x 16x 16x 16x 36x 36x 36x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 3x 3x 3x 33x 33x 33x 33x 33x 33x 33x 33x 36x 21x 21x 21x 36x 36x 36x 36x 36x 36x 36x 36x 36x 2x 36x 36x 36x 30x 36x 36x 36x 36x 36x 4x 4x 4x 4x 4x 4x 4x 36x 36x 36x 36x 1x 1x  
'use client';
 
import React, { useState, useEffect } from 'react';
import type { ConnectionStatus } from '@/hooks/useRealtimeMetrics';
 
export interface LiveIndicatorProps {
  /** Connection status */
  status: ConnectionStatus;
  /** Last update timestamp */
  lastUpdate: Date | null;
  /** Whether using polling fallback */
  isPolling?: boolean;
  /** Callback to manually reconnect */
  onReconnect?: () => void;
  /** Show compact version */
  compact?: boolean;
  /** Additional CSS classes */
  className?: string;
}
 
/**
 * Status configuration
 */
const statusConfig: Record<
  ConnectionStatus,
  { color: string; bgColor: string; pulseColor: string; label: string }
> = {
  connected: {
    color: 'bg-green-500',
    bgColor: 'bg-green-100 dark:bg-green-900/30',
    pulseColor: 'bg-green-400',
    label: 'Live',
  },
  connecting: {
    color: 'bg-blue-500',
    bgColor: 'bg-blue-100 dark:bg-blue-900/30',
    pulseColor: 'bg-blue-400',
    label: 'Connecting',
  },
  reconnecting: {
    color: 'bg-yellow-500',
    bgColor: 'bg-yellow-100 dark:bg-yellow-900/30',
    pulseColor: 'bg-yellow-400',
    label: 'Reconnecting',
  },
  disconnected: {
    color: 'bg-red-500',
    bgColor: 'bg-red-100 dark:bg-red-900/30',
    pulseColor: 'bg-red-400',
    label: 'Disconnected',
  },
  polling: {
    color: 'bg-orange-500',
    bgColor: 'bg-orange-100 dark:bg-orange-900/30',
    pulseColor: 'bg-orange-400',
    label: 'Polling',
  },
};
 
/**
 * Format relative time
 */
function formatRelativeTime(date: Date): string {
  const now = new Date();
  const diffMs = now.getTime() - date.getTime();
  const diffSec = Math.floor(diffMs / 1000);
 
  if (diffSec < 5) return 'just now';
  if (diffSec < 60) return `${diffSec}s ago`;
 
  const diffMin = Math.floor(diffSec / 60);
  if (diffMin < 60) return `${diffMin}m ago`;

  const diffHour = Math.floor(diffMin / 60);
  return `${diffHour}h ago`;
}
 
/**
 * LiveIndicator Component
 *
 * Shows real-time connection status with animated indicator.
 */
export function LiveIndicator({
  status,
  lastUpdate,
  isPolling = false,
  onReconnect,
  compact = false,
  className = '',
}: LiveIndicatorProps) {
  const [relativeTime, setRelativeTime] = useState<string>('');
  const config = statusConfig[status];
 
  // Update relative time every second
  useEffect(() => {
    if (!lastUpdate) return;
 
    const updateTime = () => {
      setRelativeTime(formatRelativeTime(lastUpdate));
    };
 
    updateTime();
    const intervalId = setInterval(updateTime, 1000);
 
    return () => clearInterval(intervalId);
  }, [lastUpdate]);
 
  if (compact) {
    return (
      <div
        className={`flex items-center gap-1.5 ${className}`}
        title={`${config.label}${lastUpdate ? ` - Updated ${relativeTime}` : ''}`}
        data-testid="live-indicator"
      >
        <span className="relative flex h-2.5 w-2.5">
          {(status === 'connected' || status === 'connecting' || status === 'reconnecting') && (
            <span
              className={`absolute inline-flex h-full w-full animate-ping rounded-full opacity-75 ${config.pulseColor}`}
            />
          )}
          <span className={`relative inline-flex h-2.5 w-2.5 rounded-full ${config.color}`} />
        </span>
        {status === 'connected' && (
          <span className="text-xs font-medium text-green-600 dark:text-green-400">LIVE</span>
        )}
      </div>
    );
  }
 
  return (
    <div
      className={`flex items-center gap-3 px-3 py-2 rounded-lg ${config.bgColor} ${className}`}
      data-testid="live-indicator"
    >
      {/* Status Dot */}
      <span className="relative flex h-3 w-3">
        {(status === 'connected' || status === 'connecting' || status === 'reconnecting') && (
          <span
            className={`absolute inline-flex h-full w-full animate-ping rounded-full opacity-75 ${config.pulseColor}`}
          />
        )}
        <span className={`relative inline-flex h-3 w-3 rounded-full ${config.color}`} />
      </span>
 
      {/* Status Text */}
      <div className="flex flex-col">
        <span className="text-sm font-medium text-gray-900 dark:text-white flex items-center gap-2">
          {config.label}
          {isPolling && status === 'polling' && (
            <span className="text-xs text-gray-500 dark:text-gray-400">(Fallback)</span>
          )}
        </span>
        {lastUpdate && (
          <span className="text-xs text-gray-500 dark:text-gray-400">Updated {relativeTime}</span>
        )}
      </div>
 
      {/* Reconnect Button (shown when disconnected) */}
      {status === 'disconnected' && onReconnect && (
        <button
          onClick={onReconnect}
          className="ml-auto px-2 py-1 text-xs font-medium text-white bg-blue-600 hover:bg-blue-700 rounded transition-colors"
          data-testid="reconnect-button"
        >
          Reconnect
        </button>
      )}
    </div>
  );
}
 
export default LiveIndicator;