import React, { useCallback, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, DataTable, DataTableSkeleton, InlineLoading, Table, TableBody, TableCell, TableContainer, TableExpandedRow, TableExpandHeader, TableExpandRow, TableHead, TableHeader, TableRow, Tile, } from '@carbon/react'; import { Add } from '@carbon/react/icons'; import { formatDate, isDesktop as isDesktopLayout, launchWorkspace2, parseDate, useConfig, useLayoutType, CardHeader, EmptyCard, ErrorState, formatPartialDate, } from '@openmrs/esm-framework'; import { PatientChartPagination } from '@openmrs/esm-patient-common-lib'; import type { ConfigObject } from '../../config-schema'; import { useProcedures } from '../../procedures.resource'; import { ProceduresActionMenu } from '../action-menu/procedures-action-menu.component'; import styles from './procedures-detailed-summary.scss'; type ProceduresDetailedSummaryProps = { patient: fhir.Patient; }; type ProcedureTableRow = { id: string; display: string; procedureType: string; bodySite: string; startDateTimeRender: string; estimatedStartDate?: string; endDateTimeRender: string; status: string; notes?: string; }; const ProceduresDetailedSummary = ({ patient }: ProceduresDetailedSummaryProps) => { const { t } = useTranslation(); const { detailedViewPageSize } = useConfig(); const headerTitle = t('procedures', 'Procedures'); const displayText = t('procedures_lower', 'procedures'); const layout = useLayoutType(); const isDesktop = isDesktopLayout(layout); const launchProceduresForm = useCallback( () => launchWorkspace2('procedures-form-workspace', { formContext: 'creating' }), [], ); const [currentPage, setCurrentPage] = useState(1); const startIndex = (currentPage - 1) * detailedViewPageSize; const { procedures, totalCount, error, isLoading, isValidating } = useProcedures( patient.id, startIndex, detailedViewPageSize, ); const headers = useMemo( () => [ { key: 'display', header: t('procedure', 'Procedure') }, { key: 'procedureType', header: t('procedureType', 'Procedure type') }, { key: 'bodySite', header: t('bodySite', 'Body site') }, { key: 'startDateTimeRender', header: t('startDate', 'Start date') }, { key: 'endDateTimeRender', header: t('endDate', 'End date') }, { key: 'status', header: t('status', 'Status') }, ], [t], ); const tableRows: ProcedureTableRow[] = useMemo( () => procedures?.map((p) => ({ id: p.uuid, display: p.display, procedureType: p.procedureType.name, bodySite: p.bodySite.display ?? '--', startDateTimeRender: p.estimatedStartDate ?? p.startDateTime, estimatedStartDate: p.estimatedStartDate, endDateTimeRender: p.endDateTime ? formatDate(parseDate(p.endDateTime), { mode: 'wide' }) : '--', status: p.status.display, notes: p.notes, })), [procedures], ); if (isLoading) { return ; } if (error) { return ; } if (totalCount > 0 || procedures?.length) { return (
{isValidating ? : null} {({ rows, headers: carbonHeaders, getRowProps, getExpandedRowProps, getHeaderProps, getTableProps }) => ( <> {carbonHeaders.map((header) => ( {header.header} ))} {rows.map((row) => { const matchingRow = tableRows?.find((r) => r.id === row.id); const matchingProcedure = procedures?.find((p) => p.uuid === row.id); return ( {row.cells.map((cell) => { if (cell.info.header === 'startDateTimeRender') { const display = matchingRow?.estimatedStartDate ? `${formatPartialDate(matchingRow.estimatedStartDate, { mode: 'wide' })}` : formatDate(parseDate(cell.value), { mode: 'wide', time: true }); return {display}; } return {cell.value}; })}

{t('duration', 'Duration')}: {matchingProcedure?.duration ? `${matchingProcedure.duration} ${matchingProcedure.durationUnit?.display ?? ''}` : '--'}

{t('notes', 'Notes')}: {matchingRow?.notes ?? '--'}

); })}
{rows.length === 0 ? (

{t('noProceduresToDisplay', 'No procedures to display')}

) : null} setCurrentPage(page)} /> )}
); } return ; }; export default ProceduresDetailedSummary;