import React, { useState, useCallback, useRef } from 'react'; import { View, Text } from '@tarojs/components'; import Taro, { useDidShow, usePullDownRefresh } from '@tarojs/taro'; import { listPatientAlerts, type Alert } from '@/services/alert'; import { useAuthStore } from '@/stores/auth'; import Loading from '@/components/Loading'; import './index.scss'; const SEVERITY_MAP: Record = { info: { label: '提示', className: 'sev-info' }, warning: { label: '警告', className: 'sev-warning' }, critical: { label: '严重', className: 'sev-critical' }, urgent: { label: '紧急', className: 'sev-urgent' }, }; const STATUS_TABS = [ { key: '', label: '全部' }, { key: 'pending', label: '待处理' }, { key: 'acknowledged', label: '已确认' }, { key: 'resolved', label: '已恢复' }, ]; export default function PatientAlerts() { const { currentPatient } = useAuthStore(); const [alerts, setAlerts] = useState([]); const [total, setTotal] = useState(0); const [page, setPage] = useState(1); const [status, setStatus] = useState(''); const [loading, setLoading] = useState(false); const loadingRef = useRef(false); const fetchAlerts = useCallback( async (pageNum: number, s: string, isRefresh = false) => { if (!currentPatient || loadingRef.current) return; loadingRef.current = true; setLoading(true); try { const res = await listPatientAlerts(currentPatient.id, { page: pageNum, page_size: 20, status: s || undefined, }); const list = res.data || []; if (isRefresh) { setAlerts(list); } else { setAlerts((prev) => [...prev, ...list]); } setTotal(res.total); setPage(pageNum); } catch { Taro.showToast({ title: '加载失败', icon: 'none' }); } finally { loadingRef.current = false; setLoading(false); } }, [currentPatient], ); useDidShow(() => { Taro.setNavigationBarTitle({ title: '健康告警' }); fetchAlerts(1, status, true); }); usePullDownRefresh(() => { fetchAlerts(1, status, true).finally(() => Taro.stopPullDownRefresh()); }); const handleTabChange = (key: string) => { setStatus(key); fetchAlerts(1, key, true); }; if (!currentPatient) { return ( 请先完善个人档案 Taro.navigateTo({ url: '/pages/pkg-profile/family-add/index' })}> 去建档 ); } return ( {STATUS_TABS.map((tab) => ( handleTabChange(tab.key)} > {tab.label} ))} {alerts.length === 0 && !loading ? ( 暂无告警记录 您的各项指标正常 ) : ( {alerts.map((item) => { const sev = SEVERITY_MAP[item.severity] || SEVERITY_MAP.warning; return ( {sev.label} {new Date(item.created_at).toLocaleDateString()} {item.title} ); })} {loading && } {!loading && alerts.length >= total && total > 0 && ( )} )} ); }