import { useState, useEffect } from "react"; import { Smartphone, Download, Check } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Dialog } from "@/components/ui/dialog"; import { patchCurrentManifest } from "@/lib/store-logo"; interface InstallButtonProps { admin?: boolean; className?: string; variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link"; size?: "default" | "sm" | "lg" | "icon"; } type InstallPromptEvent = Event & { prompt: () => void; userChoice: Promise<{ outcome: "accepted" | "dismissed" }>; }; function getPlatform() { const ua = navigator.userAgent; const isIOS = /iphone|ipad|ipod/i.test(ua) || (/macintosh/i.test(ua) && navigator.maxTouchPoints > 1); const isAndroid = /android/i.test(ua); return { isIOS, isAndroid, isDesktop: !isIOS && !isAndroid }; } export function InstallButton({ admin, className, variant = "outline", size = "sm" }: InstallButtonProps) { const [deferredPrompt, setDeferredPrompt] = useState(null); const [isInstalled, setIsInstalled] = useState(false); const [showHelp, setShowHelp] = useState(false); useEffect(() => { if (window.matchMedia("(display-mode: standalone)").matches) { setIsInstalled(true); return; } const onPrompt = (e: Event) => { e.preventDefault(); setDeferredPrompt(e as InstallPromptEvent); }; const onInstalled = () => { setIsInstalled(true); setDeferredPrompt(null); }; window.addEventListener("beforeinstallprompt", onPrompt); window.addEventListener("appinstalled", onInstalled); return () => { window.removeEventListener("beforeinstallprompt", onPrompt); window.removeEventListener("appinstalled", onInstalled); }; }, []); // Directly invoke the browser's native "Add to Home Screen / Install app" // prompt. Falls back to platform instructions when the browser does not // support the native prompt (e.g. iOS Safari). const handleInstall = async () => { if (isInstalled) return; if (deferredPrompt) { if (admin) { const link = document.querySelector('link[rel="manifest"]'); if (link) { link.href = "/manifest-admin.json"; await patchCurrentManifest(); } } deferredPrompt.prompt(); await deferredPrompt.userChoice; setDeferredPrompt(null); return; } setShowHelp(true); }; const { isIOS, isAndroid, isDesktop } = getPlatform(); return ( <> setShowHelp(false)} title="Add to Home Screen" description="Install this app on your device for quick access and a full-screen experience." > {isIOS ? (
    Tap the Share icon (square with an up arrow) in the Safari toolbar at the bottom of the screen. Scroll the share menu and tap Add to Home Screen. Tap Add in the top-right corner. The app icon will appear on your home screen.
) : isAndroid ? (
    Tap the ⋮ menu button in the top-right corner of your browser. Tap Add to Home screen or Install app. Tap Add / Install. The app icon will appear on your home screen.
) : (
    Click the install icon in the address bar of your browser (Chrome, Edge, or Safari). Click Install to add {admin ? "the Admin Panel" : "the app"} to your desktop.
)} {isDesktop && (

Tip: if no install icon appears, look for Install app or Add to Home Screen in your browser menu.

)}
); } function Step({ n, title, children }: { n: number; title: string; children: React.ReactNode }) { return (
  • {n}

    {title}

    {children}

  • ); }