From 6498856d4901797f6b2a66c374ac99b44aa6986e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 22 May 2025 12:55:15 +0000 Subject: [PATCH] Fix: Ensure getSearchResults in SearchPage returns non-undefined array Resolves a TypeScript error in `app/search/page.tsx`: "Type error: Argument of type 'any[] | undefined' is not assignable to parameter of type 'SetStateAction'." The `getSearchResults` function has been updated to: 1. Explicitly define its return type as `Promise`. 2. Ensure that all code paths return an array, defaulting to an empty array `[]` if no specific or generic results are found, or if the query is empty/whitespace. This guarantees that `setResults` always receives an array, satisfying the type constraints of `useState`. --- app/search/page.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/search/page.tsx b/app/search/page.tsx index c9f99a354..981b29c71 100644 --- a/app/search/page.tsx +++ b/app/search/page.tsx @@ -5,10 +5,12 @@ 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) { +async function getSearchResults(query: string | null): Promise { // Explicit Promise // Simulate network delay await new Promise(resolve => setTimeout(resolve, 50)); - if (!query) return []; + 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': [ @@ -18,12 +20,16 @@ async function getSearchResults(query: string | null) { 'accessory': [ { id: 'sa1', name: 'Search Result Accessory', price: { amount: '12.00', currencyCode: 'USD' }, image: { src: '/placeholder-accessory.jpg', alt: 'Accessory' } }, ], - 'generic': [ // Added a generic list for queries that don't match specific terms + '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' } }, ] }; - return mockResults[query.toLowerCase()] || mockResults['generic']; // Fallback to generic results + + 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() {