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} 21function ResultWrapper(props: IResultWrapperProps) { 22 const {tab, pluginHash, searchResult} = props; 23 const search = useSearch(); 24 const [searchState, setSearchState] = useState<RequestStateCode>( 25 searchResult?.state ?? RequestStateCode.IDLE, 26 ); 27 const query = useAtomValue(queryAtom); 28 29 const ResultComponent = renderMap[tab]!; 30 const data: any = searchResult?.data ?? []; 31 32 useEffect(() => { 33 if (searchState === RequestStateCode.IDLE) { 34 search(query, 1, tab, pluginHash); 35 } 36 }, []); 37 38 useEffect(() => { 39 setSearchState(searchResult?.state ?? RequestStateCode.IDLE); 40 }, [searchResult]); 41 42 const renderItem = ({item, index}: any) => ( 43 <ResultComponent item={item} index={index} pluginHash={pluginHash} /> 44 ); 45 46 return searchState === RequestStateCode.PENDING_FP ? ( 47 <Loading /> 48 ) : ( 49 <FlatList 50 extraData={searchState} 51 style={style.list} 52 ListEmptyComponent={() => <Empty />} 53 ListFooterComponent={() => 54 searchState === RequestStateCode.PENDING ? ( 55 <ListLoading /> 56 ) : searchState === RequestStateCode.FINISHED ? ( 57 <ListReachEnd /> 58 ) : ( 59 <></> 60 ) 61 } 62 data={data} 63 refreshing={false} 64 onRefresh={() => { 65 search(query, 1, tab, pluginHash); 66 }} 67 onEndReached={() => { 68 (searchState === RequestStateCode.PARTLY_DONE || 69 searchState === RequestStateCode.IDLE) && 70 search(undefined, undefined, tab, pluginHash); 71 }} 72 renderItem={renderItem} 73 /> 74 ); 75} 76const style = StyleSheet.create({ 77 list: { 78 flex: 1, 79 }, 80}); 81 82export default memo(ResultWrapper); 83