5f270dee08
layout.tsx pointed <link rel="manifest"> at /manifest.json, but the actual generated route (app/manifest.ts) serves at /manifest.webmanifest -- the manifest was silently never picked up. Its icons also referenced icon-192.png/icon-512.png, which don't exist (only the .svg versions do). Both together meant Chrome/Android install eligibility was broken with no visible error. Also adds apple-mobile-web-app meta tags + viewport-fit=cover for iOS home-screen installs, and a dismissible install banner driven by the standard beforeinstallprompt event (Chromium-only by spec). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { X, Download } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
const DISMISSED_KEY = "epicure:install-prompt-dismissed";
|
|
|
|
type BeforeInstallPromptEvent = Event & {
|
|
prompt: () => Promise<void>;
|
|
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
|
|
};
|
|
|
|
/** Only fires on Chromium-based browsers (Chrome/Edge/Android) — Safari and
|
|
* Firefox never dispatch beforeinstallprompt, so this banner simply never
|
|
* appears there, which is the correct behavior (no fallback UI to build). */
|
|
export function InstallPrompt() {
|
|
const [deferredEvent, setDeferredEvent] = useState<BeforeInstallPromptEvent | null>(null);
|
|
const [dismissed, setDismissed] = useState(() => typeof localStorage !== "undefined" && localStorage.getItem(DISMISSED_KEY) === "true");
|
|
|
|
useEffect(() => {
|
|
function handler(e: Event) {
|
|
e.preventDefault();
|
|
setDeferredEvent(e as BeforeInstallPromptEvent);
|
|
}
|
|
window.addEventListener("beforeinstallprompt", handler);
|
|
return () => window.removeEventListener("beforeinstallprompt", handler);
|
|
}, []);
|
|
|
|
function dismiss() {
|
|
localStorage.setItem(DISMISSED_KEY, "true");
|
|
setDismissed(true);
|
|
}
|
|
|
|
async function install() {
|
|
if (!deferredEvent) return;
|
|
await deferredEvent.prompt();
|
|
await deferredEvent.userChoice;
|
|
setDeferredEvent(null);
|
|
}
|
|
|
|
if (!deferredEvent || dismissed) return null;
|
|
|
|
return (
|
|
<div className="fixed bottom-4 left-1/2 z-50 flex w-[calc(100%-2rem)] max-w-sm -translate-x-1/2 items-center gap-3 rounded-lg border bg-background p-3 shadow-lg">
|
|
<Download className="h-5 w-5 shrink-0 text-muted-foreground" />
|
|
<div className="min-w-0 flex-1">
|
|
<p className="text-sm font-medium">Install Epicure</p>
|
|
<p className="text-xs text-muted-foreground">Add to your home screen for quick access.</p>
|
|
</div>
|
|
<Button type="button" size="sm" onClick={() => { void install(); }}>
|
|
Install
|
|
</Button>
|
|
<button type="button" onClick={dismiss} aria-label="Dismiss" className="text-muted-foreground hover:text-foreground">
|
|
<X className="h-4 w-4" />
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|