diff --git a/web/app/dashboard/page.tsx b/web/app/dashboard/page.tsx index 61f7321..b877d71 100644 --- a/web/app/dashboard/page.tsx +++ b/web/app/dashboard/page.tsx @@ -1,6 +1,7 @@ import { loadProfile } from "@/lib/auth/profile"; import { listAuftraegeFuerKanban, listFahrerNutzer } from "@/lib/dashboard/queries"; import { parseKanbanFilter } from "@/lib/dashboard/kanban-filter"; +import { InstallHinweis } from "@/components/pwa/InstallHinweis"; import { KanbanBoard } from "@/components/dashboard/KanbanBoard"; export default async function DashboardPage({ @@ -22,6 +23,7 @@ export default async function DashboardPage({ return (
+

Dashboard

+

Plattform-Verwaltung

{error && {error}} diff --git a/web/components/pwa/InstallHinweis.tsx b/web/components/pwa/InstallHinweis.tsx new file mode 100644 index 0000000..72748df --- /dev/null +++ b/web/components/pwa/InstallHinweis.tsx @@ -0,0 +1,101 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { + erkennePlattform, + ladeWeggeklicktAm, + sollHinweisZeigen, + speichereWeggeklicktAm, + type Plattform, +} from "@/lib/pwa/install-hinweis"; + +type InstallPromptEvent = Event & { prompt: () => Promise }; + +type Zustand = { plattform: Plattform; standalone: boolean; weggeklicktAm: number | null; jetzt: number }; + +function TeilenIcon() { + return ( + + ); +} + +export function InstallHinweis() { + const [zustand, setZustand] = useState(null); + const [installiert, setInstalliert] = useState(false); + const [installEvent, setInstallEvent] = useState(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 ( +
+
+

App auf dem Startbildschirm installieren

+ {plattform === "ios-safari" && ( +

+ Tippe auf Teilen und dann „Zum Home-Bildschirm“. +

+ )} + {plattform === "ios-anderer-browser" && ( +

+ Öffne die Seite in Safari, tippe auf Teilen und dann „Zum Home-Bildschirm“. +

+ )} + {plattform === "android" && !installEvent &&

Im Browsermenü ⋮ „App installieren“ wählen.

} + {plattform === "android" && installEvent && ( + + )} +
+ +
+ ); +} diff --git a/web/lib/pwa/install-hinweis.test.ts b/web/lib/pwa/install-hinweis.test.ts new file mode 100644 index 0000000..4fa64cb --- /dev/null +++ b/web/lib/pwa/install-hinweis.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, it } from "vitest"; +import { + HINWEIS_PAUSE_MS, + erkennePlattform, + ladeWeggeklicktAm, + sollHinweisZeigen, + speichereWeggeklicktAm, +} from "./install-hinweis"; + +const IPHONE_SAFARI = + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1"; +const IPHONE_CHROME = + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/123.0.6312.52 Mobile/15E148 Safari/604.1"; +const IPAD_DESKTOP_UA = + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15"; +const PIXEL_CHROME = + "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36"; +const WINDOWS_CHROME = + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"; + +describe("erkennePlattform", () => { + it("erkennt iPhone-Safari", () => expect(erkennePlattform(IPHONE_SAFARI, 5)).toBe("ios-safari")); + it("erkennt iPhone-Chrome als anderen Browser", () => + expect(erkennePlattform(IPHONE_CHROME, 5)).toBe("ios-anderer-browser")); + it("erkennt iPad mit Desktop-UA als iOS", () => expect(erkennePlattform(IPAD_DESKTOP_UA, 5)).toBe("ios-safari")); + it("Mac ohne Touch ist Desktop", () => expect(erkennePlattform(IPAD_DESKTOP_UA, 0)).toBe("desktop")); + it("erkennt Android", () => expect(erkennePlattform(PIXEL_CHROME, 5)).toBe("android")); + it("erkennt Windows als Desktop", () => expect(erkennePlattform(WINDOWS_CHROME, 0)).toBe("desktop")); +}); + +describe("sollHinweisZeigen", () => { + const jetzt = 1_800_000_000_000; + const tage = (n: number) => n * 24 * 60 * 60 * 1000; + const basis = { plattform: "android" as const, standalone: false, installiert: false, weggeklicktAm: null, jetzt }; + + it("Pause ist 30 Tage", () => expect(HINWEIS_PAUSE_MS).toBe(tage(30))); + it("false vor 29 Tagen weggeklickt", () => + expect(sollHinweisZeigen({ ...basis, weggeklicktAm: jetzt - tage(29) })).toBe(false)); + it("true vor 31 Tagen weggeklickt", () => + expect(sollHinweisZeigen({ ...basis, weggeklicktAm: jetzt - tage(31) })).toBe(true)); + it("true ohne Wegklicken", () => expect(sollHinweisZeigen(basis)).toBe(true)); + it("false bei standalone", () => expect(sollHinweisZeigen({ ...basis, standalone: true })).toBe(false)); + it("false bei desktop", () => expect(sollHinweisZeigen({ ...basis, plattform: "desktop" })).toBe(false)); + it("false bei installiert", () => expect(sollHinweisZeigen({ ...basis, installiert: true })).toBe(false)); +}); + +describe("Storage", () => { + it("getItem wirft -> null", () => + expect( + ladeWeggeklicktAm({ + getItem: () => { + throw new Error("x"); + }, + }), + ).toBeNull()); + it("ungueltiger Wert -> null", () => expect(ladeWeggeklicktAm({ getItem: () => "abc" })).toBeNull()); + it("fehlender Wert -> null", () => expect(ladeWeggeklicktAm({ getItem: () => null })).toBeNull()); + it("gueltiger Wert -> Zahl", () => expect(ladeWeggeklicktAm({ getItem: () => "1700000000000" })).toBe(1700000000000)); + it("null-Storage -> null", () => expect(ladeWeggeklicktAm(null)).toBeNull()); + it("setItem wirft -> kein Throw", () => + expect(() => + speichereWeggeklicktAm( + { + setItem: () => { + throw new Error("x"); + }, + }, + 1, + ), + ).not.toThrow()); + it("null-Storage beim Speichern -> kein Throw", () => expect(() => speichereWeggeklicktAm(null, 1)).not.toThrow()); + it("speichert den Zeitstempel als String", () => { + const calls: [string, string][] = []; + speichereWeggeklicktAm({ setItem: (k, v) => void calls.push([k, v]) }, 42); + expect(calls).toEqual([["pwa-install-hinweis-weggeklickt", "42"]]); + }); +}); diff --git a/web/lib/pwa/install-hinweis.ts b/web/lib/pwa/install-hinweis.ts new file mode 100644 index 0000000..3043e1f --- /dev/null +++ b/web/lib/pwa/install-hinweis.ts @@ -0,0 +1,44 @@ +export const HINWEIS_PAUSE_MS = 30 * 24 * 60 * 60 * 1000; +export const HINWEIS_STORAGE_KEY = "pwa-install-hinweis-weggeklickt"; + +export type Plattform = "ios-safari" | "ios-anderer-browser" | "android" | "desktop"; + +export function erkennePlattform(userAgent: string, maxTouchPoints: number): Plattform { + const istIos = /iPhone|iPad|iPod/.test(userAgent) || (/Macintosh/.test(userAgent) && maxTouchPoints > 1); + if (istIos) { + return /CriOS|FxiOS|EdgiOS/.test(userAgent) ? "ios-anderer-browser" : "ios-safari"; + } + if (/Android/.test(userAgent)) return "android"; + return "desktop"; +} + +export function sollHinweisZeigen(p: { + plattform: Plattform; + standalone: boolean; + installiert: boolean; + weggeklicktAm: number | null; + jetzt: number; +}): boolean { + if (p.plattform === "desktop" || p.standalone || p.installiert) return false; + if (p.weggeklicktAm !== null && p.jetzt - p.weggeklicktAm < HINWEIS_PAUSE_MS) return false; + return true; +} + +export function ladeWeggeklicktAm(storage: Pick | null): number | null { + try { + const wert = storage?.getItem(HINWEIS_STORAGE_KEY); + if (wert == null) return null; + const zahl = Number(wert); + return Number.isFinite(zahl) ? zahl : null; + } catch { + return null; + } +} + +export function speichereWeggeklicktAm(storage: Pick | null, jetzt: number): void { + try { + storage?.setItem(HINWEIS_STORAGE_KEY, String(jetzt)); + } catch { + // Speicher nicht verfuegbar (z.B. privater Modus): Hinweis erscheint dann einfach erneut. + } +}