- 新增 useDictionary hook 支持字典 API 获取 + fallback 降级 - DoctorList 科室/职称改用 useDictionary (health_department/health_title) - FollowUpTaskList 随访类型改用 useDictionary (health_follow_up_type) - ConsultationList 咨询类型改用 useDictionary (health_consultation_type) - FamilyMembersTab 家庭关系改用 useDictionary (health_relationship)
32 lines
896 B
TypeScript
32 lines
896 B
TypeScript
import { useEffect, useState, useMemo } from 'react';
|
|
import { listItemsByCode, type DictionaryItemInfo } from '../api/dictionaries';
|
|
|
|
export interface DictOption {
|
|
value: string;
|
|
label: string;
|
|
}
|
|
|
|
export function useDictionary(code: string, fallback?: DictOption[]) {
|
|
const [items, setItems] = useState<DictionaryItemInfo[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
setLoading(true);
|
|
listItemsByCode(code)
|
|
.then((data) => setItems(data))
|
|
.catch(() => setItems([]))
|
|
.finally(() => setLoading(false));
|
|
}, [code]);
|
|
|
|
const options = useMemo<DictOption[]>(() => {
|
|
if (items.length > 0) {
|
|
return items
|
|
.sort((a, b) => a.sort_order - b.sort_order)
|
|
.map((item) => ({ value: item.value, label: item.label }));
|
|
}
|
|
return fallback ?? [];
|
|
}, [items, fallback]);
|
|
|
|
return { items, options, loading };
|
|
}
|