|
| 1 | +import React, { useState, useEffect } from 'react' |
| 2 | +import { Box, Button, Snackbar, Alert, IconButton } from '@mui/material' |
| 3 | +import { GetApp, Close, PhoneIphone, LaptopMac } from '@mui/icons-material' |
| 4 | + |
| 5 | +interface BeforeInstallPromptEvent extends Event { |
| 6 | + prompt(): Promise<void> |
| 7 | + userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }> |
| 8 | +} |
| 9 | + |
| 10 | +export function AddToAppButton() { |
| 11 | + const [deferredPrompt, setDeferredPrompt] = useState<BeforeInstallPromptEvent | null>(null) |
| 12 | + const [showIOSInstructions, setShowIOSInstructions] = useState(false) |
| 13 | + const [isInstallable, setIsInstallable] = useState(false) |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + const handleBeforeInstallPrompt = (e: Event) => { |
| 17 | + // Prevent the mini-infobar from appearing on mobile |
| 18 | + e.preventDefault() |
| 19 | + setDeferredPrompt(e as BeforeInstallPromptEvent) |
| 20 | + setIsInstallable(true) |
| 21 | + } |
| 22 | + |
| 23 | + const handleAppInstalled = () => { |
| 24 | + setIsInstallable(false) |
| 25 | + setDeferredPrompt(null) |
| 26 | + } |
| 27 | + |
| 28 | + window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt) |
| 29 | + window.addEventListener('appinstalled', handleAppInstalled) |
| 30 | + |
| 31 | + return () => { |
| 32 | + window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt) |
| 33 | + window.removeEventListener('appinstalled', handleAppInstalled) |
| 34 | + } |
| 35 | + }, []) |
| 36 | + |
| 37 | + const isIOS = () => { |
| 38 | + return /iPad|iPhone|iPod/.test(navigator.userAgent) |
| 39 | + } |
| 40 | + |
| 41 | + const isInStandaloneMode = () => { |
| 42 | + return window.matchMedia('(display-mode: standalone)').matches || |
| 43 | + (window.navigator as any).standalone === true |
| 44 | + } |
| 45 | + |
| 46 | + const handleInstallClick = async () => { |
| 47 | + if (isIOS()) { |
| 48 | + setShowIOSInstructions(true) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + if (deferredPrompt) { |
| 53 | + deferredPrompt.prompt() |
| 54 | + const { outcome } = await deferredPrompt.userChoice |
| 55 | + if (outcome === 'accepted') { |
| 56 | + setDeferredPrompt(null) |
| 57 | + setIsInstallable(false) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Don't show the button if already installed or not installable |
| 63 | + if (isInStandaloneMode() || (!isInstallable && !isIOS())) { |
| 64 | + return null |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <Box |
| 70 | + sx={{ |
| 71 | + display: 'flex', |
| 72 | + justifyContent: 'center', |
| 73 | + mt: 2, |
| 74 | + mb: 1, |
| 75 | + }} |
| 76 | + > |
| 77 | + <Button |
| 78 | + variant="outlined" |
| 79 | + startIcon={isIOS() ? <PhoneIphone /> : <LaptopMac />} |
| 80 | + onClick={handleInstallClick} |
| 81 | + sx={{ |
| 82 | + borderRadius: 2, |
| 83 | + textTransform: 'none', |
| 84 | + px: 3, |
| 85 | + py: 1, |
| 86 | + }} |
| 87 | + > |
| 88 | + Add to {isIOS() ? 'Home Screen' : 'Desktop'} |
| 89 | + </Button> |
| 90 | + </Box> |
| 91 | + |
| 92 | + {/* iOS Installation Instructions */} |
| 93 | + <Snackbar |
| 94 | + open={showIOSInstructions} |
| 95 | + onClose={() => setShowIOSInstructions(false)} |
| 96 | + anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }} |
| 97 | + autoHideDuration={8000} |
| 98 | + > |
| 99 | + <Alert |
| 100 | + severity="info" |
| 101 | + sx={{ maxWidth: '90vw' }} |
| 102 | + action={ |
| 103 | + <IconButton |
| 104 | + size="small" |
| 105 | + color="inherit" |
| 106 | + onClick={() => setShowIOSInstructions(false)} |
| 107 | + > |
| 108 | + <Close fontSize="small" /> |
| 109 | + </IconButton> |
| 110 | + } |
| 111 | + > |
| 112 | + To install: tap the Share button in Safari, then tap "Add to Home Screen" |
| 113 | + </Alert> |
| 114 | + </Snackbar> |
| 115 | + </> |
| 116 | + ) |
| 117 | +} |
0 commit comments