1import {RequestStateCode} from '@/constants/commonConst'; 2import PluginManager from '@/core/pluginManager'; 3import produce from 'immer'; 4import {useAtom} from 'jotai'; 5import {useCallback} from 'react'; 6import {pluginsTopListAtom} from '../store/atoms'; 7 8export default function useGetTopList() { 9 const [pluginsTopList, setPluginsTopList] = useAtom(pluginsTopListAtom); 10 11 const getTopList = useCallback( 12 async (pluginHash: string) => { 13 try { 14 // 有数据/加载中直接返回 15 if ( 16 pluginsTopList[pluginHash]?.data?.length || 17 pluginsTopList[pluginHash]?.state === 18 RequestStateCode.PENDING_REST_PAGE 19 ) { 20 return; 21 } 22 // 获取plugin 23 const plugin = PluginManager.getByHash(pluginHash); 24 if (!plugin) { 25 return; 26 } 27 28 setPluginsTopList( 29 produce(draft => { 30 draft[pluginHash] = { 31 state: RequestStateCode.PENDING_REST_PAGE, 32 data: [], 33 }; 34 }), 35 ); 36 const result = await plugin?.methods?.getTopLists(); 37 setPluginsTopList( 38 produce(draft => { 39 draft[pluginHash] = { 40 data: result, 41 state: RequestStateCode.FINISHED, 42 }; 43 }), 44 ); 45 } catch { 46 setPluginsTopList( 47 produce(draft => { 48 draft[pluginHash].state = RequestStateCode.FINISHED; 49 }), 50 ); 51 } 52 }, 53 [pluginsTopList], 54 ); 55 56 return getTopList; 57} 58