Files
Kaname/src/components/command-palette.tsx
T
Claude d10a491042 Client bundles: no drizzle in the browser, lazy palette, one copy of the painting
- The enum arrays move from schema.ts to src/lib/db/enums.ts (no imports),
  which schema.ts re-exports. The endpoint form and benchmark start imported
  ENDPOINT_ENGINES/QUALITY_SECTIONS from the schema, which put drizzle-orm,
  every table definition and ulid (18 KB gzipped) on the endpoint pages. An
  ESLint rule now refuses value imports from the schema in client code.
- The ⌘K palette dialog is its own chunk, loaded on first open and preloaded
  when you hover or focus Search or press ⌘/Ctrl.
- The dot painting's brightness grid was copied into 10 page chunks (~10 KB
  gzipped each); it is now one async chunk, parsed once per tab. The canvas
  skips redraws when size, pixel ratio and theme are unchanged (the
  ResizeObserver's first callback drew everything a second time) and draws at
  most once per frame while resizing.

First-load JS per route drops by 10-29 KB gzipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
2026-11-01 20:57:53 +00:00

68 lines
2.6 KiB
TypeScript

"use client";
import { SearchIcon } from "lucide-react";
import dynamic from "next/dynamic";
import { useEffect, useState, useSyncExternalStore } from "react";
import { cn } from "@/lib/utils";
// The dialog (base-ui Dialog, all items and icons) is its own chunk, kept out of every page's first
// load: most page views never open it. It starts loading when you reach for it (hovering or focusing
// the Search button, pressing ⌘ or Ctrl) and renders on the first open.
const loadDialog = () => import("./command-palette-dialog");
const CommandPaletteDialog = dynamic(() => loadDialog().then((m) => m.CommandPaletteDialog), { ssr: false });
const preload = () => void loadDialog();
const OPEN_EVENT = "kaname:command-palette";
/** ⌘K / Ctrl+K: jump to any model, endpoint, benchmark run, settings page or action. */
export function CommandPalette() {
const [open, setOpen] = useState(false);
// Mounted from the first open on, so it keeps its state like before.
const [used, setUsed] = useState(false);
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key === "Meta" || e.key === "Control") preload();
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
e.preventDefault();
setUsed(true);
setOpen((o) => !o);
}
};
const onOpen = () => {
setUsed(true);
setOpen(true);
};
window.addEventListener("keydown", onKey);
window.addEventListener(OPEN_EVENT, onOpen);
return () => {
window.removeEventListener("keydown", onKey);
window.removeEventListener(OPEN_EVENT, onOpen);
};
}, []);
return used ? <CommandPaletteDialog open={open} setOpen={setOpen} /> : null;
}
const isMac = () => /mac|iphone|ipad/i.test(navigator.platform || navigator.userAgent);
const noSubscribe = () => () => {};
/** The menu button that opens the palette, showing its shortcut. */
export function CommandPaletteButton({ className }: { className?: string }) {
const mac = useSyncExternalStore(noSubscribe, isMac, () => false);
return (
<button
type="button"
onClick={() => window.dispatchEvent(new Event(OPEN_EVENT))}
onPointerEnter={preload}
onFocus={preload}
className={cn("flex h-8 items-center gap-2 rounded-lg px-2 text-sm text-muted-foreground transition-colors hover:bg-muted/60 hover:text-foreground", className)}
aria-label="Search"
>
<SearchIcon className="size-4" />
<span className="hidden lg:inline">Search</span>
<kbd className="hidden rounded-full border border-foreground/15 px-2 font-sans text-[10px] leading-4 lg:inline">{mac ? "⌘K" : "Ctrl K"}</kbd>
</button>
);
}