|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 3 | +import { |
| 4 | + faAndroid, |
| 5 | + faApple, |
| 6 | + faLinux, |
| 7 | + faWindows, |
| 8 | + IconDefinition, |
| 9 | +} from "@fortawesome/free-brands-svg-icons"; |
| 10 | +import { DownloadIcon } from "lucide-react"; |
| 11 | +import releaseData from "./data/release-data.json"; |
| 12 | + |
| 13 | +type Platform = "windows" | "mac" | "linux" | "ios" | "android" | "unknown"; |
| 14 | + |
| 15 | +const PlatformMetadata: Record< |
| 16 | + Platform, |
| 17 | + { name: string; icon: IconDefinition } |
| 18 | +> = { |
| 19 | + windows: { |
| 20 | + name: "Windows", |
| 21 | + icon: faWindows, |
| 22 | + }, |
| 23 | + mac: { |
| 24 | + name: "macOS", |
| 25 | + icon: faApple, |
| 26 | + }, |
| 27 | + linux: { |
| 28 | + name: "Linux", |
| 29 | + icon: faLinux, |
| 30 | + }, |
| 31 | + ios: { |
| 32 | + name: "iOS", |
| 33 | + icon: faApple, |
| 34 | + }, |
| 35 | + android: { |
| 36 | + name: "Android", |
| 37 | + icon: faAndroid, |
| 38 | + }, |
| 39 | + unknown: { |
| 40 | + name: "Unknown", |
| 41 | + icon: faAndroid, |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +type DownloadLink = { |
| 46 | + name: string; |
| 47 | + urlPattern: RegExp; |
| 48 | +}; |
| 49 | + |
| 50 | +const DownloadLinks: Record<string, DownloadLink> = { |
| 51 | + windows_exe: { |
| 52 | + name: "Windows (exe)", |
| 53 | + urlPattern: /.*\.exe/, |
| 54 | + }, |
| 55 | + windows_msi: { |
| 56 | + name: "Windows (msi)", |
| 57 | + urlPattern: /.*\.msi/, |
| 58 | + }, |
| 59 | + macos: { |
| 60 | + name: "macOS", |
| 61 | + urlPattern: /.*\.dmg/, |
| 62 | + }, |
| 63 | + linux_appimage: { |
| 64 | + name: "Linux (AppImage)", |
| 65 | + urlPattern: /.*\.AppImage/, |
| 66 | + }, |
| 67 | + linux_deb: { |
| 68 | + name: "Linux (deb)", |
| 69 | + urlPattern: /.*\.deb/, |
| 70 | + }, |
| 71 | +}; |
| 72 | + |
| 73 | +const PlatformLinks: Record<Platform, DownloadLink[]> = { |
| 74 | + windows: [DownloadLinks.windows_exe, DownloadLinks.windows_msi], |
| 75 | + mac: [DownloadLinks.macos], |
| 76 | + linux: [DownloadLinks.linux_appimage, DownloadLinks.linux_deb], |
| 77 | + ios: [], |
| 78 | + android: [], |
| 79 | + unknown: [], |
| 80 | +}; |
| 81 | + |
| 82 | +const ReleaseAssets = releaseData.assets.map((asset: any) => asset.browser_download_url); |
| 83 | +const ReleaseVersion = releaseData.tag_name; |
| 84 | + |
| 85 | +function detectPlatform(): Platform { |
| 86 | + if (typeof window === "undefined") return "unknown"; |
| 87 | + |
| 88 | + const userAgent = window.navigator.userAgent.toLowerCase(); |
| 89 | + |
| 90 | + if (userAgent.includes("win")) return "windows"; |
| 91 | + if (userAgent.includes("mac")) return "mac"; |
| 92 | + if (userAgent.includes("linux")) return "linux"; |
| 93 | + if (/iphone|ipad|ipod/.test(userAgent)) return "ios"; |
| 94 | + if (userAgent.includes("android")) return "android"; |
| 95 | + |
| 96 | + return "unknown"; |
| 97 | +} |
| 98 | + |
| 99 | +function getUrlFromPattern(assets: string[], pattern: RegExp) { |
| 100 | + const asset = assets.find((asset) => pattern.test(asset)); |
| 101 | + return asset; |
| 102 | +} |
| 103 | + |
| 104 | +export const Download = () => { |
| 105 | + const [platform, setPlatform] = useState<Platform>("unknown"); |
| 106 | + const [showAll, setShowAll] = useState(false); |
| 107 | + |
| 108 | + useEffect(() => { |
| 109 | + const platform = detectPlatform(); |
| 110 | + setPlatform(platform); |
| 111 | + if (PlatformLinks[platform].length === 0) { |
| 112 | + setShowAll(true); |
| 113 | + } |
| 114 | + }, []); |
| 115 | + |
| 116 | + return ( |
| 117 | + <div className="bg-base-200 dark:bg-base-300 text-base-content min-h-full w-full flex flex-col justify-center items-center p-10 pb-48"> |
| 118 | + <img src="/zmk-mac-app-icon.webp" alt="ZMK Studio" className="w-64" /> |
| 119 | + <div className="text-3xl mb-1">ZMK Studio</div> |
| 120 | + <div className="text-md mb-1 opacity-70"> |
| 121 | + {ReleaseVersion} |
| 122 | + </div> |
| 123 | + <div className="bg-base-100 p-8 max-w-md w-full m-2 rounded-lg shadow-lg dark:shadow-xl"> |
| 124 | + {PlatformLinks[platform].length > 0 && ( |
| 125 | + <> |
| 126 | + <div className="flex flex-col gap-3 mb-3"> |
| 127 | + {PlatformLinks[platform].map((link) => ( |
| 128 | + <a |
| 129 | + key={link.name} |
| 130 | + href={getUrlFromPattern(ReleaseAssets, link.urlPattern)} |
| 131 | + className="p-3 text-lg bg-primary hover:opacity-85 active:opacity-70 text-primary-content rounded-lg justify-center items-center gap-3 flex" |
| 132 | + > |
| 133 | + <FontAwesomeIcon icon={PlatformMetadata[platform].icon} className="h-6"/>{" "} |
| 134 | + Download for {link.name} |
| 135 | + </a> |
| 136 | + ))} |
| 137 | + </div> |
| 138 | + </> |
| 139 | + )} |
| 140 | + <div className="flex flex-col gap-3"> |
| 141 | + {PlatformLinks[platform].length > 0 && ( |
| 142 | + <button |
| 143 | + onClick={() => setShowAll(!showAll)} |
| 144 | + className="text-primary text-left hover:underline" |
| 145 | + > |
| 146 | + {showAll ? "Hide" : "Show"} all downloads |
| 147 | + </button> |
| 148 | + )} |
| 149 | + {showAll && ( |
| 150 | + <div> |
| 151 | + {Object.entries(PlatformLinks).map(([platform, links]) => ( |
| 152 | + <div key={platform}> |
| 153 | + {links.map((link) => ( |
| 154 | + <a |
| 155 | + key={link.name} |
| 156 | + href={getUrlFromPattern(ReleaseAssets, link.urlPattern)} |
| 157 | + className="flex gap-1 mb-3 text-base-content hover:underline" |
| 158 | + > |
| 159 | + <DownloadIcon className="w-5" /> |
| 160 | + {link.name} |
| 161 | + </a> |
| 162 | + ))} |
| 163 | + </div> |
| 164 | + ))} |
| 165 | + </div> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + <a |
| 170 | + className="text-md hover:underline" |
| 171 | + href="https://github.com/zmkfirmware/zmk-studio/releases" |
| 172 | + > |
| 173 | + See GitHub Releases → |
| 174 | + </a> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +}; |
0 commit comments