import React, { useEffect, useState } from 'react'; import { ActionIcon, Button, Group, Modal, Stack, Text, Loader } from '@mantine/core'; import { showErrorMessage } from '~/utils/utils.ts'; import { apiGetPatientsList } from '~/utils/hms_api.ts'; import type { PatientData } from '~/utils/models.ts'; import InfoIcon from 'mdi-react/InfoIcon'; import { iconMStyle } from '~/styles.ts'; // 缓存已加载的患者数据,避免重复请求 const patientCache = new Map(); interface PatientInfoDisplayProps { patientId: number; showIcon?: boolean; } export function PatientInfoDisplay({ patientId, showIcon = true }: PatientInfoDisplayProps) { const [patientInfo, setPatientInfo] = useState(null); const [loading, setLoading] = useState(true); const [modalOpen, setModalOpen] = useState(false); const [initialLoad, setInitialLoad] = useState(true); useEffect(() => { // 如果已经在缓存中,直接使用缓存数据 if (patientCache.has(patientId)) { setPatientInfo(patientCache.get(patientId) || null); setLoading(false); setInitialLoad(false); return; } // 否则加载患者数据 fetchPatientInfo(); }, [patientId]); const fetchPatientInfo = async () => { try { setLoading(true); // 获取所有患者信息,传递-1表示不分页,获取所有患者 const response = await apiGetPatientsList(-1); if (response.success) { const patients = response.data.patients; // 将所有患者添加到缓存 patients.forEach(patient => { patientCache.set(patient.id, patient); }); // 查找当前需要的患者 const patient = patients.find(p => p.id === patientId); if (patient) { setPatientInfo(patient); } else { console.warn(`Patient with ID ${patientId} not found`); } } else { showErrorMessage(response.message, "Failed to load patient information"); } } catch (error) { showErrorMessage("Error loading patient information", "Error"); console.error("Error fetching patient information:", error); } finally { setLoading(false); setInitialLoad(false); } }; const openModal = () => { setModalOpen(true); }; const closeModal = () => { setModalOpen(false); }; // 初始加载时显示加载状态 if (initialLoad) { return ; } // 如果无法找到患者信息,显示 ID if (!patientInfo) { return ( ID: {patientId} {showIcon && ( )} ); } return ( <> {`${patientInfo.title} ${patientInfo.name}`} {showIcon && ( )} Name: {`${patientInfo.title} ${patientInfo.name}`} Gender: {patientInfo.gender} Birth Date: {patientInfo.birth_date} Email: {patientInfo.email} Phone: {patientInfo.phone} Address: {patientInfo.address} Postcode: {patientInfo.postcode} ); }