Add offline support

This commit is contained in:
Alex Gleason committed 2025-10-25 10:25:33 -05:00
1 parent d75bd386aa
commit cfa8f69ad5
12 files changed
+4328 -89

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
import { WifiOff, Wifi } from 'lucide-react';
import { useEffect, useState } from 'react';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { useOffline } from '@/hooks/useOffline';
/**
* Component that displays an indicator when the app is offline
*/
export function OfflineIndicator() {
const { isOffline, isOnline } = useOffline();
const [showOnlineNotification, setShowOnlineNotification] = useState(false);
const [wasOffline, setWasOffline] = useState(false);
useEffect(() => {
if (isOffline) {
setWasOffline(true);
}
if (isOnline && wasOffline) {
// Show "back online" notification briefly
setShowOnlineNotification(true);
const timer = setTimeout(() => {
setShowOnlineNotification(false);
setWasOffline(false);
}, 3000);
return () => clearTimeout(timer);
}
}, [isOffline, isOnline, wasOffline]);
if (!isOffline && !showOnlineNotification) {
return null;
}
return (
<div className="fixed bottom-4 left-4 right-4 md:left-auto md:right-4 md:w-96 z-50 animate-in slide-in-from-bottom-4">
{isOffline ? (
<Alert variant="destructive" className="shadow-lg border-2">
<WifiOff className="h-4 w-4" />
<AlertDescription className="font-medium">
You're offline. Some features may be limited.
</AlertDescription>
</Alert>
) : (
<Alert className="shadow-lg border-2 border-green-500 bg-green-50 dark:bg-green-950">
<Wifi className="h-4 w-4 text-green-600 dark:text-green-400" />
<AlertDescription className="font-medium text-green-600 dark:text-green-400">
You're back online!
</AlertDescription>
</Alert>
)}
</div>
);
}
+89
View File
@@ -0,0 +1,89 @@
import { useEffect, useState } from 'react';
import { useRegisterSW } from 'virtual:pwa-register/react';
import { Button } from '@/components/ui/button';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Download, X } from 'lucide-react';
/**
* Component that prompts users to update when a new service worker is available
*/
export function PWAUpdatePrompt() {
const [showPrompt, setShowPrompt] = useState(false);
const {
offlineReady: [offlineReady],
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker,
} = useRegisterSW({
onRegisteredSW(swUrl, registration) {
console.log('[PWA] Service worker registered:', swUrl);
// Check for updates periodically (every hour)
if (registration) {
setInterval(() => {
registration.update();
}, 60 * 60 * 1000);
}
},
onRegisterError(error) {
console.error('[PWA] Service worker registration error:', error);
},
});
useEffect(() => {
if (offlineReady) {
console.log('[PWA] App ready to work offline');
}
}, [offlineReady]);
useEffect(() => {
if (needRefresh) {
setShowPrompt(true);
}
}, [needRefresh]);
const handleUpdate = () => {
updateServiceWorker(true);
};
const handleDismiss = () => {
setShowPrompt(false);
setNeedRefresh(false);
};
if (!showPrompt) {
return null;
}
return (
<div className="fixed bottom-4 left-4 right-4 md:left-auto md:right-4 md:w-96 z-50 animate-in slide-in-from-bottom-4">
<Alert className="shadow-lg border-2 border-primary">
<Download className="h-4 w-4" />
<AlertTitle className="flex items-center justify-between">
New Version Available
<Button
variant="ghost"
size="icon"
className="h-6 w-6"
onClick={handleDismiss}
>
<X className="h-4 w-4" />
</Button>
</AlertTitle>
<AlertDescription className="space-y-3">
<p className="text-sm">
A new version of Shakespeare is available. Update now to get the latest features and improvements.
</p>
<div className="flex gap-2">
<Button onClick={handleUpdate} size="sm" className="flex-1">
Update Now
</Button>
<Button onClick={handleDismiss} size="sm" variant="outline">
Later
</Button>
</div>
</AlertDescription>
</Alert>
</div>
);
}