2022-10-10 12:50:21 +00:00
|
|
|
import { isMacLike } from "../../common/util.ts";
|
|
|
|
import { FilterList } from "./filter.tsx";
|
|
|
|
import { faPersonRunning } from "../deps.ts";
|
|
|
|
import { AppCommand } from "../hooks/command.ts";
|
|
|
|
import { FilterOption } from "../../common/types.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
export function CommandPalette({
|
|
|
|
commands,
|
2022-05-16 13:09:36 +00:00
|
|
|
recentCommands,
|
2022-03-20 08:56:28 +00:00
|
|
|
onTrigger,
|
|
|
|
}: {
|
|
|
|
commands: Map<string, AppCommand>;
|
2022-05-16 13:09:36 +00:00
|
|
|
recentCommands: Map<string, Date>;
|
2022-03-20 08:56:28 +00:00
|
|
|
onTrigger: (command: AppCommand | undefined) => void;
|
|
|
|
}) {
|
2022-04-13 12:46:52 +00:00
|
|
|
let options: FilterOption[] = [];
|
2022-03-20 08:56:28 +00:00
|
|
|
const isMac = isMacLike();
|
|
|
|
for (let [name, def] of commands.entries()) {
|
|
|
|
options.push({
|
|
|
|
name: name,
|
|
|
|
hint: isMac && def.command.mac ? def.command.mac : def.command.key,
|
2022-05-16 13:09:36 +00:00
|
|
|
orderId: recentCommands.has(name)
|
|
|
|
? -recentCommands.get(name)!.getTime()
|
|
|
|
: 0,
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<FilterList
|
|
|
|
label="Run"
|
|
|
|
placeholder="Command"
|
|
|
|
options={options}
|
|
|
|
allowNew={false}
|
|
|
|
icon={faPersonRunning}
|
|
|
|
helpText="Start typing the command name to filter results, press <code>Return</code> to run."
|
|
|
|
onSelect={(opt) => {
|
|
|
|
if (opt) {
|
|
|
|
onTrigger(commands.get(opt.name));
|
|
|
|
} else {
|
|
|
|
onTrigger(undefined);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|