import { t } from "@lingui/core/macro"; import { Trans } from "@lingui/react/macro"; import { useHotkeys } from "@tanstack/react-hotkeys"; import { useEffect, useRef, useState } from "react"; import { Command, CommandEmpty, CommandInput, CommandList } from "@reactive-resume/ui/components/command"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@reactive-resume/ui/components/dialog"; import { NavigationCommandGroup } from "./pages/navigation"; import { PreferencesCommandGroup } from "./pages/preferences"; import { ResumesCommandGroup } from "./pages/resumes"; import { useCommandPaletteStore } from "./store"; export function CommandPalette() { const inputRef = useRef(null); const commandRef = useRef(null); const [selectedValue, setSelectedValue] = useState(); const { open, search, pages, setOpen, setSearch, goBack } = useCommandPaletteStore(); const isFirstPage = pages.length === 0; const currentPage = pages[pages.length - 1]; const commandListPage = currentPage ?? "root"; // Toggle command palette with Cmd+K / Ctrl+K useHotkeys([ { hotkey: "Mod+K", callback: () => { setOpen(!open); }, }, { hotkey: "Escape", callback: () => { if (!open) return; setOpen(false); }, }, { hotkey: "Backspace", callback: (event) => { // Only handle if the command palette is open if (!open) return; const input = inputRef.current; if (!input) return; // Only handle if input is focused if (document.activeElement !== input) return; // If input has text, let the default behavior handle it (delete character) if (search.length > 0) return; // If input is empty, prevent default and go back event.preventDefault(); goBack(); }, }, ]); const handleOpenChange = (newOpen: boolean) => { setOpen(newOpen); }; const handleSearchChange = (value: string) => { setSearch(value); }; useEffect(() => { if (!open) return; const firstItem = commandRef.current?.querySelector( `[data-command-page="${commandListPage}"] [cmdk-item]:not([aria-disabled="true"])`, ); const value = firstItem?.getAttribute("data-value"); if (value) setSelectedValue(value); inputRef.current?.focus(); }, [open, commandListPage]); return ( Builder Command Palette Type a command or search… The command you're looking for doesn't exist. ); }