'use client'; import { useEffect, useState } from 'react'; import { Alert, LinearProgress, Box, Typography } from '@mui/material'; import { motion, AnimatePresence } from 'framer-motion'; import { CloudOff, CloudQueue, CloudDone } from '@mui/icons-material'; interface OfflineIndicatorProps { isOnline?: boolean; pendingActionsCount?: number; syncInProgress?: boolean; } export const OfflineIndicator = ({ isOnline: propIsOnline, pendingActionsCount = 0, syncInProgress = false, }: OfflineIndicatorProps) => { const [isOnline, setIsOnline] = useState(true); useEffect(() => { // Set initial online status setIsOnline(navigator.onLine); // Listen for online/offline events const handleOnline = () => setIsOnline(true); const handleOffline = () => setIsOnline(false); window.addEventListener('online', handleOnline); window.addEventListener('offline', handleOffline); return () => { window.removeEventListener('online', handleOnline); window.removeEventListener('offline', handleOffline); }; }, []); const effectiveIsOnline = propIsOnline !== undefined ? propIsOnline : isOnline; return ( <> {!effectiveIsOnline && ( } sx={{ borderRadius: 0, boxShadow: 2, }} > You're offline {pendingActionsCount > 0 && ( {pendingActionsCount} action{pendingActionsCount !== 1 ? 's' : ''} will sync when you're back online )} )} {effectiveIsOnline && syncInProgress && ( } sx={{ borderRadius: 0, boxShadow: 2, }} > Syncing data... {pendingActionsCount > 0 && ( {pendingActionsCount} action{pendingActionsCount !== 1 ? 's' : ''} remaining )} )} {effectiveIsOnline && !syncInProgress && pendingActionsCount === 0 && typeof propIsOnline !== 'undefined' && propIsOnline && ( { // Auto-hide after 3 seconds setTimeout(() => { const element = document.getElementById('sync-complete-alert'); if (element) { element.style.display = 'none'; } }, 3000); }} style={{ position: 'fixed', top: 0, left: 0, right: 0, zIndex: 9999, }} id="sync-complete-alert" > } sx={{ borderRadius: 0, boxShadow: 2, }} > All data synced successfully! )} ); };