|
| 1 | +import React, { |
| 2 | + memo, |
| 3 | + useEffect, |
| 4 | + useState, |
| 5 | +} from "react"; |
| 6 | + |
| 7 | +import { |
| 8 | + Accordion, |
| 9 | + AccordionDetails, |
| 10 | + AccordionSummary, |
| 11 | + Box, |
| 12 | + Chip, |
| 13 | + List, |
| 14 | + Stack, |
| 15 | + Typography, |
| 16 | +} from "@mui/joy"; |
| 17 | + |
| 18 | +import DescriptionOutlinedIcon from "@mui/icons-material/DescriptionOutlined"; |
| 19 | + |
| 20 | +import {QueryResultsType} from "../../../../../typings/worker"; |
| 21 | +import Result from "./Result"; |
| 22 | + |
| 23 | +import "./ResultsGroup.css"; |
| 24 | + |
| 25 | + |
| 26 | +interface ResultsGroupProps { |
| 27 | + isAllExpanded: boolean, |
| 28 | + pageNum: number, |
| 29 | + results: QueryResultsType[], |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Renders a group of results, where each group represents a list of results from a single page. |
| 34 | + * |
| 35 | + * @param props |
| 36 | + * @param props.isAllExpanded |
| 37 | + * @param props.pageNum |
| 38 | + * @param props.results |
| 39 | + * @return |
| 40 | + */ |
| 41 | +const ResultsGroup = memo(({ |
| 42 | + isAllExpanded, |
| 43 | + pageNum, |
| 44 | + results, |
| 45 | +}: ResultsGroupProps) => { |
| 46 | + const [isExpanded, setIsExpanded] = useState<boolean>(isAllExpanded); |
| 47 | + |
| 48 | + const handleAccordionChange = ( |
| 49 | + _: React.SyntheticEvent, |
| 50 | + newValue: boolean |
| 51 | + ) => { |
| 52 | + setIsExpanded(newValue); |
| 53 | + }; |
| 54 | + |
| 55 | + // On `isAllExpanded` update, sync current results group's expand status. |
| 56 | + useEffect(() => { |
| 57 | + setIsExpanded(isAllExpanded); |
| 58 | + }, [isAllExpanded]); |
| 59 | + |
| 60 | + return ( |
| 61 | + <Accordion |
| 62 | + expanded={isExpanded} |
| 63 | + onChange={handleAccordionChange} |
| 64 | + > |
| 65 | + <AccordionSummary |
| 66 | + slotProps={{ |
| 67 | + button: {className: "results-group-summary-button"}, |
| 68 | + }} |
| 69 | + > |
| 70 | + <Box className={"results-group-summary-container"}> |
| 71 | + <Stack |
| 72 | + className={"results-group-summary-text-container"} |
| 73 | + direction={"row"} |
| 74 | + > |
| 75 | + <DescriptionOutlinedIcon fontSize={"inherit"}/> |
| 76 | + <Typography |
| 77 | + fontFamily={"Inter"} |
| 78 | + level={"title-sm"} |
| 79 | + > |
| 80 | + {"Page "} |
| 81 | + {pageNum} |
| 82 | + </Typography> |
| 83 | + </Stack> |
| 84 | + <Chip |
| 85 | + className={"results-group-summary-count"} |
| 86 | + size={"sm"} |
| 87 | + variant={"solid"} |
| 88 | + > |
| 89 | + {results.length} |
| 90 | + </Chip> |
| 91 | + </Box> |
| 92 | + </AccordionSummary> |
| 93 | + <AccordionDetails |
| 94 | + className={"results-group-details"} |
| 95 | + slotProps={{content: {className: "results-group-details-content"}}} |
| 96 | + > |
| 97 | + <List size={"sm"}> |
| 98 | + {results.map((r, index) => ( |
| 99 | + <Result |
| 100 | + key={index} |
| 101 | + logEventNum={r.logEventNum} |
| 102 | + matchRange={r.matchRange} |
| 103 | + message={r.message}/> |
| 104 | + ))} |
| 105 | + </List> |
| 106 | + </AccordionDetails> |
| 107 | + </Accordion> |
| 108 | + ); |
| 109 | +}); |
| 110 | + |
| 111 | +ResultsGroup.displayName = "ResultsGroup"; |
| 112 | + |
| 113 | + |
| 114 | +export default ResultsGroup; |
0 commit comments