Spaces:
Running
Running
Download src/hooks/useInstallPrompt.ts from ahmdelbaz28/AHMDRTAP: direct link, hf CLI and curl.
- Browser
- Download file 5.58 kB
-
https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/hooks/useInstallPrompt.ts
- Command line
-
hf download hf://spaces/ahmdelbaz28/AHMDRTAP/src/hooks/useInstallPrompt.ts
-
curl -L -o useInstallPrompt.ts https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/hooks/useInstallPrompt.ts
5.58 kB
| import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | |
| import { useAuth } from "@/hooks/useAuth"; | |
| import { | |
| isIosUserAgent, | |
| isRunningStandalone, | |
| markDismissed, | |
| markInstalled, | |
| readInstallDecision, | |
| shouldSuggestInstall, | |
| } from "@/lib/pwa-install"; | |
| /** | |
| * PWA install-suggestion hook — the React binding for the pure policy in | |
| * src/lib/pwa-install.ts. | |
| * | |
| * Owner requirement: once ANY user is signed in, suggest installing the | |
| * platform as an app (faster access + notifications that actually arrive). | |
| * The hook: | |
| * 1. captures the browser's `beforeinstallprompt` (suppressing Chrome's own | |
| * mini-infobar so OUR in-app banner is the single source of the offer); | |
| * 2. tracks `appinstalled` → permanently silent afterwards; | |
| * 3. never shows inside an installed (standalone) session; | |
| * 4. waits for auth to settle, then a 5s dwell so the banner never fights | |
| * the login redirect; | |
| * 5. exposes promptInstall() (native) and dismiss() (14-day mute). | |
| */ | |
| /** Dwell time after authentication before the banner may appear. */ | |
| const DWELL_MS = 5_000; | |
| /** iOS-only standalone flag (Safari sets it when launched from the home screen). */ | |
| declare global { | |
| interface Navigator { | |
| readonly standalone?: boolean; | |
| } | |
| } | |
| export type InstallFlavor = "native" | "ios"; | |
| interface BeforeInstallPromptEvent extends Event { | |
| prompt(): Promise<void>; | |
| userChoice: Promise<{ outcome: "accepted" | "dismissed" }>; | |
| } | |
| export interface InstallPromptState { | |
| /** The banner may render. */ | |
| visible: boolean; | |
| /** Which flavor: native prompt available vs iOS manual steps. */ | |
| flavor: InstallFlavor; | |
| /** Fire the browser's native install dialog (native flavor only). */ | |
| promptInstall: () => Promise<"accepted" | "dismissed" | "unavailable">; | |
| /** "Not now" — persists a 14-day mute. */ | |
| dismiss: () => void; | |
| } | |
| export function useInstallPrompt(): InstallPromptState { | |
| const { isAuthenticated, isLoading } = useAuth(); | |
| const [deferred, setDeferred] = useState<BeforeInstallPromptEvent | null>(null); | |
| const [appInstalled, setAppInstalled] = useState(false); | |
| const [standalone, setStandalone] = useState(false); | |
| const [dwellDone, setDwellDone] = useState(false); | |
| const [dismissedAt, setDismissedAt] = useState(0); | |
| /** Epoch ms captured when the dwell completes — keeps render pure. */ | |
| const [nowAtDwell, setNowAtDwell] = useState(0); | |
| const dwellTimer = useRef<number | null>(null); | |
| // ── Standalone detection + lifecycle listeners (mount once) ────────── | |
| useEffect(() => { | |
| const mq = window.matchMedia("(display-mode: standalone)"); | |
| const syncStandalone = () => setStandalone(mq.matches || window.navigator.standalone === true); | |
| syncStandalone(); | |
| mq.addEventListener?.("change", syncStandalone); | |
| const decision = readInstallDecision(window.localStorage); | |
| setDismissedAt(decision.dismissedAt); | |
| if (decision.installed) setAppInstalled(true); | |
| const onBeforeInstall = (e: Event) => { | |
| // Suppress the browser's own mini-infobar — our banner replaces it. | |
| e.preventDefault(); | |
| setDeferred(e as BeforeInstallPromptEvent); | |
| }; | |
| const onInstalled = () => { | |
| setAppInstalled(true); | |
| markInstalled(window.localStorage); | |
| setDeferred(null); | |
| }; | |
| window.addEventListener("beforeinstallprompt", onBeforeInstall); | |
| window.addEventListener("appinstalled", onInstalled); | |
| return () => { | |
| mq.removeEventListener?.("change", syncStandalone); | |
| window.removeEventListener("beforeinstallprompt", onBeforeInstall); | |
| window.removeEventListener("appinstalled", onInstalled); | |
| }; | |
| }, []); | |
| // ── Auth-settle gate + dwell timer ──────────────────────────────────── | |
| useEffect(() => { | |
| if (isLoading || !isAuthenticated) { | |
| setDwellDone(false); | |
| if (dwellTimer.current !== null) { | |
| window.clearTimeout(dwellTimer.current); | |
| dwellTimer.current = null; | |
| } | |
| return; | |
| } | |
| if (dwellTimer.current !== null) return; | |
| dwellTimer.current = window.setTimeout(() => { | |
| dwellTimer.current = null; | |
| setDwellDone(true); | |
| setNowAtDwell(Date.now()); | |
| }, DWELL_MS); | |
| return () => { | |
| if (dwellTimer.current !== null) { | |
| window.clearTimeout(dwellTimer.current); | |
| dwellTimer.current = null; | |
| } | |
| }; | |
| }, [isAuthenticated, isLoading]); | |
| const touchCapable = useMemo(() => "ontouchend" in document, []); | |
| const flavor: InstallFlavor = isIosUserAgent(window.navigator.userAgent, touchCapable) | |
| ? "ios" | |
| : "native"; | |
| const visible = | |
| dwellDone && | |
| !appInstalled && | |
| !standalone && | |
| dismissedAt === 0 && | |
| (deferred !== null || flavor === "ios") && | |
| shouldSuggestInstall( | |
| { dismissedAt, installed: appInstalled }, | |
| isRunningStandalone(standalone, window.navigator.standalone === true), | |
| window.navigator.standalone === true, | |
| nowAtDwell, | |
| ); | |
| const promptInstall = useCallback(async () => { | |
| if (!deferred) return "unavailable"; | |
| await deferred.prompt(); | |
| const choice = await deferred.userChoice; | |
| if (choice.outcome === "accepted") { | |
| setAppInstalled(true); | |
| markInstalled(window.localStorage); | |
| } | |
| setDeferred(null); | |
| return choice.outcome; | |
| }, [deferred]); | |
| const dismiss = useCallback(() => { | |
| const now = Date.now(); | |
| markDismissed(window.localStorage, now); | |
| setDismissedAt(now); | |
| }, []); | |
| return { visible, flavor, promptInstall, dismiss }; | |
| } | |