"use client"; // Command palette ⌘K and keyboard shortcuts (PEP-52). One document keydown listener: ⌘K/Ctrl+K // toggles the palette anywhere; the rest (/, g x, j/k, a, ?) only outside text fields. import { useRouter } from "next/navigation"; import { useCallback, useEffect, useRef, useState, useTransition } from "react"; import { Bot, CircleHelp, FileText, FolderKanban, Keyboard, MessageCircleQuestion, Play, Search } from "lucide-react"; import { matchPalette, type PaletteItem } from "@peppa/shared/palette"; import { moveTaskCard } from "@/app/(app)/actions"; import { PALETTE_KEYS, shortcutsFor } from "@/lib/shortcuts"; import { useWords } from "@/components/Words"; import { cn } from "@/lib/utils"; import { useT } from "@/components/I18n"; const ICON = { project: FolderKanban, story: FileText, runner: Bot, question: MessageCircleQuestion, page: CircleHelp } as const; const GO: Record = { h: "/", p: "/projects", r: "/runners", s: "/settings" }; const CACHE_MS = 30_000; const typing = (t: EventTarget | null) => { const el = t as HTMLElement | null; return !!el && (el.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(el.tagName)); }; export function Kbd({ children }: { children: React.ReactNode }) { return {children}; } export function ShortcutsList() { const w = useWords(); const t = useT(); return ( {shortcutsFor(w).map((s) => ( ))}
{s.keys.map((k, i) => ( {i > 0 && {s.keys[0]!.length > 1 ? "/" : " "}} {k} ))} {t(s.what)} {s.where && — {t(s.where)}}
); } export function CommandPalette() { const t = useT(); const router = useRouter(); const palette = useRef(null); const help = useRef(null); const input = useRef(null); const [items, setItems] = useState([]); const [loadedAt, setLoadedAt] = useState(0); const [query, setQuery] = useState(""); const [sel, setSel] = useState(0); const [note, setNote] = useState(null); const w = useWords(); // PEP-123: «g r» (terminals of agents) works in «Инженер» only. const engineer = useRef(w.engineer); engineer.current = w.engineer; const [pending, start] = useTransition(); const shown = matchPalette(items, query); const load = useCallback(async () => { if (Date.now() - loadedAt < CACHE_MS) return; try { const r = await fetch("/api/palette", { cache: "no-store" }); if (r.ok) { setItems(((await r.json()) as { items: PaletteItem[] }).items); setLoadedAt(Date.now()); } } catch {} }, [loadedAt]); const open = useCallback(() => { help.current?.close(); setQuery(""); setSel(0); setNote(null); if (!palette.current?.open) palette.current?.showModal(); input.current?.focus(); void load(); }, [load]); const close = () => palette.current?.close(); const go = (item: PaletteItem) => { close(); router.push(item.href); }; const startStory = (item: PaletteItem) => { if (!item.canStart) return; const f = new FormData(); f.set("taskKey", item.key); f.set("to", "in_progress"); f.set("back", item.href); setNote(item.key + " → " + t("В работу…")); start(async () => { close(); setLoadedAt(0); await moveTaskCard(f); // redirects to the story page }); }; // Global keys. useEffect(() => { let prefix: { key: string; at: number } | null = null; const onKey = (e: KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") { e.preventDefault(); if (palette.current?.open) close(); else open(); return; } if (e.metaKey || e.ctrlKey || e.altKey || typing(e.target) || palette.current?.open || help.current?.open) return; if (prefix && prefix.key === "g" && Date.now() - prefix.at < 1000 && GO[e.key] && (e.key !== "r" || engineer.current)) { e.preventDefault(); prefix = null; router.push(GO[e.key]!); return; } prefix = null; switch (e.key) { case "g": prefix = { key: "g", at: Date.now() }; return; case "/": { const el = document.querySelector("[data-hotkey-search]"); if (el) { e.preventDefault(); el.focus(); el.select(); } else { e.preventDefault(); open(); } return; } case "?": e.preventDefault(); help.current?.showModal(); return; case "a": { const el = document.querySelector("[data-hotkey-answer]"); if (el) { e.preventDefault(); el.scrollIntoView({ block: "center" }); el.focus(); } return; } case "j": case "k": { const list = [...document.querySelectorAll("[data-hotkey-item]")]; if (!list.length) return; e.preventDefault(); const cur = list.findIndex((a) => a.getAttribute("aria-current") === "true"); const next = list[Math.min(list.length - 1, Math.max(0, cur < 0 ? 0 : cur + (e.key === "j" ? 1 : -1)))]!; next.focus(); next.click(); return; } } }; document.addEventListener("keydown", onKey); return () => document.removeEventListener("keydown", onKey); }, [open, router]); // Anyone can open the palette from a button: )} ); })}
{note ?? t(PALETTE_KEYS)}
e.target === help.current && help.current?.close()} >

{t("Горячие клавиши")}

{t("Клавиши без ⌘ работают вне полей ввода. Список — на странице")}{" "} {t("Помощь → Горячие клавиши")} .

); }