'use client'; import React, { useContext } from 'react'; import OfflineContext from '@/context/OfflineContext'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faWifi, faWifiSlash, faSync, faCircle, faExclamationTriangle, faCheck } from '@fortawesome/free-solid-svg-icons'; /** * OfflineIndicator - Displays current online/offline status and sync progress * Allows user to toggle manual offline mode */ export default function OfflineIndicator() { const { offlineMode, toggleOfflineMode, syncNow } = useContext(OfflineContext); const { isOffline, isManuallyOffline, isNetworkOnline, isDatabaseInitialized, syncProgress, lastSyncAt, error } = offlineMode; // Determine status color and text const getStatusInfo = () => { if (!isDatabaseInitialized) { return { color: 'text-gray-400', bgColor: 'bg-gray-100', icon: faCircle, text: 'DB non initialisée' }; } if (error) { return { color: 'text-red-500', bgColor: 'bg-red-50', icon: faExclamationTriangle, text: 'Erreur de sync' }; } if (syncProgress.isSyncing) { return { color: 'text-blue-500', bgColor: 'bg-blue-50', icon: faSync, text: 'Synchronisation...', spin: true }; } if (isOffline) { if (syncProgress.pendingChanges > 0) { return { color: 'text-orange-500', bgColor: 'bg-orange-50', icon: faWifiSlash, text: `Hors ligne (${syncProgress.pendingChanges} en attente)` }; } return { color: 'text-orange-500', bgColor: 'bg-orange-50', icon: faWifiSlash, text: isManuallyOffline ? 'Mode hors ligne' : 'Hors ligne' }; } if (syncProgress.pendingChanges > 0) { return { color: 'text-yellow-500', bgColor: 'bg-yellow-50', icon: faSync, text: `${syncProgress.pendingChanges} changements à sync` }; } return { color: 'text-green-500', bgColor: 'bg-green-50', icon: faCheck, text: 'Synchronisé' }; }; const statusInfo = getStatusInfo(); // Format last sync time const formatLastSync = () => { if (!lastSyncAt) return 'Jamais'; const diff = Date.now() - lastSyncAt; const minutes = Math.floor(diff / 60000); const hours = Math.floor(minutes / 60); if (hours > 0) return `Il y a ${hours}h`; if (minutes > 0) return `Il y a ${minutes}min`; return 'À l\'instant'; }; const handleSyncNow = async () => { if (!isOffline && !syncProgress.isSyncing) { await syncNow(); } }; return (
{/* Status indicator */}
{statusInfo.text}
{/* Last sync time */} {isDatabaseInitialized && !isOffline && ( Dernière sync: {formatLastSync()} )} {/* Error message */} {error && ( {error} )} {/* Actions */}
{/* Manual sync button */} {isDatabaseInitialized && !isOffline && !syncProgress.isSyncing && ( )} {/* Offline toggle */} {isDatabaseInitialized && ( )}
); }