// TODO: Refactor this component: into a different file, use this file to export the components import React, { useState, useCallback, useEffect } from "react"; // next import { useRouter } from "next/router"; // swr import useSWR from "swr"; // hooks import { Combobox, Dialog, Transition } from "@headlessui/react"; import { FolderIcon, RectangleStackIcon, ClipboardDocumentListIcon, MagnifyingGlassIcon, } from "@heroicons/react/24/outline"; import useTheme from "hooks/use-theme"; import useToast from "hooks/use-toast"; import useUser from "hooks/use-user"; // services import userService from "services/user.service"; // components import ShortcutsModal from "components/command-palette/shortcuts"; import { CreateProjectModal } from "components/project"; import { CreateUpdateIssueModal } from "components/issues/modal"; import CreateUpdateCycleModal from "components/project/cycles/create-update-cycle-modal"; import CreateUpdateModuleModal from "components/project/modules/create-update-module-modal"; import BulkDeleteIssuesModal from "components/common/bulk-delete-issues-modal"; // headless ui // helpers import { copyTextToClipboard } from "helpers/string.helper"; // types import { IIssue } from "types"; // ui import { Button } from "components/ui"; // icons // fetch-keys import { USER_ISSUE } from "constants/fetch-keys"; const CommandPalette: React.FC = () => { 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 [isCreateModuleModalOpen, setIsCreateModuleModalOpen] = useState(false); const [isBulkDeleteIssuesModalOpen, setIsBulkDeleteIssuesModalOpen] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { user } = useUser(); const { setToastAlert } = useToast(); const { toggleCollapsed } = useTheme(); const { data: myIssues } = useSWR( workspaceSlug ? USER_ISSUE(workspaceSlug as string) : null, workspaceSlug ? () => userService.userIssues(workspaceSlug as string) : null ); const filteredIssues: IIssue[] = query === "" ? myIssues ?? [] : myIssues?.filter( (issue) => issue.name.toLowerCase().includes(query.toLowerCase()) || `${issue.project_detail.identifier}-${issue.sequence_id}` .toLowerCase() .includes(query.toLowerCase()) ) ?? []; const quickActions = [ { name: "Add new issue...", icon: RectangleStackIcon, hide: !projectId, shortcut: "I", onClick: () => { setIsIssueModalOpen(true); }, }, { name: "Add new project...", icon: ClipboardDocumentListIcon, hide: !workspaceSlug, shortcut: "P", onClick: () => { setIsProjectModalOpen(true); }, }, ]; const handleCommandPaletteClose = () => { setIsPaletteOpen(false); setQuery(""); }; const handleKeyDown = useCallback( (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key === "/") { e.preventDefault(); setIsPaletteOpen(true); } else if ((e.ctrlKey || e.metaKey) && e.key === "i") { e.preventDefault(); setIsIssueModalOpen(true); } else if ((e.ctrlKey || e.metaKey) && e.key === "p") { e.preventDefault(); setIsProjectModalOpen(true); } else if ((e.ctrlKey || e.metaKey) && e.key === "b") { e.preventDefault(); toggleCollapsed(); } else if ((e.ctrlKey || e.metaKey) && e.key === "h") { e.preventDefault(); setIsShortcutsModalOpen(true); } else if ((e.ctrlKey || e.metaKey) && e.key === "q") { e.preventDefault(); setIsCreateCycleModalOpen(true); } else if ((e.ctrlKey || e.metaKey) && e.key === "m") { e.preventDefault(); setIsCreateModuleModalOpen(true); } else if ((e.ctrlKey || e.metaKey) && e.key === "d") { e.preventDefault(); setIsBulkDeleteIssuesModalOpen(true); } else if ((e.ctrlKey || e.metaKey) && 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]); if (!user) return null; return ( <> {workspaceSlug && ( )} {projectId && ( <> )} setIsIssueModalOpen(false)} /> setQuery("")} appear >
{ if (value?.url) router.push(value.url); else if (value?.onClick) value.onClick(); handleCommandPaletteClose(); }} >
{filteredIssues.length > 0 && ( <>
  • {query === "" && (

    Select issues

    )}
      {filteredIssues.map((issue) => ( `flex cursor-pointer select-none items-center justify-between rounded-md px-3 py-2 ${ active ? "bg-gray-500 bg-opacity-5 text-gray-900" : "" }` } > {({ active }) => ( <>
      {issue.project_detail?.identifier}-{issue.sequence_id} {issue.name}
      )}
      ))}
  • )} {query === "" && (
  • Quick actions

      {quickActions.map( (action) => !action.hide && ( `flex cursor-default select-none items-center rounded-md px-3 py-2 ${ active ? "bg-gray-500 bg-opacity-5 text-gray-900" : "" }` } > {({ active }) => ( <> ) )}
  • )}
    {query !== "" && filteredIssues.length === 0 && (
    )}
    ); }; export default CommandPalette;