forked from fahrPool/fahrPool
feat: Installationshinweis fuer die PWA
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6e4b784bf9
commit
8f1d9d715b
|
|
@ -1,6 +1,7 @@
|
||||||
import { loadProfile } from "@/lib/auth/profile";
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
import { listAuftraegeFuerKanban, listFahrerNutzer } from "@/lib/dashboard/queries";
|
import { listAuftraegeFuerKanban, listFahrerNutzer } from "@/lib/dashboard/queries";
|
||||||
import { parseKanbanFilter } from "@/lib/dashboard/kanban-filter";
|
import { parseKanbanFilter } from "@/lib/dashboard/kanban-filter";
|
||||||
|
import { InstallHinweis } from "@/components/pwa/InstallHinweis";
|
||||||
import { KanbanBoard } from "@/components/dashboard/KanbanBoard";
|
import { KanbanBoard } from "@/components/dashboard/KanbanBoard";
|
||||||
|
|
||||||
export default async function DashboardPage({
|
export default async function DashboardPage({
|
||||||
|
|
@ -22,6 +23,7 @@ export default async function DashboardPage({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mx-auto max-w-6xl p-4 md:p-8">
|
<div className="mx-auto max-w-6xl p-4 md:p-8">
|
||||||
|
<InstallHinweis />
|
||||||
<h1 className="mb-6 text-xl font-semibold text-asphalt-grey">Dashboard</h1>
|
<h1 className="mb-6 text-xl font-semibold text-asphalt-grey">Dashboard</h1>
|
||||||
<KanbanBoard
|
<KanbanBoard
|
||||||
initialSpalten={spalten}
|
initialSpalten={spalten}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { loadProfile } from "@/lib/auth/profile";
|
||||||
import { pruefeSuperadmin } from "@/lib/plattform/superadmin";
|
import { pruefeSuperadmin } from "@/lib/plattform/superadmin";
|
||||||
import { listFirmen } from "@/lib/plattform/queries";
|
import { listFirmen } from "@/lib/plattform/queries";
|
||||||
import { legeFirmaAn } from "./actions";
|
import { legeFirmaAn } from "./actions";
|
||||||
|
import { InstallHinweis } from "@/components/pwa/InstallHinweis";
|
||||||
import { Alert } from "@/components/ui/Alert";
|
import { Alert } from "@/components/ui/Alert";
|
||||||
import { Button } from "@/components/ui/Button";
|
import { Button } from "@/components/ui/Button";
|
||||||
import { Card } from "@/components/ui/Card";
|
import { Card } from "@/components/ui/Card";
|
||||||
|
|
@ -19,6 +20,7 @@ export default async function PlattformPage({ searchParams }: { searchParams: Pr
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mx-auto flex max-w-2xl flex-col gap-6 p-4 md:p-8">
|
<div className="mx-auto flex max-w-2xl flex-col gap-6 p-4 md:p-8">
|
||||||
|
<InstallHinweis />
|
||||||
<h1 className="text-xl font-semibold text-asphalt-grey">Plattform-Verwaltung</h1>
|
<h1 className="text-xl font-semibold text-asphalt-grey">Plattform-Verwaltung</h1>
|
||||||
{error && <Alert>{error}</Alert>}
|
{error && <Alert>{error}</Alert>}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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<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>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -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"]]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -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<Storage, "getItem"> | 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<Storage, "setItem"> | 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.
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue