forked from fahrPool/fahrPool
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
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.
|
|
}
|
|
}
|