From 71a6a26355bec8a3b15d91c187fa674c0915985a Mon Sep 17 00:00:00 2001 From: Rachel Powers <508861+Ryex@users.noreply.github.com> Date: Thu, 11 Apr 2024 15:02:24 -0700 Subject: [PATCH] order fuzy search results correctly Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com> --- www/src/ts/virtual_machine/device.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/www/src/ts/virtual_machine/device.ts b/www/src/ts/virtual_machine/device.ts index 6f7610e..d70349c 100644 --- a/www/src/ts/virtual_machine/device.ts +++ b/www/src/ts/virtual_machine/device.ts @@ -496,6 +496,8 @@ export class VMAddDeviceButton extends BaseElement { private _searchResults: DeviceDBEntry[]; + private filterTimeout: number | undefined; + performSearch() { if (this.filter) { const datapoints: [string, string][] = []; @@ -504,9 +506,9 @@ export class VMAddDeviceButton extends BaseElement { } const haystack: string[] = datapoints.map((data) => data[0]); const uf = new uFuzzy({}); - const [idxs, _info, _order] = uf.search(haystack, this._filter, 0, 1e3); + const [_idxs, info, order] = uf.search(haystack, this._filter, 0, 1e3); - const filtered = idxs?.map((idx) => datapoints[idx]); + const filtered = order?.map((infoIdx) => datapoints[info.idx[infoIdx]]); const names = filtered ?.map((data) => data[1]) @@ -579,7 +581,14 @@ export class VMAddDeviceButton extends BaseElement { _handleSearchInput(e: CustomEvent) { console.log("search-input", e); - this.filter = this.searchInput.value; + if (this.filterTimeout) { + clearTimeout(this.filterTimeout); + } + const that = this; + this.filterTimeout = setTimeout(() => { + that.filter = that.searchInput.value; + that.filterTimeout = undefined; + }, 200); } _handleAddButtonClick() {