cjber commited on
Commit
ca10871
·
1 Parent(s): c9d0842

add react app frontend

Browse files
frontend/.gitignore ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2
+
3
+ # dependencies
4
+ /node_modules
5
+ /.pnp
6
+ .pnp.js
7
+
8
+ # testing
9
+ /coverage
10
+
11
+ # production
12
+ /build
13
+
14
+ # misc
15
+ .DS_Store
16
+ .env.local
17
+ .env.development.local
18
+ .env.test.local
19
+ .env.production.local
20
+
21
+ npm-debug.log*
22
+ yarn-debug.log*
23
+ yarn-error.log*
frontend/README.md ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Getting Started with Create React App
2
+
3
+ This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4
+
5
+ ## Available Scripts
6
+
7
+ In the project directory, you can run:
8
+
9
+ ### `npm start`
10
+
11
+ Runs the app in the development mode.\
12
+ Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13
+
14
+ The page will reload when you make changes.\
15
+ You may also see any lint errors in the console.
16
+
17
+ ### `npm test`
18
+
19
+ Launches the test runner in the interactive watch mode.\
20
+ See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21
+
22
+ ### `npm run build`
23
+
24
+ Builds the app for production to the `build` folder.\
25
+ It correctly bundles React in production mode and optimizes the build for the best performance.
26
+
27
+ The build is minified and the filenames include the hashes.\
28
+ Your app is ready to be deployed!
29
+
30
+ See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31
+
32
+ ### `npm run eject`
33
+
34
+ **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35
+
36
+ If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37
+
38
+ Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39
+
40
+ You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41
+
42
+ ## Learn More
43
+
44
+ You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45
+
46
+ To learn React, check out the [React documentation](https://reactjs.org/).
47
+
48
+ ### Code Splitting
49
+
50
+ This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51
+
52
+ ### Analyzing the Bundle Size
53
+
54
+ This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55
+
56
+ ### Making a Progressive Web App
57
+
58
+ This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59
+
60
+ ### Advanced Configuration
61
+
62
+ This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63
+
64
+ ### Deployment
65
+
66
+ This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67
+
68
+ ### `npm run build` fails to minify
69
+
70
+ This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
frontend/package-lock.json ADDED
The diff for this file is too large to render. See raw diff
 
frontend/package.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "semantic-search",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "dependencies": {
6
+ "@emotion/react": "^11.13.0",
7
+ "@emotion/styled": "^11.13.0",
8
+ "@mui/icons-material": "^5.16.7",
9
+ "@mui/material": "^5.16.7",
10
+ "@testing-library/jest-dom": "^5.17.0",
11
+ "@testing-library/react": "^13.4.0",
12
+ "@testing-library/user-event": "^13.5.0",
13
+ "axios": "^1.7.4",
14
+ "react": "^18.3.1",
15
+ "react-dom": "^18.3.1",
16
+ "react-router-dom": "^6.26.1",
17
+ "react-scripts": "5.0.1",
18
+ "web-vitals": "^2.1.4"
19
+ },
20
+ "scripts": {
21
+ "start": "react-scripts start",
22
+ "build": "react-scripts build",
23
+ "test": "react-scripts test",
24
+ "eject": "react-scripts eject"
25
+ },
26
+ "eslintConfig": {
27
+ "extends": [
28
+ "react-app",
29
+ "react-app/jest"
30
+ ]
31
+ },
32
+ "browserslist": {
33
+ "production": [
34
+ ">0.2%",
35
+ "not dead",
36
+ "not op_mini all"
37
+ ],
38
+ "development": [
39
+ "last 1 chrome version",
40
+ "last 1 firefox version",
41
+ "last 1 safari version"
42
+ ]
43
+ }
44
+ }
frontend/public/favicon.ico ADDED
frontend/public/index.html ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+ <meta name="theme-color" content="#000000" />
8
+ <meta
9
+ name="description"
10
+ content="Web site created using create-react-app"
11
+ />
12
+ <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13
+ <!--
14
+ manifest.json provides metadata used when your web app is installed on a
15
+ user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16
+ -->
17
+ <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18
+ <!--
19
+ Notice the use of %PUBLIC_URL% in the tags above.
20
+ It will be replaced with the URL of the `public` folder during the build.
21
+ Only files inside the `public` folder can be referenced from the HTML.
22
+
23
+ Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24
+ work correctly both with client-side routing and a non-root public URL.
25
+ Learn how to configure a non-root public URL by running `npm run build`.
26
+ -->
27
+ <title>React App</title>
28
+ </head>
29
+ <body>
30
+ <noscript>You need to enable JavaScript to run this app.</noscript>
31
+ <div id="root"></div>
32
+ <!--
33
+ This HTML file is a template.
34
+ If you open it directly in the browser, you will see an empty page.
35
+
36
+ You can add webfonts, meta tags, or analytics to this file.
37
+ The build step will place the bundled scripts into the <body> tag.
38
+
39
+ To begin the development, run `npm start` or `yarn start`.
40
+ To create a production bundle, use `npm run build` or `yarn build`.
41
+ -->
42
+ </body>
43
+ </html>
frontend/public/logo192.png ADDED
frontend/public/logo512.png ADDED
frontend/public/manifest.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "short_name": "React App",
3
+ "name": "Create React App Sample",
4
+ "icons": [
5
+ {
6
+ "src": "favicon.ico",
7
+ "sizes": "64x64 32x32 24x24 16x16",
8
+ "type": "image/x-icon"
9
+ },
10
+ {
11
+ "src": "logo192.png",
12
+ "type": "image/png",
13
+ "sizes": "192x192"
14
+ },
15
+ {
16
+ "src": "logo512.png",
17
+ "type": "image/png",
18
+ "sizes": "512x512"
19
+ }
20
+ ],
21
+ "start_url": ".",
22
+ "display": "standalone",
23
+ "theme_color": "#000000",
24
+ "background_color": "#ffffff"
25
+ }
frontend/public/robots.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # https://www.robotstxt.org/robotstxt.html
2
+ User-agent: *
3
+ Disallow:
frontend/src/App.css ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .top-bar {
2
+ display: flex;
3
+ flex-direction: column;
4
+ gap: 16px;
5
+ margin-bottom: 16px;
6
+ }
7
+
8
+ .source-logo {
9
+ width: 50px; /* Adjust the size of the logos */
10
+ }
11
+
12
+ .result {
13
+ border-radius: 8px;
14
+ padding: 16px;
15
+ margin-bottom: 16px;
16
+ background: transparent; /* Use theme background instead */
17
+ }
18
+
19
+ h2 {
20
+ font-size: 1.2em;
21
+ margin: 0;
22
+ color: white; /* Ensure headings are visible in dark mode */
23
+ }
24
+
25
+ p {
26
+ margin: 8px 0;
27
+ color: #b0bec5; /* Light grey for paragraph text */
28
+ }
frontend/src/App.js ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from "react";
2
+ import { Box, Typography, CircularProgress, Container } from "@mui/material";
3
+ import SearchBar from "./components/SearchBar";
4
+ import ResultsGrid from "./components/ResultsGrid";
5
+ import CombinedResults from "./components/CombinedResults";
6
+ import axios from "axios";
7
+
8
+ const App = () => {
9
+ const [results, setResults] = useState({});
10
+ const [threadId, setThreadId] = useState(null); // State for threadId
11
+ const [hasSearched, setHasSearched] = useState(false);
12
+ const [loading, setLoading] = useState(false);
13
+
14
+ const handleSearch = async (query) => {
15
+ setLoading(true);
16
+ setHasSearched(true);
17
+
18
+ try {
19
+ const response = await axios.post(
20
+ `http://localhost:8000/query?q=${encodeURIComponent(query)}`,
21
+ );
22
+ const { documents, thread_id } = response.data; // Destructure thread_id from the response
23
+
24
+ // Preserve original index
25
+ const documentsWithIndex = documents.map((doc, index) => ({
26
+ ...doc,
27
+ originalIndex: index,
28
+ }));
29
+
30
+ const groupedResults = documentsWithIndex.reduce((acc, doc) => {
31
+ const { source } = doc.metadata;
32
+ if (!acc[source]) {
33
+ acc[source] = [];
34
+ }
35
+ acc[source].push(doc);
36
+ return acc;
37
+ }, {});
38
+
39
+ setResults(groupedResults);
40
+ setThreadId(thread_id); // Set the threadId
41
+ } catch (error) {
42
+ console.error("Error fetching query results:", error);
43
+ } finally {
44
+ setLoading(false);
45
+ }
46
+ };
47
+
48
+ const handleClear = () => {
49
+ setResults({});
50
+ setThreadId(null);
51
+ setHasSearched(false);
52
+ };
53
+
54
+ const combinedResults = Object.values(results).flat();
55
+
56
+ return (
57
+ <Container maxWidth="lg">
58
+ <Box sx={{ py: 4 }}>
59
+ <Typography
60
+ variant="h2"
61
+ align="center"
62
+ gutterBottom
63
+ sx={{ cursor: "pointer" }}
64
+ onClick={handleClear}
65
+ >
66
+ Semantic Catalogue Search
67
+ </Typography>
68
+ <SearchBar onSearch={handleSearch} />
69
+ {loading && (
70
+ <Box sx={{ display: "flex", justifyContent: "center", mt: 4 }}>
71
+ <CircularProgress />
72
+ </Box>
73
+ )}
74
+ {!loading && hasSearched && (
75
+ <>
76
+ <ResultsGrid results={results} threadId={threadId} />
77
+ <CombinedResults results={combinedResults} threadId={threadId} />
78
+ </>
79
+ )}
80
+ {!hasSearched && !loading && (
81
+ <Typography
82
+ variant="h6"
83
+ sx={{ mt: 4, textAlign: "center", color: "text.secondary" }}
84
+ >
85
+ Please perform a search to see results.
86
+ </Typography>
87
+ )}
88
+ </Box>
89
+ </Container>
90
+ );
91
+ };
92
+
93
+ export default App;
94
+
frontend/src/App.test.js ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import { render, screen } from '@testing-library/react';
2
+ import App from './App';
3
+
4
+ test('renders learn react link', () => {
5
+ render(<App />);
6
+ const linkElement = screen.getByText(/learn react/i);
7
+ expect(linkElement).toBeInTheDocument();
8
+ });
frontend/src/components/CombinedResults.js ADDED
@@ -0,0 +1,333 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from "react";
2
+ import {
3
+ Box,
4
+ Card,
5
+ CardContent,
6
+ Typography,
7
+ IconButton,
8
+ Link,
9
+ Divider,
10
+ Button,
11
+ CircularProgress,
12
+ } from "@mui/material";
13
+ import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
14
+ import LinkIcon from "@mui/icons-material/Link"; // For document URL link
15
+ import HelpOutlineIcon from "@mui/icons-material/HelpOutline"; // For explain feature
16
+ import CloseIcon from "@mui/icons-material/Close"; // For collapsing explanation
17
+ import axios from "axios";
18
+
19
+ const CombinedResults = ({ results = [], threadId }) => {
20
+ const [expandedDocs, setExpandedDocs] = useState({});
21
+ const [visibleCount, setVisibleCount] = useState(5); // Number of results to show initially
22
+ const [explanations, setExplanations] = useState({});
23
+ const [loadingExplanation, setLoadingExplanation] = useState({});
24
+ const [showExplanation, setShowExplanation] = useState({});
25
+
26
+ // Toggle expand state for a specific document
27
+ const handleToggleExpand = (index) => {
28
+ setExpandedDocs((prev) => ({
29
+ ...prev,
30
+ [index]: !prev[index],
31
+ }));
32
+ };
33
+
34
+ const handleLoadMore = () => {
35
+ setVisibleCount((prevCount) => prevCount + 5); // Show 10 more results
36
+ };
37
+
38
+ // Fetch explanation for a document
39
+ const handleFetchExplanation = async (originalIndex) => {
40
+ if (!threadId) {
41
+ return;
42
+ }
43
+
44
+ // Set loading state immediately
45
+ setLoadingExplanation((prev) => ({
46
+ ...prev,
47
+ [originalIndex]: true,
48
+ }));
49
+
50
+ // Check if the explanation is already available
51
+ if (explanations[`${threadId}-${originalIndex}`]) {
52
+ setLoadingExplanation((prev) => ({
53
+ ...prev,
54
+ [originalIndex]: false,
55
+ }));
56
+ return;
57
+ }
58
+
59
+ try {
60
+ const response = await axios.get(
61
+ `http://localhost:8000/explain/${threadId}?docid=${originalIndex}`,
62
+ );
63
+ setExplanations((prev) => ({
64
+ ...prev,
65
+ [`${threadId}-${originalIndex}`]: response.data,
66
+ }));
67
+ } catch (error) {
68
+ console.error("Error fetching explanation:", error);
69
+ } finally {
70
+ setLoadingExplanation((prev) => ({
71
+ ...prev,
72
+ [originalIndex]: false,
73
+ }));
74
+ }
75
+ };
76
+
77
+ // Toggle explanation visibility
78
+ const handleToggleExplanation = (originalIndex) => {
79
+ setShowExplanation((prev) => ({
80
+ ...prev,
81
+ [originalIndex]: !prev[originalIndex],
82
+ }));
83
+
84
+ // Fetch explanation if not already available
85
+ if (!explanations[`${threadId}-${originalIndex}`]) {
86
+ handleFetchExplanation(originalIndex);
87
+ }
88
+ };
89
+
90
+ // Format date for display
91
+ const formatDate = (dateString) => {
92
+ const date = new Date(dateString);
93
+ return date.toLocaleDateString(undefined, {
94
+ year: "numeric",
95
+ month: "long",
96
+ day: "numeric",
97
+ });
98
+ };
99
+
100
+ if (!Array.isArray(results)) {
101
+ console.warn("Results prop is not an array:", results);
102
+ return <Typography variant="h6">No results to display.</Typography>;
103
+ }
104
+
105
+ // Slice results to only show the number of visible results
106
+ const displayedResults = results.slice(0, visibleCount);
107
+
108
+ return (
109
+ <Box sx={{ mt: 4, p: 2 }}>
110
+ {results.length === 0 ? (
111
+ <Typography variant="h6" sx={{ textAlign: "center" }}>
112
+ No results found.
113
+ </Typography>
114
+ ) : (
115
+ <Card
116
+ sx={{
117
+ borderRadius: 2,
118
+ boxShadow: 3,
119
+ overflow: "hidden",
120
+ width: "100%",
121
+ backgroundColor: "background.default",
122
+ }}
123
+ >
124
+ <CardContent
125
+ sx={{
126
+ display: "flex",
127
+ flexDirection: "column",
128
+ p: 3,
129
+ }}
130
+ >
131
+ {displayedResults.map((doc, index) => (
132
+ <Box key={index} sx={{ mb: 3 }}>
133
+ <Box sx={{ display: "flex", alignItems: "center", mb: 1 }}>
134
+ <Link
135
+ href={doc.metadata.url}
136
+ target="_blank"
137
+ rel="noopener"
138
+ sx={{
139
+ textDecoration: "none",
140
+ color: "text.primary",
141
+ flex: 1,
142
+ "&:hover": {
143
+ textDecoration: "underline",
144
+ },
145
+ }}
146
+ >
147
+ <Typography
148
+ variant="h6"
149
+ sx={{
150
+ fontSize: "1.125rem",
151
+ fontWeight: "bold",
152
+ overflow: "hidden",
153
+ textOverflow: "ellipsis",
154
+ }}
155
+ >
156
+ {index + 1 + ". " + doc.metadata.title || "Untitled"}
157
+ </Typography>
158
+ </Link>
159
+ <IconButton
160
+ href={doc.metadata.url}
161
+ target="_blank"
162
+ rel="noopener"
163
+ sx={{
164
+ ml: 1,
165
+ color: "primary.main",
166
+ transition: "color 0.3s",
167
+ "&:hover": {
168
+ color: "primary.dark",
169
+ },
170
+ }}
171
+ >
172
+ <LinkIcon />
173
+ </IconButton>
174
+ <IconButton
175
+ onClick={() => handleToggleExpand(index)}
176
+ sx={{
177
+ ml: 2,
178
+ color: expandedDocs[index]
179
+ ? "text.primary"
180
+ : "primary.main",
181
+ }}
182
+ >
183
+ <ExpandMoreIcon
184
+ sx={{
185
+ color: "secondary.light",
186
+ transform: expandedDocs[index]
187
+ ? "rotate(180deg)"
188
+ : "rotate(0deg)",
189
+ transition: "transform 0.3s",
190
+ }}
191
+ />
192
+ </IconButton>
193
+ </Box>
194
+
195
+ {/* Date Created */}
196
+ <Typography
197
+ variant="caption"
198
+ sx={{
199
+ color: "text.secondary",
200
+ mb: 1,
201
+ fontStyle: "italic",
202
+ }}
203
+ >
204
+ {doc.metadata.date_created
205
+ ? `Created on: ${formatDate(doc.metadata.date_created)}`
206
+ : "Date not available"}
207
+ </Typography>
208
+
209
+ <Typography
210
+ variant="body2"
211
+ sx={{
212
+ fontSize: "0.875rem",
213
+ color: expandedDocs[index]
214
+ ? "text.secondary"
215
+ : "text.disabled", // Change color based on expansion
216
+ overflow: "hidden",
217
+ textOverflow: "none",
218
+ WebkitBoxOrient: "vertical",
219
+ WebkitLineClamp: expandedDocs[index] ? "none" : 3, // Limit lines when not expanded
220
+ backgroundColor: "background.default",
221
+ // display: "-webkit-box",
222
+ p: 2,
223
+ borderRadius: 1,
224
+ lineHeight: "1.5",
225
+ maxHeight: expandedDocs[index] ? "100em" : "5em", // Control max height for transition
226
+ transition: "color 0.5s ease, max-height 0.5s ease", // Add transition for color and max-height
227
+ }}
228
+ dangerouslySetInnerHTML={{
229
+ __html: doc.page_content?.startsWith("Dataset Title:")
230
+ ? doc.page_content.slice(doc.metadata.title.length + 15, 5000).replace(/\n/g, '<p></p>')
231
+ : (doc.page_content || "No preview available.").replace(/\n/g, '<p></p>')
232
+ }}
233
+ >
234
+ </Typography>
235
+
236
+ {/* Explain Feature */}
237
+ <Box sx={{ mt: 2 }}>
238
+ <Button
239
+ variant="contained"
240
+ color="primary"
241
+ startIcon={
242
+ showExplanation[doc.originalIndex] ? (
243
+ <CloseIcon />
244
+ ) : (
245
+ <HelpOutlineIcon />
246
+ )
247
+ }
248
+ onClick={() => handleToggleExplanation(doc.originalIndex)}
249
+ sx={{
250
+ fontWeight: "bold",
251
+ borderRadius: 1,
252
+ textTransform: "none",
253
+ mb: 1,
254
+ transition: "background-color 0.3s",
255
+ "&:hover": {
256
+ backgroundColor: "info.dark",
257
+ },
258
+ }}
259
+ >
260
+ {showExplanation[doc.originalIndex]
261
+ ? "Collapse Explanation"
262
+ : "Ask AI"}
263
+ </Button>
264
+
265
+ {showExplanation[doc.originalIndex] && (
266
+ <Box
267
+ sx={{
268
+ mt: 2,
269
+ p: 2,
270
+ borderRadius: 2,
271
+ backgroundColor: "info.light",
272
+ color: "info.contrastText",
273
+ border: "1px solid",
274
+ borderColor: "info.dark",
275
+ maxHeight: "12em",
276
+ overflowY: "auto",
277
+ textAlign: "justify",
278
+ scrollbarWidth: "none",
279
+ msOverflowStyle: "none",
280
+ "&::-webkit-scrollbar": {
281
+ display: "none",
282
+ },
283
+ }}
284
+ >
285
+ <Typography
286
+ variant="subtitle2"
287
+ sx={{ mb: 1, fontWeight: "bold" }}
288
+ >
289
+ AI Explanation
290
+ </Typography>
291
+ {loadingExplanation[doc.originalIndex] ? (
292
+ <Box
293
+ sx={{
294
+ display: "flex",
295
+ justifyContent: "center",
296
+ alignItems: "center",
297
+ }}
298
+ >
299
+ <CircularProgress size={20} color="inherit" />
300
+ </Box>
301
+ ) : (
302
+ <Typography
303
+ variant="body2"
304
+ sx={{ fontSize: "0.875rem", lineHeight: "1.6" }}
305
+ >
306
+ {explanations[`${threadId}-${doc.originalIndex}`]
307
+ ?.generation || "No explanation available."}
308
+ </Typography>
309
+ )}
310
+ </Box>
311
+ )}
312
+ </Box>
313
+ <Divider sx={{ my: 2 }} />
314
+ </Box>
315
+ ))}
316
+ {visibleCount < results.length && (
317
+ <Button
318
+ onClick={handleLoadMore}
319
+ variant="contained"
320
+ color="secondary"
321
+ sx={{ mt: 2, alignSelf: "center" }}
322
+ >
323
+ Load More
324
+ </Button>
325
+ )}
326
+ </CardContent>
327
+ </Card>
328
+ )}
329
+ </Box>
330
+ );
331
+ };
332
+
333
+ export default CombinedResults;
frontend/src/components/DocumentItem.js ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from "react";
2
+ import {
3
+ Typography,
4
+ Box,
5
+ IconButton,
6
+ Link,
7
+ Collapse,
8
+ Tooltip,
9
+ Chip,
10
+ Paper,
11
+ } from "@mui/material";
12
+ import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
13
+ import HelpOutlineIcon from "@mui/icons-material/HelpOutline";
14
+ import CloseIcon from "@mui/icons-material/Close";
15
+ import ExplanationBox from "./ExplanationBox";
16
+
17
+ function DocumentItem({ document, threadId }) {
18
+ const [expanded, setExpanded] = useState(false);
19
+ const [explanationVisible, setExplanationVisible] = useState(false);
20
+
21
+ const handleToggleExpand = () => {
22
+ setExpanded(!expanded);
23
+ };
24
+
25
+ const handleToggleExplanation = () => {
26
+ setExplanationVisible(!explanationVisible);
27
+ };
28
+
29
+ return (
30
+ <Paper sx={{ mb: 0, overflow: "hidden" }}>
31
+ <Box
32
+ sx={{
33
+ display: "flex",
34
+ alignItems: "center",
35
+ p: 0,
36
+ bgcolor: "background.paper",
37
+ }}
38
+ >
39
+ <Box sx={{ flex: 1, minWidth: 0 }}>
40
+ <Link
41
+ href={document.metadata.url}
42
+ target="_blank"
43
+ rel="noopener"
44
+ sx={{
45
+ textDecoration: "none",
46
+ color: "primary.main",
47
+ }}
48
+ >
49
+ <Typography
50
+ variant="subtitle2"
51
+ sx={{
52
+ fontWeight: "bold",
53
+ fontSize: "0.75rem",
54
+ overflow: "hidden",
55
+ textOverflow: "ellipsis",
56
+ whiteSpace: "nowrap",
57
+ color: "text.secondary",
58
+ "&:hover": {
59
+ color: "primary.main",
60
+ },
61
+ }}
62
+ >
63
+ {document.metadata.title || "Untitled"}
64
+ </Typography>
65
+ </Link>
66
+ </Box>
67
+ <IconButton onClick={handleToggleExpand} size="small">
68
+ <ExpandMoreIcon
69
+ sx={{
70
+ color: "secondary.light",
71
+ transform: expanded ? "rotate(180deg)" : "rotate(0deg)",
72
+ transition: "transform 0.3s",
73
+ }}
74
+ />
75
+ </IconButton>
76
+ <Tooltip
77
+ title={explanationVisible ? "Hide Explanation" : "Show Explanation"}
78
+ >
79
+ <IconButton onClick={handleToggleExplanation} size="small">
80
+ {explanationVisible ? (
81
+ <CloseIcon sx={{ color: "error.main" }} fontSize="small" />
82
+ ) : (
83
+ <HelpOutlineIcon sx={{ color: "info.dark" }} fontSize="small" />
84
+ )}
85
+ </IconButton>
86
+ </Tooltip>
87
+ </Box>
88
+ <Collapse in={expanded}>
89
+ <Box sx={{ p: 0.5, bgcolor: "background.paper" }}>
90
+ <Typography
91
+ variant="body2"
92
+ sx={{
93
+ color: "text.secondary",
94
+ fontSize: "0.75rem",
95
+ mb: 0.5,
96
+ overflow: "hidden",
97
+ textOverflow: "ellipsis",
98
+ display: "-webkit-box",
99
+ WebkitLineClamp: 10,
100
+ WebkitBoxOrient: "vertical",
101
+ }}
102
+ >
103
+ {document.page_content || "No preview available."}
104
+ </Typography>
105
+ <Typography
106
+ variant="caption"
107
+ sx={{
108
+ mb: 0.5,
109
+ color: "text.disabled",
110
+ fontStyle: "italic",
111
+ }}
112
+ >
113
+ {`Created: ${new Date(document.metadata.date_created).toLocaleDateString()}`}
114
+ </Typography>
115
+ </Box>
116
+ </Collapse>
117
+ {explanationVisible && (
118
+ <Box sx={{ p: 0.5, bgcolor: "background.paper" }}>
119
+ <ExplanationBox
120
+ documentId={document.originalIndex}
121
+ threadId={threadId}
122
+ />
123
+ </Box>
124
+ )}
125
+ </Paper>
126
+ );
127
+ }
128
+
129
+ export default DocumentItem;
frontend/src/components/ExplanationBox.js ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState, useEffect } from "react";
2
+ import {
3
+ Box,
4
+ Typography,
5
+ CircularProgress,
6
+ Paper,
7
+ Tooltip,
8
+ } from "@mui/material";
9
+ import axios from "axios";
10
+
11
+ // Create a cache object to store explanations
12
+ const explanationCache = {};
13
+
14
+ function ExplanationBox({ documentId, threadId }) {
15
+ const [explanation, setExplanation] = useState(null);
16
+ const [loading, setLoading] = useState(true);
17
+ const [error, setError] = useState(null);
18
+
19
+ useEffect(() => {
20
+ const fetchExplanation = async () => {
21
+ setLoading(true);
22
+ setError(null);
23
+ try {
24
+ // Check the cache before making a request
25
+ if (explanationCache[`${documentId}-${threadId}`]) {
26
+ setExplanation(explanationCache[`${documentId}-${threadId}`]);
27
+ setLoading(false);
28
+ return;
29
+ }
30
+
31
+ const response = await axios.get(
32
+ `http://localhost:8000/explain/${threadId}?docid=${documentId}`,
33
+ );
34
+ setExplanation(response.data);
35
+ // Store the fetched explanation in the cache
36
+ explanationCache[`${documentId}-${threadId}`] = response.data;
37
+ } catch (error) {
38
+ console.error("Error fetching explanation:", error);
39
+ setError("Failed to fetch explanation");
40
+ } finally {
41
+ setLoading(false);
42
+ }
43
+ };
44
+
45
+ fetchExplanation();
46
+ }, [documentId, threadId]);
47
+
48
+ if (loading) {
49
+ return (
50
+ <Box sx={{ display: "flex", justifyContent: "center", py: 2 }}>
51
+ <CircularProgress size={20} color="primary" />
52
+ </Box>
53
+ );
54
+ }
55
+
56
+ if (error) {
57
+ return (
58
+ <Typography variant="body2" color="error" sx={{ py: 1 }}>
59
+ {error}
60
+ </Typography>
61
+ );
62
+ }
63
+
64
+ const renderExplanationWithLinks = (text, chunks) => {
65
+ const parts = text.split(/(\[\d+\])/);
66
+ return parts.map((part, index) => {
67
+ const match = part.match(/\[(\d+)\]/);
68
+ if (match) {
69
+ const chunkIndex = parseInt(match[1]);
70
+ return (
71
+ <Tooltip
72
+ key={index + 1}
73
+ title={chunks[chunkIndex]?.page_content || "No content available"}
74
+ arrow
75
+ placement="top"
76
+ >
77
+ <Typography
78
+ component="span"
79
+ sx={{
80
+ fontSize: "0.75em",
81
+ color: "text.secondary",
82
+ cursor: "pointer",
83
+ "&:hover": {
84
+ textDecoration: "underline",
85
+ color: "primary.main",
86
+ },
87
+ }}
88
+ >
89
+ {`[${chunkIndex + 1}]`}
90
+ </Typography>
91
+ </Tooltip>
92
+ );
93
+ }
94
+ return <span key={index + 1}>{part}</span>;
95
+ });
96
+ };
97
+
98
+ return (
99
+ <Paper
100
+ elevation={0}
101
+ sx={{ p: 2, bgcolor: "primary.main", borderRadius: 1 }}
102
+ >
103
+ <Typography
104
+ variant="body2"
105
+ sx={{ color: "secondary.contrastText", fontSize: "0.75em" }}
106
+ >
107
+ {explanation?.generation
108
+ ? renderExplanationWithLinks(
109
+ explanation.generation,
110
+ explanation.chunks,
111
+ )
112
+ : "No explanation available."}
113
+ </Typography>
114
+ </Paper>
115
+ );
116
+ }
117
+
118
+ export default ExplanationBox;
frontend/src/components/ResultsGrid.js ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from "react";
2
+ import {
3
+ Grid,
4
+ Typography,
5
+ Box,
6
+ Card,
7
+ CardContent,
8
+ Button,
9
+ } from "@mui/material";
10
+ import SourceCard from "./SourceCard";
11
+
12
+ function ResultsGrid({ results, threadId }) {
13
+ if (!results || Object.keys(results).length === 0) {
14
+ return (
15
+ <Typography
16
+ variant="h6"
17
+ sx={{ p: 2, textAlign: "center", color: "text.secondary" }}
18
+ >
19
+ No results found.
20
+ </Typography>
21
+ );
22
+ }
23
+
24
+ return (
25
+ <Box sx={{ p: 2 }}>
26
+ <Typography
27
+ variant="h4"
28
+ sx={{ mb: 4, color: "text.primary", textAlign: "center" }}
29
+ ></Typography>
30
+ <Grid container spacing={3}>
31
+ {Object.keys(results).map((source, idx) => (
32
+ <Grid item xs={12} sm={6} md={6} key={idx}>
33
+ <Card
34
+ sx={{
35
+ borderRadius: 2,
36
+ boxShadow: 3,
37
+ overflow: "hidden",
38
+ bgcolor: "background.paper",
39
+ }}
40
+ >
41
+ <CardContent sx={{ p: 2, bgcolor: "background.paper" }}>
42
+ <Typography
43
+ variant="subtitle1"
44
+ sx={{ fontWeight: "bold", mb: 2 }}
45
+ >
46
+ {source}
47
+ </Typography>
48
+ <SourceCard
49
+ source={source}
50
+ documents={results[source]}
51
+ threadId={threadId}
52
+ />
53
+ </CardContent>
54
+ </Card>
55
+ </Grid>
56
+ ))}
57
+ </Grid>
58
+ </Box>
59
+ );
60
+ }
61
+
62
+ export default ResultsGrid;
frontend/src/components/ResultsGrid_tmp.js ADDED
@@ -0,0 +1,356 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from "react";
2
+ import {
3
+ Grid,
4
+ Card,
5
+ CardContent,
6
+ Typography,
7
+ Box,
8
+ Divider,
9
+ IconButton,
10
+ Link,
11
+ CircularProgress,
12
+ } from "@mui/material";
13
+ import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
14
+ import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
15
+ import ChevronRightIcon from "@mui/icons-material/ChevronRight";
16
+ import HelpOutlineIcon from "@mui/icons-material/HelpOutline"; // Icon for explanations
17
+ import CloseIcon from "@mui/icons-material/Close"; // Icon to close explanation
18
+ import axios from "axios";
19
+
20
+ function ResultsGrid({ results, threadId }) {
21
+ const [expandedDocs, setExpandedDocs] = useState({});
22
+ const [pageIndices, setPageIndices] = useState({});
23
+ const [explanations, setExplanations] = useState({});
24
+ const [loadingExplanation, setLoadingExplanation] = useState({});
25
+ const [explanationVisible, setExplanationVisible] = useState({}); // Track visibility of explanations
26
+
27
+ const handleToggleExpand = (source, index) => {
28
+ setExpandedDocs((prev) => ({
29
+ ...prev,
30
+ [`${source}-${index}`]: !prev[`${source}-${index}`],
31
+ }));
32
+ };
33
+
34
+ const handlePageChange = (source, direction) => {
35
+ setPageIndices((prev) => ({
36
+ ...prev,
37
+ [source]: Math.max(
38
+ 0,
39
+ Math.min(
40
+ results[source].length - 4,
41
+ (prev[source] || 0) + direction * 4,
42
+ ),
43
+ ),
44
+ }));
45
+ };
46
+
47
+ const handleFetchExplanation = async (originalIndex) => {
48
+ if (explanationVisible[originalIndex]) {
49
+ setExplanationVisible((prev) => ({
50
+ ...prev,
51
+ [originalIndex]: false,
52
+ }));
53
+ return;
54
+ }
55
+
56
+ setExplanationVisible((prev) => ({
57
+ ...prev,
58
+ [originalIndex]: true,
59
+ }));
60
+
61
+ if (explanations[`${threadId}-${originalIndex}`]) return;
62
+
63
+ setLoadingExplanation((prev) => ({
64
+ ...prev,
65
+ [originalIndex]: true,
66
+ }));
67
+
68
+ try {
69
+ const response = await axios.get(
70
+ `http://localhost:8000/explain/${threadId}?docid=${originalIndex}`,
71
+ );
72
+ setExplanations((prev) => ({
73
+ ...prev,
74
+ [`${threadId}-${originalIndex}`]: response.data,
75
+ }));
76
+ } catch (error) {
77
+ console.error("Error fetching explanation:", error);
78
+ } finally {
79
+ setLoadingExplanation((prev) => ({
80
+ ...prev,
81
+ [originalIndex]: false,
82
+ }));
83
+ }
84
+ };
85
+
86
+ if (!results || Object.keys(results).length === 0) {
87
+ return (
88
+ <Typography variant="h6" sx={{ p: 2 }}>
89
+ No results found.
90
+ </Typography>
91
+ );
92
+ }
93
+
94
+ return (
95
+ <Box sx={{ p: 2 }}>
96
+ <Grid container spacing={3}>
97
+ {Object.keys(results).map((source, idx) => (
98
+ <Grid item xs={12} sm={6} md={6} key={idx}>
99
+ <Card
100
+ sx={{
101
+ borderRadius: 2,
102
+ boxShadow: 3,
103
+ overflow: "hidden",
104
+ display: "flex",
105
+ flexDirection: "column",
106
+ height: "auto",
107
+ bgcolor: "background.paper",
108
+ transition: "transform 0.2s, box-shadow 0.2s", // Smooth transition on hover
109
+ "&:hover": {
110
+ transform: "translateY(-4px)", // Lift card slightly on hover
111
+ boxShadow: 6,
112
+ },
113
+ }}
114
+ >
115
+ <CardContent
116
+ sx={{
117
+ display: "flex",
118
+ flexDirection: "column",
119
+ height: "100%",
120
+ }}
121
+ >
122
+ <Box
123
+ sx={{
124
+ display: "flex",
125
+ justifyContent: "space-between",
126
+ p: 1,
127
+ }}
128
+ >
129
+ <IconButton
130
+ onClick={() => handlePageChange(source, -1)}
131
+ disabled={(pageIndices[source] || 0) === 0}
132
+ sx={{
133
+ color: "text.secondary",
134
+ "&:hover": {
135
+ color: "text.primary",
136
+ },
137
+ }}
138
+ >
139
+ <ChevronLeftIcon />
140
+ </IconButton>
141
+ <Typography
142
+ variant="h6"
143
+ gutterBottom
144
+ sx={{
145
+ textAlign: "center",
146
+ mb: 0,
147
+ fontWeight: "bold",
148
+ fontSize: "1.2rem",
149
+ color: "text.secondary",
150
+ }}
151
+ >
152
+ {source}
153
+ </Typography>
154
+ <IconButton
155
+ onClick={() => handlePageChange(source, 1)}
156
+ disabled={
157
+ (pageIndices[source] || 0) + 4 >= results[source].length
158
+ }
159
+ sx={{
160
+ color: "text.secondary",
161
+ "&:hover": {
162
+ color: "text.primary",
163
+ },
164
+ }}
165
+ >
166
+ <ChevronRightIcon />
167
+ </IconButton>
168
+ </Box>
169
+ {results[source]
170
+ .slice(
171
+ pageIndices[source] || 0,
172
+ (pageIndices[source] || 0) + 4,
173
+ )
174
+ .map((document, index) => (
175
+ <Box key={index} sx={{ mb: 2 }}>
176
+ <Box
177
+ sx={{ display: "flex", alignItems: "center", mb: 1 }}
178
+ >
179
+ <Link
180
+ href={document.metadata.url}
181
+ target="_blank"
182
+ rel="noopener"
183
+ sx={{
184
+ textDecoration: "none",
185
+ flex: 1,
186
+ "&:hover": {
187
+ textDecoration: "underline",
188
+ },
189
+ }}
190
+ >
191
+ <Typography
192
+ variant="body1"
193
+ sx={{
194
+ fontSize: "0.9rem",
195
+ fontWeight: "bold",
196
+ overflow: "hidden",
197
+ textOverflow: "ellipsis",
198
+ color: "text.primary",
199
+ }}
200
+ >
201
+ {document.metadata.title || "Untitled"}
202
+ </Typography>
203
+ </Link>
204
+ <IconButton
205
+ onClick={() => handleToggleExpand(source, index)}
206
+ sx={{
207
+ ml: 1,
208
+ color: expandedDocs[`${source}-${index}`]
209
+ ? "primary.main"
210
+ : "text.primary",
211
+ fontSize: "1rem",
212
+ transition: "color 0.3s",
213
+ "&:hover": {
214
+ color: "primary.dark",
215
+ },
216
+ }}
217
+ >
218
+ <ExpandMoreIcon
219
+ sx={{
220
+ transform: expandedDocs[`${source}-${index}`]
221
+ ? "rotate(180deg)"
222
+ : "rotate(0deg)",
223
+ transition: "transform 0.3s",
224
+ }}
225
+ />
226
+ </IconButton>
227
+ <IconButton
228
+ onClick={() =>
229
+ handleFetchExplanation(document.originalIndex)
230
+ }
231
+ sx={{
232
+ ml: 1,
233
+ color: explanationVisible[document.originalIndex]
234
+ ? "error.main"
235
+ : "info.main",
236
+ fontSize: "1.2rem",
237
+ transition: "color 0.3s",
238
+ "&:hover": {
239
+ color: explanationVisible[document.originalIndex]
240
+ ? "error.dark"
241
+ : "info.dark",
242
+ },
243
+ }}
244
+ >
245
+ {explanationVisible[document.originalIndex] ? (
246
+ <CloseIcon />
247
+ ) : (
248
+ <HelpOutlineIcon />
249
+ )}
250
+ </IconButton>
251
+ </Box>
252
+ <Box
253
+ sx={{
254
+ maxHeight: expandedDocs[`${source}-${index}`]
255
+ ? "none"
256
+ : "5em",
257
+ overflow: "hidden",
258
+ transition: "max-height 0.3s ease",
259
+ position: "relative",
260
+ }}
261
+ >
262
+ <Typography
263
+ variant="body2"
264
+ sx={{
265
+ fontSize: "0.85rem",
266
+ color: "text.secondary",
267
+ backgroundColor: "background.default",
268
+ p: 2,
269
+ borderRadius: 1,
270
+ maxHeight: expandedDocs[`${source}-${index}`]
271
+ ? "none"
272
+ : "calc(1.25em * 10)",
273
+ overflow: "hidden",
274
+ textOverflow: "ellipsis",
275
+ whiteSpace: "normal",
276
+ scrollbarWidth: "none",
277
+ msOverflowStyle: "none",
278
+ "&::-webkit-scrollbar": {
279
+ display: "none",
280
+ },
281
+ textAlign: "justify",
282
+ lineHeight: "1.6",
283
+ }}
284
+ >
285
+ {/* Conditionally render page_content based on its starting text */}
286
+ {document.page_content?.startsWith("Dataset Title:")
287
+ ? document.page_content.slice(
288
+ document.metadata.title.length + 15,
289
+ 50000,
290
+ )
291
+ : document.page_content || "No preview available."}
292
+ </Typography>
293
+ </Box>
294
+ {/* Explanation Box */}
295
+ {explanationVisible[document.originalIndex] && (
296
+ <Box
297
+ sx={{
298
+ mt: 2,
299
+ p: 2,
300
+ borderRadius: 1,
301
+ bgcolor: "info.light",
302
+ color: "info.contrastText",
303
+ border: "1px solid",
304
+ borderColor: "info.dark",
305
+ overflowY: "auto",
306
+ textAlign: "justify",
307
+ scrollbarWidth: "none",
308
+ msOverflowStyle: "none",
309
+ "&::-webkit-scrollbar": {
310
+ display: "none",
311
+ },
312
+ position: "relative",
313
+ }}
314
+ >
315
+ <Typography
316
+ variant="subtitle2"
317
+ sx={{ mb: 1, fontWeight: "bold" }}
318
+ >
319
+ Explanation
320
+ </Typography>
321
+ {loadingExplanation[document.originalIndex] ? (
322
+ <Box
323
+ sx={{
324
+ display: "flex",
325
+ justifyContent: "center",
326
+ alignItems: "center",
327
+ height: "100%",
328
+ }}
329
+ >
330
+ <CircularProgress size={20} color="inherit" />
331
+ </Box>
332
+ ) : (
333
+ <Typography
334
+ variant="body2"
335
+ sx={{ fontSize: "0.85rem", lineHeight: "1.6" }}
336
+ >
337
+ {explanations[
338
+ `${threadId}-${document.originalIndex}`
339
+ ]?.generation || "No explanation available."}
340
+ </Typography>
341
+ )}
342
+ </Box>
343
+ )}
344
+ <Divider sx={{ my: 1 }} />
345
+ </Box>
346
+ ))}
347
+ </CardContent>
348
+ </Card>
349
+ </Grid>
350
+ ))}
351
+ </Grid>
352
+ </Box>
353
+ );
354
+ }
355
+
356
+ export default ResultsGrid;
frontend/src/components/SearchBar.js ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from 'react';
2
+ import { TextField, Button, Box } from '@mui/material';
3
+
4
+ const SearchBar = ({ onSearch }) => {
5
+ const [query, setQuery] = useState('');
6
+
7
+ const handleSearchClick = () => {
8
+ onSearch(query);
9
+ };
10
+
11
+ const handleKeyDown = (e) => {
12
+ if (e.key === 'Enter') {
13
+ handleSearchClick();
14
+ }
15
+ };
16
+
17
+ return (
18
+ <Box sx={{ display: 'flex', justifyContent: 'center', my: 4 }}>
19
+ <TextField
20
+ variant="outlined"
21
+ label="Search"
22
+ fullWidth
23
+ sx={{ maxWidth: '800px' }}
24
+ value={query}
25
+ onChange={(e) => setQuery(e.target.value)}
26
+ onKeyDown={handleKeyDown} // Add this line to handle Enter key press
27
+ />
28
+ <Button
29
+ variant="contained"
30
+ color="primary"
31
+ onClick={handleSearchClick}
32
+ sx={{ ml: 2 }}
33
+ >
34
+ Search
35
+ </Button>
36
+ </Box>
37
+ );
38
+ };
39
+
40
+ export default SearchBar;
frontend/src/components/SourceCard.js ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from "react";
2
+ import {
3
+ Card,
4
+ CardContent,
5
+ Typography,
6
+ Box,
7
+ IconButton,
8
+ Divider,
9
+ } from "@mui/material";
10
+ import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
11
+ import ChevronRightIcon from "@mui/icons-material/ChevronRight";
12
+ import DocumentItem from "./DocumentItem";
13
+
14
+ function SourceCard({ source, documents, threadId }) {
15
+ const [pageIndex, setPageIndex] = useState(0);
16
+ const itemsPerPage = 8;
17
+
18
+ const handlePageChange = (direction) => {
19
+ setPageIndex((prev) =>
20
+ Math.max(
21
+ 0,
22
+ Math.min(
23
+ documents.length - itemsPerPage,
24
+ prev + direction * itemsPerPage,
25
+ ),
26
+ ),
27
+ );
28
+ };
29
+
30
+ return (
31
+ <Card
32
+ sx={{
33
+ borderRadius: 0,
34
+ boxShadow: 0,
35
+ overflow: "hidden",
36
+ display: "flex",
37
+ flexDirection: "column",
38
+ background: "primary.main",
39
+ }}
40
+ >
41
+ <Divider />
42
+ <CardContent sx={{ p: 0 }}>
43
+ <Box
44
+ sx={{
45
+ display: "flex",
46
+ flexDirection: "column",
47
+ bgcolor: "background.paper",
48
+ }}
49
+ >
50
+ {documents
51
+ .slice(pageIndex, pageIndex + itemsPerPage)
52
+ .map((document, index) => (
53
+ <DocumentItem
54
+ key={`${document.metadata.id}-${pageIndex + index}`}
55
+ document={document}
56
+ threadId={threadId}
57
+ />
58
+ ))}
59
+ </Box>
60
+ </CardContent>
61
+ <Divider />
62
+ <Box
63
+ sx={{
64
+ p: 0,
65
+ bgcolor: "background.paper",
66
+ borderRadius: 0,
67
+ boxShadow: 0,
68
+ }}
69
+ >
70
+ <Box
71
+ sx={{
72
+ display: "flex",
73
+ justifyContent: "space-between",
74
+ alignItems: "center",
75
+ }}
76
+ >
77
+ <IconButton
78
+ onClick={() => handlePageChange(-1)}
79
+ disabled={pageIndex === 0}
80
+ size="small"
81
+ sx={{ color: "primary.main" }}
82
+ >
83
+ <ChevronLeftIcon />
84
+ </IconButton>
85
+ <Typography variant="caption" sx={{ color: "text.secondary" }}>
86
+ {`${pageIndex + 1} - ${Math.min(pageIndex + itemsPerPage, documents.length)} of ${documents.length}`}
87
+ </Typography>
88
+ <IconButton
89
+ onClick={() => handlePageChange(1)}
90
+ disabled={pageIndex + itemsPerPage >= documents.length}
91
+ size="small"
92
+ sx={{ color: "primary.main" }}
93
+ >
94
+ <ChevronRightIcon />
95
+ </IconButton>
96
+ </Box>
97
+ </Box>
98
+ </Card>
99
+ );
100
+ }
101
+
102
+ export default SourceCard;
frontend/src/index.css ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ margin: 0;
3
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4
+ 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5
+ sans-serif;
6
+ -webkit-font-smoothing: antialiased;
7
+ -moz-osx-font-smoothing: grayscale;
8
+ }
9
+
10
+ code {
11
+ font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12
+ monospace;
13
+ }
frontend/src/index.js ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import App from './App';
4
+ import { ThemeProvider, createTheme, CssBaseline } from '@mui/material';
5
+
6
+ const theme = createTheme({
7
+ palette: {
8
+ mode: 'dark', // Set to dark mode
9
+ primary: {
10
+ main: '#1976d2', // Default MUI primary color
11
+ },
12
+ secondary: {
13
+ main: '#dc004e', // Default MUI secondary color
14
+ },
15
+ background: {
16
+ default: '#121212', // Dark background
17
+ paper: '#1e1e1e', // Slightly lighter background for cards
18
+ },
19
+ text: {
20
+ primary: '#ffffff', // White text for primary
21
+ secondary: '#b0bec5', // Light grey for secondary text
22
+ },
23
+ },
24
+ typography: {
25
+ fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
26
+ h1: {
27
+ fontSize: '2rem',
28
+ fontWeight: 700,
29
+ },
30
+ h2: {
31
+ fontSize: '1.5rem',
32
+ fontWeight: 600,
33
+ },
34
+ body1: {
35
+ fontSize: '1rem',
36
+ lineHeight: 1.5,
37
+ },
38
+ body2: {
39
+ fontSize: '0.875rem',
40
+ lineHeight: 1.4,
41
+ },
42
+ },
43
+ shape: {
44
+ borderRadius: 8, // Rounded corners for cards and buttons
45
+ },
46
+ });
47
+
48
+ ReactDOM.render(
49
+ <ThemeProvider theme={theme}>
50
+ <CssBaseline />
51
+ <App />
52
+ </ThemeProvider>,
53
+ document.getElementById('root')
54
+ );
frontend/src/logo.svg ADDED
frontend/src/reportWebVitals.js ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const reportWebVitals = onPerfEntry => {
2
+ if (onPerfEntry && onPerfEntry instanceof Function) {
3
+ import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4
+ getCLS(onPerfEntry);
5
+ getFID(onPerfEntry);
6
+ getFCP(onPerfEntry);
7
+ getLCP(onPerfEntry);
8
+ getTTFB(onPerfEntry);
9
+ });
10
+ }
11
+ };
12
+
13
+ export default reportWebVitals;
frontend/src/setupTests.js ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ // jest-dom adds custom jest matchers for asserting on DOM nodes.
2
+ // allows you to do things like:
3
+ // expect(element).toHaveTextContent(/react/i)
4
+ // learn more: https://github.com/testing-library/jest-dom
5
+ import '@testing-library/jest-dom';
frontend/src/theme.js ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { createTheme } from '@mui/material/styles';
2
+
3
+ const theme = createTheme({
4
+ palette: {
5
+ mode: 'dark',
6
+ primary: {
7
+ main: '#BB86FC', // Light purple for primary elements
8
+ },
9
+ secondary: {
10
+ main: '#03DAC6', // Teal for secondary elements
11
+ },
12
+ background: {
13
+ default: '#121212', // Dark background
14
+ paper: '#1E1E1E', // Slightly lighter background for cards
15
+ },
16
+ text: {
17
+ primary: '#FFFFFF', // White text for primary
18
+ secondary: '#B0BEC5', // Light grey for secondary text
19
+ },
20
+ },
21
+ typography: {
22
+ fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
23
+ h1: {
24
+ fontSize: '2rem',
25
+ fontWeight: 700,
26
+ },
27
+ h2: {
28
+ fontSize: '1.5rem',
29
+ fontWeight: 600,
30
+ },
31
+ body1: {
32
+ fontSize: '1rem',
33
+ lineHeight: 1.5,
34
+ },
35
+ body2: {
36
+ fontSize: '0.875rem',
37
+ lineHeight: 1.4,
38
+ },
39
+ caption: {
40
+ fontSize: '0.75rem',
41
+ lineHeight: 1.2,
42
+ },
43
+ },
44
+ shape: {
45
+ borderRadius: 8, // Rounded corners for cards and buttons
46
+ },
47
+ });
48
+
49
+ export default theme;