'use client' import { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { X, Download } from 'lucide-react' interface BeforeInstallPromptEvent extends Event { readonly platforms: string[] readonly userChoice: Promise<{ outcome: 'accepted' | 'dismissed' platform: string }> prompt(): Promise } export default function PWAInstallPrompt() { const [deferredPrompt, setDeferredPrompt] = useState(null) const [showPrompt, setShowPrompt] = useState(false) const [isIOS, setIsIOS] = useState(false) const [isStandalone, setIsStandalone] = useState(false) useEffect(() => { // Check if running on iOS const iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) setIsIOS(iOS) // Check if already installed (standalone mode) const standalone = window.matchMedia('(display-mode: standalone)').matches || (window.navigator as any).standalone === true setIsStandalone(standalone) // Don't show prompt if already installed if (standalone) return // Handle beforeinstallprompt event (Android/Chrome) const handleBeforeInstallPrompt = (e: Event) => { e.preventDefault() setDeferredPrompt(e as BeforeInstallPromptEvent) // Show prompt after a short delay to avoid being too aggressive setTimeout(() => { setShowPrompt(true) }, 3000) } // Handle app installed event const handleAppInstalled = () => { setShowPrompt(false) setDeferredPrompt(null) } window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt) window.addEventListener('appinstalled', handleAppInstalled) // For iOS, show prompt after delay if not already installed if (iOS && !standalone) { setTimeout(() => { setShowPrompt(true) }, 5000) } return () => { window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt) window.removeEventListener('appinstalled', handleAppInstalled) } }, []) const handleInstallClick = async () => { if (deferredPrompt) { // Android/Chrome install deferredPrompt.prompt() const { outcome } = await deferredPrompt.userChoice if (outcome === 'accepted') { setDeferredPrompt(null) setShowPrompt(false) } } // For iOS, the prompt just shows instructions } const handleDismiss = () => { setShowPrompt(false) // Don't show again for this session sessionStorage.setItem('pwa-prompt-dismissed', 'true') } // Don't show if already dismissed in this session useEffect(() => { const dismissed = sessionStorage.getItem('pwa-prompt-dismissed') if (dismissed) { setShowPrompt(false) } }, []) // Don't render if already installed or shouldn't show if (isStandalone || !showPrompt) return null return (

Install SiliconPin

{isIOS ? 'Add to your home screen for a better experience' : 'Install our app for quick access and offline use'}

{isIOS ? (
Tap the share button 📤 and select "Add to Home Screen"
) : null}
) }