|
4 | 4 | @inject NavigationManager NavigationManager |
5 | 5 | @inject IJSRuntime JSRuntime |
6 | 6 |
|
7 | | -<div class="relative"> |
| 7 | +<div class="relative" @onclick:stopPropagation="true" data-search-component> |
8 | 8 | <div class="flex items-center bg-white border border-gray-300 rounded-full shadow-sm hover:shadow-md transition-shadow"> |
9 | 9 | <input type="text" |
10 | 10 | @bind="searchTerm" |
|
22 | 22 | </div> |
23 | 23 |
|
24 | 24 | <!-- Search Suggestions Dropdown --> |
25 | | - @if (showSuggestions && searchSuggestions.Any()) |
| 25 | + @if (showSuggestions && searchSuggestions.Any() && !searchResults.Any()) |
26 | 26 | { |
27 | 27 | <div class="absolute top-full left-0 right-0 mt-1 bg-white border border-gray-200 rounded-lg shadow-lg z-50 max-h-60 overflow-y-auto"> |
28 | 28 | @foreach (var suggestion in searchSuggestions) |
|
40 | 40 | </div> |
41 | 41 | } |
42 | 42 |
|
43 | | - <!-- Search Results Count --> |
| 43 | + <!-- Search Results Preview --> |
44 | 44 | @if (!string.IsNullOrEmpty(searchTerm) && searchResults.Any()) |
45 | 45 | { |
46 | | - <div class="absolute top-full left-0 right-0 mt-1 bg-white border border-gray-200 rounded-lg shadow-lg z-40 p-3"> |
47 | | - <div class="flex items-center justify-between text-sm text-gray-600"> |
48 | | - <span>@searchResults.Count() results found</span> |
49 | | - <button class="text-red-600 hover:text-red-800 font-medium" |
50 | | - @onclick="NavigateToSearchResults"> |
51 | | - View all results |
52 | | - </button> |
| 46 | + <div class="absolute top-full left-0 right-0 mt-1 bg-white border border-gray-200 rounded-lg shadow-lg z-50 max-h-80 overflow-y-auto"> |
| 47 | + <div class="p-3 border-b border-gray-200"> |
| 48 | + <div class="flex items-center justify-between text-sm text-gray-600"> |
| 49 | + <span>@searchResults.Count() results found</span> |
| 50 | + <button class="text-red-600 hover:text-red-800 font-medium" |
| 51 | + @onclick="NavigateToSearchResults"> |
| 52 | + View all results |
| 53 | + </button> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | + |
| 57 | + <!-- Quick Results Preview --> |
| 58 | + <div class="p-3 space-y-2"> |
| 59 | + @foreach (var video in searchResults.Take(3)) |
| 60 | + { |
| 61 | + <div class="flex items-center space-x-3 p-2 hover:bg-gray-50 rounded-lg cursor-pointer" |
| 62 | + @onclick="() => NavigateToVideo(video)"> |
| 63 | + <div class="w-12 h-8 bg-gray-200 rounded overflow-hidden flex-shrink-0"> |
| 64 | + <img src="@video.Thumbnail" alt="@video.Description" class="w-full h-full object-cover" /> |
| 65 | + </div> |
| 66 | + <div class="flex-1 min-w-0"> |
| 67 | + <p class="text-sm font-medium text-gray-900 truncate">@GetCleanTitle(video.Description)</p> |
| 68 | + <p class="text-xs text-gray-500">Tom & Jerry</p> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + } |
| 72 | + |
| 73 | + @if (searchResults.Count() > 3) |
| 74 | + { |
| 75 | + <div class="text-center pt-2"> |
| 76 | + <button class="text-xs text-red-600 hover:text-red-800 font-medium" |
| 77 | + @onclick="NavigateToSearchResults"> |
| 78 | + +@(searchResults.Count() - 3) more results |
| 79 | + </button> |
| 80 | + </div> |
| 81 | + } |
53 | 82 | </div> |
54 | 83 | </div> |
55 | 84 | } |
|
62 | 91 | private bool showSuggestions = false; |
63 | 92 | private Timer? searchTimer; |
64 | 93 |
|
| 94 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 95 | + { |
| 96 | + if (firstRender) |
| 97 | + { |
| 98 | + await JSRuntime.InvokeVoidAsync("addClickOutsideHandler", DotNetObjectReference.Create(this)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
65 | 102 | protected override void OnInitialized() |
66 | 103 | { |
67 | 104 | SearchService.OnSearchResultsChanged += OnSearchResultsChanged; |
68 | 105 |
|
69 | | - // Initialize search timer for debouncing |
| 106 | + // Initialize search timer for debouncing (reduced to 150ms for better responsiveness) |
70 | 107 | searchTimer = new Timer(async _ => await DebouncedSearch(), null, Timeout.Infinite, Timeout.Infinite); |
71 | 108 | } |
72 | 109 |
|
73 | 110 | private void OnSearchResultsChanged(IEnumerable<TomAndJerry.Model.Video> results) |
74 | 111 | { |
75 | 112 | searchResults = results; |
| 113 | + |
| 114 | + // Clear suggestions when we have search results |
| 115 | + if (searchResults.Any()) |
| 116 | + { |
| 117 | + searchSuggestions = Enumerable.Empty<string>(); |
| 118 | + showSuggestions = false; |
| 119 | + } |
| 120 | + |
76 | 121 | InvokeAsync(StateHasChanged); |
77 | 122 | } |
78 | 123 |
|
|
87 | 132 | return; |
88 | 133 | } |
89 | 134 |
|
90 | | - // Get search suggestions |
91 | | - searchSuggestions = await SearchService.GetSearchSuggestionsAsync(searchTerm); |
| 135 | + // Perform immediate search for preview |
| 136 | + var immediateResults = await VideoService.SearchVideosAsync(searchTerm); |
| 137 | + searchResults = immediateResults; |
92 | 138 |
|
93 | | - // Perform search |
| 139 | + // Only show suggestions if no search results found and search term is long enough |
| 140 | + if (!searchResults.Any() && searchTerm.Length >= 2) |
| 141 | + { |
| 142 | + searchSuggestions = await SearchService.GetSearchSuggestionsAsync(searchTerm); |
| 143 | + showSuggestions = searchSuggestions.Any(); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + // Clear suggestions when we have results or search term is too short |
| 148 | + searchSuggestions = Enumerable.Empty<string>(); |
| 149 | + showSuggestions = false; |
| 150 | + } |
| 151 | + |
| 152 | + // Also trigger the debounced search for suggestions |
94 | 153 | await SearchService.SearchAsync(searchTerm); |
95 | 154 |
|
96 | | - showSuggestions = searchSuggestions.Any(); |
97 | 155 | await InvokeAsync(StateHasChanged); |
98 | 156 | } |
99 | 157 |
|
100 | | - private async Task HandleKeyDown(KeyboardEventArgs e) |
| 158 | + private void HandleKeyDown(KeyboardEventArgs e) |
101 | 159 | { |
102 | 160 | if (e.Key == "Enter") |
103 | 161 | { |
104 | 162 | PerformSearch(); |
105 | 163 | } |
106 | 164 | else if (e.Key == "Escape") |
107 | 165 | { |
108 | | - showSuggestions = false; |
109 | | - await InvokeAsync(StateHasChanged); |
| 166 | + CloseDropdown(); |
110 | 167 | } |
111 | 168 | else |
112 | 169 | { |
113 | | - // Debounce search input |
114 | | - searchTimer?.Change(300, Timeout.Infinite); |
| 170 | + // Debounce search input (reduced to 150ms for better responsiveness) |
| 171 | + searchTimer?.Change(150, Timeout.Infinite); |
115 | 172 | } |
116 | 173 | } |
117 | 174 |
|
118 | 175 | private void PerformSearch() |
119 | 176 | { |
120 | 177 | if (!string.IsNullOrEmpty(searchTerm)) |
121 | 178 | { |
| 179 | + CloseDropdown(); |
122 | 180 | NavigationManager.NavigateTo($"Search/{Uri.EscapeDataString(searchTerm)}"); |
123 | 181 | } |
124 | | - showSuggestions = false; |
125 | 182 | } |
126 | 183 |
|
127 | 184 | private void SelectSuggestion(string suggestion) |
128 | 185 | { |
129 | 186 | searchTerm = suggestion; |
130 | | - showSuggestions = false; |
| 187 | + CloseDropdown(); |
131 | 188 | PerformSearch(); |
132 | 189 | } |
133 | 190 |
|
134 | 191 | private void NavigateToSearchResults() |
135 | 192 | { |
136 | 193 | if (!string.IsNullOrEmpty(searchTerm)) |
137 | 194 | { |
| 195 | + CloseDropdown(); |
138 | 196 | NavigationManager.NavigateTo($"Search/{Uri.EscapeDataString(searchTerm)}"); |
139 | 197 | } |
140 | 198 | } |
141 | 199 |
|
| 200 | + private void NavigateToVideo(TomAndJerry.Model.Video video) |
| 201 | + { |
| 202 | + CloseDropdown(); |
| 203 | + NavigationManager.NavigateTo($"playmedia/{video.Id}"); |
| 204 | + } |
| 205 | + |
| 206 | + private void CloseDropdown() |
| 207 | + { |
| 208 | + showSuggestions = false; |
| 209 | + searchSuggestions = Enumerable.Empty<string>(); |
| 210 | + searchResults = Enumerable.Empty<TomAndJerry.Model.Video>(); |
| 211 | + InvokeAsync(StateHasChanged); |
| 212 | + } |
| 213 | + |
| 214 | + private string GetCleanTitle(string description) |
| 215 | + { |
| 216 | + return string.Join(" ", description.Split(".").Where(x => x != "mkv").Select(x => x)); |
| 217 | + } |
| 218 | + |
| 219 | + [JSInvokable] |
| 220 | + public void OnClickOutside() |
| 221 | + { |
| 222 | + CloseDropdown(); |
| 223 | + } |
| 224 | + |
142 | 225 | public void Dispose() |
143 | 226 | { |
144 | 227 | SearchService.OnSearchResultsChanged -= OnSearchResultsChanged; |
|
0 commit comments