FIX: Stream routing on ResultsPage;

This commit is contained in:
Chris-1010
2025-02-16 23:57:57 +00:00
parent d1d2785c38
commit d0aa591116
3 changed files with 16 additions and 22 deletions

View File

@@ -9,16 +9,17 @@ const SearchBar: React.FC = () => {
const navigate = useNavigate(); const navigate = useNavigate();
// Debounce the search query // Debounce the search query
{/* {
/*
useEffect(() => { useEffect(() => {
const timer = setTimeout(() => { const timer = setTimeout(() => {
setDebouncedQuery(searchQuery); setDebouncedQuery(searchQuery);
}, 500); // Wait 500ms after user stops typing }, 500); // Wait 500ms after user stops typing
return () => clearTimeout(timer); return () => clearTimeout(timer);
}, [searchQuery]); */} }, [searchQuery]); */
}
// Perform search when debounced query changes
const handleSearch = async () => { const handleSearch = async () => {
if (!searchQuery.trim()) return; if (!searchQuery.trim()) return;
@@ -28,13 +29,15 @@ const SearchBar: React.FC = () => {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ query: searchQuery }), // <-- Fixed payload body: JSON.stringify({ query: searchQuery }),
}); });
const data = await response.json(); const data = await response.json();
console.log("Search results:", data); console.log("Search results:", data);
navigate("/results", { state: { searchResults: data, query: searchQuery } }); navigate("/results", {
state: { searchResults: data, query: searchQuery },
});
// Handle the search results here // Handle the search results here
} catch (error) { } catch (error) {
@@ -44,24 +47,15 @@ const SearchBar: React.FC = () => {
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => { const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") { if (e.key === "Enter") {
console.log("Key pressed:", e.key); // Debugging e.preventDefault();
handleSearch();
e.preventDefault(); // Prevent unintended form submission
handleSearch(); // Trigger search when Enter key is pressed
} }
}; };
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchQuery(e.target.value); setSearchQuery(e.target.value);
}; };
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
handleSearch();
}
};
return ( return (
<div id="search-bar" className="flex items-center"> <div id="search-bar" className="flex items-center">
<Input <Input
@@ -73,8 +67,6 @@ const SearchBar: React.FC = () => {
extraClasses="pr-[30px] focus:outline-none focus:border-purple-500 focus:w-[30vw]" extraClasses="pr-[30px] focus:outline-none focus:border-purple-500 focus:w-[30vw]"
/> />
<SearchIcon className="-translate-x-[28px] top-1/2 h-6 w-6 text-white" /> <SearchIcon className="-translate-x-[28px] top-1/2 h-6 w-6 text-white" />
</div> </div>
); );

View File

@@ -63,9 +63,9 @@ const ResultsPage: React.FC = ({}) => {
<li <li
key={index} key={index}
className="border p-2 rounded my-2 cursor-pointer" className="border p-2 rounded my-2 cursor-pointer"
onClick={() => navigate(`/${stream.streamer_name}`)} onClick={() => navigate(`/${stream.username}`)}
> >
{stream.title} - {stream.num_viewers} viewers {stream.title} - {stream.username} - {stream.num_viewers} viewers
</li> </li>
))} ))}
</ul> </ul>

View File

@@ -41,9 +41,10 @@ def search_results():
# 3 streams # 3 streams
streams = db.fetchall(""" streams = db.fetchall("""
SELECT bm25(stream_fts) AS score, s.user_id, s.title, s.num_viewers, s.category_id SELECT bm25(stream_fts) AS score, s.user_id, s.title, s.num_viewers, s.category_id, u.username
FROM streams AS s FROM streams AS s
INNER JOIN stream_fts AS f ON s.user_id = f.user_id INNER JOIN stream_fts AS f ON s.user_id = f.user_id
INNER JOIN users AS u ON s.user_id = u.user_id
WHERE f.title LIKE '%' || ? || '%' WHERE f.title LIKE '%' || ? || '%'
ORDER BY score ASC ORDER BY score ASC
LIMIT 3; LIMIT 3;
@@ -123,9 +124,10 @@ def search_streams():
# Fetch the ranked data and send to JSON to be displayed # Fetch the ranked data and send to JSON to be displayed
streams = db.fetchall(""" streams = db.fetchall("""
SELECT bm25(stream_fts) AS score, s.user_id, s.title, s.num_viewers, s.category_id SELECT bm25(stream_fts) AS score, s.user_id, s.title, s.num_viewers, s.category_id, u.username
FROM streams AS s FROM streams AS s
INNER JOIN stream_fts AS f ON s.user_id = f.user_id INNER JOIN stream_fts AS f ON s.user_id = f.user_id
INNER JOIN users AS u ON s.user_id = u.user_id
WHERE f.title LIKE '%' || ? || '%' WHERE f.title LIKE '%' || ? || '%'
ORDER BY score ASC; ORDER BY score ASC;
""", (query,)) """, (query,))