From 5c6860a07421d1c38904be511fd6d8fec29d44ff Mon Sep 17 00:00:00 2001 From: cc Date: Wed, 2 Dec 2026 02:40:50 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20command=20palette=20(=E2=8C=98=E2=87=A7?= =?UTF-8?q?P)=20for=20navigation=20and=20versions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Desktop only (⌘⇧P / Ctrl+Shift+P, or ⌘K): go to home, keys, files or the shortcut help; switch theme, copy the page link, open the value filter; jump to any build of any platform. Only the OS lists are fetched, on first open. Co-Authored-By: Claude Opus 5.5 --- src/components/command-palette.tsx | 295 ++++++++++++++++++++++++++ src/components/keyboard-shortcuts.tsx | 8 +- src/components/oslist.tsx | 2 +- 3 files changed, 303 insertions(+), 2 deletions(-) create mode 100644 src/components/command-palette.tsx diff --git a/src/components/command-palette.tsx b/src/components/command-palette.tsx new file mode 100644 index 0000000..fca2b8e --- /dev/null +++ b/src/components/command-palette.tsx @@ -0,0 +1,295 @@ +"use client"; + +import { + Braces, + Folder, + Home, + Keyboard, + KeyRound, + Link as LinkIcon, + SunMoon, +} from "lucide-react"; +import { useTheme } from "next-themes"; +import { usePathname, useRouter, useSearchParams } from "next/navigation"; +import { useEffect, useState } from "react"; + +import { BetaBadge } from "@/components/beta-badge"; +import { VALUE_FILTER_ATTR } from "@/components/keyboard-shortcuts"; +import { getPlatformName } from "@/components/oslist"; +import { + Command, + CommandEmpty, + CommandGroup, + CommandInput, + CommandItem, + CommandList, + CommandShortcut, +} from "@/components/ui/command"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogTitle, +} from "@/components/ui/dialog"; +import { createEngine } from "@/lib/engine"; +import { basePath, dataURL } from "@/lib/env"; +import type { OS } from "@/lib/types"; + +/** + * ⌘⇧P / Ctrl+Shift+P (or ⌘K): jump to a page or to any OS version, plus + * a few local actions. Only the OS lists are fetched (cached per page). + * Desktop only; phones have no shortcut to open it. + */ + +const DESKTOP = "(min-width: 640px) and (pointer: fine)"; + +/** + * Every word must appear (cmdk's default fuzzy match lets "26.6" hit + * 18.6.2 and "tahoe" hit "theme"); a version that starts with the query + * ranks first. + */ +function filter(value: string, search: string, keywords?: string[]) { + const haystack = [value, ...(keywords ?? [])].join(" ").toLowerCase(); + const words = search.toLowerCase().split(/\s+/).filter(Boolean); + if (!words.every((word) => haystack.includes(word))) return 0; + return keywords?.[0]?.toLowerCase().startsWith(words[0]) ? 1 : 0.5; +} + +interface Platform { + name: string; + /** Newest first. */ + list: OS[]; +} + +/** Every platform's builds; the engine caches the lists per page. */ +async function loadPlatforms(): Promise { + const names: string[] = await fetch(`${dataURL}/groups.json`).then((r) => + r.json(), + ); + return Promise.all( + names.map(async (name) => { + const list = await (await createEngine(name)).listOS(); + return { name, list: list.reverse() }; + }), + ); +} + +export function CommandPalette({ + os, + onShowShortcuts, +}: { + os: string; + onShowShortcuts: () => void; +}) { + const router = useRouter(); + const pathname = usePathname(); + const searchParams = useSearchParams(); + const { resolvedTheme, setTheme } = useTheme(); + const [open, setOpen] = useState(false); + const [platforms, setPlatforms] = useState([]); + + const [group, build] = os ? os.split("/") : ["", ""]; + + useEffect(() => { + const onKey = (event: KeyboardEvent) => { + const key = event.key.toLowerCase(); + const hit = + (event.metaKey || event.ctrlKey) && + !event.altKey && + (event.shiftKey ? key === "p" : key === "k"); + if (!hit || !window.matchMedia(DESKTOP).matches) return; + event.preventDefault(); + setOpen((was) => !was); + }; + window.addEventListener("keydown", onKey); + return () => window.removeEventListener("keydown", onKey); + }, []); + + useEffect(() => { + if (open && platforms.length === 0) { + loadPlatforms().then(setPlatforms, () => {}); + } + }, [open, platforms.length]); + + const go = (href: string) => { + setOpen(false); + router.push(href); + }; + + /** + * Same platform: stay on this page with the other build, like the version + * picker. Another platform: its keys page (paths and keys differ). + */ + const openOS = (platform: string, version: OS) => { + const tag = `${platform}/${version.version}_${version.build}`; + const page = pathname.replace(basePath, ""); + if (platform === group && page.startsWith("/os/")) { + const next = new URLSearchParams(searchParams.toString()); + next.set("os", tag); + go(`${page}?${next.toString()}`); + } else { + go(`/os/keys?os=${encodeURIComponent(tag)}`); + } + }; + + const encodedOs = encodeURIComponent(os); + const onFindPage = pathname.replace(basePath, "").startsWith("/os/find"); + const dark = resolvedTheme === "dark"; + // The current platform first. + const ordered = [...platforms].sort( + (a, b) => Number(b.name === group) - Number(a.name === group), + ); + + return ( + + + Command palette + + Go to a page or an OS version + + + + + No results. + + go("/")}> + + Home + g h + + {os && ( + <> + go(`/os/keys?os=${encodedOs}`)} + > + + Keys + g k + + go(`/os/files?os=${encodedOs}`)} + > + + Files + g f + + + )} + { + setOpen(false); + onShowShortcuts(); + }} + > + + Keyboard shortcuts + ? + + + + + { + setTheme(dark ? "light" : "dark"); + setOpen(false); + }} + > + + {dark ? "Switch to light theme" : "Switch to dark theme"} + + { + navigator.clipboard.writeText(window.location.href); + setOpen(false); + }} + > + + Copy link to this page + + {onFindPage && ( + { + setOpen(false); + // After the dialog closes, so focus lands in the popover. + setTimeout( + () => + document + .querySelector(`[${VALUE_FILTER_ATTR}]`) + ?.click(), + 0, + ); + }} + > + + Filter by value… + = + + )} + + + {ordered.map((platform) => { + const label = getPlatformName(platform.name); + return ( + + {platform.list.map((version) => { + const current = + platform.name === group && + `${version.version}_${version.build}` === build; + return ( + + current + ? setOpen(false) + : openOS(platform.name, version) + } + > + + {version.version} + + + {version.build} + + {version.beta && } + {current && ( + + current + + )} + + ); + })} + + ); + })} + + + + + ); +} diff --git a/src/components/keyboard-shortcuts.tsx b/src/components/keyboard-shortcuts.tsx index 2b4e531..25ce351 100644 --- a/src/components/keyboard-shortcuts.tsx +++ b/src/components/keyboard-shortcuts.tsx @@ -2,7 +2,9 @@ import { Keyboard } from "lucide-react"; import { useRouter } from "next/navigation"; -import { useEffect, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; + +import { CommandPalette } from "@/components/command-palette"; import { HEADER_PORTAL_ID } from "@/components/header-portal"; import { @@ -19,6 +21,7 @@ import { * j / k next / previous item ? this help * g h|k|f go home / keys / files v version picker * = value filter (find page) + * ⌘⇧P / ⌘K command palette (desktop) * * Lists opt in by marking focusable rows with `data-nav-item`. Widgets that * own their keys (the namespace tree, file trees) handle them first and call @@ -91,6 +94,7 @@ function stepItems(direction: 1 | -1) { export function KeyboardShortcuts({ os }: { os: string }) { const router = useRouter(); const [helpOpen, setHelpOpen] = useState(false); + const showHelp = useCallback(() => setHelpOpen(true), []); useEffect(() => { let gotoAt = 0; @@ -172,6 +176,7 @@ export function KeyboardShortcuts({ os }: { os: string }) { return ( <> +