|  | 
|  | 1 | +import React, { useState, useEffect } from 'react' | 
|  | 2 | +import { styled } from '@mui/material/styles' | 
|  | 3 | +import { useParams } from 'react-router' | 
|  | 4 | +import PropTypes from 'prop-types' | 
|  | 5 | +import ArrowForwardIosSharpIcon from '@mui/icons-material/ArrowForwardIosSharp' | 
|  | 6 | +import MuiAccordion from '@mui/material/Accordion' | 
|  | 7 | +import MuiAccordionSummary from '@mui/material/AccordionSummary' | 
|  | 8 | +import MuiAccordionDetails from '@mui/material/AccordionDetails' | 
|  | 9 | +import Typography from '@mui/material/Typography' | 
|  | 10 | + | 
|  | 11 | +const ReadMore = ({ id, text, amountOfWords = 36 }) => { | 
|  | 12 | +  const [isExpanded, setIsExpanded] = useState(false) | 
|  | 13 | +  const splittedText = text.split(' ') | 
|  | 14 | +  const itCanOverflow = splittedText.length > amountOfWords | 
|  | 15 | +  const beginText = itCanOverflow | 
|  | 16 | +    ? splittedText.slice(0, amountOfWords - 1).join(' ') | 
|  | 17 | +    : text | 
|  | 18 | +  const endText = splittedText.slice(amountOfWords - 1).join(' ') | 
|  | 19 | +   | 
|  | 20 | +  const handleKeyboard = (e) => { | 
|  | 21 | +    if (e.code === 'Space' || e.code === 'Enter') { | 
|  | 22 | +      setIsExpanded(!isExpanded) | 
|  | 23 | +    } | 
|  | 24 | +  } | 
|  | 25 | + | 
|  | 26 | +  return ( | 
|  | 27 | +    <span id={id}> | 
|  | 28 | +      {beginText} | 
|  | 29 | +      {itCanOverflow && ( | 
|  | 30 | +        <> | 
|  | 31 | +          {!isExpanded && <span>... </span>} | 
|  | 32 | +          <span  | 
|  | 33 | +            className={`${!isExpanded && 'hidden'}`}  | 
|  | 34 | +            aria-hidden={!isExpanded} | 
|  | 35 | +          > | 
|  | 36 | +            {endText} | 
|  | 37 | +          </span> | 
|  | 38 | +          <span | 
|  | 39 | +            className='text-violet-400 ml-2' | 
|  | 40 | +            role="button" | 
|  | 41 | +            tabIndex={0} | 
|  | 42 | +            aria-expanded={isExpanded} | 
|  | 43 | +            aria-controls={id} | 
|  | 44 | +            onKeyDown={handleKeyboard} | 
|  | 45 | +            onClick={() => setIsExpanded(!isExpanded)} | 
|  | 46 | +          > | 
|  | 47 | +            {isExpanded ? 'show less' : 'show more'} | 
|  | 48 | +          </span> | 
|  | 49 | +        </> | 
|  | 50 | +      )} | 
|  | 51 | +    </span> | 
|  | 52 | +  ) | 
|  | 53 | +} | 
|  | 54 | + | 
|  | 55 | +ReadMore.propTypes = { | 
|  | 56 | +  id: PropTypes.string, | 
|  | 57 | +  text: PropTypes.string, | 
|  | 58 | +  amountOfWords: PropTypes.number | 
|  | 59 | +} | 
|  | 60 | + | 
|  | 61 | +const Accordion = styled((props) => ( | 
|  | 62 | +  <MuiAccordion disableGutters elevation={0} square {...props} /> | 
|  | 63 | +))(({ theme }) => ({ | 
|  | 64 | +  border: `1px solid ${theme.palette.divider}`, | 
|  | 65 | +  '&:not(:last-child)': { | 
|  | 66 | +    borderBottom: 0, | 
|  | 67 | +  }, | 
|  | 68 | +  '&::before': { | 
|  | 69 | +    display: 'none', | 
|  | 70 | +  }, | 
|  | 71 | +})); | 
|  | 72 | + | 
|  | 73 | +const AccordionSummary = styled((props) => ( | 
|  | 74 | +  <MuiAccordionSummary | 
|  | 75 | +    expandIcon={<ArrowForwardIosSharpIcon sx={{ fontSize: '0.9rem' }} />} | 
|  | 76 | +    {...props} | 
|  | 77 | +  /> | 
|  | 78 | +))(({ theme }) => ({ | 
|  | 79 | +  backgroundColor: | 
|  | 80 | +    theme.palette.mode === 'dark' | 
|  | 81 | +      ? 'rgba(255, 255, 255, .05)' | 
|  | 82 | +      : 'rgba(0, 0, 0, .03)', | 
|  | 83 | +  flexDirection: 'row-reverse', | 
|  | 84 | +  '& .MuiAccordionSummary-expandIconWrapper.Mui-expanded': { | 
|  | 85 | +    transform: 'rotate(90deg)', | 
|  | 86 | +  }, | 
|  | 87 | +  '& .MuiAccordionSummary-content': { | 
|  | 88 | +    marginLeft: theme.spacing(1), | 
|  | 89 | +  }, | 
|  | 90 | +})); | 
|  | 91 | + | 
|  | 92 | +const AccordionDetails = styled(MuiAccordionDetails)(({ theme }) => ({ | 
|  | 93 | +  padding: theme.spacing(2), | 
|  | 94 | +  borderTop: '1px solid rgba(0, 0, 0, .125)', | 
|  | 95 | +})); | 
|  | 96 | + | 
|  | 97 | + | 
|  | 98 | +const ShowVEXPage = () => { | 
|  | 99 | +  const params = useParams() | 
|  | 100 | +  const [vex, set_vex] = useState([]) | 
|  | 101 | +  const [statements, set_statments] = useState([]) | 
|  | 102 | +  const [expanded, setExpanded] = useState(''); | 
|  | 103 | + | 
|  | 104 | +  const handleChange = (panel) => (event, newExpanded) => { | 
|  | 105 | +    setExpanded(newExpanded ? panel : false); | 
|  | 106 | +  }; | 
|  | 107 | + | 
|  | 108 | +  useEffect(() => { | 
|  | 109 | +    const access_token = localStorage.getItem('access_token') | 
|  | 110 | +    fetch('http://localhost:8000/vex/show/' + params.id, { | 
|  | 111 | +      method: 'GET', | 
|  | 112 | +      headers: { | 
|  | 113 | +        'Content-Type': 'application/json', | 
|  | 114 | +        Authorization: `Bearer ${access_token}` | 
|  | 115 | +      } | 
|  | 116 | +    }) | 
|  | 117 | +      .then((r) => r.json()) | 
|  | 118 | +      .then((r) => { | 
|  | 119 | +        set_vex(r) | 
|  | 120 | +        set_statments(r.vex.statements) | 
|  | 121 | +      }) | 
|  | 122 | +  }, [params.id]) | 
|  | 123 | + | 
|  | 124 | +  return ( | 
|  | 125 | +    <div className='flex flex-col h-screen justify-center items-center m-auto w-8/12'> | 
|  | 126 | +      <p className='mb-6 text-lg font-normal text-gray-500 lg:text-xl sm:px-16 xl:px-48 dark:text-gray-400 text-center'> | 
|  | 127 | +        {vex.owner}/{vex.name}: {vex.sbom_path} | 
|  | 128 | +      </p> | 
|  | 129 | +      {statements.map((statement, index) => ( | 
|  | 130 | +        <Accordion className='w-full' key={index} expanded={expanded === 'panel'+index} onChange={handleChange('panel'+index)}> | 
|  | 131 | +          <AccordionSummary aria-controls="panel1d-content" id="panel1d-header"> | 
|  | 132 | +            <Typography>Statement {index}</Typography> | 
|  | 133 | +          </AccordionSummary> | 
|  | 134 | +          <AccordionDetails> | 
|  | 135 | +            <Typography> | 
|  | 136 | +              <span>- Vulnerability:<br /></span> | 
|  | 137 | +              <span>   {'\u2022'} @id: {statement.vulnerability["@id"]}<br /></span> | 
|  | 138 | +              <span>   {'\u2022'} name: {statement.vulnerability.name}<br /></span>  | 
|  | 139 | +              <span>   {'\u2022'} description: <ReadMore id="read-more-text" text={statement.vulnerability.description} /><br /></span> | 
|  | 140 | +              <span>- Timestamp: {statement.timestamp}<br /></span> | 
|  | 141 | +              <span>- Last Updated: {statement.last_updated}<br /></span> | 
|  | 142 | +              <span>- Status: {statement.status}<br /></span> | 
|  | 143 | +              <span>- Justification: {statement.justification}<br /></span> | 
|  | 144 | +              <span>- Supplier: {statement.supplier}</span> | 
|  | 145 | +            </Typography> | 
|  | 146 | +          </AccordionDetails> | 
|  | 147 | +        </Accordion> | 
|  | 148 | +      ))} | 
|  | 149 | +    </div> | 
|  | 150 | +  ) | 
|  | 151 | +} | 
|  | 152 | + | 
|  | 153 | +export { ShowVEXPage } | 
0 commit comments