import React, { useCallback, useMemo, useState } from 'react'; import fuzzy from 'fuzzy'; import { useTranslation } from 'react-i18next'; import { DataTable, DataTableSkeleton, Layer, Link, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, TableToolbar, TableToolbarContent, TableToolbarSearch, Tile, } from '@carbon/react'; import { EmptyDataIllustration, type PatientWorkspace2DefinitionProps } from '@openmrs/esm-patient-common-lib'; import { launchWorkspace, useLayoutType, Workspace2 } from '@openmrs/esm-framework'; import { usePatientLists } from '../patient-lists.resource'; import styles from './patient-lists.scss'; function PatientListsWorkspace({ launchChildWorkspace }: PatientWorkspace2DefinitionProps) { const { t } = useTranslation(); const layout = useLayoutType(); const responsiveSize = layout === 'tablet' ? 'lg' : 'sm'; const [searchTerm, setSearchTerm] = useState(''); const { patientLists, isLoading } = usePatientLists(); const launchListDetailsWorkspace = useCallback( (list) => { launchChildWorkspace('patient-list-details', { list }); }, [launchChildWorkspace], ); const tableHeaders = [ { key: 'name', header: t('listName', 'List name'), }, { key: 'type', header: t('listType', 'List type'), }, { key: 'size', header: t('numberOfPatients', 'No. of patients'), }, ]; const filteredLists = useMemo(() => { if (!searchTerm) { return patientLists; } return fuzzy .filter(searchTerm, patientLists, { extract: (list) => `${list.name} ${list.type}`, }) .sort((r1, r2) => r1.score - r2.score) .map((result) => result.original); }, [searchTerm, patientLists]); const handleSearchTermChange = (e: React.ChangeEvent) => setSearchTerm(e.target.value); if (isLoading) return (
); if (patientLists?.length > 0) { return (
{({ rows, headers, getTableProps, getHeaderProps, getRowProps }) => ( <>
{rows.length > 0 ? ( {headers.map((header) => ( {header.header} ))} {rows.map((row) => { const currentList = patientLists?.find((list) => list?.id === row.id); return ( launchListDetailsWorkspace(currentList)}> {currentList?.name ?? ''} {currentList?.type ?? ''} {currentList?.size ?? ''} ); })}
) : null}
{filteredLists?.length === 0 ? (

{t('noMatchingListsFound', 'No matching lists to display')}

{t('checkFilters', 'Check the filters above')}

) : null} )}
); } return (

{t('noPatientListsToDisplay', 'No patient lists to display')}

); } export default PatientListsWorkspace;