Spaces:
Sleeping
Sleeping
Fix Next.js pre-rendering error by wrapping useSearchParams in Suspense
Browse files- client/src/app/page.jsx +17 -22
client/src/app/page.jsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
'use client';
|
| 2 |
|
| 3 |
-
import { useState, useEffect, useMemo } from 'react';
|
| 4 |
import { useSearchParams } from 'next/navigation';
|
| 5 |
import SearchBar from '../components/SearchBar';
|
| 6 |
import ResultCard from '../components/ResultCard';
|
|
@@ -15,9 +15,7 @@ import { search } from '../api/client';
|
|
| 15 |
import { useGeolocation } from '../hooks/useGeolocation';
|
| 16 |
import './Home.css';
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
export default function Home() {
|
| 21 |
const [results, setResults] = useState([]);
|
| 22 |
const [intent, setIntent] = useState(null);
|
| 23 |
const [loading, setLoading] = useState(false);
|
|
@@ -40,8 +38,6 @@ export default function Home() {
|
|
| 40 |
const rerunQuery = searchParams.get('rerunQuery');
|
| 41 |
if (rerunQuery) {
|
| 42 |
handleSearch(rerunQuery);
|
| 43 |
-
// In Next.js, updating the URL without reloading is done via router.replace
|
| 44 |
-
// but for now, we'll just handle the search.
|
| 45 |
}
|
| 46 |
}, [searchParams]);
|
| 47 |
|
|
@@ -68,21 +64,17 @@ export default function Home() {
|
|
| 68 |
setScopeMessage(null);
|
| 69 |
setHasSearched(true);
|
| 70 |
|
| 71 |
-
// Show auto-scraping message if backend takes longer than 3 seconds
|
| 72 |
const timeoutId = setTimeout(() => {
|
| 73 |
setLoadingMsg('Scraping fresh data from the web (this may take 10-15s)...');
|
| 74 |
}, 3000);
|
| 75 |
|
| 76 |
try {
|
| 77 |
-
// Include `userLocation` dynamically if the user has requested it
|
| 78 |
const data = await search(query, null, filters, userLocation, clarificationContext);
|
| 79 |
clearTimeout(timeoutId);
|
| 80 |
|
| 81 |
if (data.isOutOfScope) {
|
| 82 |
setScopeMessage(data.scopeMessage);
|
| 83 |
setResults([]);
|
| 84 |
-
setIntent(data.intent || null);
|
| 85 |
-
setMeta(null);
|
| 86 |
setLoading(false);
|
| 87 |
return;
|
| 88 |
}
|
|
@@ -90,14 +82,11 @@ export default function Home() {
|
|
| 90 |
if (data.needsClarification) {
|
| 91 |
setClarification({ originalQuery: query, question: data.clarificationQuestion });
|
| 92 |
setResults([]);
|
| 93 |
-
setIntent(null);
|
| 94 |
-
setMeta(null);
|
| 95 |
setLoading(false);
|
| 96 |
return;
|
| 97 |
}
|
| 98 |
|
| 99 |
setClarification(null);
|
| 100 |
-
|
| 101 |
const res = data.results || [];
|
| 102 |
setResults(res);
|
| 103 |
setIntent(data.intent || null);
|
|
@@ -111,7 +100,6 @@ export default function Home() {
|
|
| 111 |
setError(errorMsg);
|
| 112 |
addToast(errorMsg, 'error');
|
| 113 |
setResults([]);
|
| 114 |
-
setIntent(null);
|
| 115 |
} finally {
|
| 116 |
setLoading(false);
|
| 117 |
}
|
|
@@ -119,7 +107,6 @@ export default function Home() {
|
|
| 119 |
|
| 120 |
const filteredResults = useMemo(() => {
|
| 121 |
let filtered = [...results];
|
| 122 |
-
|
| 123 |
dynamicFilters.forEach(schema => {
|
| 124 |
const val = filters[schema.id];
|
| 125 |
if (!val) return;
|
|
@@ -153,16 +140,9 @@ export default function Home() {
|
|
| 153 |
if (!pa) return 1; if (!pb) return -1;
|
| 154 |
return pa - pb;
|
| 155 |
});
|
| 156 |
-
} else if (val === 'price_high') {
|
| 157 |
-
filtered.sort((a, b) => {
|
| 158 |
-
const pa = parseInt(a.priceRange?.match(/([₹$£€])(\d+)/)?.[2] || '0', 10);
|
| 159 |
-
const pb = parseInt(b.priceRange?.match(/([₹$£€])(\d+)/)?.[2] || '0', 10);
|
| 160 |
-
return pb - pa;
|
| 161 |
-
});
|
| 162 |
}
|
| 163 |
}
|
| 164 |
});
|
| 165 |
-
|
| 166 |
return filtered;
|
| 167 |
}, [results, filters, dynamicFilters]);
|
| 168 |
|
|
@@ -340,3 +320,18 @@ export default function Home() {
|
|
| 340 |
</main>
|
| 341 |
);
|
| 342 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
'use client';
|
| 2 |
|
| 3 |
+
import { useState, useEffect, useMemo, Suspense } from 'react';
|
| 4 |
import { useSearchParams } from 'next/navigation';
|
| 5 |
import SearchBar from '../components/SearchBar';
|
| 6 |
import ResultCard from '../components/ResultCard';
|
|
|
|
| 15 |
import { useGeolocation } from '../hooks/useGeolocation';
|
| 16 |
import './Home.css';
|
| 17 |
|
| 18 |
+
function SearchContent() {
|
|
|
|
|
|
|
| 19 |
const [results, setResults] = useState([]);
|
| 20 |
const [intent, setIntent] = useState(null);
|
| 21 |
const [loading, setLoading] = useState(false);
|
|
|
|
| 38 |
const rerunQuery = searchParams.get('rerunQuery');
|
| 39 |
if (rerunQuery) {
|
| 40 |
handleSearch(rerunQuery);
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
}, [searchParams]);
|
| 43 |
|
|
|
|
| 64 |
setScopeMessage(null);
|
| 65 |
setHasSearched(true);
|
| 66 |
|
|
|
|
| 67 |
const timeoutId = setTimeout(() => {
|
| 68 |
setLoadingMsg('Scraping fresh data from the web (this may take 10-15s)...');
|
| 69 |
}, 3000);
|
| 70 |
|
| 71 |
try {
|
|
|
|
| 72 |
const data = await search(query, null, filters, userLocation, clarificationContext);
|
| 73 |
clearTimeout(timeoutId);
|
| 74 |
|
| 75 |
if (data.isOutOfScope) {
|
| 76 |
setScopeMessage(data.scopeMessage);
|
| 77 |
setResults([]);
|
|
|
|
|
|
|
| 78 |
setLoading(false);
|
| 79 |
return;
|
| 80 |
}
|
|
|
|
| 82 |
if (data.needsClarification) {
|
| 83 |
setClarification({ originalQuery: query, question: data.clarificationQuestion });
|
| 84 |
setResults([]);
|
|
|
|
|
|
|
| 85 |
setLoading(false);
|
| 86 |
return;
|
| 87 |
}
|
| 88 |
|
| 89 |
setClarification(null);
|
|
|
|
| 90 |
const res = data.results || [];
|
| 91 |
setResults(res);
|
| 92 |
setIntent(data.intent || null);
|
|
|
|
| 100 |
setError(errorMsg);
|
| 101 |
addToast(errorMsg, 'error');
|
| 102 |
setResults([]);
|
|
|
|
| 103 |
} finally {
|
| 104 |
setLoading(false);
|
| 105 |
}
|
|
|
|
| 107 |
|
| 108 |
const filteredResults = useMemo(() => {
|
| 109 |
let filtered = [...results];
|
|
|
|
| 110 |
dynamicFilters.forEach(schema => {
|
| 111 |
const val = filters[schema.id];
|
| 112 |
if (!val) return;
|
|
|
|
| 140 |
if (!pa) return 1; if (!pb) return -1;
|
| 141 |
return pa - pb;
|
| 142 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
}
|
| 145 |
});
|
|
|
|
| 146 |
return filtered;
|
| 147 |
}, [results, filters, dynamicFilters]);
|
| 148 |
|
|
|
|
| 320 |
</main>
|
| 321 |
);
|
| 322 |
}
|
| 323 |
+
|
| 324 |
+
export default function Home() {
|
| 325 |
+
return (
|
| 326 |
+
<Suspense fallback={
|
| 327 |
+
<main className="home">
|
| 328 |
+
<section className="home-hero">
|
| 329 |
+
<div className="home-hero-glow"></div>
|
| 330 |
+
<h2 className="home-headline fade-in-up">Initializing RedThread...</h2>
|
| 331 |
+
</section>
|
| 332 |
+
</main>
|
| 333 |
+
}>
|
| 334 |
+
<SearchContent />
|
| 335 |
+
</Suspense>
|
| 336 |
+
);
|
| 337 |
+
}
|