"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 ? : 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 (
);
}