import React, { useMemo } from 'react'; import classNames from 'classnames'; import { DataTable, TableContainer, Table, TableHead, TableRow, TableHeader, TableBody, TableCell, Layer, } from '@carbon/react'; import { useTranslation } from 'react-i18next'; import { formatDate, isDesktop, parseDate, useLayoutType } from '@openmrs/esm-framework'; import { formatRangeWithUnits } from '../grouped-timeline/reference-range-helpers'; import { formatResultDate } from '../helpers'; import { getClass } from './helper'; import type { GroupedObservation } from '../../types'; import styles from './lab-set-panel.scss'; interface LabSetPanelProps { panel: GroupedObservation; activePanel: GroupedObservation; setActivePanel: React.Dispatch>; } const LabSetPanel: React.FC = ({ panel, activePanel, setActivePanel }) => { const { t } = useTranslation(); const date = panel.entries[0]?.obsDatetime ? parseDate(panel.entries[0].obsDatetime) : new Date(panel.date); const layout = useLayoutType(); const getColumnClass = (columnKey: string) => styles[`col-${columnKey}`]; const hasRange = panel.entries.some((entry) => entry.range); const headers = useMemo( () => hasRange ? [ { id: 'testName', key: 'testName', header: t('testName', 'Test name'), }, { id: 'resultDate', key: 'resultDate', header: t('resultDate', 'Result date') }, { id: 'value', key: 'value', header: t('value', 'Value'), }, { id: 'referenceRange', key: 'referenceRange', header: t('referenceRange', 'Reference range'), }, ] : [ { id: 'testName', key: 'testName', header: t('testName', 'Test name'), }, { id: 'resultDate', key: 'resultDate', header: t('resultDate', 'Result date') }, { id: 'value', key: 'value', header: t('value', 'Value'), }, ], [t, hasRange], ); const rowsData = useMemo( () => hasRange ? panel.entries.map((test) => { const units = test.units ?? ''; const range = formatRangeWithUnits(test.range, units); return { id: test.conceptUuid, testName: test.display, resultDate: formatResultDate(test.obsDatetime), value: { content: {`${test.value} ${units}`}, }, interpretation: test.interpretation, referenceRange: range, }; }) : panel.entries.map((test) => { const units = test.units ?? ''; return { id: test.conceptUuid, testName: test.display, resultDate: formatResultDate(test.obsDatetime), value: { content: {`${test.value} ${units}`}, }, interpretation: test.interpretation, }; }), [panel, hasRange], ); return (
setActivePanel(panel)} role="button" tabIndex={0}>

{panel.key}

{formatDate(date, { mode: 'wide', time: true, })}

{({ rows, headers, getHeaderProps, getTableProps }) => ( {headers.map((header) => { const headerProps = getHeaderProps({ header }); return ( {header.header} ); })} {rows.map((row, indx) => ( {row.cells.map((cell) => ( {cell?.value?.content ?? cell.value} ))} ))}
)}
); }; export default LabSetPanel;