Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 6489x 6489x 6489x 6489x 6489x 15x 15x 6489x 51x 233x 6x 6x 6x 6x 51x 51x 4x 6489x 45x 41x 4x 4x 8x 4x 45x 6489x 6489x 56x 44x 244x 113x 1119x 29x | import {
CommandBinding,
GroupedResult,
useCommandSearch,
} from "just-search-it";
import { Search } from "lucide-react";
import React, { useState } from "react";
import {
SearchDialog,
SearchDialogContent,
SearchDialogTitle,
SearchDialogTrigger,
} from "@/ahuora-design-system/ui/search-dialog";
import { Shortcut } from "@/ahuora-design-system/ui/shortcut";
import { Button } from "../ahuora-design-system/ui/button";
import { DialogDescription } from "../ahuora-design-system/ui/dialog";
import { Input } from "../ahuora-design-system/ui/input";
import { ScrollArea, ScrollBar } from "../ahuora-design-system/ui/scroll-area";
import { CommandBrowser } from "./CommandBrowser";
import { CommandFeature } from "./CommandFeature";
export function CommandPalette() {
const [searchTerm, setSearchTerm] = useState("");
const [isSearching, setIsSearching] = useState(false);
const [recentCommands, setRecentCommands] = useState<CommandBinding<any>[]>(
[],
);
const numColumns = 1;
const [inputRef, featured, commands, selected] = useCommandSearch(
numColumns,
searchTerm,
isSearching,
(command) => {
setIsSearching(false);
setSearchTerm("");
},
);
// TODO; useKeypress hook
React.useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "/" || (event.key === "k" && event.ctrlKey)) {
event.preventDefault();
setIsSearching(true);
setSearchTerm("");
return;
}
};
window.addEventListener("keydown", handleKeyDown);
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, []);
const onCommandClick = (recentCommand) => {
// Adds a command that doesn't already exist to the recents list
if (!recentCommands.includes(recentCommand)) {
setRecentCommands((prevCommands) => [recentCommand, ...prevCommands]);
// Adds a command that already exists to the top of the recents list
} else if (recentCommands.includes(recentCommand)) {
setRecentCommands(
recentCommands.filter(
(command) => command.metadata.name !== recentCommand.metadata.name,
),
);
setRecentCommands((prevCommands) => [recentCommand, ...prevCommands]);
}
setSearchTerm("");
};
// Removes a command from the recents list
const onCommandDelete = (selectedCommand) => {
setRecentCommands(
recentCommands?.filter(
(command) => command.metadata.name !== selectedCommand.metadata.name,
),
);
};
return (
<SearchDialog open={isSearching} onOpenChange={setIsSearching}>
<SearchDialogTrigger asChild>
<Button
className="w-full justify-between align-middle p-2 m-2 rounded-xl border-2 border-primary"
aria-label="Open Command Palette"
variant="outline"
onClick={() => setIsSearching(true)}
>
<div className="flex gap-3 align-middle">
{" "}
<Search size={14} />
<span className="text-xs">Search Commands...</span>
</div>
<div className="ml-auto flex gap-1">
<Shortcut>Ctrl</Shortcut>
<Shortcut>K</Shortcut>
</div>
</Button>
</SearchDialogTrigger>
<div>
<SearchDialogContent className="flex flex-col pt-3">
<div className="ml-3">
<Input
ref={inputRef}
value={searchTerm}
startIcon={Search}
aria-label="Command Palette Input"
placeholder="Type a command or search..."
onChange={(e) => {
setSearchTerm(e.target.value);
}}
divClassName="search-input"
></Input>
</div>
<hr className="w-full border-secondary border-t-1" />
<ScrollArea className="w-full">
<SearchDialogTitle className="hidden">
Search Dialog
</SearchDialogTitle>
<DialogDescription className="hidden">
Search for commands or features
</DialogDescription>
{searchTerm.length == 0 && (
<CommandBrowser
onClickHandler={onCommandClick}
onClickDelete={onCommandDelete}
setIsSearching={setIsSearching}
recentCommandsArray={recentCommands}
/>
)}
<div className="w-[calc(100%-1.25rem)]">
<SearchItem
items={featured}
selected={selected}
onClickHandler={onCommandClick}
/>
</div>
<ScrollBar orientation="vertical"></ScrollBar>
</ScrollArea>
</SearchDialogContent>
</div>
</SearchDialog>
);
}
function SearchItem({
items,
selected,
onClickHandler,
}: {
items: GroupedResult;
selected?: CommandBinding<any>;
onClickHandler?: (command: CommandBinding<any>) => void;
}) {
return (
<>
{items.map(([group, items]) => (
<div key={group} className="pl-5 ">
{items.map((item) => (
<CommandFeature
key={item.item.key}
selected={item.item.command === selected}
commandBinding={item.item.command}
onClick={() => onClickHandler?.(item.item.command)}
/>
))}
</div>
))}
</>
);
}
|