All files / src/components OfflineIndicator.tsx

0% Statements 0/249
0% Branches 0/1
0% Functions 0/1
0% Lines 0/249

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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
/**
 * OfflineIndicator Component
 *
 * Displays offline mode status, pending message count, and reconnection info.
 * Shows a prominent banner when the app is offline with visual feedback.
 */
 
import { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import {
  WifiOff,
  CloudOff,
  RefreshCw,
  Clock,
  AlertCircle,
  CheckCircle,
  Send,
  X,
  ChevronDown,
  ChevronUp,
} from 'lucide-react';
import { useOfflineStore, type QueuedMessage } from '../store/offlineStore';
import { useConnectionStore } from '../store/connectionStore';
 
interface OfflineIndicatorProps {
  /** Show compact version (minimal) */
  compact?: boolean;
  /** Show pending messages list */
  showQueue?: boolean;
  /** Additional CSS classes */
  className?: string;
  /** Callback when reconnect button is clicked */
  onReconnect?: () => void;
}
 
/**
 * Format relative time
 */
function formatRelativeTime(timestamp: number): string {
  const seconds = Math.floor((Date.now() - timestamp) / 1000);
 
  if (seconds < 60) return '刚刚';
  if (seconds < 3600) return `${Math.floor(seconds / 60)}分钟前`;
  if (seconds < 86400) return `${Math.floor(seconds / 3600)}小时前`;
  return `${Math.floor(seconds / 86400)}天前`;
}
 
/**
 * Format reconnect delay for display
 */
function formatReconnectDelay(delay: number): string {
  if (delay < 1000) return '立即';
  if (delay < 60000) return `${Math.ceil(delay / 1000)}秒`;
  return `${Math.ceil(delay / 60000)}分钟`;
}
 
/**
 * Truncate message content for display
 */
function truncateContent(content: string, maxLength: number = 50): string {
  if (content.length <= maxLength) return content;
  return content.slice(0, maxLength) + '...';
}
 
/**
 * Full offline indicator with banner, queue, and reconnect info
 */
export function OfflineIndicator({
  compact = false,
  showQueue = true,
  className = '',
  onReconnect,
}: OfflineIndicatorProps) {
  const {
    isOffline,
    isReconnecting,
    reconnectAttempt,
    nextReconnectDelay,
    queuedMessages,
    cancelReconnect,
  } = useOfflineStore();
 
  const connect = useConnectionStore((s) => s.connect);
 
  const [showMessageQueue, setShowMessageQueue] = useState(false);
  const [countdown, setCountdown] = useState<number | null>(null);
 
  // Countdown timer for reconnection
  useEffect(() => {
    if (!isReconnecting || !nextReconnectDelay) {
      setCountdown(null);
      return;
    }
 
    const endTime = Date.now() + nextReconnectDelay;
    setCountdown(nextReconnectDelay);
 
    const interval = setInterval(() => {
      const remaining = Math.max(0, endTime - Date.now());
      setCountdown(remaining);
 
      if (remaining === 0) {
        clearInterval(interval);
      }
    }, 1000);
 
    return () => clearInterval(interval);
  }, [isReconnecting, nextReconnectDelay]);
 
  // Handle manual reconnect
  const handleReconnect = async () => {
    onReconnect?.();
    try {
      await connect();
    } catch (err) {
      console.error('[OfflineIndicator] Manual reconnect failed:', err);
    }
  };
 
  const pendingCount = queuedMessages.filter(
    (m) => m.status === 'pending' || m.status === 'failed'
  ).length;
 
  // Don't show if online and no pending messages
  if (!isOffline && pendingCount === 0) {
    return null;
  }
 
  // Compact version for headers/toolbars
  if (compact) {
    return (
      <div className={`flex items-center gap-2 ${className}`}>
        {isOffline ? (
          <>
            <CloudOff className="w-4 h-4 text-orange-500" />
            <span className="text-sm text-orange-500 font-medium">
              离线模式
            </span>
            {pendingCount > 0 && (
              <span className="text-xs bg-orange-100 dark:bg-orange-900/30 text-orange-600 dark:text-orange-400 px-1.5 py-0.5 rounded">
                {pendingCount} 条待发
              </span>
            )}
          </>
        ) : (
          <>
            <CheckCircle className="w-4 h-4 text-green-500" />
            <span className="text-sm text-green-500">已恢复连接</span>
            {pendingCount > 0 && (
              <span className="text-xs bg-blue-100 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 px-1.5 py-0.5 rounded">
                发送中 {pendingCount} 条
              </span>
            )}
          </>
        )}
      </div>
    );
  }
 
  // Full banner version
  return (
    <AnimatePresence>
      <motion.div
        initial={{ opacity: 0, y: -20 }}
        animate={{ opacity: 1, y: 0 }}
        exit={{ opacity: 0, y: -20 }}
        className={`${className}`}
      >
        {/* Main Banner */}
        <div
          className={`flex items-center gap-3 px-4 py-3 rounded-lg ${
            isOffline
              ? 'bg-orange-50 dark:bg-orange-900/20 border border-orange-200 dark:border-orange-800'
              : 'bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800'
          }`}
        >
          {/* Status Icon */}
          <motion.div
            animate={isReconnecting ? { rotate: 360 } : {}}
            transition={
              isReconnecting
                ? { duration: 1, repeat: Infinity, ease: 'linear' }
                : {}
            }
          >
            {isOffline ? (
              <WifiOff className="w-5 h-5 text-orange-500" />
            ) : (
              <CheckCircle className="w-5 h-5 text-green-500" />
            )}
          </motion.div>
 
          {/* Status Text */}
          <div className="flex-1">
            <div
              className={`text-sm font-medium ${
                isOffline ? 'text-orange-700 dark:text-orange-400' : 'text-green-700 dark:text-green-400'
              }`}
            >
              {isOffline ? '后端服务不可用' : '连接已恢复'}
            </div>
            <div className="text-xs text-gray-500 dark:text-gray-400">
              {isReconnecting ? (
                <>
                  正在尝试重连 ({reconnectAttempt}次)
                  {countdown !== null && (
                    <span className="ml-2">
                      {formatReconnectDelay(countdown)}后重试
                    </span>
                  )}
                </>
              ) : isOffline ? (
                '消息将保存在本地,连接后自动发送'
              ) : pendingCount > 0 ? (
                `正在发送 ${pendingCount} 条排队消息...`
              ) : (
                '所有消息已同步'
              )}
            </div>
          </div>
 
          {/* Actions */}
          <div className="flex items-center gap-2">
            {isOffline && !isReconnecting && (
              <button
                onClick={handleReconnect}
                className="flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-white bg-orange-500 hover:bg-orange-600 rounded-md transition-colors"
              >
                <RefreshCw className="w-4 h-4" />
                重连
              </button>
            )}
            {isReconnecting && (
              <button
                onClick={cancelReconnect}
                className="flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-gray-600 dark:text-gray-300 bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 rounded-md transition-colors"
              >
                <X className="w-4 h-4" />
                取消
              </button>
            )}
            {showQueue && pendingCount > 0 && (
              <button
                onClick={() => setShowMessageQueue(!showMessageQueue)}
                className="flex items-center gap-1 px-2 py-1 text-xs text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
              >
                {showMessageQueue ? (
                  <ChevronUp className="w-4 h-4" />
                ) : (
                  <ChevronDown className="w-4 h-4" />
                )}
                {pendingCount} 条待发
              </button>
            )}
          </div>
        </div>
 
        {/* Message Queue */}
        <AnimatePresence>
          {showMessageQueue && pendingCount > 0 && (
            <motion.div
              initial={{ opacity: 0, height: 0 }}
              animate={{ opacity: 1, height: 'auto' }}
              exit={{ opacity: 0, height: 0 }}
              className="mt-2 bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden"
            >
              <div className="px-4 py-2 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
                <span className="text-sm font-medium text-gray-700 dark:text-gray-300">
                  排队消息
                </span>
              </div>
              <div className="max-h-48 overflow-y-auto">
                {queuedMessages
                  .filter((m) => m.status === 'pending' || m.status === 'failed')
                  .map((msg) => (
                    <QueuedMessageItem key={msg.id} message={msg} />
                  ))}
              </div>
            </motion.div>
          )}
        </AnimatePresence>
      </motion.div>
    </AnimatePresence>
  );
}
 
/**
 * Individual queued message item
 */
function QueuedMessageItem({ message }: { message: QueuedMessage }) {
  const { removeMessage } = useOfflineStore();
 
  const statusConfig = {
    pending: { icon: Clock, color: 'text-gray-400', label: '等待中' },
    sending: { icon: Send, color: 'text-blue-500', label: '发送中' },
    failed: { icon: AlertCircle, color: 'text-red-500', label: '发送失败' },
    sent: { icon: CheckCircle, color: 'text-green-500', label: '已发送' },
  };
 
  const config = statusConfig[message.status];
  const StatusIcon = config.icon;
 
  return (
    <div className="flex items-start gap-3 px-4 py-2 border-b border-gray-100 dark:border-gray-800 last:border-b-0">
      <StatusIcon className={`w-4 h-4 mt-0.5 ${config.color}`} />
 
      <div className="flex-1 min-w-0">
        <p className="text-sm text-gray-700 dark:text-gray-300 truncate">
          {truncateContent(message.content)}
        </p>
        <div className="flex items-center gap-2 mt-1">
          <span className="text-xs text-gray-400">
            {formatRelativeTime(message.timestamp)}
          </span>
          {message.status === 'failed' && message.lastError && (
            <span className="text-xs text-red-500">{message.lastError}</span>
          )}
        </div>
      </div>
 
      {message.status === 'failed' && (
        <button
          onClick={() => removeMessage(message.id)}
          className="p-1 text-gray-400 hover:text-red-500 transition-colors"
          title="删除消息"
        >
          <X className="w-4 h-4" />
        </button>
      )}
    </div>
  );
}
 
/**
 * Minimal connection status indicator for headers
 */
export function ConnectionStatusBadge({ className = '' }: { className?: string }) {
  const connectionState = useConnectionStore((s) => s.connectionState);
  const queuedMessages = useOfflineStore((s) => s.queuedMessages);
 
  const pendingCount = queuedMessages.filter(
    (m) => m.status === 'pending' || m.status === 'failed'
  ).length;
 
  const isConnected = connectionState === 'connected';
 
  return (
    <div className={`flex items-center gap-1.5 ${className}`}>
      <span
        className={`w-2 h-2 rounded-full ${
          isConnected
            ? 'bg-green-400'
            : connectionState === 'reconnecting'
            ? 'bg-orange-400 animate-pulse'
            : 'bg-red-400'
        }`}
      />
      <span
        className={`text-xs ${
          isConnected
            ? 'text-green-500'
            : connectionState === 'reconnecting'
            ? 'text-orange-500'
            : 'text-red-500'
        }`}
      >
        {isConnected ? '在线' : connectionState === 'reconnecting' ? '重连中' : '离线'}
      </span>
      {pendingCount > 0 && (
        <span className="text-xs bg-orange-100 dark:bg-orange-900/30 text-orange-600 dark:text-orange-400 px-1.5 py-0.5 rounded">
          {pendingCount}
        </span>
      )}
    </div>
  );
}
 
export default OfflineIndicator;