Advertisement
VladNitu

chatMiriPromptSharedState

Jun 1st, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. I have this React component that displays a list of the 5 most similar articles with my similar article:
  2.  
  3. import ProgressLineCustom from './ProgressLineCustom'
  4. import { Button, Container, Row, Col, Card, Accordion } from 'react-bootstrap'
  5. import SideBySideRender from './SideBySideRender'
  6. import { useState, useEffect } from 'react'
  7.  
  8. /**
  9. * Displays a list of articles (title, publisher, date) together with their similarities.
  10. * If the width of the page on which the component is displayed is smaller or equal than 992, the list items will
  11. * be displayed in an Accordion expandable component.
  12. * @param sourceUrl the url of the input article used for the side-by-side rendering
  13. * @param articles the list of similar articles to be displayed
  14. * @returns {JSX.Element} the element that contains the list
  15. */
  16.  
  17. export default function ListURLs ({ sourceUrl, articles }) {
  18. const [showModal, setShowModal] = useState(false)
  19. const handleClose = () => setShowModal(false)
  20. const handleShow = () => setShowModal(true)
  21.  
  22. const [width, setWidth] = useState(window.innerWidth)
  23.  
  24. /**
  25. * Update the width on resizing
  26. */
  27. const updateDimensions = () => {
  28. setWidth(window.innerWidth)
  29. }
  30.  
  31. /**
  32. * When the size of the windows are changed call the updateDimensions method
  33. */
  34. useEffect(() => {
  35. window.addEventListener('resize', updateDimensions)
  36. return () => window.removeEventListener('resize', updateDimensions)
  37. }, [])
  38.  
  39. if (articles.length === 0) {
  40. return <p>No articles were found</p>
  41. } else {
  42. return (
  43.  
  44. <div>
  45. <Row>
  46. {width >= 992
  47. ? articles.map((article, index) => (
  48. <Col xs={12} className='mb-3' key={index}>
  49. <Card className='d-flex flex-row'>
  50. <Container fluid className='d-flex flex-row my-3'>
  51. <div className='pe-3 title-wrapper'>
  52. <div style={{ fontWeight: 'bold' }}>Title</div>
  53. <a href={article.url} target='_blank' rel='noopener noreferrer' className='forLinks'>
  54. {article.title}
  55. </a>
  56. </div>
  57. <div className='pe-3 publisher-wrapper'>
  58. <div style={{ fontWeight: 'bold' }}>Publisher</div>
  59. {/* {prefix the site with // if it does not already include it} */}
  60. <a href={article.publisher.startsWith('http://') || article.publisher.startsWith('https://') ? article.publisher : `//${article.publisher}`} target='_blank' rel='noopener noreferrer' className='forLinks'>
  61. {article.publisher}
  62. </a>
  63. </div>
  64. <div className='pe-3'>
  65. <div style={{ fontWeight: 'bold' }}>Published on</div>
  66. <div>{article.date}</div>
  67. </div>
  68. <div className='pe-3 my-auto ms-auto'>
  69. <ProgressLineCustom progress={article.similarity} />
  70. </div>
  71. <div className='ms-auto my-auto'>
  72. {/* Render button */}
  73. <Button className='mx-auto custom-outline-button' variant='outline-success' onClick={handleShow}>Compare</Button>
  74. {/* Render SideBySideRender component */}
  75. <SideBySideRender urlLeft={sourceUrl} urlRight={article.url} showModal={showModal} handleClose={handleClose} />
  76. </div>
  77. </Container>
  78. </Card>
  79. </Col>
  80. ))
  81. : (
  82. <Accordion alwaysOpen>
  83. {articles.map((article, index) => (
  84. <Accordion.Item eventKey={index} key={index} style={{ marginBottom: '20px' }}>
  85. <Accordion.Header className='d-flex flex-row'>
  86. <div className='pe-3 title-wrapper'>
  87. <div style={{ fontWeight: 'bold' }}>Title</div>
  88. <a href={article.url} target='_blank' rel='noopener noreferrer' className='forLinks'>
  89. {article.title}
  90. </a>
  91. </div>
  92. </Accordion.Header>
  93. {/* <Card style={{ marginBottom: '10px' }}> */}
  94. <Accordion.Body>
  95. <div className='pe-3 mb-2' style={{ display: 'flex' }}>
  96. <div style={{ fontWeight: 'bold', width: '50%' }}>Publisher</div>
  97. {/* {prefix the site with // if it does not already include it} */}
  98. <a href={article.publisher.startsWith('http://') || article.publisher.startsWith('https://') ? article.publisher : `//${article.publisher}`} target='_blank' rel='noopener noreferrer' className='forLinks'>
  99. {article.publisher}
  100. </a>
  101. </div>
  102. <div className='pe-3 mb-4' style={{ display: 'flex' }}>
  103. <div style={{ fontWeight: 'bold', width: '50%' }}>Published on</div>
  104. <div>{article.date}</div>
  105. </div>
  106. <div className='my-auto mb-3'>
  107. <ProgressLineCustom progress={article.similarity} />
  108. </div>
  109. <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  110. {/* Render button */}
  111. <Button className='mx-auto custom-outline-button' variant='outline-success' onClick={handleShow}>Compare</Button>
  112. {/* Render SideBySideRender component */}
  113. <SideBySideRender urlLeft={sourceUrl} urlRight={article.url} showModal={showModal} handleClose={handleClose} />
  114. </div>
  115. </Accordion.Body>
  116. {/* </Card> */}
  117.  
  118. </Accordion.Item>
  119. ))}
  120. </Accordion>)}
  121. </Row>
  122. </div>
  123. )
  124. }
  125. }
  126.  
  127.  
  128. However, when I press Compare, the SideBySide render component is used:
  129.  
  130. import { Row, Col, Container, Modal, ModalBody, ModalHeader } from 'react-bootstrap'
  131. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  132. import { faChevronLeft } from '@fortawesome/free-solid-svg-icons'
  133. import IframeRenderer from './IframeRenderer'
  134. import { useState } from 'react'
  135.  
  136. /**
  137. * SideBySideRender is a component for displaying two iframes side by side.
  138. *
  139. * @component
  140. * @param {Object} props - The properties passed to the component.
  141. * @param {string} props.urlLeft - The URL to load in the left iframe.
  142. * @param {string} props.urlRight - The URL to load in the right iframe.
  143. * @param {boolean} props.showModal - Controls the visibility of the modal.
  144. * @param {function} props.handleClose - Function to call when the modal needs to be closed.
  145. *
  146. * @returns {JSX.Element} The SideBySideRender component.
  147. */
  148. export default function SideBySideRender ({ urlLeft, urlRight, showModal, handleClose }) {
  149. const [backgroundColorLeft, setBackgroundColorLeft] = useState('#fff')
  150. const [backgroundColorRight, setBackgroundColorRight] = useState('#fff')
  151.  
  152. return (
  153. <Modal show={showModal} onHide={handleClose} fullscreen>
  154. <ModalHeader>
  155. <a title='close_button' onClick={() => { handleClose(); setBackgroundColorLeft('#fff') }} className='custom_cursor'>
  156. <FontAwesomeIcon icon={faChevronLeft} className='pe-1' />
  157. <span>Go back</span>
  158. </a>
  159. </ModalHeader>
  160. <ModalBody style={{ padding: 0 }}>
  161. <div title='wrapper' className='d-flex flex-column' style={{ height: '100%', backgroundColor: backgroundColorLeft === backgroundColorRight && backgroundColorLeft === '#000' ? '#000' : '#fff' }}>
  162. <Container fluid style={{ height: '100%' }}>
  163. <Row style={{ height: '100%' }}>
  164. {/* px-0 pe-sm-2: no x-axis padding on 'xs' devices and from 'sm' to 'xl' devices add 1rem padding-right. */}
  165. {/* pb-2 pb-sm-0: padding-bottom on 'xs' devices and from 'sm' to 'xl' devices remove padding bottom (required splitting page horizontally on small devices). */}
  166. {/* custom-iframe-height: on small devices, set height of each frame to 50%, while on the other screens set it to 100%. */}
  167. <Col sm={6} className='px-0 pe-sm-2 pb-2 pb-sm-0 custom-iframe-height' id='left_article'>
  168. <IframeRenderer url={urlLeft} id='left_article' changeBackground={() => setBackgroundColorLeft('#000')} />
  169. </Col>
  170. <Col sm={6} className='px-0 ps-sm-2 pt-2 pt-sm-0 custom-iframe-height' id='right_article'>
  171. <IframeRenderer url={urlRight} id='right_article' changeBackground={() => setBackgroundColorRight('#000')} />
  172. </Col>
  173. </Row>
  174. </Container>
  175. </div>
  176. </ModalBody>
  177. </Modal>
  178. )
  179. }
  180.  
  181.  
  182. Expected behavior: compare sourceUrl with the URL's specific compare button
  183. Obtained behavior: comparison is always done between the sourceUrl and THE LAST SIMILAR URL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement