120e2869eS猫头猫import {errorLog, trace} from '@/common/logManager'; 220e2869eS猫头猫import {pluginManager} from '@/common/pluginManager'; 320e2869eS猫头猫import {RequestStateCode} from '@/constants/commonConst'; 420e2869eS猫头猫import {makeTag} from '@/utils/makeTag'; 520e2869eS猫头猫import produce from 'immer'; 620e2869eS猫头猫import {useAtom} from 'jotai'; 720e2869eS猫头猫import {useCallback} from 'react'; 820e2869eS猫头猫import {queryResultAtom} from '../store/atoms'; 920e2869eS猫头猫 1020e2869eS猫头猫export default function useQueryArtist(pluginHash: string) { 1120e2869eS猫头猫 const [queryResults, setQueryResults] = useAtom(queryResultAtom); 1220e2869eS猫头猫 1320e2869eS猫头猫 const queryArtist = useCallback( 1420e2869eS猫头猫 async ( 1520e2869eS猫头猫 artist: IArtist.IArtistItem, 1620e2869eS猫头猫 page?: number, 1720e2869eS猫头猫 type: IArtist.ArtistMediaType = 'music', 1820e2869eS猫头猫 ) => { 1920e2869eS猫头猫 const plugin = pluginManager.getPluginByHash(pluginHash); 2020e2869eS猫头猫 2120e2869eS猫头猫 const prevResult = queryResults[type]; 2220e2869eS猫头猫 if ( 2320e2869eS猫头猫 prevResult?.state === RequestStateCode.PENDING || 2420e2869eS猫头猫 prevResult?.state === RequestStateCode.FINISHED 2520e2869eS猫头猫 ) { 2620e2869eS猫头猫 return; 2720e2869eS猫头猫 } 2820e2869eS猫头猫 page = page ?? (prevResult.page ?? 0) + 1; 2920e2869eS猫头猫 console.log('获取作者详情', {artist, page, type, pluginHash}); 3020e2869eS猫头猫 try { 3120e2869eS猫头猫 setQueryResults( 3220e2869eS猫头猫 produce(draft => { 3320e2869eS猫头猫 draft[type].state = RequestStateCode.PENDING; 3420e2869eS猫头猫 }), 3520e2869eS猫头猫 ); 36*a3b33415S猫头猫 3720e2869eS猫头猫 const result = await plugin?.instance?.queryArtistWorks?.( 3820e2869eS猫头猫 artist, 3920e2869eS猫头猫 page, 4020e2869eS猫头猫 type, 4120e2869eS猫头猫 ); 4220e2869eS猫头猫 setQueryResults( 4320e2869eS猫头猫 produce(draft => { 4420e2869eS猫头猫 draft[type].page = page; 45*a3b33415S猫头猫 draft[type].state = 4620e2869eS猫头猫 result?.isEnd === false 4720e2869eS猫头猫 ? RequestStateCode.PARTLY_DONE 48*a3b33415S猫头猫 : RequestStateCode.FINISHED; 49*a3b33415S猫头猫 draft[type].data = (draft[type].data ?? []).concat( 5020e2869eS猫头猫 makeTag(result?.data ?? [], plugin?.name ?? ''), 51*a3b33415S猫头猫 ); 5220e2869eS猫头猫 }), 5320e2869eS猫头猫 ); 5420e2869eS猫头猫 } catch (e) { 5520e2869eS猫头猫 errorLog('拉取作者信息失败', e); 5620e2869eS猫头猫 setQueryResults( 5720e2869eS猫头猫 produce(draft => { 5820e2869eS猫头猫 draft[type].state = RequestStateCode.PARTLY_DONE; 5920e2869eS猫头猫 }), 6020e2869eS猫头猫 ); 6120e2869eS猫头猫 } 6220e2869eS猫头猫 }, 6320e2869eS猫头猫 [queryResults], 6420e2869eS猫头猫 ); 6520e2869eS猫头猫 6620e2869eS猫头猫 return queryArtist; 6720e2869eS猫头猫} 68