import * as React from 'react'; interface BeforeInstallPromptEvent extends Event { readonly platforms: string[]; readonly userChoice: Promise<{ outcome: 'accepted' | 'dismissed'; platform: string; }>; prompt(): Promise; } export function InstallPrompt() { const [deferredPrompt, setDeferredPrompt] = React.useState(null); const [showPrompt, setShowPrompt] = React.useState(false); const [isIOS, setIsIOS] = React.useState(false); const [isInstalled, setIsInstalled] = React.useState(false); React.useEffect(() => { // Check if already installed if (window.matchMedia('(display-mode: standalone)').matches) { setIsInstalled(true); return; } // Check if iOS const ua = window.navigator.userAgent; const isIOSDevice = /iPad|iPhone|iPod/.test(ua) && !(window as any).MSStream; setIsIOS(isIOSDevice); // Check if user has dismissed the prompt recently const dismissedAt = localStorage.getItem('pwa-prompt-dismissed'); if (dismissedAt) { const dismissedTime = new Date(dismissedAt).getTime(); const now = Date.now(); const dayInMs = 24 * 60 * 60 * 1000; if (now - dismissedTime < 7 * dayInMs) { return; } } // For non-iOS devices, wait for the beforeinstallprompt event const handleBeforeInstallPrompt = (e: Event) => { e.preventDefault(); setDeferredPrompt(e as BeforeInstallPromptEvent); setShowPrompt(true); }; window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt); // For iOS, show prompt after a delay if not installed if (isIOSDevice && !navigator.standalone) { const timer = setTimeout(() => { setShowPrompt(true); }, 3000); return () => clearTimeout(timer); } return () => { window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt); }; }, []); const handleInstall = async () => { if (!deferredPrompt) return; deferredPrompt.prompt(); const { outcome } = await deferredPrompt.userChoice; if (outcome === 'accepted') { setShowPrompt(false); setIsInstalled(true); } setDeferredPrompt(null); }; const handleDismiss = () => { setShowPrompt(false); localStorage.setItem('pwa-prompt-dismissed', new Date().toISOString()); }; if (!showPrompt || isInstalled) { return null; } return (
{/* App Icon */}
{/* Content */}

Install LocalGreenChain

{isIOS ? 'Tap the share button and select "Add to Home Screen"' : 'Install our app for a better experience with offline support'}

{/* Close button */}
{/* iOS Instructions */} {isIOS && (
Then tap "Add to Home Screen"
)} {/* Install Button (non-iOS) */} {!isIOS && deferredPrompt && (
)}
); } export default InstallPrompt;