From 020bbeb078704f9bda4992c3f7423d7b04697221 Mon Sep 17 00:00:00 2001 From: June Date: Wed, 19 Nov 2025 21:40:34 +1300 Subject: [PATCH] added a decent enough search bar will refactor --- web/src/components/Header.astro | 8 +-- web/src/pages/search.astro | 88 +++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 web/src/pages/search.astro diff --git a/web/src/components/Header.astro b/web/src/components/Header.astro index a7cb544..8102063 100644 --- a/web/src/components/Header.astro +++ b/web/src/components/Header.astro @@ -4,10 +4,10 @@ const links = [ txt: "new", lnk: "/recipe/new" }, - { - txt: "add", - lnk: "/recipe/import" - }, + // { + // txt: "add", + // lnk: "/recipe/import" + // }, { txt: "tags", lnk: "/tags" diff --git a/web/src/pages/search.astro b/web/src/pages/search.astro new file mode 100644 index 0000000..d850a8e --- /dev/null +++ b/web/src/pages/search.astro @@ -0,0 +1,88 @@ +--- +import { Recipe } from "@tmlmt/cooklang-parser" +import { authPB } from "@data/pb"; +import RecipeCard from "@component/index/card" +import Base from "@layout/Base"; + +const pb = await authPB() + +// Get search query from URL +const url = new URL(Astro.request.url); +const query = url.searchParams.get('q') || ''; + +let recipes: Recipe[] = []; +let ids: string[] = []; +let images: string[] = []; +let totalResults = 0; + +if (query) { + // Search in title, description, and cooklang content + const result = await pb.collection('recipes').getList(1, 50, { + filter: `cooklang ~ "${query}"` + }); + + totalResults = result.totalItems; + recipes = result.items.map((r: any) => new Recipe(r.cooklang)); + ids = result.items.map((r: any) => r.id); + images = await Promise.all( + result.items.map((r: any) => '/api' + pb.files.getURL(r, r.images[0]).split('api')[1]) + ); +} +--- + + +
+ + +
+
+ + +
+
+ + {query && ( +

+ {totalResults} result{totalResults !== 1 ? 's' : ''} for "{query}" +

+ )} +
+ + {query && recipes.length > 0 ? ( +
+ { + recipes.map((r, i) => ( + + )) + } +
+ ) : query ? ( +
+

No recipes found

+

Try a different search term

+
+ ) : ( +
+

Start searching for recipes

+

Enter a search term above to find your favorite recipes

+
+ )} + \ No newline at end of file