mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add offline support
This commit is contained in:
12 files changed
+4328
-89
No files matched your search
@@ -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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user