All files / app/(tabs)/ranking/screens RankingScreen.tsx

60.86% Statements 14/23
33.33% Branches 6/18
50% Functions 3/6
63.63% Lines 14/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                                                  1x 1x     1x       1x                                   1x   1x     1x               1x   1x   1x                         1x 1x                                                                   1x                                 1x                                                                          
/**
 * RankingScreen - 猫粮排行榜页面(重构版)
 *
 * 企业级最佳实践:
 * - 组件化:拆分成多个小组件,职责单一
 * - 性能优化:useMemo、useCallback、React.memo、懒加载
 * - 状态管理:Zustand store + 选择器模式
 * - 用户体验:下拉刷新、无限滚动、空状态
 * - 代码质量:TypeScript、清晰的注释
 * - 可维护性:小文件、易于理解和修改
 */
 
import React from 'react';
import { FlatList, RefreshControl, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useRouter } from 'expo-router';
import { YStack } from 'tamagui';
import { AppHeader } from '@/src/components/AppHeader';
import { CatFoodCard } from '@/src/components/CatFoodCard';
import { Skeleton } from '@/src/components/lazy';
import type { CatFood } from '@/src/types/catFood';
import { useLazyLoad } from '@/src/hooks';
 
import {
  EmptyState,
  ImagePreviewModal,
  ListFooter,
  SearchFilterSection,
  TopRankingSwiper,
} from '../components';
import { useImagePreview, useRankingData, useRankingFilter } from '../hooks';
 
export function RankingScreen() {
  const insets = useSafeAreaInsets();
  const router = useRouter();
 
  // 懒加载控制
  const { isReady: isSwiperReady } = useLazyLoad({ delay: 150, waitForInteraction: true });
 
  // 使用自定义 hooks
  const { catfoods, isLoading, isRefreshing, isLoadingMore, handleRefresh, handleLoadMore } =
    useRankingData();
 
  const {
    sortBy,
    setSortBy,
    selectedBrand,
    brandMenuExpanded,
    searchQuery,
    brandList,
    brandCounts,
    filteredCatFoods,
    topCatFoods,
    listCatFoods,
    handleSearchChange,
    handleClearSearch,
    handleSelectBrand,
    toggleBrandMenu,
    resetFilters,
  } = useRankingFilter(catfoods);
 
  const { previewVisible, previewImageUrl, handleImagePress, closePreview } = useImagePreview();
 
  // 跳转到详情页
  const handleCatFoodPress = (catfood: CatFood) => {
    router.push({
      pathname: '/detail',
      params: { id: catfood.id },
    });
  };
 
  // 渲染猫粮卡片
  const renderCatFoodCard = ({ item, index }: { item: CatFood; index: number }) => {
    // 如果有轮播图(前5名),列表从第6名开始显示
    const displayIndex = topCatFoods.length > 0 ? index + 5 : index;
 
    return (
      <View testID="catfood-item">
        <CatFoodCard
          catfood={item}
          index={displayIndex}
          onPress={handleCatFoodPress}
          onImagePress={handleImagePress}
        />
      </View>
    );
  };
 
  // 渲染列表头部
  const renderHeader = () => {
    return (
      <>
        {/* 热门推荐轮播图 - 懒加载 */}
        {topCatFoods.length > 0 &&
          (isSwiperReady ? (
            <TopRankingSwiper data={topCatFoods} topCount={5} onPress={handleCatFoodPress} />
          ) : (
            <YStack height={200} margin="$3" borderRadius="$4" overflow="hidden">
              <Skeleton width="100%" height={200} borderRadius={12} />
            </YStack>
          ))}
 
        {/* 搜索框和筛选区域 */}
        <SearchFilterSection
          searchQuery={searchQuery}
          onSearch={handleSearchChange}
          onClearSearch={handleClearSearch}
          sortBy={sortBy}
          onSortChange={setSortBy}
          selectedBrand={selectedBrand}
          brandList={brandList}
          brandCounts={brandCounts}
          brandMenuExpanded={brandMenuExpanded}
          onSelectBrand={handleSelectBrand}
          onToggleBrandMenu={toggleBrandMenu}
          onResetFilters={resetFilters}
          filteredCount={filteredCatFoods.length}
          totalCount={catfoods.length}
        />
      </>
    );
  };
 
  // 渲染空状态
  const renderEmpty = () => {
    if (isLoading) return null;
 
    // 搜索结果为空
    if (searchQuery.trim()) {
      return <EmptyState type="search" searchQuery={searchQuery} onReset={resetFilters} />;
    }
 
    // 列表为空但有轮播图
    if (topCatFoods.length > 0 && listCatFoods.length === 0) {
      return <EmptyState type="complete" />;
    }
 
    // 原始空状态
    return <EmptyState type="default" onRefresh={handleRefresh} />;
  };
 
  return (
    <YStack testID="ranking-screen" flex={1} backgroundColor="$background">
      {/* Header */}
      <AppHeader title="猫粮排行榜" insets={insets} />
 
      {/* 图片预览模态框 */}
      <ImagePreviewModal
        visible={previewVisible}
        imageUrl={previewImageUrl}
        onClose={closePreview}
      />
 
      {/* 猫粮列表 */}
      <FlatList
        testID="catfood-list"
        data={listCatFoods}
        renderItem={renderCatFoodCard}
        keyExtractor={(item) => item.id.toString()}
        ListHeaderComponent={renderHeader}
        contentContainerStyle={{ paddingBottom: Math.max(10, insets.bottom) }}
        refreshControl={
          <RefreshControl
            testID="refresh-indicator"
            refreshing={isRefreshing}
            onRefresh={handleRefresh}
          />
        }
        onEndReached={searchQuery.trim() ? undefined : handleLoadMore}
        onEndReachedThreshold={0.5}
        ListFooterComponent={
          searchQuery.trim() ? null : <ListFooter isLoadingMore={isLoadingMore} />
        }
        ListEmptyComponent={renderEmpty}
      />
    </YStack>
  );
}