|
1 | 1 | <script> |
2 | | - import { createEventDispatcher } from "svelte"; |
3 | | -
|
4 | | - export let entries = []; |
5 | | - export let searchText = ""; |
| 2 | + import { createEventDispatcher, onMount, onDestroy } from "svelte"; |
| 3 | + import { getEntriesfromText } from "./geodata"; |
| 4 | + export let searchQuery = ""; |
6 | 5 |
|
7 | 6 | const dispatch = createEventDispatcher(); |
8 | 7 |
|
9 | | - let filteredEntries = []; |
| 8 | + let searchResults = []; |
10 | 9 | let isActive = false; |
11 | | -
|
| 10 | + let isLoading = false; |
| 11 | + let debounceTimer = null; |
| 12 | +
|
| 13 | + // Debounced search function |
| 14 | + function debouncedSearch() { |
| 15 | + isLoading = true; |
| 16 | +
|
| 17 | + // Clear any existing timer |
| 18 | + if (debounceTimer) clearTimeout(debounceTimer); |
| 19 | +
|
| 20 | + // Set a new timer |
| 21 | + debounceTimer = setTimeout(async () => { |
| 22 | + if (searchQuery && searchQuery.length > 1) { |
| 23 | + searchResults = await getEntriesfromText(searchQuery); |
| 24 | + isActive = true; |
| 25 | + } else { |
| 26 | + searchResults = []; |
| 27 | + isActive = false; |
| 28 | + } |
| 29 | + isLoading = false; |
| 30 | + }, 300); // 0.3 seconds debounce |
| 31 | + } |
| 32 | +
|
| 33 | + // Search text reactive statement |
12 | 34 | $: { |
13 | | - if (searchText && searchText.length > 1) { |
14 | | - const searchLower = searchText.toLowerCase(); |
15 | | - filteredEntries = entries |
16 | | - .filter((entry) => entry.page_title.toLowerCase().includes(searchLower)) |
17 | | - .sort((a, b) => { |
18 | | - // Sort by whether the title starts with the search text, then by length |
19 | | - const aStarts = a.page_title.toLowerCase().startsWith(searchLower); |
20 | | - const bStarts = b.page_title.toLowerCase().startsWith(searchLower); |
21 | | -
|
22 | | - if (aStarts && !bStarts) return -1; |
23 | | - if (!aStarts && bStarts) return 1; |
24 | | -
|
25 | | - return a.page_title.length - b.page_title.length; |
26 | | - }) |
27 | | - .slice(0, 10); // Limit to 10 results |
28 | | - isActive = true; |
| 35 | + if (searchQuery && searchQuery.length > 1) { |
| 36 | + isLoading = true; |
| 37 | + searchResults = []; |
| 38 | + debouncedSearch(); |
29 | 39 | } else { |
30 | | - filteredEntries = []; |
| 40 | + searchResults = []; |
31 | 41 | isActive = false; |
| 42 | + isLoading = false; |
| 43 | + // Clear any pending search |
| 44 | + if (debounceTimer) clearTimeout(debounceTimer); |
32 | 45 | } |
33 | 46 | } |
34 | 47 |
|
| 48 | + onDestroy(() => { |
| 49 | + // Clean up any pending timers when component is destroyed |
| 50 | + if (debounceTimer) clearTimeout(debounceTimer); |
| 51 | + }); |
| 52 | +
|
35 | 53 | function handleSelect(entry) { |
36 | 54 | dispatch("select", entry); |
37 | | - searchText = ""; |
| 55 | + searchQuery = ""; |
38 | 56 | isActive = false; |
39 | 57 | } |
40 | 58 |
|
|
46 | 64 | } |
47 | 65 |
|
48 | 66 | function handleFocus() { |
49 | | - if (searchText && searchText.length > 1) { |
| 67 | + if (searchQuery && searchQuery.length > 1) { |
50 | 68 | isActive = true; |
51 | 69 | } |
52 | 70 | } |
|
56 | 74 | <div class="search-input-wrapper"> |
57 | 75 | <input |
58 | 76 | type="text" |
59 | | - placeholder="Search locations..." |
60 | | - bind:value={searchText} |
| 77 | + placeholder="Search locations (regions already visited only)" |
| 78 | + bind:value={searchQuery} |
61 | 79 | on:focus={handleFocus} |
62 | 80 | on:blur={handleBlur} |
63 | 81 | class="search-input" |
64 | 82 | /> |
65 | | - {#if searchText} |
| 83 | + {#if searchQuery} |
66 | 84 | <button |
67 | 85 | class="clear-button" |
68 | 86 | on:click={() => { |
69 | | - searchText = ""; |
| 87 | + searchQuery = ""; |
70 | 88 | isActive = false; |
71 | 89 | }} |
72 | 90 | > |
|
78 | 96 | </div> |
79 | 97 | </div> |
80 | 98 |
|
81 | | - {#if isActive && filteredEntries.length > 0} |
| 99 | + {#if isActive && searchResults.length > 0} |
82 | 100 | <div class="suggestions" role="listbox"> |
83 | | - {#each filteredEntries as entry} |
| 101 | + {#each searchResults as entry} |
84 | 102 | <div |
85 | 103 | class="suggestion-item" |
86 | 104 | on:mousedown={() => handleSelect(entry)} |
|
96 | 114 | </div> |
97 | 115 | {/each} |
98 | 116 | </div> |
99 | | - {:else if isActive && searchText.length > 1} |
| 117 | + {:else if isActive && searchQuery.length > 1} |
100 | 118 | <div class="suggestions" role="listbox"> |
101 | 119 | <div class="no-results" role="presentation"> |
102 | | - No matching locations found |
| 120 | + {#if isLoading} |
| 121 | + Searching... |
| 122 | + {:else} |
| 123 | + No matching locations found |
| 124 | + {/if} |
103 | 125 | </div> |
104 | 126 | </div> |
105 | 127 | {/if} |
|
0 commit comments