'use client' import * as React from 'react' import { X, Download, Smartphone } from 'lucide-react' import { Button } from '@/components/ui/button' interface InstallPromptProps { onDismiss: () => void onInstall: () => void } export function InstallPrompt({ onDismiss, onInstall }: InstallPromptProps) { const [deferredPrompt, setDeferredPrompt] = React.useState(null) const [isIOS, setIsIOS] = React.useState(false) const [showIOSInstructions, setShowIOSInstructions] = React.useState(false) React.useEffect(() => { // Check if iOS const ua = navigator.userAgent const isIOSDevice = /iPad|iPhone|iPod/.test(ua) setIsIOS(isIOSDevice) // Check if already installed const isStandalone = window.matchMedia('(display-mode: standalone)').matches const isInWebAppiOS = (window.navigator as any).standalone === true if (isStandalone || isInWebAppiOS) { onDismiss() return } // Check if dismissed before const dismissed = localStorage.getItem('offlineacademy-install-dismissed') if (dismissed) { onDismiss() return } // Listen for beforeinstallprompt event (Android/Chrome) const handleBeforeInstallPrompt = (e: Event) => { e.preventDefault() setDeferredPrompt(e as BeforeInstallPromptEvent) } window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt) return () => { window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt) } }, [onDismiss]) const handleInstall = async () => { if (deferredPrompt) { deferredPrompt.prompt() const { outcome } = await deferredPrompt.userChoice if (outcome === 'accepted') { localStorage.setItem('offlineacademy-install-dismissed', 'true') onDismiss() } setDeferredPrompt(null) } else { // iOS - show instructions setShowIOSInstructions(true) } } const handleDismiss = () => { localStorage.setItem('offlineacademy-install-dismissed', 'true') onDismiss() } if (showIOSInstructions) { return (

Install on iOS

Tap the Share button (square with arrow up) in Safari, then scroll down and tap "Add to Home Screen".

) } return (

Install OfflineAcademy

Add to home screen for offline access, faster loads, and app-like experience.

) } // Type for beforeinstallprompt event interface BeforeInstallPromptEvent extends Event { prompt: () => Promise userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }> }