1242960d3S猫头猫import {errorLog, trace} from '@/utils/log'; 220e2869eS猫头猫import {RequestStateCode} from '@/constants/commonConst'; 320e2869eS猫头猫import produce from 'immer'; 420e2869eS猫头猫import {useAtom} from 'jotai'; 520e2869eS猫头猫import {useCallback} from 'react'; 620e2869eS猫头猫import {queryResultAtom} from '../store/atoms'; 7*8b88e961S猫头猫import PluginManager from '@/core/plugin'; 820e2869eS猫头猫 920e2869eS猫头猫export default function useQueryArtist(pluginHash: string) { 1020e2869eS猫头猫 const [queryResults, setQueryResults] = useAtom(queryResultAtom); 1120e2869eS猫头猫 1220e2869eS猫头猫 const queryArtist = useCallback( 1320e2869eS猫头猫 async ( 1420e2869eS猫头猫 artist: IArtist.IArtistItem, 1520e2869eS猫头猫 page?: number, 1620e2869eS猫头猫 type: IArtist.ArtistMediaType = 'music', 1720e2869eS猫头猫 ) => { 18*8b88e961S猫头猫 const plugin = PluginManager.getByHash(pluginHash); 1920e2869eS猫头猫 2020e2869eS猫头猫 const prevResult = queryResults[type]; 2120e2869eS猫头猫 if ( 2220e2869eS猫头猫 prevResult?.state === RequestStateCode.PENDING || 2320e2869eS猫头猫 prevResult?.state === RequestStateCode.FINISHED 2420e2869eS猫头猫 ) { 2520e2869eS猫头猫 return; 2620e2869eS猫头猫 } 2720e2869eS猫头猫 page = page ?? (prevResult.page ?? 0) + 1; 2820e2869eS猫头猫 try { 2920e2869eS猫头猫 setQueryResults( 3020e2869eS猫头猫 produce(draft => { 3120e2869eS猫头猫 draft[type].state = RequestStateCode.PENDING; 3220e2869eS猫头猫 }), 3320e2869eS猫头猫 ); 34a3b33415S猫头猫 35*8b88e961S猫头猫 const result = await plugin?.methods?.queryArtistWorks?.( 3620e2869eS猫头猫 artist, 3720e2869eS猫头猫 page, 3820e2869eS猫头猫 type, 3920e2869eS猫头猫 ); 4020e2869eS猫头猫 setQueryResults( 4120e2869eS猫头猫 produce(draft => { 4220e2869eS猫头猫 draft[type].page = page; 43a3b33415S猫头猫 draft[type].state = 4420e2869eS猫头猫 result?.isEnd === false 4520e2869eS猫头猫 ? RequestStateCode.PARTLY_DONE 46a3b33415S猫头猫 : RequestStateCode.FINISHED; 47a3b33415S猫头猫 draft[type].data = (draft[type].data ?? []).concat( 48*8b88e961S猫头猫 result?.data ?? [], 49a3b33415S猫头猫 ); 5020e2869eS猫头猫 }), 5120e2869eS猫头猫 ); 5220e2869eS猫头猫 } catch (e) { 5320e2869eS猫头猫 errorLog('拉取作者信息失败', e); 5420e2869eS猫头猫 setQueryResults( 5520e2869eS猫头猫 produce(draft => { 5620e2869eS猫头猫 draft[type].state = RequestStateCode.PARTLY_DONE; 5720e2869eS猫头猫 }), 5820e2869eS猫头猫 ); 5920e2869eS猫头猫 } 6020e2869eS猫头猫 }, 6120e2869eS猫头猫 [queryResults], 6220e2869eS猫头猫 ); 6320e2869eS猫头猫 6420e2869eS猫头猫 return queryArtist; 6520e2869eS猫头猫} 66