import { useProxyCannotAddToRecord } from '../../../../hooks/useProxyCannotAddToRecord'; import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { Device } from "fhir/r4"; import { FhirDeviceDisplay } from "../../../../components"; import { useFhirDeviceBroadcast, useFhirDevices, useFhirDeviceTargetedBroadcast, } from "../../../../hooks"; import { useAuthServiceContext } from "../../../../services"; import { Button } from "../../../../components/ui/button.tsx"; import { ChevronRight, Cpu, Edit, Plus, Radio } from "lucide-react"; import { Skeleton } from "../../../../components/ui/skeleton.tsx"; import { Sheet, SheetContent } from "../../../../components/ui/sheet.tsx"; const Devices = () => { const navigate = useNavigate(); const { userRole, USER_ROLES } = useAuthServiceContext(); const { loading, error, devicesList } = useFhirDevices(); const [selectedDevice, setSelectedDevice] = useState(null); const [showDisplayModal, setShowDisplayModal] = useState(false); useFhirDeviceBroadcast(); useFhirDeviceTargetedBroadcast({ device: selectedDevice || undefined, isOpen: showDisplayModal, onUpdate: (updatedDevice) => { setSelectedDevice(updatedDevice); }, onDelete: () => { setShowDisplayModal(false); setSelectedDevice(null); }, }); const viewDevice = (device: Device) => { setSelectedDevice(device); setShowDisplayModal(true); }; const editDevice = (event: React.MouseEvent, device: Device) => { event.stopPropagation(); navigate(device.id as string, { relative: "path" }); }; const addDevice = () => { navigate("new", { relative: "path" }); }; const proxyCannotAdd = useProxyCannotAddToRecord(); const isAuthorizedToEdit = () => { return ( !proxyCannotAdd && userRole && [ USER_ROLES.PATIENT, USER_ROLES.RELATED_PERSON, USER_ROLES.PRACTITIONER, ].includes(userRole as string) ); }; return (

Devices

View and manage medical devices, implants, and associated traceability information.{" "}

{isAuthorizedToEdit() && ( )}
{" "} {error ? (
{" "}

Could not load Devices

Try again later.

) : !loading && devicesList.length === 0 ? (

No Devices Added Yet

There are currently no medical devices linked to your health record. Devices are added by your healthcare provider and will appear here once assigned.

) : loading ? (
{[...Array(7)].map((_, index) => (
))}
) : ( devicesList.length !== 0 && (
{devicesList.map((device: Device) => { return (
viewDevice(device)} onKeyDown={(event: React.KeyboardEvent) => { if (event.key !== 'Enter' && event.key !== ' ') return; event.preventDefault(); viewDevice(device); }} >

{device?.deviceName?.map((name) => ( {name?.name} )) || "Device name not available"} {device.status ? device.status : "Status unknown"}

{[ device?.manufacturer, device?.modelNumber, device?.serialNumber, ] .filter(Boolean) .join(" | ") || "-"}

{" "}
{isAuthorizedToEdit() && (
)}
); })}
) )} {selectedDevice && ( { if (!open) setSelectedDevice(null); }} > { setShowDisplayModal(false); setSelectedDevice(null); }} /> )}
); }; export default Devices; // import { useNavigate } from 'react-router-dom'; // import { Device } from 'fhir/r4'; // import { AppSpinner, FhirDeviceDisplay, ScrollableTab } from '../../../../components'; // import { formatFhirCodeableConcept } from '../../../../utils/fhirCodeableConcept.tsx'; // import { useFhirDeviceBroadcast, useFhirDevices, useFhirDeviceTargetedBroadcast } from '../../../../hooks'; // import { useAuthServiceContext, useDeviceServiceContext } from '../../../../services'; // const Devices = () => { // const navigate = useNavigate(); // const { userRole, USER_ROLES } = useAuthServiceContext(); // const { getDeviceDisplay } = useDeviceServiceContext(); // const { loading, error, clearError, devicesList } = useFhirDevices(); // const [isGuideOpen, setIsGuideOpen] = useState(false); // const [selectedDevice, setSelectedDevice] = useState(null); // const [showDisplayModal, setShowDisplayModal] = useState(false); // const spacerMargin = '24px'; // useFhirDeviceBroadcast(); // useFhirDeviceTargetedBroadcast({ // device: selectedDevice || undefined, // isOpen: showDisplayModal, // onUpdate: (updatedDevice) => { // setSelectedDevice(updatedDevice); // }, // onDelete: () => { // setShowDisplayModal(false); // setSelectedDevice(null); // } // }); // const viewDevice = (device: Device) => { // setSelectedDevice(device); // setShowDisplayModal(true); // }; // const editDevice = (event: React.MouseEvent, device: Device) => { // event.stopPropagation(); // navigate(device.id as string, { relative: 'path' }); // }; // const addDevice = () => { // navigate('new', { relative: 'path' }); // }; // const isAuthorizedToEdit = () => { // return userRole && [USER_ROLES.PATIENT, USER_ROLES.RELATED_PERSON, USER_ROLES.PRACTITIONER].includes(userRole as string); // }; // const getStatusDisplay = (device: Device): { text: string, color?: string } => { // if (!device) return { text: 'Unknown' }; // switch (device.status) { // case 'active': // return { text: 'Active', color: '#00a32a' }; // case 'inactive': // return { text: 'Inactive', color: '#757575' }; // case 'entered-in-error': // return { text: 'Error', color: '#cc1818' }; // case 'unknown': // default: // return { text: device.status?.charAt(0).toUpperCase() + device.status?.slice(1) || 'Unknown' }; // } // }; // const getDeviceDetails = (device: Device) => { // const details = []; // if (device.manufacturer) { // details.push(device.manufacturer); // } // if (device.modelNumber) { // details.push(`Model: ${device.modelNumber}`); // } // if (device.serialNumber) { // details.push(`SN: ${device.serialNumber}`); // } // return details.join(' - '); // }; // return ( // // setIsGuideOpen(false)} // resourceType="Device"/> // // // // // // // {isAuthorizedToEdit() && ( // // // // )} // // // {error && ( // <> // // {error} // // // // )} // {!loading && devicesList.length === 0 && ( // <> // // No devices found. // // // // )} // <> // {loading ? ( // // ): ( // // {devicesList.length !== 0 && ( // // {devicesList.map((device: Device) => { // const displayTitle = getDeviceDisplay(device); // const { text: statusText, color: statusColor } = getStatusDisplay(device); // const deviceDetails = getDeviceDetails(device); // const deviceType = device.type ? formatFhirCodeableConcept(device.type): ''; // return ( // viewDevice(device)}> // // // // // // // // // {displayTitle} // // {deviceType && ( // // Type: {deviceType} // // )} // {deviceDetails && ( // // {deviceDetails} // // )} // // {isAuthorizedToEdit() && ( // // // // )} // // // // // ); // })} // // )} // // )} // // {selectedDevice && ( // { // setShowDisplayModal(false); // setSelectedDevice(null); // }} // /> // )} // // ); // }; // export default Devices;