|
4 | 4 | import { PUBLIC_APP_ASSETS, PUBLIC_ORIGIN } from "$env/static/public"; |
5 | 5 | import { isHuggingChat } from "$lib/utils/isHuggingChat"; |
6 | 6 |
|
7 | | -import { tick } from "svelte"; |
8 | 7 | import { goto } from "$app/navigation"; |
9 | 8 | import { base } from "$app/paths"; |
10 | 9 | import { page } from "$app/stores"; |
|
29 | 28 |
|
30 | 29 | const SEARCH_DEBOUNCE_DELAY = 400; |
31 | 30 | let filterInputEl: HTMLInputElement; |
32 | | -let searchDisabled = false; |
| 31 | +let filterValue = data.query; |
| 32 | +let isFilterInPorgress = false; |
33 | 33 |
|
34 | 34 | const onModelChange = (e: Event) => { |
35 | 35 | const newUrl = getHref($page.url, { |
|
39 | 39 | goto(newUrl); |
40 | 40 | }; |
41 | 41 |
|
42 | | -const filterOnName = debounce(async (e: Event) => { |
43 | | -searchDisabled = true; |
44 | | -const value = (e.target as HTMLInputElement).value; |
| 42 | +const filterOnName = debounce(async (value: string) => { |
| 43 | +filterValue = value; |
| 44 | +
|
| 45 | +if (isFilterInPorgress) { |
| 46 | +return; |
| 47 | +} |
| 48 | +
|
| 49 | +isFilterInPorgress = true; |
45 | 50 | const newUrl = getHref($page.url, { |
46 | 51 | newKeys: { q: value }, |
47 | 52 | existingKeys: { behaviour: "delete", keys: ["p"] }, |
48 | 53 | }); |
49 | 54 | await goto(newUrl); |
50 | | -setTimeout(async () => { |
51 | | -searchDisabled = false; |
52 | | -await tick(); |
53 | | -filterInputEl.focus(); |
54 | | -}, 0); |
| 55 | +setTimeout(() => filterInputEl.focus(), 0); |
| 56 | +isFilterInPorgress = false; |
| 57 | +
|
| 58 | +// there was a new filter query before server returned response |
| 59 | +if (filterValue !== value) { |
| 60 | +filterOnName(filterValue); |
| 61 | +} |
55 | 62 | }, SEARCH_DEBOUNCE_DELAY); |
56 | 63 |
|
57 | 64 | const settings = useSettingsStore(); |
|
171 | 178 | <input |
172 | 179 | class="h-[30px] w-full bg-transparent pl-5 focus:outline-none" |
173 | 180 | placeholder="Filter by name" |
174 | | -value={data.query} |
175 | | -on:input={filterOnName} |
| 181 | +value={filterValue} |
| 182 | +on:input={(e) => filterOnName(e.currentTarget.value)} |
176 | 183 | bind:this={filterInputEl} |
177 | 184 | maxlength="150" |
178 | 185 | type="search" |
179 | | -disabled={searchDisabled} |
180 | 186 | /> |
181 | 187 | </div> |
182 | 188 | </div> |
|
0 commit comments