* Get filtered recipes
()
| 200 | * Get filtered recipes |
| 201 | */ |
| 202 | function getFilteredRecipes(): CookbookRecipeMatch[] { |
| 203 | if (!samplesData || !search) return []; |
| 204 | |
| 205 | const searchInput = document.getElementById( |
| 206 | "search-input" |
| 207 | ) as HTMLInputElement; |
| 208 | const query = searchInput?.value.trim() || ""; |
| 209 | |
| 210 | let results: CookbookRecipeMatch[] = []; |
| 211 | |
| 212 | if (query) { |
| 213 | // Use fuzzy search - returns SearchableItem[] directly |
| 214 | const searchResults = search.search(query); |
| 215 | results = searchResults.map((item) => { |
| 216 | const recipe = item as SearchableItem & { cookbookId: string }; |
| 217 | const cookbook = samplesData!.cookbooks.find( |
| 218 | (c) => c.id === recipe.cookbookId |
| 219 | )!; |
| 220 | return { |
| 221 | cookbook, |
| 222 | recipe: recipe as unknown as CookbookRecipeMatch["recipe"], |
| 223 | highlightedName: search!.highlight(recipe.title, query), |
| 224 | }; |
| 225 | }); |
| 226 | } else { |
| 227 | // No search query - return all recipes |
| 228 | results = samplesData.cookbooks.flatMap((cookbook) => |
| 229 | cookbook.recipes.map((recipe) => ({ cookbook, recipe })) |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | // Apply language filter using per-recipe languages array |
| 234 | if (selectedLanguage) { |
| 235 | results = results.filter(({ recipe }) => |
| 236 | recipe.languages.includes(selectedLanguage!) |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | // Apply tag filter |
| 241 | if (selectedTags.length > 0) { |
| 242 | results = results.filter(({ recipe }) => |
| 243 | selectedTags.some((tag) => recipe.tags.includes(tag)) |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | return results; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Render cookbooks and recipes |
no test coverage detected