xref: /MusicFree/src/pages/artistDetail/components/resultList.tsx (revision b4c389f44ac4dad056e7314478fadd2eca82a4b1)
120e2869eS猫头猫import React, {useEffect, useRef, useState} from 'react';
220e2869eS猫头猫import rpx from '@/utils/rpx';
320e2869eS猫头猫import {FlatList} from 'react-native-gesture-handler';
44060c00aS猫头猫import {useAtom} from 'jotai';
520e2869eS猫头猫import {IQueryResult, scrollToTopAtom} from '../store/atoms';
620e2869eS猫头猫import {RequestStateCode} from '@/constants/commonConst';
720e2869eS猫头猫import useQueryArtist from '../hooks/useQuery';
821f1ca13S猫头猫import Empty from '@/components/base/empty';
921f1ca13S猫头猫import ListLoading from '@/components/base/listLoading';
1021f1ca13S猫头猫import ListReachEnd from '@/components/base/listReachEnd';
11*b4c389f4Smaotoumaoimport {useParams} from '@/core/router';
1220e2869eS猫头猫
1320e2869eS猫头猫const ITEM_HEIGHT = rpx(120);
1420e2869eS猫头猫
1520e2869eS猫头猫interface IResultListProps<T = IArtist.ArtistMediaType> {
1620e2869eS猫头猫    tab: T;
1720e2869eS猫头猫    data: IQueryResult<T>;
1820e2869eS猫头猫    renderItem: (...args: any) => any;
1920e2869eS猫头猫}
2020e2869eS猫头猫export default function ResultList(props: IResultListProps) {
2120e2869eS猫头猫    const {data, renderItem, tab} = props;
22a3b33415S猫头猫    const [scrollToTopState, setScrollToTopState] = useAtom(scrollToTopAtom);
2320e2869eS猫头猫    const lastScrollY = useRef<number>(0);
24e7fa3837S猫头猫    const {pluginHash, artistItem} = useParams<'artist-detail'>();
2520e2869eS猫头猫    const [queryState, setQueryState] = useState<RequestStateCode>(
2620e2869eS猫头猫        data?.state ?? RequestStateCode.IDLE,
2720e2869eS猫头猫    );
2820e2869eS猫头猫
2920e2869eS猫头猫    const queryArtist = useQueryArtist(pluginHash);
3020e2869eS猫头猫
3120e2869eS猫头猫    useEffect(() => {
3220e2869eS猫头猫        queryState === RequestStateCode.IDLE && queryArtist(artistItem, 1, tab);
3320e2869eS猫头猫    }, []);
3420e2869eS猫头猫
3520e2869eS猫头猫    useEffect(() => {
3620e2869eS猫头猫        setQueryState(data?.state ?? RequestStateCode.IDLE);
3720e2869eS猫头猫    }, [data]);
3820e2869eS猫头猫
3920e2869eS猫头猫    return (
4020e2869eS猫头猫        <FlatList
4120e2869eS猫头猫            onScroll={e => {
4220e2869eS猫头猫                const currentY = e.nativeEvent.contentOffset.y;
434060c00aS猫头猫                if (
444060c00aS猫头猫                    !scrollToTopState &&
454060c00aS猫头猫                    currentY < ITEM_HEIGHT * 8 - rpx(350)
464060c00aS猫头猫                ) {
4720e2869eS猫头猫                    currentY < lastScrollY.current && setScrollToTopState(true);
4820e2869eS猫头猫                } else {
49a3b33415S猫头猫                    if (scrollToTopState && currentY > ITEM_HEIGHT * 8) {
504060c00aS猫头猫                        currentY > lastScrollY.current &&
514060c00aS猫头猫                            setScrollToTopState(false);
52a3b33415S猫头猫                    }
5320e2869eS猫头猫                }
5420e2869eS猫头猫                lastScrollY.current = currentY;
5520e2869eS猫头猫            }}
564060c00aS猫头猫            ListEmptyComponent={<Empty />}
5721f1ca13S猫头猫            ListFooterComponent={
58956ee1b7S猫头猫                queryState === RequestStateCode.PENDING_REST_PAGE ? (
594060c00aS猫头猫                    <ListLoading />
604060c00aS猫头猫                ) : queryState === RequestStateCode.FINISHED &&
614060c00aS猫头猫                  data.data?.length !== 0 ? (
624060c00aS猫头猫                    <ListReachEnd />
631fbef60aS猫头猫                ) : null
6421f1ca13S猫头猫            }
6520e2869eS猫头猫            onEndReached={() => {
66a3b33415S猫头猫                (queryState === RequestStateCode.IDLE ||
67a3b33415S猫头猫                    queryState === RequestStateCode.PARTLY_DONE) &&
6820e2869eS猫头猫                    queryArtist(artistItem, undefined, tab);
6920e2869eS猫头猫            }}
7020e2869eS猫头猫            getItemLayout={(_, index) => ({
7120e2869eS猫头猫                length: ITEM_HEIGHT,
7220e2869eS猫头猫                offset: ITEM_HEIGHT * index,
7320e2869eS猫头猫                index,
7420e2869eS猫头猫            })}
7520e2869eS猫头猫            overScrollMode="always"
7620e2869eS猫头猫            data={data.data ?? []}
774060c00aS猫头猫            renderItem={renderItem}
784060c00aS猫头猫        />
7920e2869eS猫头猫    );
8020e2869eS猫头猫}
81