|
| 1 | +import React, { useState } from 'react' |
| 2 | +import Constants from 'common/constants' |
| 3 | +import Collapse from '@material-ui/core/Collapse' |
| 4 | +import { IonIcon } from '@ionic/react' |
| 5 | +import { chevronDown, chevronUp } from 'ionicons/icons' |
| 6 | +import { FeatureHealthEventReasonTextBlock } from 'common/types/responses' |
| 7 | + |
| 8 | +interface EventTextBlocksProps { |
| 9 | + textBlocks: FeatureHealthEventReasonTextBlock[] | undefined |
| 10 | +} |
| 11 | + |
| 12 | +const EventTextBlocks: React.FC<EventTextBlocksProps> = ({ textBlocks }) => { |
| 13 | + // Index is used here only because the data is read only. |
| 14 | + // Backend sorts created_at in descending order. |
| 15 | + const initialValue = |
| 16 | + textBlocks?.map((_, index) => ({ collapsed: index !== 0, id: index })) ?? [] |
| 17 | + const [collapsibleItems, setCollapsibleItems] = |
| 18 | + useState<{ id: number; collapsed: boolean }[]>(initialValue) |
| 19 | + |
| 20 | + const handleCollapse = (index: number) => { |
| 21 | + if (!collapsibleItems?.[index]) { |
| 22 | + return null |
| 23 | + } |
| 24 | + |
| 25 | + setCollapsibleItems((prev) => { |
| 26 | + const updatedItems = [...prev] |
| 27 | + updatedItems[index].collapsed = !updatedItems?.[index]?.collapsed |
| 28 | + return updatedItems |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + const color = Constants.featureHealth.unhealthyColor |
| 33 | + |
| 34 | + if (!textBlocks?.length) { |
| 35 | + return <></> |
| 36 | + } |
| 37 | + |
| 38 | + return ( |
| 39 | + <div className='d-flex flex-column m-0 gap-2 flex-1'> |
| 40 | + <strong className='text-body'>Incident Insights</strong> |
| 41 | + {textBlocks.map((textBlock, index) => ( |
| 42 | + <div key={`${textBlock.text}-${index}`}> |
| 43 | + {textBlock.title && ( |
| 44 | + <div className='mb-2 text-body'> |
| 45 | + <strong style={{ color }}>{textBlock.title ?? 'Event'}</strong> |
| 46 | + {!!textBlock.text && ( |
| 47 | + <IonIcon |
| 48 | + style={{ color, marginBottom: -2 }} |
| 49 | + className='ms-1' |
| 50 | + icon={ |
| 51 | + collapsibleItems?.[index]?.collapsed |
| 52 | + ? chevronDown |
| 53 | + : chevronUp |
| 54 | + } |
| 55 | + onClick={() => handleCollapse(index)} |
| 56 | + /> |
| 57 | + )} |
| 58 | + </div> |
| 59 | + )} |
| 60 | + <Collapse key={index} in={!collapsibleItems?.[index]?.collapsed}> |
| 61 | + {textBlock.text} |
| 62 | + </Collapse> |
| 63 | + </div> |
| 64 | + ))} |
| 65 | + </div> |
| 66 | + ) |
| 67 | +} |
| 68 | +export default EventTextBlocks |
0 commit comments