|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import { Download, X } from "lucide-react"; |
| 5 | +import { Button } from "@/app/_components/ui/elements/button"; |
| 6 | + |
| 7 | +export function InstallPrompt() { |
| 8 | + const [deferredPrompt, setDeferredPrompt] = useState<any>(null); |
| 9 | + const [showInstallPrompt, setShowInstallPrompt] = useState(false); |
| 10 | + const [isInstalled, setIsInstalled] = useState(false); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + // Check if app is already installed |
| 14 | + if (window.matchMedia("(display-mode: standalone)").matches) { |
| 15 | + setIsInstalled(true); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + // Check if user has dismissed the prompt before |
| 20 | + const dismissed = localStorage.getItem("pwa-prompt-dismissed"); |
| 21 | + if (dismissed) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + // Listen for the beforeinstallprompt event |
| 26 | + const handleBeforeInstallPrompt = (e: Event) => { |
| 27 | + console.log("beforeinstallprompt event fired"); |
| 28 | + e.preventDefault(); |
| 29 | + setDeferredPrompt(e); |
| 30 | + setShowInstallPrompt(true); |
| 31 | + }; |
| 32 | + |
| 33 | + window.addEventListener("beforeinstallprompt", handleBeforeInstallPrompt); |
| 34 | + |
| 35 | + return () => { |
| 36 | + window.removeEventListener( |
| 37 | + "beforeinstallprompt", |
| 38 | + handleBeforeInstallPrompt |
| 39 | + ); |
| 40 | + }; |
| 41 | + }, []); |
| 42 | + |
| 43 | + const handleInstallClick = async () => { |
| 44 | + if (deferredPrompt) { |
| 45 | + deferredPrompt.prompt(); |
| 46 | + const { outcome } = await deferredPrompt.userChoice; |
| 47 | + |
| 48 | + if (outcome === "accepted") { |
| 49 | + setIsInstalled(true); |
| 50 | + } |
| 51 | + |
| 52 | + setDeferredPrompt(null); |
| 53 | + } |
| 54 | + |
| 55 | + setShowInstallPrompt(false); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleDismiss = () => { |
| 59 | + setShowInstallPrompt(false); |
| 60 | + localStorage.setItem("pwa-prompt-dismissed", "true"); |
| 61 | + }; |
| 62 | + |
| 63 | + if (isInstalled || !showInstallPrompt) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className="fixed bottom-4 left-4 right-4 z-50 bg-background border border-border rounded-lg shadow-lg p-4"> |
| 69 | + <div className="flex items-center justify-between"> |
| 70 | + <div className="flex items-center gap-3"> |
| 71 | + <div className="w-10 h-10 bg-primary rounded-lg flex items-center justify-center"> |
| 72 | + <Download className="h-5 w-5 text-primary-foreground" /> |
| 73 | + </div> |
| 74 | + <div> |
| 75 | + <h3 className="font-medium text-foreground">Install rwMarkable</h3> |
| 76 | + <p className="text-sm text-muted-foreground"> |
| 77 | + Add to your home screen for quick access |
| 78 | + </p> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + <div className="flex items-center gap-2"> |
| 82 | + <Button |
| 83 | + onClick={handleInstallClick} |
| 84 | + size="sm" |
| 85 | + className="bg-primary text-primary-foreground hover:bg-primary/90" |
| 86 | + > |
| 87 | + Install |
| 88 | + </Button> |
| 89 | + <Button |
| 90 | + onClick={handleDismiss} |
| 91 | + variant="ghost" |
| 92 | + size="sm" |
| 93 | + className="h-8 w-8 p-0" |
| 94 | + > |
| 95 | + <X className="h-4 w-4" /> |
| 96 | + </Button> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
0 commit comments