import { useEffect, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from "react" import { ANDROID_PATH, IOS_PATH } from "./base" import type { Platform } from "./platform" import { RELEASE } from "./release" export function AppleMark() { return ( ) } export function WindowsMark() { return ( ) } export function LinuxMark() { return ( ) } export function MobileMark() { return ( ) } export type Os = "mac" | "windows" | "linux" | "mobile" export type DesktopOs = Exclude // The key and name each desktop tab shows, so the hero follows the tab the // visitor picks instead of staying on their own OS. export const OS_SHORTCUTS: Record = { mac: "Option", windows: "Alt", linux: "Alt+Space", } export const OS_NAMES: Record = { mac: "Mac", windows: "Windows", linux: "Linux", } // The hero keycap's legend, in the site's lowercase keycap voice. export const OS_KEY_LEGENDS: Record = { mac: "option", windows: "alt", linux: "alt + space", mobile: "option", } function useCopy(): [boolean, (text: string) => void] { const [copied, setCopied] = useState(false) useEffect(() => { if (!copied) return const timer = setTimeout(() => setCopied(false), 1600) return () => clearTimeout(timer) }, [copied]) return [ copied, (text) => { void navigator.clipboard?.writeText(text).then(() => setCopied(true)) }, ] } type Arch = "x64" | "arm64" interface Choice { id: string label: string } interface Row { label: string note?: string file?: string href?: string external?: boolean actionLabel?: string command?: string // A small switch inside the row, e.g. x64/ARM64 or the Linux package format. choices?: Choice[] chosen?: string choose?: (id: string) => void } type Package = "deb" | "rpm" | "nix" | "aur" interface Choices { arch: Arch setArch: (arch: Arch) => void pkg: Package setPkg: (pkg: Package) => void } const ARCHES: Choice[] = [ { id: "x64", label: "x64" }, { id: "arm64", label: "ARM64" }, ] const OSES: { id: Os; label: string; mark: ReactNode }[] = [ { id: "mac", label: "macOS", mark: }, { id: "windows", label: "Windows", mark: }, { id: "linux", label: "Linux", mark: }, { id: "mobile", label: "Mobile", mark: }, ] function rows(os: Os, intelMac: boolean, { arch, setArch, pkg, setPkg }: Choices): Row[] { const linux = RELEASE.linux const archSwitch = { choices: ARCHES, chosen: arch, choose: (id: string) => setArch(id as Arch) } // Hex runs only on Apple silicon, so an Intel Mac gets the reason, not a download. if (os === "mac" && intelMac) return [{ label: "Download", note: "Requires an Apple silicon Mac" }] if (os === "mac") return [ { label: "Download", note: "Apple silicon, macOS 15 or later", file: RELEASE.mac.url }, { label: "Homebrew", command: RELEASE.homebrew }, ] if (os === "windows") return [ { label: "Installer", file: arch === "arm64" ? RELEASE.windowsArm64.url : RELEASE.windowsX64.url, ...archSwitch }, { label: "PowerShell", command: RELEASE.windows.command }, ] if (os === "mobile") return [ { label: "iOS", note: "App Store, iOS 18 or later", href: IOS_PATH, external: true, actionLabel: "Get Hex for iOS on the App Store", }, { label: "Android", note: "Hex Preview APK, Android 12 or later", href: ANDROID_PATH, actionLabel: "Get Hex Preview for Android", }, ] const packages: (Choice & { command: string })[] = [ { id: "deb", label: "Debian", command: linux.deb[arch] }, { id: "rpm", label: "Fedora", command: linux.rpm[arch] }, { id: "nix", label: "Nix", command: RELEASE.nix }, ...(linux.aur !== undefined ? [{ id: "aur", label: "AUR", command: linux.aur }] : []), ] const chosenPackage = packages.find((entry) => entry.id === pkg) ?? packages[0] return [ { label: "Script", command: linux.command }, { label: "Package", command: chosenPackage.command, choices: packages, chosen: chosenPackage.id, choose: (id) => setPkg(id as Package), }, { label: "Binary", file: linux.binary[arch], ...archSwitch }, ] } export function osOf(platform: Platform): Os { if (platform.startsWith("windows")) return "windows" if (platform.startsWith("linux")) return "linux" if (platform === "mobile") return "mobile" return "mac" } const archOf = (platform: Platform): Arch => (platform.endsWith("arm64") ? "arm64" : "x64") const ICON = { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1.8, strokeLinecap: "round", strokeLinejoin: "round" } as const function CopyIcon() { return ( ) } function CheckIcon() { return ( ) } function DownloadIcon() { return ( ) } function ExternalIcon() { return ( ) } function ArrowRightIcon() { return ( ) } function StackRow({ row }: { row: Row }) { const [copied, copy] = useCopy() return (
{row.label} {row.choices && ( {row.choices.map((choice) => ( ))} )} {row.command !== undefined ? {row.command} : {row.note}} {row.command === undefined && row.file === undefined && row.href === undefined ? null : row.command !== undefined ? ( ) : row.href !== undefined ? ( {row.external ? : } ) : ( )}
) } // Places the selected-tab pill (`.install__os::before`) under the chosen tab. // Until the first measurement the selected tab paints its own background, so // the prerendered page and a page without scripts still show the selection. // The pill slides only after a click (`data-animate`), so the OS picked from // the user agent on load doesn't animate in from macOS. function useTabIndicator(list: RefObject, selected: Os) { useLayoutEffect(() => { const element = list.current if (!element) return const place = () => { const tab = element.querySelector('[aria-selected="true"]') if (!tab) return element.style.setProperty("--tab-x", `${tab.offsetLeft}px`) element.style.setProperty("--tab-width", `${tab.offsetWidth}px`) element.dataset.measured = "" } place() // Tab widths change when Inter finishes loading or the viewport resizes. const observer = new ResizeObserver(place) observer.observe(element) return () => observer.disconnect() }, [list, selected]) } // Every OS, architecture, and package for a visitor without scripts, because // the tabs and the switches inside a row are buttons. It is hidden whenever // scripts run; index.html's