// app/search/page.tsx 'use client'; // Required for using client-side features like useState import { useState, useEffect } from 'react'; import { useSearchParams } from 'next/navigation'; // To get query from URL // Simulate fetching search results async function getSearchResults(query: string | null): Promise { // Explicit Promise // Simulate network delay await new Promise(resolve => setTimeout(resolve, 50)); if (!query || !query.trim()) { // Also handle empty/whitespace query after trimming return []; // Return empty array for null, empty, or whitespace-only query } const mockResults: { [key: string]: any[] } = { 'shirt': [ { id: 'sr1', name: 'Search Result Shirt 1', price: { amount: '29.99', currencyCode: 'USD' }, image: { src: '/placeholder-shirt1.jpg', alt: 'Shirt 1' } }, { id: 'sr2', name: 'Search Result Shirt 2', price: { amount: '35.50', currencyCode: 'USD' }, image: { src: '/placeholder-shirt2.jpg', alt: 'Shirt 2' } }, ], 'accessory': [ { id: 'sa1', name: 'Search Result Accessory', price: { amount: '12.00', currencyCode: 'USD' }, image: { src: '/placeholder-accessory.jpg', alt: 'Accessory' } }, ], 'generic': [ { id: 'g1', name: 'Generic Product A', price: { amount: '10.00', currencyCode: 'USD' }, image: { src: '/placeholder-generic1.jpg', alt: 'Generic A' } }, { id: 'g2', name: 'Generic Product B', price: { amount: '15.00', currencyCode: 'USD' }, image: { src: '/placeholder-generic2.jpg', alt: 'Generic B' } }, ] }; const results = mockResults[query.toLowerCase()]; // If specific results are found, return them. Otherwise, return generic results. // If generic results are also somehow undefined (they are not, in this mock), fallback to an empty array. return results !== undefined ? results : (mockResults['generic'] || []); } export default function SearchPage() { const searchParams = useSearchParams(); const initialQuery = searchParams.get('q'); const [query, setQuery] = useState(initialQuery || ''); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); const handleSearch = async (currentQuery: string) => { if (!currentQuery.trim()) { // Trim query to avoid searching for empty spaces setResults([]); return; } setLoading(true); const fetchedResults = await getSearchResults(currentQuery); setResults(fetchedResults); setLoading(false); }; // Perform search when initialQuery (from URL) changes or when component mounts with an initialQuery useEffect(() => { if (initialQuery) { setQuery(initialQuery); // Ensure input field is updated if query comes from URL handleSearch(initialQuery); } // eslint-disable-next-line react-hooks/exhaustive-deps // Disabled exhaustive-deps because handleSearch reference might change but we only care about initialQuery }, [initialQuery]); return (

Search Products

{ e.preventDefault(); // Update URL with the new search query window.history.pushState(null, '', `?q=${encodeURIComponent(query)}`); handleSearch(query); }} style={{ display: 'flex', justifyContent: 'center', marginBottom: '20px' }} > setQuery(e.target.value)} placeholder="Search for products..." style={{ padding: '10px', fontSize: '1em', width: '300px', marginRight: '10px' }} />

Personalization by Relewise will be implemented here.

{loading &&

Loading...

} {!loading && results.length > 0 && (

Results for "{query}"

{results.map((product) => (
{product.image.alt}

{product.name}

{product.price.amount} {product.price.currencyCode}

View Details
))}
)} {!loading && results.length === 0 && query && (

No specific results found for "{query}".

Did you mean: t-shirt, accessory, cap, generic?

{/* Placeholder suggestions */}
)} {!loading && results.length === 0 && !query && (

Enter a search term above to find products.

)}
); }