"use client"; import React, { useEffect, useState } from "react"; import { Command } from "cmdk"; import { observer } from "mobx-react"; import { useParams, useRouter } from "next/navigation"; import useSWR from "swr"; import { FolderPlus, Search, Settings } from "lucide-react"; import { Dialog, Transition } from "@headlessui/react"; // icons import { IWorkspaceSearchResults } from "@plane/types"; // hooks import { LayersIcon, Loader, ToggleSwitch, Tooltip } from "@plane/ui"; import { CommandPaletteThemeActions, ChangeIssueAssignee, ChangeIssuePriority, ChangeIssueState, CommandPaletteHelpActions, CommandPaletteIssueActions, CommandPaletteProjectActions, CommandPaletteWorkspaceSettingsActions, CommandPaletteSearchResults, } from "@/components/command-palette"; import { EmptyState } from "@/components/empty-state"; import { EmptyStateType } from "@/constants/empty-state"; import { ISSUE_DETAILS } from "@/constants/fetch-keys"; import { useCommandPalette, useEventTracker, useProject } from "@/hooks/store"; import useDebounce from "@/hooks/use-debounce"; import { usePlatformOS } from "@/hooks/use-platform-os"; // services import { IssueService } from "@/services/issue"; import { WorkspaceService } from "@/services/workspace.service"; // ui // components // types // fetch-keys // constants const workspaceService = new WorkspaceService(); const issueService = new IssueService(); export const CommandModal: React.FC = observer(() => { // hooks const { getProjectById, workspaceProjectIds } = useProject(); const { isMobile } = usePlatformOS(); // states const [placeholder, setPlaceholder] = useState("Type a command or search..."); const [resultsCount, setResultsCount] = useState(0); const [isLoading, setIsLoading] = useState(false); const [isSearching, setIsSearching] = useState(false); const [searchTerm, setSearchTerm] = useState(""); const [results, setResults] = useState({ results: { workspace: [], project: [], issue: [], cycle: [], module: [], issue_view: [], page: [], }, }); const [isWorkspaceLevel, setIsWorkspaceLevel] = useState(false); const [pages, setPages] = useState([]); const { isCommandPaletteOpen, toggleCommandPaletteModal, toggleCreateIssueModal, toggleCreateProjectModal } = useCommandPalette(); const { setTrackElement } = useEventTracker(); // router const router = useRouter(); // router params const { workspaceSlug, projectId, issueId } = useParams(); const page = pages[pages.length - 1]; const debouncedSearchTerm = useDebounce(searchTerm, 500); // TODO: update this to mobx store const { data: issueDetails } = useSWR( workspaceSlug && projectId && issueId ? ISSUE_DETAILS(issueId.toString()) : null, workspaceSlug && projectId && issueId ? () => issueService.retrieve(workspaceSlug.toString(), projectId.toString(), issueId.toString()) : null ); const closePalette = () => { toggleCommandPaletteModal(false); }; const createNewWorkspace = () => { closePalette(); router.push("/create-workspace"); }; useEffect( () => { if (!workspaceSlug) return; setIsLoading(true); if (debouncedSearchTerm) { setIsSearching(true); workspaceService .searchWorkspace(workspaceSlug.toString(), { ...(projectId ? { project_id: projectId.toString() } : {}), search: debouncedSearchTerm, workspace_search: !projectId ? true : isWorkspaceLevel, }) .then((results) => { setResults(results); const count = Object.keys(results.results).reduce( (accumulator, key) => (results.results as any)[key].length + accumulator, 0 ); setResultsCount(count); }) .finally(() => { setIsLoading(false); setIsSearching(false); }); } else { setResults({ results: { workspace: [], project: [], issue: [], cycle: [], module: [], issue_view: [], page: [], }, }); setIsLoading(false); setIsSearching(false); } }, [debouncedSearchTerm, isWorkspaceLevel, projectId, workspaceSlug] // Only call effect if debounced search term changes ); const projectDetails = getProjectById(issueDetails?.project_id ?? ""); return ( setSearchTerm("")} as={React.Fragment}> closePalette()}>
{ if (value.toLowerCase().includes(search.toLowerCase())) return 1; return 0; }} onKeyDown={(e) => { // when search term is not empty, esc should clear the search term if (e.key === "Escape" && searchTerm) setSearchTerm(""); // when user tries to close the modal with esc if (e.key === "Escape" && !page && !searchTerm) closePalette(); // Escape goes to previous page // Backspace goes to previous page when search is empty if (e.key === "Escape" || (e.key === "Backspace" && !searchTerm)) { e.preventDefault(); setPages((pages) => pages.slice(0, -1)); setPlaceholder("Type a command or search..."); } }} >
{issueDetails && (
{projectDetails?.identifier}-{issueDetails.sequence_id} {issueDetails.name}
)} {projectId && (
setIsWorkspaceLevel((prevData) => !prevData)} />
)}
{searchTerm !== "" && (
Search results for{" "} {'"'} {searchTerm} {'"'} {" "} in {!projectId || isWorkspaceLevel ? "workspace" : "project"}:
)} {!isLoading && resultsCount === 0 && searchTerm !== "" && debouncedSearchTerm !== "" && (
)} {(isLoading || isSearching) && ( )} {debouncedSearchTerm !== "" && ( )} {!page && ( <> {/* issue actions */} {issueId && ( setPages(newPages)} setPlaceholder={(newPlaceholder) => setPlaceholder(newPlaceholder)} setSearchTerm={(newSearchTerm) => setSearchTerm(newSearchTerm)} /> )} {workspaceSlug && workspaceProjectIds && workspaceProjectIds.length > 0 && ( { closePalette(); setTrackElement("Command Palette"); toggleCreateIssueModal(true); }} className="focus:bg-custom-background-80" >
Create new issue
C
)} {workspaceSlug && ( { closePalette(); setTrackElement("Command palette"); toggleCreateProjectModal(true); }} className="focus:outline-none" >
Create new project
P
)} {/* project actions */} {projectId && } { setPlaceholder("Search workspace settings..."); setSearchTerm(""); setPages([...pages, "settings"]); }} className="focus:outline-none" >
Search settings...
Create new workspace
{ setPlaceholder("Change interface theme..."); setSearchTerm(""); setPages([...pages, "change-interface-theme"]); }} className="focus:outline-none" >
Change interface theme...
{/* help options */} )} {/* workspace settings actions */} {page === "settings" && workspaceSlug && ( )} {/* issue details page actions */} {page === "change-issue-state" && issueDetails && ( )} {page === "change-issue-priority" && issueDetails && ( )} {page === "change-issue-assignee" && issueDetails && ( )} {/* theme actions */} {page === "change-interface-theme" && ( { closePalette(); setPages((pages) => pages.slice(0, -1)); }} /> )}
); });