Spaces:
Runtime error
Runtime error
File size: 3,776 Bytes
b68f453 |
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import Paper from "@mui/material/Paper";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import ListItemText from "@mui/material/ListItemText";
import ListItemIcon from "@mui/material/ListItemIcon";
import DescriptionIcon from '@mui/icons-material/Description';
import TextField from "@mui/material/TextField";
export const topicsSizeFraction = (topicsCentroids, topicSize) => {
const totalSize = topicsCentroids.reduce((sum, topic) => sum + topic.size, 0);
return Math.round((topicSize / totalSize) * 100);
}
function TextContainer({ topicName, topicSizeFraction, content }) {
const [selectedDocument, setSelectedDocument] = useState(null);
const [searchQuery, setSearchQuery] = useState("");
const [filteredContent, setFilteredContent] = useState([]);
useEffect(() => {
// Update the filtered content based on the search query
if (searchQuery.trim() === "") {
setFilteredContent(content);
} else {
const filtered = content.filter((doc) =>
doc.toLowerCase().includes(searchQuery.toLowerCase())
);
setFilteredContent(filtered);
}
// Automatically select the first document when search results change
setSelectedDocument(0);
}, [searchQuery, content]);
const handleDocumentClick = (docIndex) => {
setSelectedDocument(docIndex);
};
return (
<div id="topic-box-container">
<Box className="topic-box">
<Box
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
background: "rgb(94, 163, 252)",
padding: "8px",
color: "white",
textAlign: "center",
borderRadius: "20px", // Make it rounder
margin: "0 auto", // Center horizontally
width: "80%", // Adjust the width as needed
}}
>
<Typography variant="h6" style={{ marginBottom: "8px" }}>
{topicName}
</Typography>
</Box>
<Typography
variant="h5"
style={{
marginBottom: "20px",
marginTop: "20px",
textAlign: "center",
}}
>
{topicSizeFraction}% of the Territory
</Typography>
<TextField
label="Search"
variant="outlined"
fullWidth
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
/>
<Paper elevation={3} style={{ maxHeight: "70vh", overflowY: "auto" }}>
<List>
{filteredContent.map((doc, index) => (
<ListItem
button
key={`textcontainerdoc-${index}`}
onClick={() => handleDocumentClick(index)}
selected={selectedDocument === index}
>
<ListItemIcon>
<DescriptionIcon /> {/* Display a document icon */}
</ListItemIcon>
<ListItemText primary={<span style={{ fontSize: "14px" }}>{doc}</span>} />
</ListItem>
))}
</List>
</Paper>
<Box
style={{
marginTop: "20px",
padding: "8px",
width: "80%", // Adjust the width as needed
margin: "0 auto", // Center horizontally
}}
>
</Box>
</Box>
</div>
);
}
TextContainer.propTypes = {
topicName: PropTypes.string.isRequired,
topicSizeFraction: PropTypes.number.isRequired,
content: PropTypes.array.isRequired,
};
export default TextContainer; |