import { create } from 'zustand'; import { getSimpleDictDataList, type DictData } from '@/api/system/dict-data'; interface DictState { dictMap: Map; isLoaded: boolean; fetchDictData: () => Promise; getDictOptions: (type: string) => DictData[]; getDictLabel: (type: string, value: string | number) => string; } export const useDictStore = create()((set, get) => ({ dictMap: new Map(), isLoaded: false, fetchDictData: async () => { if (get().isLoaded) return; try { const { data } = await getSimpleDictDataList(); const map = new Map(); for (const item of data) { const list = map.get(item.dictType) || []; list.push(item); map.set(item.dictType, list); } set({ dictMap: map, isLoaded: true }); } catch { // silently fail } }, getDictOptions: (type) => { return get().dictMap.get(type) || []; }, getDictLabel: (type, value) => { const options = get().dictMap.get(type) || []; const item = options.find((d) => String(d.value) === String(value)); return item?.label || String(value); }, }));