1import React, {memo, useEffect, useState} from 'react'; 2import {useAtomValue} from 'jotai'; 3import {ISearchResult, queryAtom} from '../../store/atoms'; 4import {renderMap} from './results'; 5import useSearch from '../../hooks/useSearch'; 6import Loading from '@/components/base/loading'; 7import {FlatList, StyleSheet} from 'react-native'; 8import {RequestStateCode} from '@/constants/commonConst'; 9import ListLoading from '@/components/base/listLoading'; 10import Empty from '@/components/base/empty'; 11import ListReachEnd from '@/components/base/listReachEnd'; 12 13interface IResultWrapperProps< 14 T extends ICommon.SupportMediaType = ICommon.SupportMediaType, 15> { 16 tab: T; 17 pluginHash: string; 18 pluginName: string; 19 searchResult: ISearchResult<T>; 20 pluginSearchResultRef: React.MutableRefObject<ISearchResult<T>>; 21} 22function ResultWrapper(props: IResultWrapperProps) { 23 const {tab, pluginHash, searchResult, pluginSearchResultRef} = props; 24 const search = useSearch(); 25 const [searchState, setSearchState] = useState<RequestStateCode>( 26 searchResult?.state ?? RequestStateCode.IDLE, 27 ); 28 const query = useAtomValue(queryAtom); 29 30 const ResultComponent = renderMap[tab]!; 31 const data: any = searchResult?.data ?? []; 32 33 useEffect(() => { 34 if (searchState === RequestStateCode.IDLE) { 35 search(query, 1, tab, pluginHash); 36 } 37 }, []); 38 39 useEffect(() => { 40 setSearchState(searchResult?.state ?? RequestStateCode.IDLE); 41 }, [searchResult]); 42 43 const renderItem = ({item, index}: any) => ( 44 <ResultComponent 45 item={item} 46 index={index} 47 pluginHash={pluginHash} 48 pluginSearchResultRef={pluginSearchResultRef} 49 /> 50 ); 51 52 return searchState === RequestStateCode.PENDING_FP ? ( 53 <Loading /> 54 ) : ( 55 <FlatList 56 extraData={searchState} 57 style={style.list} 58 ListEmptyComponent={() => <Empty />} 59 ListFooterComponent={() => 60 searchState === RequestStateCode.PENDING ? ( 61 <ListLoading /> 62 ) : searchState === RequestStateCode.FINISHED ? ( 63 <ListReachEnd /> 64 ) : ( 65 <></> 66 ) 67 } 68 data={data} 69 refreshing={false} 70 onRefresh={() => { 71 search(query, 1, tab, pluginHash); 72 }} 73 onEndReached={() => { 74 (searchState === RequestStateCode.PARTLY_DONE || 75 searchState === RequestStateCode.IDLE) && 76 search(undefined, undefined, tab, pluginHash); 77 }} 78 renderItem={renderItem} 79 /> 80 ); 81} 82const style = StyleSheet.create({ 83 list: { 84 flex: 1, 85 }, 86}); 87 88export default memo(ResultWrapper); 89