-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandMenu.tsx
More file actions
39 lines (32 loc) · 880 Bytes
/
CommandMenu.tsx
File metadata and controls
39 lines (32 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { useMemo } from 'react';
import { COMMAND } from '../../constants';
import { SelectPrompt } from '../SelectPrompt';
interface Props {
input: string;
onSubmit: (value: string) => void;
}
function getMatchingCommands(input: string) {
const normalizedInput = input.trim().toLowerCase();
if (!normalizedInput.startsWith('/')) {
return [];
}
return COMMAND.LIST.filter(({ name }) =>
name.toLowerCase().startsWith(normalizedInput),
).map(({ name, description }) => ({
label: `${name} - ${description}`,
value: name,
}));
}
export function CommandMenu({ input, onSubmit }: Props) {
const commandOptions = useMemo(() => getMatchingCommands(input), [input]);
if (!commandOptions.length) {
return null;
}
return (
<SelectPrompt
highlightText={input}
onChange={onSubmit}
options={commandOptions}
/>
);
}