'use client'; import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Download, Share, Plus, MoreVertical, Smartphone } from 'lucide-react'; interface BeforeInstallPromptEvent extends Event { prompt: () => Promise; userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }>; } export function InstallAppButton() { const [showInstructions, setShowInstructions] = useState(false); const [deferredPrompt, setDeferredPrompt] = useState(null); const [isIOS, setIsIOS] = useState(false); const [isAndroid, setIsAndroid] = useState(false); const [isStandalone, setIsStandalone] = useState(false); useEffect(() => { // Check if already installed as PWA const isAppInstalled = window.matchMedia('(display-mode: standalone)').matches || (window.navigator as Navigator & { standalone?: boolean }).standalone === true; setIsStandalone(isAppInstalled); // Detect platform const userAgent = window.navigator.userAgent.toLowerCase(); const isIOSDevice = /iphone|ipad|ipod/.test(userAgent); const isAndroidDevice = /android/.test(userAgent); setIsIOS(isIOSDevice); setIsAndroid(isAndroidDevice); // Listen for beforeinstallprompt (Android/Chrome) const handleBeforeInstallPrompt = (e: Event) => { e.preventDefault(); setDeferredPrompt(e as BeforeInstallPromptEvent); }; window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt); return () => { window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt); }; }, []); const handleInstallClick = async () => { if (deferredPrompt) { // Use the native install prompt (Android/Chrome) await deferredPrompt.prompt(); const { outcome } = await deferredPrompt.userChoice; if (outcome === 'accepted') { setDeferredPrompt(null); } } else { // Show manual instructions setShowInstructions(true); } }; // Don't show if already installed as PWA if (isStandalone) { return null; } return ( <> Add QuitTraq to Home Screen Get quick access to track your progress right from your phone's home screen.
{isIOS ? ( // iOS Instructions

For iPhone / iPad (Safari):

  1. 1

    Tap the Share button

  2. 2

    Scroll down and tap "Add to Home Screen"

    Add to Home Screen
  3. 3

    Tap "Add" to confirm

) : isAndroid ? ( // Android Instructions

For Android (Chrome):

  1. 1

    Tap the menu button (3 dots)

  2. 2

    Tap "Add to Home screen" or "Install app"

  3. 3

    Tap "Add" or "Install" to confirm

) : ( // Desktop / Unknown

On mobile device:

  • iPhone/iPad: Use Safari, tap Share → Add to Home Screen
  • Android: Use Chrome, tap Menu → Add to Home screen

Open this page on your phone to add QuitTraq to your home screen for quick access!

)}

📱 Once added, tap the QuitTraq icon to quickly log your usage!

); }