You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.0 KiB
81 lines
2.0 KiB
/** |
|
* 消息状态管理 |
|
* 使用 Zustand + AsyncStorage 持久化 |
|
*/ |
|
|
|
import { create } from 'zustand'; |
|
import storageManager, { STORAGE_KEYS } from '@/utils/storageManager'; |
|
import { filter } from 'lodash-es'; |
|
// import { useShallow } from 'zustand/react/shallow'; |
|
// import { tenantService } from '@/services'; |
|
|
|
/** |
|
* 状态 |
|
*/ |
|
interface State { |
|
notices: Record<string, any>[]; |
|
homeBanner: Record<string, any>[]; |
|
mineBanner: Record<string, any>[]; |
|
unreadMessage: number; |
|
unreadFeedback: number; |
|
unreadMyMessage: number; |
|
} |
|
|
|
/** |
|
* 操作 |
|
*/ |
|
interface Actions { |
|
setNotices: (list: Record<string, any>[]) => void; |
|
setBanners: (list: Record<string, any>[]) => void; |
|
} |
|
|
|
/** |
|
* 租户状态 Store |
|
*/ |
|
const useMsgStore = create<State & Actions>()((set, get) => ({ |
|
// state |
|
notices: [], |
|
homeBanner: [], |
|
mineBanner: [], |
|
unreadMessage: 0, // 未读收件箱 |
|
unreadFeedback: 0, // 未读问题反馈 |
|
unreadMyMessage: 0, // 未读我的消息 |
|
|
|
// actions |
|
setNotices: (list: Record<string, any>[]) => { |
|
set({ notices: list }); |
|
|
|
if (__DEV__) { |
|
console.log('💾 notices saved:', list); |
|
} |
|
}, |
|
setBanners: (list: Record<string, any>[]) => { |
|
const homeBanner = filter(list, (item) => item.content1 && item.content1.includes('1')); |
|
const mineBanner = filter(list, (item) => item.content1 && item.content1.includes('2')); |
|
set({ homeBanner, mineBanner }); |
|
|
|
if (__DEV__) { |
|
console.log('💾 banners saved:', list); |
|
} |
|
}, |
|
})); |
|
|
|
export const useUnreadMessageTotal = () => |
|
useMsgStore((state) => state.unreadMessage + state.unreadFeedback + state.unreadMyMessage); |
|
|
|
// 从 AsyncStorage 恢复状态的函数 |
|
export const restoreMsgState = async () => { |
|
try { |
|
const stored = storageManager.session.getItem(STORAGE_KEYS.MSG_STORE); |
|
if (stored) { |
|
useMsgStore.setState(stored); |
|
if (__DEV__) { |
|
console.log('✅ Msg state restored from storage'); |
|
} |
|
} |
|
} catch (error) { |
|
console.error('Failed to restore msg state:', error); |
|
} |
|
}; |
|
|
|
export default useMsgStore;
|
|
|