⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
import { Fragment, useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { useTranslations } from 'next-intl';
import { useDebouncedValue } from '@/hooks/useDebouncedValue';
import { useIssueSearchQuery, useOtherProjectIssueSearch } from '@/services/issues.service';
import { issuePath } from '@/utils/paths';
import type { Command, CommandPage, CommandSection } from '@/utils/commands';
import { substringFilter } from '@/utils/commandFilter';
import CommandPaletteDocuments from './CommandPaletteDocuments';
import CommandPaletteIssues from '@/components/layout/CommandPaletteIssues';
import CommandPaletteOtherIssues from './CommandPaletteOtherIssues';
import CommandPaletteScope from './CommandPaletteScope';
import { usePaletteSearchScope } from './hooks/usePaletteSearchScope';
import CommandPaletteRow from '@/components/layout/CommandPaletteRow';
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandList,
CommandSeparator,
} from '@/components/ui/command';
// Command palette (⌘K). Renders the sections it is given, in order: the commands
// for the issue in front of the user, then the board, the general commands, the
// project list and every section they may open. A command with a submenu opens a
// second level (status, priority, assignee, labels); Backspace on an empty input
// goes back. While the user is typing, an "Issues" group lists issues of the current
// project the server matches by identifier, title, description, number or custom
// fields (archived issues included), and the last groups list the matches in every
// other project the user can read, one group per project. While a project is open, a
// switch narrows the search to it.
export default function CommandPalette({
open,
onOpenChange,
sections,
currentProjectKey,
currentProjectId,
onOpenIssue,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
sections: CommandSection[];
currentProjectKey: string | null;
currentProjectId: number | null;
onOpenIssue: (sequenceNumber: number) => void;
}) {
const t = useTranslations('palette');
const router = useRouter();
const [query, setQuery] = useState('');
const [page, setPage] = useState<CommandPage | null>(null);
// Reset the query and the open submenu whenever the palette closes, so it
// reopens at the top level and empty.
useEffect(() => {
if (!open) {
setQuery('');
setPage(null);
}
}, [open]);
// Scroll the results back to the top on every query change; otherwise the list
// keeps its previous scroll offset and a match can land mid-list out of view.
useEffect(() => {
const id = requestAnimationFrame(() => {
const list = document.querySelector('[data-slot="command-list"]');
if (list) list.scrollTop = 0;
});
return () => cancelAnimationFrame(id);
}, [query, page]);
// Debounce the input before it drives the search request, so a burst of
// keystrokes issues one query, not one per character.
const debounced = useDebouncedValue(query, 250);
const search = useIssueSearchQuery(currentProjectKey, debounced, { enabled: open && !page });
const hits = search.data ?? [];
const { scope, setScope } = usePaletteSearchScope();
const allProjects = currentProjectKey === null || scope === 'all';
const others = useOtherProjectIssueSearch(
debounced,
{ key: currentProjectKey, id: currentProjectId },
{ enabled: open && !page && allProjects },
);
const hasProject = currentProjectId !== null;
const searching = query.trim().length > 0;
function run(command: Command) {
if (command.submenu) {
setPage(command.submenu);
setQuery('');
return;
}
if (!command.keepOpen) onOpenChange(false);
command.run?.();
}
return (
<CommandDialog open={open} onOpenChange={onOpenChange} filter={substringFilter}>
<CommandInput
placeholder={page ? page.placeholder : t('placeholder')}
value={query}
onValueChange={setQuery}
// Backspace on an empty input leaves the submenu, the way a nested menu
// closes with the left arrow.
onKeyDown={(e) => {
if (page && e.key === 'Backspace' && query === '') {
e.preventDefault();
setPage(null);
}
}}
/>
{!page && currentProjectKey !== null && (
<CommandPaletteScope scope={scope} onChange={setScope} />
)}
<CommandList>
<CommandEmpty>{t('noResults')}</CommandEmpty>
{page ? (
<CommandGroup heading={page.heading}>
{page.items.map((command) => (
<CommandPaletteRow key={command.id} command={command} onRun={run} />
))}
</CommandGroup>
) : (
sections.map((section, i) => (
<Fragment key={section.id}>
{i > 0 && <CommandSeparator />}
<CommandGroup heading={section.heading}>
{section.items.map((command) => (
<CommandPaletteRow key={command.id} command={command} onRun={run} />
))}
</CommandGroup>
</Fragment>
))
)}
<CommandPaletteDocuments
projectKey={currentProjectKey}
query={debounced}
enabled={open && !page}
onClose={() => onOpenChange(false)}
/>
{/* Issue results only appear while the user is typing, otherwise the
palette would list the entire project on open. */}
{!page && hasProject && searching && (hits.length > 0 || search.isFetching) && (
<CommandPaletteIssues
heading={t('issues')}
hits={hits}
fetching={search.isFetching}
onOpenIssue={(hit) => {
onOpenChange(false);
onOpenIssue(hit.sequenceNumber);
}}
/>
)}
{!page && searching && (others.hits.length > 0 || others.fetching) && (
<CommandPaletteOtherIssues
hits={others.hits}
fetching={others.fetching}
loadingHeading={hasProject ? t('otherProjectIssues') : t('issues')}
onOpenIssue={(hit) => {
onOpenChange(false);
router.push(issuePath(hit.projectRef, hit.sequenceNumber));
}}
/>
)}
</CommandList>
</CommandDialog>
);
}