|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { APP_STORE_URL, PLAY_STORE_URL, siteName } from "../../utils/constants"; |
| 3 | + |
| 4 | +type MobileDeviceType = "android" | "apple" | null; |
| 5 | + |
| 6 | +const getMobileDeviceType = (): MobileDeviceType => { |
| 7 | + if (typeof window === "undefined" || !window.navigator?.userAgent) |
| 8 | + return null; |
| 9 | + |
| 10 | + const userAgent = window.navigator.userAgent.toLowerCase(); |
| 11 | + |
| 12 | + if (/iphone|ipad|ipod/.test(userAgent)) return "apple"; |
| 13 | + if (/android/.test(userAgent)) return "android"; |
| 14 | + |
| 15 | + return null; |
| 16 | +}; |
| 17 | + |
| 18 | +const STORE_URLS: Record<NonNullable<MobileDeviceType>, string> = { |
| 19 | + apple: APP_STORE_URL, |
| 20 | + android: PLAY_STORE_URL, |
| 21 | +}; |
| 22 | + |
| 23 | +const AppShare = () => { |
| 24 | + const [showDownloadLinks, setShowDownloadLinks] = useState(false); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const deviceType = getMobileDeviceType(); |
| 28 | + |
| 29 | + if (deviceType) { |
| 30 | + window.location.replace(STORE_URLS[deviceType]); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + setShowDownloadLinks(true); |
| 35 | + }, []); |
| 36 | + |
| 37 | + if (!showDownloadLinks) { |
| 38 | + return ( |
| 39 | + <div className="flex min-h-screen items-center justify-center bg-gray-50 px-4"> |
| 40 | + <p className="text-sm text-gray-500">Redirecting to the app store…</p> |
| 41 | + </div> |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="flex min-h-screen items-center justify-center bg-gray-50 px-4"> |
| 47 | + <div className="w-full max-w-md rounded-lg border border-gray-200 bg-white p-8 shadow-sm"> |
| 48 | + <h1 className="text-center text-2xl font-semibold text-gray-900"> |
| 49 | + Get {siteName} |
| 50 | + </h1> |
| 51 | + <p className="mt-2 text-center text-sm text-gray-500"> |
| 52 | + Download the app for daily reading, practice, and community on your |
| 53 | + device. |
| 54 | + </p> |
| 55 | + |
| 56 | + <div className="mt-8 flex flex-col gap-3"> |
| 57 | + <a |
| 58 | + href={APP_STORE_URL} |
| 59 | + target="_blank" |
| 60 | + rel="noopener noreferrer" |
| 61 | + aria-label={`Download ${siteName} on the App Store`} |
| 62 | + className="flex items-center justify-center rounded-md bg-amber-600 px-4 py-3 text-sm font-medium text-white hover:bg-amber-700" |
| 63 | + > |
| 64 | + Download on the App Store |
| 65 | + </a> |
| 66 | + <a |
| 67 | + href={PLAY_STORE_URL} |
| 68 | + target="_blank" |
| 69 | + rel="noopener noreferrer" |
| 70 | + aria-label={`Download ${siteName} on Google Play`} |
| 71 | + className="flex items-center justify-center rounded-md border border-gray-300 bg-white px-4 py-3 text-sm font-medium text-gray-900 hover:bg-gray-50" |
| 72 | + > |
| 73 | + Get it on Google Play |
| 74 | + </a> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + ); |
| 79 | +}; |
| 80 | + |
| 81 | +export default AppShare; |
0 commit comments