From 7005eec4c14f9208e4cad98fc3b7a8c5cf952fbd Mon Sep 17 00:00:00 2001 From: felipe Date: Thu, 1 May 2025 22:29:37 -0300 Subject: [PATCH 1/4] search update --- src/components/search/AlbumSearch.astro | 38 ++++++++++++------------- src/components/search/SearchNav.astro | 25 ++++++++++++++++ src/pages/search.astro | 4 +-- 3 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 src/components/search/SearchNav.astro diff --git a/src/components/search/AlbumSearch.astro b/src/components/search/AlbumSearch.astro index c2dc899..1fc3f11 100644 --- a/src/components/search/AlbumSearch.astro +++ b/src/components/search/AlbumSearch.astro @@ -4,6 +4,7 @@ import type { DefaultArgs } from '@prisma/client/runtime/library' import { Image } from 'astro:assets' import prismaClient from 'utils/prisma-client' +import SearchNav from './SearchNav.astro' interface Props { query: string @@ -40,24 +41,18 @@ const [count, search] = await Promise.all([ ]) --- -
-
- Albums ({count}) { - count > take ? ( - - {' '} - / Showing {page === 1 ? `first ${take}` : `${(page - 1) * take}-${Math.min(page * take, count)}`} results - - ) : null - } -
-
- { - Array.from({ length: Math.ceil(count / take) }, (_, i) => - page === i + 1 ? {i + 1} : {i + 1} - ) - } -
+
+ Albums ({count}) { + count > take ? ( + + {' '} + / Showing {page === 1 ? `first ${take}` : `${(page - 1) * take}-${Math.min(page * take, count)}`} results + + ) : null + } +
+
+
{ @@ -75,9 +70,14 @@ const [count, search] = await Promise.all([
-
{album.releaseDate?.toISOString().split('T')[0]}
+
{album.releaseDate?.toISOString().split('T')[0]}
)) }
+
+
+ +
+
diff --git a/src/components/search/SearchNav.astro b/src/components/search/SearchNav.astro new file mode 100644 index 0000000..439e5c3 --- /dev/null +++ b/src/components/search/SearchNav.astro @@ -0,0 +1,25 @@ +--- +interface Props { + count: number + take: number + page: number + query: string +} + +const { count, take, page, query } = Astro.props +const pageCount = Math.ceil(count / take) +--- + +{ + pageCount > 1 + ? Array.from({ length: pageCount }, (_, i) => + page === i + 1 ? ( + {i + 1} + ) : ( + + {i + 1} + + ) + ) + : null +} diff --git a/src/pages/search.astro b/src/pages/search.astro index 42c9de2..0e81959 100644 --- a/src/pages/search.astro +++ b/src/pages/search.astro @@ -3,7 +3,7 @@ import AlbumSearch from 'components/search/AlbumSearch.astro' import BaseLayout from 'layouts/base.astro' const query = Astro.url.searchParams.get('q') -const page = Astro.url.searchParams.get('page') || "1" +const page = Astro.url.searchParams.get('page') || '1' if (!query) return Astro.redirect(404) --- @@ -12,7 +12,7 @@ if (!query) return Astro.redirect(404)
Search results for: {query}
-
+
From 4485529545e5ace53367856d68ae95d5eb88c643 Mon Sep 17 00:00:00 2001 From: Jorge Vargas Date: Thu, 1 May 2025 20:03:25 -0600 Subject: [PATCH 2/4] Add localization for search page --- messages/en.json | 5 ++++- src/components/search/AlbumSearch.astro | 20 +++++++++----------- src/pages/search.astro | 4 +++- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/messages/en.json b/messages/en.json index 38d19b1..834e20b 100644 --- a/messages/en.json +++ b/messages/en.json @@ -87,5 +87,8 @@ "latestReleases": "Latest releases", "firstRelease": "First Release", "newestRelease": "Newest Release", - "variousGames": "Various Games" + "variousGames": "Various Games", + "searchResultsFor": "Search results for", + "firstResults": "Showing first {take} results", + "moreResults": "Showing {start}-{end} results" } diff --git a/src/components/search/AlbumSearch.astro b/src/components/search/AlbumSearch.astro index 1fc3f11..56f96f8 100644 --- a/src/components/search/AlbumSearch.astro +++ b/src/components/search/AlbumSearch.astro @@ -1,4 +1,5 @@ --- +import * as m from 'paraglide/messages' import type { Prisma } from '@prisma/client' import type { DefaultArgs } from '@prisma/client/runtime/library' import { Image } from 'astro:assets' @@ -39,16 +40,14 @@ const [count, search] = await Promise.all([ prismaClient.albums.count(countQuery), prismaClient.albums.findMany({ ...findQuery, take, skip: (page - 1) * take }) ]) + +const start = (page - 1) * take +const end = Math.min(page * take, count) ---
- Albums ({count}) { - count > take ? ( - - {' '} - / Showing {page === 1 ? `first ${take}` : `${(page - 1) * take}-${Math.min(page * take, count)}`} results - - ) : null + {m.albums()} ({count}) { + count > take ? / {page === 1 ? m.firstResults({ take }) : m.moreResults({ start, end })} : null }
@@ -76,8 +75,7 @@ const [count, search] = await Promise.all([ )) }
-
-
- -
+ +
+
diff --git a/src/pages/search.astro b/src/pages/search.astro index 0e81959..7e76522 100644 --- a/src/pages/search.astro +++ b/src/pages/search.astro @@ -1,4 +1,6 @@ --- +import * as m from 'paraglide/messages' + import AlbumSearch from 'components/search/AlbumSearch.astro' import BaseLayout from 'layouts/base.astro' @@ -11,7 +13,7 @@ if (!query) return Astro.redirect(404)
-
Search results for: {query}
+
{m.searchResultsFor()}: {query}
From 87b2fc0796b79a8ce13bcb066553a648dac3cf40 Mon Sep 17 00:00:00 2001 From: Jorge Vargas Date: Thu, 1 May 2025 20:21:57 -0600 Subject: [PATCH 3/4] More strict search query parsing --- src/components/search/SearchBar.astro | 6 +++++- src/pages/search.astro | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/components/search/SearchBar.astro b/src/components/search/SearchBar.astro index 19cf271..b5e48bd 100644 --- a/src/components/search/SearchBar.astro +++ b/src/components/search/SearchBar.astro @@ -44,8 +44,12 @@ const placeholder = getRandom(placeholders) searchForm?.addEventListener('submit', (ev: Event) => { ev.preventDefault() const formData = new FormData(searchForm) + const query = (formData.get('query') as string).trim() + + if (!query || query.length < 1) return + const url = new URL('/search', window.location.origin) - url.searchParams.append('q', formData.get('query') as string) + url.searchParams.append('q', query) window.location.href = url.toString() }) diff --git a/src/pages/search.astro b/src/pages/search.astro index 7e76522..841124c 100644 --- a/src/pages/search.astro +++ b/src/pages/search.astro @@ -4,10 +4,10 @@ import * as m from 'paraglide/messages' import AlbumSearch from 'components/search/AlbumSearch.astro' import BaseLayout from 'layouts/base.astro' -const query = Astro.url.searchParams.get('q') +const query = (Astro.url.searchParams.get('q') ?? '').trim() const page = Astro.url.searchParams.get('page') || '1' -if (!query) return Astro.redirect(404) +if (!query || query.length < 1) return Astro.redirect(404) --- From 1cd11fea2b549e9987b20530ceaf96ae756bcb3f Mon Sep 17 00:00:00 2001 From: Jorge Vargas Date: Thu, 1 May 2025 20:44:20 -0600 Subject: [PATCH 4/4] Improve multiple word search --- src/components/search/AlbumSearch.astro | 10 +++++----- src/components/search/SearchNav.astro | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/components/search/AlbumSearch.astro b/src/components/search/AlbumSearch.astro index 56f96f8..c01cc1c 100644 --- a/src/components/search/AlbumSearch.astro +++ b/src/components/search/AlbumSearch.astro @@ -19,17 +19,17 @@ let { page } = Astro.props if (page < 1) page = 1 const queryString = query - .toLowerCase() - .split('_') - .map((w) => `+${w}`) + .trim() + .split(/[\s_\-:]+/) .join(' ') + const findQuery: Prisma.albumsFindManyArgs = { select: { title: true, releaseDate: true, id: true }, where: { OR: [{ title: { search: queryString } }, { subTitle: { search: queryString } }] }, orderBy: { - _relevance: { fields: ['title', 'subTitle'], sort: 'desc', search: query.toLowerCase() } + _relevance: { fields: ['title', 'subTitle'], sort: 'desc', search: queryString } } } const countQuery: Prisma.albumsCountArgs = { @@ -76,6 +76,6 @@ const end = Math.min(page * take, count) }
-
+
diff --git a/src/components/search/SearchNav.astro b/src/components/search/SearchNav.astro index 439e5c3..7b9fa2a 100644 --- a/src/components/search/SearchNav.astro +++ b/src/components/search/SearchNav.astro @@ -14,9 +14,9 @@ const pageCount = Math.ceil(count / take) pageCount > 1 ? Array.from({ length: pageCount }, (_, i) => page === i + 1 ? ( - {i + 1} + {i + 1} ) : ( - + {i + 1} )