Filtering for cmd palette

This commit is contained in:
Gregory Schier
2024-03-22 10:42:45 -07:00
parent 5f86802d88
commit e292235792
2 changed files with 18 additions and 8 deletions

View File

@@ -23,7 +23,7 @@ use log::{debug, error, info, warn};
use rand::random; use rand::random;
use serde_json::{json, Value}; use serde_json::{json, Value};
use sqlx::migrate::Migrator; use sqlx::migrate::Migrator;
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode}; use sqlx::sqlite::{SqliteConnectOptions};
use sqlx::types::Json; use sqlx::types::Json;
use sqlx::{Pool, Sqlite, SqlitePool}; use sqlx::{Pool, Sqlite, SqlitePool};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]

View File

@@ -15,6 +15,7 @@ export function CommandPalette({ onClose }: { onClose: () => void }) {
const activeEnvironmentId = useActiveEnvironmentId(); const activeEnvironmentId = useActiveEnvironmentId();
const workspaces = useWorkspaces(); const workspaces = useWorkspaces();
const requests = useRequests(); const requests = useRequests();
const [command, setCommand] = useState<string>('');
const items = useMemo<{ label: string; onSelect: () => void; key: string }[]>(() => { const items = useMemo<{ label: string; onSelect: () => void; key: string }[]>(() => {
const items = []; const items = [];
@@ -47,10 +48,17 @@ export function CommandPalette({ onClose }: { onClose: () => void }) {
return items; return items;
}, [activeEnvironmentId, requests, routes, workspaces]); }, [activeEnvironmentId, requests, routes, workspaces]);
const handleSelectAndClose = (cb: () => void) => { const filteredItems = useMemo(() => {
onClose(); return items.filter((v) => v.label.toLowerCase().includes(command.toLowerCase()));
cb(); }, [command, items]);
};
const handleSelectAndClose = useCallback(
(cb: () => void) => {
onClose();
cb();
},
[onClose],
);
const handleKeyDown = useCallback( const handleKeyDown = useCallback(
(e: KeyboardEvent) => { (e: KeyboardEvent) => {
@@ -59,13 +67,13 @@ export function CommandPalette({ onClose }: { onClose: () => void }) {
} else if (e.key === 'ArrowUp') { } else if (e.key === 'ArrowUp') {
setSelectedIndex((prev) => prev - 1); setSelectedIndex((prev) => prev - 1);
} else if (e.key === 'Enter') { } else if (e.key === 'Enter') {
const item = items[selectedIndex]; const item = filteredItems[selectedIndex];
if (item) { if (item) {
handleSelectAndClose(item.onSelect); handleSelectAndClose(item.onSelect);
} }
} }
}, },
[items, onClose, selectedIndex], [filteredItems, handleSelectAndClose, selectedIndex],
); );
return ( return (
@@ -76,11 +84,13 @@ export function CommandPalette({ onClose }: { onClose: () => void }) {
name="command" name="command"
label="Command" label="Command"
placeholder="Type a command" placeholder="Type a command"
defaultValue=""
onChange={setCommand}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
/> />
</div> </div>
<div className="h-full px-1.5 overflow-y-auto"> <div className="h-full px-1.5 overflow-y-auto">
{items.map((v, i) => ( {filteredItems.map((v, i) => (
<CommandPaletteItem <CommandPaletteItem
active={i === selectedIndex} active={i === selectedIndex}
key={v.key} key={v.key}