All files / components/pwa PWAInstallPrompt.tsx

95.06% Statements 77/81
90.9% Branches 20/22
100% Functions 4/4
95.06% Lines 77/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x                     1x 44x 44x 44x   44x   15x 1x 1x     14x 15x             14x 14x   15x   7x 7x 7x     7x 5x 5x   5x 5x   7x   7x 7x 7x 44x   44x 2x   2x 2x   2x 2x 2x   2x 2x   44x 3x 3x 3x   44x   17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 7x 7x   7x   10x   10x   44x 10x 10x   10x 10x   10x 10x   44x 7x   7x   44x 44x 44x 44x 44x   44x 44x 44x 44x 44x   44x  
'use client';
 
import { useState, useEffect } from 'react';
import { Download, X } from 'lucide-react';
import { Button } from '@/components/ui/button';
 
interface BeforeInstallPromptEvent extends Event {
  prompt: () => Promise<void>;
  userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }>;
}
 
export function PWAInstallPrompt() {
  const [deferredPrompt, setDeferredPrompt] = useState<BeforeInstallPromptEvent | null>(null);
  const [showPrompt, setShowPrompt] = useState(false);
  const [isIOS, setIsIOS] = useState(false);
 
  useEffect(() => {
    // Check if already installed
    if (window.matchMedia('(display-mode: standalone)').matches) {
      return;
    }
 
    // Check if dismissed recently
    const dismissed = localStorage.getItem('pwa-prompt-dismissed');
    if (dismissed) {
      const dismissedDate = new Date(dismissed);
      const daysSinceDismissed = (Date.now() - dismissedDate.getTime()) / (1000 * 60 * 60 * 24);
      if (daysSinceDismissed < 7) return; // Don't show for 7 days after dismissal
    }
 
    // Check for iOS
    const isIOSDevice = /iPad|iPhone|iPod/.test(navigator.userAgent);
    setIsIOS(isIOSDevice);
 
    if (isIOSDevice) {
      // Show custom prompt for iOS after a delay
      const timer = setTimeout(() => setShowPrompt(true), 3000);
      return () => clearTimeout(timer);
    }
 
    // Handle beforeinstallprompt for Android/Desktop
    const handler = (e: Event) => {
      e.preventDefault();
      setDeferredPrompt(e as BeforeInstallPromptEvent);
      // Show prompt after a delay
      setTimeout(() => setShowPrompt(true), 3000);
    };
 
    window.addEventListener('beforeinstallprompt', handler);
 
    return () => {
      window.removeEventListener('beforeinstallprompt', handler);
    };
  }, []);
 
  const handleInstall = async () => {
    if (!deferredPrompt) return;
 
    deferredPrompt.prompt();
    const { outcome } = await deferredPrompt.userChoice;
 
    if (outcome === 'accepted') {
      setShowPrompt(false);
    }
 
    setDeferredPrompt(null);
  };
 
  const handleDismiss = () => {
    setShowPrompt(false);
    localStorage.setItem('pwa-prompt-dismissed', new Date().toISOString());
  };
 
  if (!showPrompt) return null;
 
  return (
    <div className="fixed bottom-4 left-4 right-4 z-50 mx-auto max-w-md animate-in slide-in-from-bottom-4">
      <div className="rounded-lg border bg-background p-4 shadow-lg">
        <div className="flex items-start gap-3">
          <div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-primary/10">
            <Download className="h-5 w-5 text-primary" />
          </div>
          <div className="flex-1">
            <h3 className="font-semibold">Install ProofID</h3>
            {isIOS ? (
              <p className="mt-1 text-sm text-muted-foreground">
                Tap the share button <span className="inline-block rotate-90">⎋</span> and select
                &quot;Add to Home Screen&quot; for the best experience.
              </p>
            ) : (
              <p className="mt-1 text-sm text-muted-foreground">
                Install our app for quick access and offline support.
              </p>
            )}
            {!isIOS && deferredPrompt && (
              <div className="mt-3 flex gap-2">
                <Button size="sm" onClick={handleInstall}>
                  Install
                </Button>
                <Button size="sm" variant="ghost" onClick={handleDismiss}>
                  Not now
                </Button>
              </div>
            )}
            {isIOS && (
              <Button size="sm" variant="ghost" className="mt-2" onClick={handleDismiss}>
                Got it
              </Button>
            )}
          </div>
          <button
            onClick={handleDismiss}
            className="text-muted-foreground hover:text-foreground"
            aria-label="Dismiss"
          >
            <X className="h-4 w-4" />
          </button>
        </div>
      </div>
    </div>
  );
}