-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: generalize command palette entity shortcuts #7792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: preview
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||||||
"use client"; | ||||||||||
|
||||||||||
import React, { useCallback, useEffect, FC, useMemo } from "react"; | ||||||||||
import React, { useCallback, useEffect, FC, useMemo, useRef } from "react"; | ||||||||||
import { observer } from "mobx-react"; | ||||||||||
import { useParams } from "next/navigation"; | ||||||||||
import useSWR from "swr"; | ||||||||||
|
@@ -10,11 +10,13 @@ | |||||||||
// components | ||||||||||
import { copyTextToClipboard } from "@plane/utils"; | ||||||||||
import { CommandModal, ShortcutsModal } from "@/components/command-palette"; | ||||||||||
import { COMMAND_CONFIG, CommandConfig } from "@/components/command-palette"; | ||||||||||
// helpers | ||||||||||
// hooks | ||||||||||
import { captureClick } from "@/helpers/event-tracker.helper"; | ||||||||||
import { useAppTheme } from "@/hooks/store/use-app-theme"; | ||||||||||
import { useCommandPalette } from "@/hooks/store/use-command-palette"; | ||||||||||
import type { CommandPaletteEntity } from "@/store/base-command-palette.store"; | ||||||||||
import { useIssueDetail } from "@/hooks/store/use-issue-detail"; | ||||||||||
import { useUser, useUserPermissions } from "@/hooks/store/user"; | ||||||||||
import { usePlatformOS } from "@/hooks/use-platform-os"; | ||||||||||
|
@@ -41,7 +43,8 @@ | |||||||||
const { toggleSidebar } = useAppTheme(); | ||||||||||
const { platform } = usePlatformOS(); | ||||||||||
const { data: currentUser, canPerformAnyCreateAction } = useUser(); | ||||||||||
const { toggleCommandPaletteModal, isShortcutModalOpen, toggleShortcutModal, isAnyModalOpen } = useCommandPalette(); | ||||||||||
const { toggleCommandPaletteModal, isShortcutModalOpen, toggleShortcutModal, isAnyModalOpen, activateEntity } = | ||||||||||
useCommandPalette(); | ||||||||||
const { allowPermissions } = useUserPermissions(); | ||||||||||
|
||||||||||
// derived values | ||||||||||
|
@@ -158,6 +161,17 @@ | |||||||||
[] | ||||||||||
); | ||||||||||
|
||||||||||
const keySequence = useRef(""); | ||||||||||
const sequenceTimeout = useRef<NodeJS.Timeout | null>(null); | ||||||||||
|
||||||||||
Comment on lines
+164
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use browser-safe timer type. Aligns with DOM setTimeout return type. - const sequenceTimeout = useRef<NodeJS.Timeout | null>(null);
+ const sequenceTimeout = useRef<ReturnType<typeof setTimeout> | null>(null); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||
const commandSequenceMap = useMemo(() => { | ||||||||||
const map: Record<string, CommandConfig> = {}; | ||||||||||
COMMAND_CONFIG.forEach((cmd) => { | ||||||||||
map[cmd.sequence] = cmd; | ||||||||||
}); | ||||||||||
return map; | ||||||||||
}, []); | ||||||||||
|
||||||||||
const handleKeyDown = useCallback( | ||||||||||
(e: KeyboardEvent) => { | ||||||||||
const { key, ctrlKey, metaKey, altKey, shiftKey } = e; | ||||||||||
|
@@ -186,6 +200,21 @@ | |||||||||
toggleShortcutModal(true); | ||||||||||
} | ||||||||||
|
||||||||||
if (!cmdClicked && !altKey && !shiftKey && !isAnyModalOpen) { | ||||||||||
keySequence.current = (keySequence.current + keyPressed).slice(-2); | ||||||||||
if (sequenceTimeout.current) clearTimeout(sequenceTimeout.current); | ||||||||||
sequenceTimeout.current = setTimeout(() => { | ||||||||||
keySequence.current = ""; | ||||||||||
}, 500); | ||||||||||
const cmd = commandSequenceMap[keySequence.current]; | ||||||||||
if (cmd && (!cmd.enabled || cmd.enabled())) { | ||||||||||
e.preventDefault(); | ||||||||||
activateEntity(cmd.entity); | ||||||||||
keySequence.current = ""; | ||||||||||
return; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
if (deleteKey) { | ||||||||||
if (performProjectBulkDeleteActions()) { | ||||||||||
shortcutsList.project.delete.action(); | ||||||||||
|
@@ -240,6 +269,7 @@ | |||||||||
projectId, | ||||||||||
shortcutsList, | ||||||||||
toggleCommandPaletteModal, | ||||||||||
activateEntity, | ||||||||||
toggleShortcutModal, | ||||||||||
toggleSidebar, | ||||||||||
workspaceSlug, | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { CommandPaletteEntity } from "@/store/base-command-palette.store"; | ||
|
||
export interface CommandConfig { | ||
/** | ||
* Unique identifier for the command | ||
*/ | ||
id: string; | ||
/** | ||
* Key sequence that triggers the command. Should be lowercase. | ||
*/ | ||
sequence: string; | ||
/** | ||
* Display label shown in the command palette. | ||
*/ | ||
title: string; | ||
/** | ||
* Keys displayed as shortcut hint. | ||
*/ | ||
keys: string[]; | ||
/** | ||
* Entity that the command opens. | ||
*/ | ||
entity: CommandPaletteEntity; | ||
/** | ||
* Optional predicate controlling command availability | ||
*/ | ||
enabled?: () => boolean; | ||
} | ||
|
||
export const COMMAND_CONFIG: CommandConfig[] = [ | ||
{ | ||
id: "open-project", | ||
sequence: "op", | ||
title: "Open project...", | ||
keys: ["O", "P"], | ||
entity: "project", | ||
}, | ||
{ | ||
id: "open-cycle", | ||
sequence: "oc", | ||
title: "Open cycle...", | ||
keys: ["O", "C"], | ||
entity: "cycle", | ||
}, | ||
{ | ||
id: "open-issue", | ||
sequence: "oi", | ||
title: "Open issue...", | ||
keys: ["O", "I"], | ||
entity: "issue", | ||
}, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import type { ICycle } from "@plane/types"; | ||
import { CommandPaletteEntityList } from "./entity-list"; | ||
|
||
interface Props { | ||
cycles: ICycle[]; | ||
onSelect: (cycle: ICycle) => void; | ||
} | ||
|
||
export const CommandPaletteCycleSelector: React.FC<Props> = ({ cycles, onSelect }) => ( | ||
<CommandPaletteEntityList | ||
heading="Cycles" | ||
items={cycles} | ||
getKey={(cycle) => cycle.id} | ||
getLabel={(cycle) => cycle.name} | ||
onSelect={onSelect} | ||
emptyText="No cycles found" | ||
/> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { Command } from "cmdk"; | ||
import { cn } from "@plane/utils"; | ||
|
||
interface CommandPaletteEntityListProps<T> { | ||
heading: string; | ||
items: T[]; | ||
onSelect: (item: T) => void; | ||
getKey?: (item: T) => string; | ||
getLabel: (item: T) => string; | ||
renderItem?: (item: T) => React.ReactNode; | ||
emptyText?: string; | ||
} | ||
|
||
export const CommandPaletteEntityList = <T,>({ | ||
heading, | ||
items, | ||
onSelect, | ||
getKey, | ||
getLabel, | ||
renderItem, | ||
emptyText = "No results found", | ||
}: CommandPaletteEntityListProps<T>) => { | ||
if (items.length === 0) return <div className="px-3 py-8 text-center text-sm text-custom-text-300">{emptyText}</div>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainLocate the lint “Unexpected any” offender. If ESLint is flagging this area, it’s likely from generic inference at JSX call sites. Let’s confirm the exact file/line. 🏁 Script executed: #!/bin/bash
# Find explicit 'any' in the affected surfaces
rg -nP '\b(any|as any|<any>)\b' apps/web/core/components/command-palette apps/web/core/store | sed 's/^/ANY: /'
# Also flag NodeJS.Timeout usages in web code
rg -nP 'NodeJS\.Timeout' apps/web/core/components/command-palette apps/web/core/store | sed 's/^/TIMEOUT: /' Length of output: 5539 'Unexpected any' isn't coming from entity-list.tsx — explicit No
Fixes: define proper types for search results/items (avoid |
||
|
||
return ( | ||
<Command.Group heading={heading}> | ||
{items.map((item) => ( | ||
<Command.Item | ||
key={getKey ? getKey(item) : getLabel(item)} | ||
value={getLabel(item)} | ||
onSelect={() => onSelect(item)} | ||
className={cn("focus:outline-none")} | ||
> | ||
{renderItem ? renderItem(item) : getLabel(item)} | ||
</Command.Item> | ||
))} | ||
</Command.Group> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import type { TPartialProject } from "@/plane-web/types"; | ||
import { CommandPaletteEntityList } from "./entity-list"; | ||
|
||
interface Props { | ||
projects: TPartialProject[]; | ||
onSelect: (project: TPartialProject) => void; | ||
} | ||
|
||
export const CommandPaletteProjectSelector: React.FC<Props> = ({ projects, onSelect }) => ( | ||
<CommandPaletteEntityList | ||
heading="Projects" | ||
items={projects} | ||
getKey={(project) => project.id} | ||
getLabel={(project) => project.name} | ||
onSelect={onSelect} | ||
emptyText="No projects found" | ||
/> | ||
); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"use client"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { useRef } from "react"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export const useKeySequence = (handlers: Record<string, () => void>, timeout = 500) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const sequence = useRef(""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const sequenceTimeout = useRef<NodeJS.Timeout | null>(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return (e: React.KeyboardEvent) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const key = e.key.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sequence.current = (sequence.current + key).slice(-2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (sequenceTimeout.current) clearTimeout(sequenceTimeout.current); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sequenceTimeout.current = setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sequence.current = ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, timeout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const action = handlers[sequence.current]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (action) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e.preventDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
action(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sequence.current = ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+5
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix browser timer type, ignore modifier/non-char keys, and clear timer on hit. Prevents type noise in web envs, avoids accidental matches (e.g., Arrow keys), and stops a dangling timeout after a match. -import { useRef } from "react";
+import { useRef } from "react";
+import type React from "react";
export const useKeySequence = (handlers: Record<string, () => void>, timeout = 500) => {
const sequence = useRef("");
- const sequenceTimeout = useRef<NodeJS.Timeout | null>(null);
+ const sequenceTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
- return (e: React.KeyboardEvent) => {
- const key = e.key.toLowerCase();
+ return (e: React.KeyboardEvent) => {
+ if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey || e.repeat) return;
+ const keyRaw = e.key;
+ if (!keyRaw || keyRaw.length !== 1) return; // ignore non-character keys
+ const key = keyRaw.toLowerCase();
sequence.current = (sequence.current + key).slice(-2);
- if (sequenceTimeout.current) clearTimeout(sequenceTimeout.current);
+ if (sequenceTimeout.current) clearTimeout(sequenceTimeout.current);
sequenceTimeout.current = setTimeout(() => {
sequence.current = "";
}, timeout);
const action = handlers[sequence.current];
if (action) {
e.preventDefault();
action();
sequence.current = "";
+ if (sequenceTimeout.current) {
+ clearTimeout(sequenceTimeout.current);
+ sequenceTimeout.current = null;
+ }
}
};
}; 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused type import.
Avoids lint noise.
-import type { CommandPaletteEntity } from "@/store/base-command-palette.store";
📝 Committable suggestion
🤖 Prompt for AI Agents