'use client' import { useState, useEffect } from 'react' import { motion, AnimatePresence } from 'framer-motion' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { X, Download, Smartphone } from 'lucide-react' interface BeforeInstallPromptEvent extends Event { prompt(): Promise userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }> } export default function PWAInstallPrompt() { const [deferredPrompt, setDeferredPrompt] = useState(null) const [showPrompt, setShowPrompt] = useState(false) const [isInstalled, setIsInstalled] = useState(false) useEffect(() => { // Check if app is already installed const checkInstalled = () => { const isStandalone = window.matchMedia('(display-mode: standalone)').matches const isInWebAppiOS = (window.navigator as any).standalone === true const isAndroidInstalled = document.referrer.includes('android-app://') setIsInstalled(isStandalone || isInWebAppiOS || isAndroidInstalled) } checkInstalled() const handleBeforeInstallPrompt = (e: Event) => { e.preventDefault() setDeferredPrompt(e as BeforeInstallPromptEvent) // Don't show prompt immediately, wait a bit for user engagement setTimeout(() => { if (!isInstalled && !localStorage.getItem('pwa-install-dismissed')) { setShowPrompt(true) } }, 10000) // Show after 10 seconds } const handleAppInstalled = () => { setShowPrompt(false) setIsInstalled(true) setDeferredPrompt(null) } window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt) window.addEventListener('appinstalled', handleAppInstalled) return () => { window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt) window.removeEventListener('appinstalled', handleAppInstalled) } }, [isInstalled]) const handleInstallClick = async () => { if (!deferredPrompt) return try { await deferredPrompt.prompt() const choiceResult = await deferredPrompt.userChoice if (choiceResult.outcome === 'accepted') { console.log('User accepted the install prompt') } else { console.log('User dismissed the install prompt') } setDeferredPrompt(null) setShowPrompt(false) } catch (error) { console.error('Error showing install prompt:', error) } } const handleDismiss = () => { setShowPrompt(false) localStorage.setItem('pwa-install-dismissed', 'true') // Show again after 7 days setTimeout(() => { localStorage.removeItem('pwa-install-dismissed') }, 7 * 24 * 60 * 60 * 1000) } if (isInstalled || !showPrompt || !deferredPrompt) { return null } return (

Install Padmaaja Rasooi App

Get the full experience with offline access, faster loading, and push notifications.

) } // Alternative iOS Safari install instructions export function IOSInstallPrompt() { const [showIOS, setShowIOS] = useState(false) useEffect(() => { const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) const isInStandaloneMode = (window.navigator as any).standalone === true const hasPromptBeenShown = localStorage.getItem('ios-install-prompt-shown') if (isIOS && !isInStandaloneMode && !hasPromptBeenShown) { setTimeout(() => setShowIOS(true), 15000) // Show after 15 seconds on iOS } }, []) const handleDismiss = () => { setShowIOS(false) localStorage.setItem('ios-install-prompt-shown', 'true') } if (!showIOS) return null return (

Add to Home Screen

Tap the share button ⬆️ below and select "Add to Home Screen"

) }