1
0
Fork 0
fahrPool/web/components/pwa/InstallHinweis.tsx

102 lines
4.0 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import {
erkennePlattform,
ladeWeggeklicktAm,
sollHinweisZeigen,
speichereWeggeklicktAm,
type Plattform,
} from "@/lib/pwa/install-hinweis";
type InstallPromptEvent = Event & { prompt: () => Promise<void> };
type Zustand = { plattform: Plattform; standalone: boolean; weggeklicktAm: number | null; jetzt: number };
function TeilenIcon() {
return (
<svg aria-hidden="true" viewBox="0 0 24 24" className="mx-1 inline h-5 w-5 align-text-bottom" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<path d="M12 3v12M8 7l4-4 4 4M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-7" />
</svg>
);
}
export function InstallHinweis() {
const [zustand, setZustand] = useState<Zustand | null>(null);
const [installiert, setInstalliert] = useState(false);
const [installEvent, setInstallEvent] = useState<InstallPromptEvent | null>(null);
useEffect(() => {
const nav = navigator as Navigator & { standalone?: boolean };
// eslint-disable-next-line react-hooks/set-state-in-effect -- Browser-Werte erst nach Mount lesen (kein Hydration-Mismatch)
setZustand({
plattform: erkennePlattform(navigator.userAgent, navigator.maxTouchPoints),
standalone: window.matchMedia("(display-mode: standalone)").matches || nav.standalone === true,
weggeklicktAm: ladeWeggeklicktAm(window.localStorage),
jetzt: Date.now(),
});
const beiPrompt = (e: Event) => {
e.preventDefault();
setInstallEvent(e as InstallPromptEvent);
};
const beiInstalliert = () => {
setInstalliert(true);
speichereWeggeklicktAm(window.localStorage, Number.MAX_SAFE_INTEGER);
};
window.addEventListener("beforeinstallprompt", beiPrompt);
window.addEventListener("appinstalled", beiInstalliert);
return () => {
window.removeEventListener("beforeinstallprompt", beiPrompt);
window.removeEventListener("appinstalled", beiInstalliert);
};
}, []);
if (!zustand) return null;
if (!sollHinweisZeigen({ ...zustand, installiert })) return null;
function schliessen() {
const jetzt = Date.now();
speichereWeggeklicktAm(window.localStorage, jetzt);
setZustand((z) => (z ? { ...z, weggeklicktAm: jetzt, jetzt } : z));
}
async function installieren() {
if (!installEvent) return;
const event = installEvent;
setInstallEvent(null);
await event.prompt();
}
const { plattform } = zustand;
return (
<div role="region" aria-label="App installieren" className="mb-4 flex items-start gap-3 rounded-lg border border-aluminium-grey bg-white p-4 shadow-sm">
<div className="min-w-0 flex-1 text-sm text-asphalt-grey">
<p className="font-semibold">App auf dem Startbildschirm installieren</p>
{plattform === "ios-safari" && (
<p className="mt-1">
Tippe auf <TeilenIcon /> Teilen und dann „Zum Home-Bildschirm“.
</p>
)}
{plattform === "ios-anderer-browser" && (
<p className="mt-1">
Öffne die Seite in Safari, tippe auf <TeilenIcon /> Teilen und dann „Zum Home-Bildschirm“.
</p>
)}
{plattform === "android" && !installEvent && <p className="mt-1">Im Browsermenü ⋮ „App installieren“ wählen.</p>}
{plattform === "android" && installEvent && (
<button type="button" onClick={installieren} className="mt-2 inline-flex min-h-11 items-center rounded-md bg-performance-blue px-4 font-medium text-white">
Installieren
</button>
)}
</div>
<button type="button" onClick={schliessen} aria-label="Hinweis schließen" className="-m-2 flex h-11 w-11 shrink-0 items-center justify-center text-asphalt-grey hover:text-performance-blue">
<svg aria-hidden="true" viewBox="0 0 24 24" className="h-5 w-5" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
<path d="M6 6l12 12M18 6L6 18" />
</svg>
</button>
</div>
);
}