mirror of
https://github.com/ChiChou/entdb.git
synced 2026-12-06 11:43:25 +01:00
feat: command palette (⌘⇧P) for navigation and versions
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
62fa2a7d8c
commit
5c6860a074
3 files changed
+303
-2
No files matched your search
@@ -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<Platform[]> {
|
||||
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<Platform[]>([]);
|
||||
|
||||
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 (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogContent
|
||||
showCloseButton={false}
|
||||
className="top-[12vh] translate-y-0 gap-0 overflow-hidden p-0 sm:max-w-xl"
|
||||
>
|
||||
<DialogTitle className="sr-only">Command palette</DialogTitle>
|
||||
<DialogDescription className="sr-only">
|
||||
Go to a page or an OS version
|
||||
</DialogDescription>
|
||||
<Command
|
||||
loop
|
||||
filter={filter}
|
||||
className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:pt-2 [&_[cmdk-group-heading]]:pb-1 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground"
|
||||
>
|
||||
<CommandInput
|
||||
className="h-12 text-base"
|
||||
placeholder="Go to a page or a version…"
|
||||
/>
|
||||
<CommandList className="max-h-[min(60vh,28rem)]">
|
||||
<CommandEmpty>No results.</CommandEmpty>
|
||||
<CommandGroup heading="Go to">
|
||||
<CommandItem value="Home" onSelect={() => go("/")}>
|
||||
<Home />
|
||||
Home
|
||||
<CommandShortcut>g h</CommandShortcut>
|
||||
</CommandItem>
|
||||
{os && (
|
||||
<>
|
||||
<CommandItem
|
||||
value="Keys"
|
||||
onSelect={() => go(`/os/keys?os=${encodedOs}`)}
|
||||
>
|
||||
<KeyRound />
|
||||
Keys
|
||||
<CommandShortcut>g k</CommandShortcut>
|
||||
</CommandItem>
|
||||
<CommandItem
|
||||
value="Files"
|
||||
onSelect={() => go(`/os/files?os=${encodedOs}`)}
|
||||
>
|
||||
<Folder />
|
||||
Files
|
||||
<CommandShortcut>g f</CommandShortcut>
|
||||
</CommandItem>
|
||||
</>
|
||||
)}
|
||||
<CommandItem
|
||||
value="Keyboard shortcuts"
|
||||
onSelect={() => {
|
||||
setOpen(false);
|
||||
onShowShortcuts();
|
||||
}}
|
||||
>
|
||||
<Keyboard />
|
||||
Keyboard shortcuts
|
||||
<CommandShortcut>?</CommandShortcut>
|
||||
</CommandItem>
|
||||
</CommandGroup>
|
||||
|
||||
<CommandGroup heading="Actions">
|
||||
<CommandItem
|
||||
value="Theme"
|
||||
keywords={["dark", "light", "mode"]}
|
||||
onSelect={() => {
|
||||
setTheme(dark ? "light" : "dark");
|
||||
setOpen(false);
|
||||
}}
|
||||
>
|
||||
<SunMoon />
|
||||
{dark ? "Switch to light theme" : "Switch to dark theme"}
|
||||
</CommandItem>
|
||||
<CommandItem
|
||||
value="Copy link"
|
||||
keywords={["url", "share"]}
|
||||
onSelect={() => {
|
||||
navigator.clipboard.writeText(window.location.href);
|
||||
setOpen(false);
|
||||
}}
|
||||
>
|
||||
<LinkIcon />
|
||||
Copy link to this page
|
||||
</CommandItem>
|
||||
{onFindPage && (
|
||||
<CommandItem
|
||||
value="Filter by value"
|
||||
onSelect={() => {
|
||||
setOpen(false);
|
||||
// After the dialog closes, so focus lands in the popover.
|
||||
setTimeout(
|
||||
() =>
|
||||
document
|
||||
.querySelector<HTMLElement>(`[${VALUE_FILTER_ATTR}]`)
|
||||
?.click(),
|
||||
0,
|
||||
);
|
||||
}}
|
||||
>
|
||||
<Braces />
|
||||
Filter by value…
|
||||
<CommandShortcut>=</CommandShortcut>
|
||||
</CommandItem>
|
||||
)}
|
||||
</CommandGroup>
|
||||
|
||||
{ordered.map((platform) => {
|
||||
const label = getPlatformName(platform.name);
|
||||
return (
|
||||
<CommandGroup key={platform.name} heading={label}>
|
||||
{platform.list.map((version) => {
|
||||
const current =
|
||||
platform.name === group &&
|
||||
`${version.version}_${version.build}` === build;
|
||||
return (
|
||||
<CommandItem
|
||||
key={version.build}
|
||||
value={`${platform.name}/${version.build}`}
|
||||
keywords={[
|
||||
version.version,
|
||||
version.build,
|
||||
label,
|
||||
version.name ?? "",
|
||||
]}
|
||||
onSelect={() =>
|
||||
current
|
||||
? setOpen(false)
|
||||
: openOS(platform.name, version)
|
||||
}
|
||||
>
|
||||
<span className="font-mono text-[13px]">
|
||||
{version.version}
|
||||
</span>
|
||||
<span className="font-mono text-xs text-muted-foreground">
|
||||
{version.build}
|
||||
</span>
|
||||
{version.beta && <BetaBadge />}
|
||||
{current && (
|
||||
<CommandShortcut className="tracking-normal">
|
||||
current
|
||||
</CommandShortcut>
|
||||
)}
|
||||
</CommandItem>
|
||||
);
|
||||
})}
|
||||
</CommandGroup>
|
||||
);
|
||||
})}
|
||||
</CommandList>
|
||||
</Command>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<>
|
||||
<CommandPalette os={os} onShowShortcuts={showHelp} />
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setHelpOpen(true)}
|
||||
@@ -233,6 +238,7 @@ const SHORTCUTS: { title: string; items: [string[], string][] }[] = [
|
||||
[["v"], "Switch version"],
|
||||
[["="], "Filter by value"],
|
||||
[["?"], "Show shortcuts"],
|
||||
[["⌘", "⇧", "P"], "Command palette"],
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -47,7 +47,7 @@ const PLATFORM_NAMES: Record<string, string> = {
|
||||
osx: "OS X",
|
||||
};
|
||||
|
||||
function getPlatformName(key: string): string {
|
||||
export function getPlatformName(key: string): string {
|
||||
return PLATFORM_NAMES[key] || key;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user