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.

73 lines
1.7 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>[];
}
/**
*
*/
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: [],
// 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);
}
},
}));
// 从 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;