Spaces:
Runtime error
Runtime error
File size: 2,693 Bytes
ca10871 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import React, { useState } from "react";
import { Box, Typography, CircularProgress, Container } from "@mui/material";
import SearchBar from "./components/SearchBar";
import ResultsGrid from "./components/ResultsGrid";
import CombinedResults from "./components/CombinedResults";
import axios from "axios";
const App = () => {
const [results, setResults] = useState({});
const [threadId, setThreadId] = useState(null); // State for threadId
const [hasSearched, setHasSearched] = useState(false);
const [loading, setLoading] = useState(false);
const handleSearch = async (query) => {
setLoading(true);
setHasSearched(true);
try {
const response = await axios.post(
`http://localhost:8000/query?q=${encodeURIComponent(query)}`,
);
const { documents, thread_id } = response.data; // Destructure thread_id from the response
// Preserve original index
const documentsWithIndex = documents.map((doc, index) => ({
...doc,
originalIndex: index,
}));
const groupedResults = documentsWithIndex.reduce((acc, doc) => {
const { source } = doc.metadata;
if (!acc[source]) {
acc[source] = [];
}
acc[source].push(doc);
return acc;
}, {});
setResults(groupedResults);
setThreadId(thread_id); // Set the threadId
} catch (error) {
console.error("Error fetching query results:", error);
} finally {
setLoading(false);
}
};
const handleClear = () => {
setResults({});
setThreadId(null);
setHasSearched(false);
};
const combinedResults = Object.values(results).flat();
return (
<Container maxWidth="lg">
<Box sx={{ py: 4 }}>
<Typography
variant="h2"
align="center"
gutterBottom
sx={{ cursor: "pointer" }}
onClick={handleClear}
>
Semantic Catalogue Search
</Typography>
<SearchBar onSearch={handleSearch} />
{loading && (
<Box sx={{ display: "flex", justifyContent: "center", mt: 4 }}>
<CircularProgress />
</Box>
)}
{!loading && hasSearched && (
<>
<ResultsGrid results={results} threadId={threadId} />
<CombinedResults results={combinedResults} threadId={threadId} />
</>
)}
{!hasSearched && !loading && (
<Typography
variant="h6"
sx={{ mt: 4, textAlign: "center", color: "text.secondary" }}
>
Please perform a search to see results.
</Typography>
)}
</Box>
</Container>
);
};
export default App;
|