From fa72b42899fbfbeccc00712a8477b6c5a4335bc0 Mon Sep 17 00:00:00 2001 From: CristhianZ2022 Date: Wed, 26 Nov 2025 02:39:25 -0500 Subject: [PATCH 1/4] Create the dynamic route for the search --- src/pages/search/[...search].astro | 82 ++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/pages/search/[...search].astro diff --git a/src/pages/search/[...search].astro b/src/pages/search/[...search].astro new file mode 100644 index 0000000..b4d2a90 --- /dev/null +++ b/src/pages/search/[...search].astro @@ -0,0 +1,82 @@ +--- +import Layout from '../../layouts/Layout.astro'; +import SearchButton from '../../components/SearchButton'; +import PlayListItemCard from '../../components/PlayListItemCard.astro'; +import { playlists } from '../../lib/data'; + +const query = Astro.url.searchParams.get('q')?.toLowerCase() || ''; + +const searchPlaylists = query + ? playlists.filter( + (playlist) => + playlist.title.toLowerCase().includes(query) || + playlist.artists.some((artist) => + artist.toLowerCase().includes(query) + ) || + playlist.albumId.toString().includes(query) + ) + : playlists; +--- + + +
+ + +
+
+ { + query ? ( +

+ Resultados para "{query}" +

+ ) : ( +

Explora todo

+ ) + } + +
+ { + searchPlaylists.map((playlist) => ( + + )) + } +
+
+
+
+ +
From 157a7c2254f2e3f8198036643a573aef16a98ecd Mon Sep 17 00:00:00 2001 From: CristhianZ2022 Date: Wed, 26 Nov 2025 02:40:11 -0500 Subject: [PATCH 2/4] Add the Search component, and its functionality. --- src/components/SearchButton.tsx | 90 +++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/components/SearchButton.tsx diff --git a/src/components/SearchButton.tsx b/src/components/SearchButton.tsx new file mode 100644 index 0000000..e7f8b50 --- /dev/null +++ b/src/components/SearchButton.tsx @@ -0,0 +1,90 @@ +import { useEffect, useState } from 'react'; +import { useDebouncedCallback } from 'use-debounce'; + +interface SearchInputProps { + initialQuery?: string; +} + +const BlackSearchIcon = () => ( + + + +); + +const BsX = () => ( + + + + + +); + +export default function SearchButton({ initialQuery = '' }: SearchInputProps) { + const [value, setValue] = useState(initialQuery); + + useEffect(() => { + setValue(initialQuery); + }, [initialQuery]); + + const handleSearch = useDebouncedCallback((term: string) => { + const url = new URL(window.location.href); + if (term.trim()) { + url.searchParams.set('q', term.trim()); + } else { + url.searchParams.delete('q'); + } + window.history.replaceState({}, '', url); + + // Dispara un evento para que Astro reaccione + window.dispatchEvent(new CustomEvent('search-change')); + }, 600); + + const clearSearch = (e: React.MouseEvent) => { + e.preventDefault(); + setValue(''); + handleSearch(''); + }; + + return ( +
+
+ +
+ { + const val = e.target.value; + setValue(val); + handleSearch(val); + }} + placeholder="¿Qué quieres escuchar?" + className="w-full h-12 pl-14 pr-12 bg-[#181818] text-white rounded-full placeholder-gray-400 text-base font-medium focus:outline-none focus:bg-[#282828] focus:ring-2 focus:ring-white/20 hover:bg-[#282828] transition-all duration-200 caret-green-400 + + [&::-webkit-search-cancel-button]:hidden + [&::-webkit-search-results-decoration]:hidden + " + /> + {value && ( + + )} +
+ ); +} From 6e98eaad439a3c35ea292041f095808aa3cc47b4 Mon Sep 17 00:00:00 2001 From: CristhianZ2022 Date: Wed, 26 Nov 2025 02:40:35 -0500 Subject: [PATCH 3/4] Add the path /search --- src/components/AsideMenu.astro | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/AsideMenu.astro b/src/components/AsideMenu.astro index b174410..b086499 100644 --- a/src/components/AsideMenu.astro +++ b/src/components/AsideMenu.astro @@ -13,12 +13,12 @@ import { playlists } from "@/lib/data"
    - Inicio + Inicio - + - Buscar + Buscar
@@ -27,7 +27,7 @@ import { playlists } from "@/lib/data"
    - Tu biblioteca + Tu biblioteca {playlists.map((playlist) => )} From 50a7237fe93b0a5e9a4ddafb703cc22fd970601f Mon Sep 17 00:00:00 2001 From: CristhianZ2022 Date: Wed, 26 Nov 2025 02:41:39 -0500 Subject: [PATCH 4/4] use-debounce dependency for Search --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 5197a59..3410753 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "react-dom": "18.2.0", "svelte": "4.2.8", "tailwindcss": "3.4.0", + "use-debounce": "^10.0.6", "zustand": "4.4.3" }, "name": "spotify-twitch-clone",