'use client'
import { useState, useEffect } from 'react'
// import { useTranslations } from 'next-intl'
import {
Box,
Card,
CardContent,
Typography,
Button,
IconButton,
Slide,
Paper,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
List,
ListItem,
ListItemIcon,
ListItemText
} from '@mui/material'
import {
Close,
GetApp,
Smartphone,
CloudOff,
Speed,
Notifications,
Home,
Info
} from '@mui/icons-material'
import { usePWA } from '@/components/pwa/service-worker-provider'
interface InstallPromptProps {
autoShow?: boolean
}
export function InstallPrompt({ autoShow = false }: InstallPromptProps) {
// const t = useTranslations('pwa')
const [showPrompt, setShowPrompt] = useState(false)
const [showInfo, setShowInfo] = useState(false)
const [isInstalled, setIsInstalled] = useState(false)
const { canInstall, installPrompt, isInstalled: pwaInstalled } = usePWA()
useEffect(() => {
// Check if app is already installed
if (window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) {
setIsInstalled(true)
}
// Auto show prompt if configured and can install
if (autoShow && canInstall && !isInstalled && !hasUserDismissed()) {
const timer = setTimeout(() => {
setShowPrompt(true)
}, 3000) // Show after 3 seconds
return () => clearTimeout(timer)
}
}, [autoShow, canInstall, isInstalled])
useEffect(() => {
setIsInstalled(pwaInstalled)
}, [pwaInstalled])
const hasUserDismissed = () => {
return localStorage.getItem('pwa-install-dismissed') === 'true'
}
const markAsDismissed = () => {
localStorage.setItem('pwa-install-dismissed', 'true')
}
const handleInstall = async () => {
try {
await installPrompt()
setShowPrompt(false)
setIsInstalled(true)
} catch (error) {
console.error('Installation failed:', error)
}
}
const handleDismiss = () => {
setShowPrompt(false)
markAsDismissed()
}
const handleShowInfo = () => {
setShowInfo(true)
}
const getPlatformInstructions = () => {
const userAgent = navigator.userAgent.toLowerCase()
if (userAgent.includes('iphone') || userAgent.includes('ipad')) {
return {
platform: 'iOS (Safari)',
steps: [
'Tap the Share button in Safari',
'Scroll down and tap "Add to Home Screen"',
'Tap "Add" to install the app'
]
}
} else if (userAgent.includes('android')) {
return {
platform: 'Android (Chrome)',
steps: [
'Tap the menu (three dots) in Chrome',
'Select "Add to Home screen"',
'Tap "Add" to install the app'
]
}
} else {
return {
platform: 'Desktop',
steps: [
'Look for the install icon in your browser\'s address bar',
'Click the install button or use the browser menu',
'Follow the prompts to install the app'
]
}
}
}
if (isInstalled) {
return null // Don't show if already installed
}
return (
<>
{/* Install Prompt Card */}
Install Biblical Guide
Install our app for offline Bible reading, faster access, and a native app experience.
{canInstall ? (
}
>
Install App
) : (
}
>
How to Install
)}
{/* Installation Info Dialog */}
>
)
}
// Export a hook for manual installation prompt
export function useInstallPrompt() {
const { canInstall, installPrompt } = usePWA()
const [isInstalled, setIsInstalled] = useState(false)
useEffect(() => {
if (window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) {
setIsInstalled(true)
}
}, [])
const showInstallPrompt = async () => {
if (canInstall && !isInstalled) {
try {
await installPrompt()
setIsInstalled(true)
return true
} catch (error) {
console.error('Installation failed:', error)
return false
}
}
return false
}
return {
canInstall: canInstall && !isInstalled,
isInstalled,
showInstallPrompt
}
}