feat: 首页更新
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
import { useEffect, useState, useMemo } from 'react';
|
||||
import { useEffect, useState, useMemo, useCallback } from 'react';
|
||||
import useGameStore from '@/stores/gameStore';
|
||||
import { GameMainTypesEnum, defaultHomeGameTabMenus, gameMainTypesMap } from '@/constants/game';
|
||||
import { ThemeEnum } from '@/constants/theme';
|
||||
import { forEach, cloneDeep, map, filter } from 'lodash-es';
|
||||
import { useIsLoggedIn } from '@/stores/userStore';
|
||||
import { useShallow } from 'zustand/react/shallow';
|
||||
import storageManager, { STORAGE_KEYS } from '@/utils/storageManager';
|
||||
|
||||
|
||||
type GameMenu = {
|
||||
name: string;
|
||||
key: string;
|
||||
icon?: string;
|
||||
logo?: string;
|
||||
children?: GameMenu[];
|
||||
};
|
||||
|
||||
// 有子菜单的游戏类型
|
||||
const hasSubGameMainTypes = [
|
||||
@@ -78,10 +88,68 @@ export const useGameMainMenus = (theme: ThemeEnum) => {
|
||||
],
|
||||
(item) => ({
|
||||
...item,
|
||||
// 为了在 React Native 中正确加载本地图片,使用 require 的方式
|
||||
// 这里保留原始的 icon 名称,在组件中使用 require 加载
|
||||
icon: item.icon,
|
||||
key: `${item.key}`,
|
||||
})
|
||||
);
|
||||
) as GameMenu[];
|
||||
}, [theme, isLogin, menuSort, gameBigClass]);
|
||||
};
|
||||
|
||||
export const useMenuDataLoaded = () => useGameStore((state) => state.menuSort?.length > 0);
|
||||
|
||||
/**
|
||||
* 游戏分类选择 Hook(统一管理)
|
||||
*
|
||||
* 管理当前选中的游戏分类,支持:
|
||||
* - 从 gameStore 获取当前选中分类
|
||||
* - 更新选中分类并保存到 session storage
|
||||
* - 页面刷新后恢复选中分类
|
||||
*
|
||||
* @returns {Object} 包含 selectedCategory 和 setSelectedCategory 的对象
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const { selectedCategory, setSelectedCategory } = useSelectedCategory();
|
||||
*
|
||||
* // 获取当前选中分类
|
||||
* console.log(selectedCategory); // '103'
|
||||
*
|
||||
* // 更新选中分类
|
||||
* setSelectedCategory('1');
|
||||
* ```
|
||||
*/
|
||||
export const useSelectedCategory = () => {
|
||||
const selectedCategory = useGameStore((state) => state.selectedCategory);
|
||||
const setSelectedCategoryInStore = useGameStore((state) => state.setSelectedCategory);
|
||||
|
||||
// 初始化时从 session storage 恢复选中分类
|
||||
useEffect(() => {
|
||||
const initializeSelectedCategory = async () => {
|
||||
try {
|
||||
const savedCategory = storageManager.session.getItem(STORAGE_KEYS.APP_ACTIVE_MAIN_MENU_TAB);
|
||||
if (savedCategory) {
|
||||
setSelectedCategoryInStore(savedCategory);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to restore selected category:', error);
|
||||
}
|
||||
};
|
||||
|
||||
initializeSelectedCategory();
|
||||
}, [setSelectedCategoryInStore]);
|
||||
|
||||
// 更新选中分类的回调函数
|
||||
const setSelectedCategory = useCallback(
|
||||
(categoryId: string) => {
|
||||
setSelectedCategoryInStore(categoryId);
|
||||
},
|
||||
[setSelectedCategoryInStore]
|
||||
);
|
||||
|
||||
return {
|
||||
selectedCategory,
|
||||
setSelectedCategory,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user