import React, { useState, useCallback, useEffect } from "react"; // next import { useRouter } from "next/router"; // headless ui import { Combobox, Dialog, Transition } from "@headlessui/react"; // hooks import useUser from "lib/hooks/useUser"; import useTheme from "lib/hooks/useTheme"; import useToast from "lib/hooks/useToast"; // icons import { MagnifyingGlassIcon } from "@heroicons/react/20/solid"; import { DocumentPlusIcon, FolderPlusIcon, FolderIcon } from "@heroicons/react/24/outline"; // commons import { classNames, copyTextToClipboard } from "constants/common"; // components import ShortcutsModal from "components/command-palette/shortcuts"; import CreateProjectModal from "components/project/CreateProjectModal"; import CreateUpdateIssuesModal from "components/project/issues/CreateUpdateIssueModal"; import CreateUpdateCycleModal from "components/project/cycles/CreateUpdateCyclesModal"; // types import { IIssue } from "types"; type ItemType = { name: string; url?: string; onClick?: () => void; }; const CommandPalette: React.FC = () => { const router = useRouter(); const [query, setQuery] = useState(""); const [isPaletteOpen, setIsPaletteOpen] = useState(false); const [isIssueModalOpen, setIsIssueModalOpen] = useState(false); const [isProjectModalOpen, setIsProjectModalOpen] = useState(false); const [isShortcutsModalOpen, setIsShortcutsModalOpen] = useState(false); const [isCreateCycleModalOpen, setIsCreateCycleModalOpen] = useState(false); const { issues, activeProject } = useUser(); const { toggleCollapsed } = useTheme(); const { setToastAlert } = useToast(); const filteredIssues: IIssue[] = query === "" ? issues?.results ?? [] : issues?.results.filter((issue) => issue.name.toLowerCase().includes(query.toLowerCase())) ?? []; const quickActions = [ { name: "Add new issue...", icon: DocumentPlusIcon, shortcut: "I", onClick: () => { setIsIssueModalOpen(true); }, }, { name: "Add new project...", icon: FolderPlusIcon, shortcut: "P", onClick: () => { setIsProjectModalOpen(true); }, }, ]; const handleCommandPaletteClose = () => { setIsPaletteOpen(false); setQuery(""); }; const handleKeyDown = useCallback( (e: KeyboardEvent) => { if (e.ctrlKey && e.key === "/") { e.preventDefault(); setIsPaletteOpen(true); } else if (e.ctrlKey && e.key === "i") { e.preventDefault(); setIsIssueModalOpen(true); } else if (e.ctrlKey && e.key === "p") { e.preventDefault(); setIsProjectModalOpen(true); } else if (e.ctrlKey && e.key === "b") { e.preventDefault(); toggleCollapsed(); } else if (e.ctrlKey && e.key === "h") { e.preventDefault(); setIsShortcutsModalOpen(true); } else if (e.ctrlKey && e.key === "q") { e.preventDefault(); setIsCreateCycleModalOpen(true); } else if (e.ctrlKey && e.altKey && e.key === "c") { e.preventDefault(); if (!router.query.issueId) return; const url = new URL(window.location.href); copyTextToClipboard(url.href) .then(() => { setToastAlert({ type: "success", title: "Copied to clipboard", }); }) .catch(() => { setToastAlert({ type: "error", title: "Some error occurred", }); }); } }, [toggleCollapsed, setToastAlert, router] ); useEffect(() => { document.addEventListener("keydown", handleKeyDown); return () => document.removeEventListener("keydown", handleKeyDown); }, [handleKeyDown]); return ( <> {activeProject && ( )} setQuery("")} appear > { const { url, onClick } = item; if (url) router.push(url); else if (onClick) onClick(); handleCommandPaletteClose(); }} > setQuery(event.target.value)} /> {filteredIssues.length > 0 && ( <> {query === "" && ( Issues )} {filteredIssues.map((issue) => ( classNames( "flex cursor-pointer select-none items-center rounded-md px-3 py-2", active ? "bg-gray-900 bg-opacity-5 text-gray-900" : "" ) } > {({ active }) => ( <> {issue.name} {active && ( Jump to... )} > )} ))} > )} {query === "" && ( Quick actions {quickActions.map((action) => ( classNames( "flex cursor-default select-none items-center rounded-md px-3 py-2", active ? "bg-gray-900 bg-opacity-5 text-gray-900" : "" ) } > {({ active }) => ( <> {action.name} ⌘ {action.shortcut} > )} ))} )} {query !== "" && filteredIssues.length === 0 && ( We couldn{"'"}t find any issue with that term. Please try again. )} > ); }; export default CommandPalette;
We couldn{"'"}t find any issue with that term. Please try again.