tack-up-now/app/useInstallState.ts

49 lines
1.1 KiB
TypeScript

import { usePush } from "./usePush"
type IOSInstallStep =
| "loading"
| "install"
| "open safari"
| "enable notifications"
| "unsupported"
export default function useInstallState({
isSupported,
isMobileSafari,
}: {
isSupported: boolean
isMobileSafari: boolean
}) {
const isClient = typeof window !== "undefined"
if (!isClient)
return {
step: isSupported ? "loading" : ("unsupported" as IOSInstallStep),
installed: false,
}
const { canSendPush } = usePush()
const notificationsEnabled =
("Notification" in window &&
window.Notification.permission === "granted") ||
canSendPush
const isRunningPWA =
("standalone" in navigator && (navigator.standalone as boolean)) ||
matchMedia("(dislay-mode: standalone)").matches
return {
step: !isSupported
? "unsupported"
: !isMobileSafari
? "open safari"
: !isRunningPWA && isMobileSafari
? "install"
: !notificationsEnabled
? "enable notifications"
: (null as IOSInstallStep | null),
installed: notificationsEnabled,
}
}