Improve design of PWA update prompt

This commit is contained in:
Alex Gleason committed 2025-10-27 01:33:00 -05:00
1 parent 05834af874
commit 511a02312d
1 file changed
+15 -37
+15 -37
View File
@@ -1,18 +1,17 @@
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';
import { Sparkles } 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 [isUpdating, setIsUpdating] = useState(false);
const {
offlineReady: [offlineReady],
needRefresh: [needRefresh, setNeedRefresh],
needRefresh: [needRefresh],
updateServiceWorker,
} = useRegisterSW({
onRegisteredSW(swUrl, registration) {
@@ -43,47 +42,26 @@ export function PWAUpdatePrompt() {
}, [needRefresh]);
const handleUpdate = () => {
setIsUpdating(true);
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 className="fixed bottom-4 left-1/2 -translate-x-1/2 md:top-4 md:right-4 md:bottom-auto md:left-auto md:translate-x-0 z-50 animate-in slide-in-from-bottom-4 md:slide-in-from-top-4 duration-500 max-w-full truncate">
<button
onClick={handleUpdate}
disabled={isUpdating}
className="group relative flex items-center gap-2 px-4 py-2.5 bg-gradient-to-r from-amber-100 to-yellow-100 dark:from-amber-900 dark:to-yellow-900 border border-amber-300 dark:border-amber-700 rounded-full shadow-lg hover:shadow-xl transition-all duration-300 hover:scale-105 active:scale-95 disabled:cursor-not-allowed disabled:hover:scale-100 max-w-full truncate"
>
<Sparkles className={`h-4 w-4 text-amber-600 dark:text-amber-400 ${isUpdating ? 'animate-spin' : 'group-hover:rotate-12 transition-transform duration-300'}`} />
<span className="text-sm font-medium whitespace-nowrap max-w-full truncate text-amber-900 dark:text-amber-100">
{isUpdating ? 'Updating...' : 'Update available — Click to refresh'}
</span>
</button>
</div>
);
}