import { useEffect, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from "react" import type { Platform } from "./platform" import { RELEASE } from "./release" export function AppleMark() { return ( ) } export function WindowsMark() { return ( ) } export function LinuxMark() { return ( ) } export const PLATFORM_NAMES: Record = { mac: "Mac", "mac-intel": "Mac", "windows-x64": "Windows", "windows-arm64": "Windows", linux: "Linux", "linux-arm64": "Linux", mobile: "Mac, Windows, and Linux", } export const SHORTCUTS: Record = { mac: "Option", "mac-intel": "Option", "windows-x64": "Ctrl+Space", "windows-arm64": "Ctrl+Space", linux: "Alt+Space", "linux-arm64": "Alt+Space", mobile: "a shortcut", } 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 Os = "mac" | "windows" | "linux" type Arch = "x64" | "arm64" interface Choice { id: string label: string } interface Row { label: string note?: string file?: 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: }, ] 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 }, ] 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 }, ] } function osOf(platform: Platform): Os { if (platform.startsWith("windows")) return "windows" if (platform.startsWith("linux")) return "linux" 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 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 ? null : row.command !== undefined ? ( ) : ( )}
) } // 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]) } export default function Download({ platform }: { platform: Platform }) { const [chosenOs, setOs] = useState() const os = chosenOs ?? osOf(platform) const [chosenArch, setArch] = useState() const arch = chosenArch ?? archOf(platform) const [pkg, setPkg] = useState("deb") const choices = { arch, setArch, pkg, setPkg } const tabs = useRef(null) useTabIndicator(tabs, os) return (
{OSES.map((entry) => ( ))}
{/* Every OS's stack shares one grid cell, so the tallest reserves the height and switching tabs never moves the page. */}
{OSES.map((entry) => (
{rows(entry.id, platform === "mac-intel", choices).map((row) => ( ))}
))}
) }