import { useLocalSearchParams, useRouter } from 'expo-router' import { StatusBar } from 'expo-status-bar' import { useEffect } from 'react' import { Platform, ScrollView, StyleSheet, StatusBar as RNStatusBar, View } from 'react-native' import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context' import { TitleBar, HeroSlider, TabNavigation, TemplateGrid } from '@/components/blocks/home' import ErrorState from '@/components/ErrorState' import LoadingState from '@/components/LoadingState' import { useActivates } from '@/hooks/use-activates' import { useCategories } from '@/hooks/use-categories' import { useStickyTabs } from '@/hooks/use-sticky-tabs' import { useTabNavigation } from '@/hooks/use-tab-navigation' import { useTemplateFilter } from '@/hooks/use-template-filter' export default function HomeScreen() { const insets = useSafeAreaInsets() const router = useRouter() const params = useLocalSearchParams() // 数据加载 const { load: loadCategories, data: categoriesData, loading: categoriesLoading, error: categoriesError } = useCategories() const { load: loadActivates, data: activatesData } = useActivates() // 标签导航状态 const { activeIndex, selectedCategoryId, currentCategory, tabs, selectTab, selectCategoryById, } = useTabNavigation({ categories: categoriesData?.categories || [], initialCategoryId: params.categoryId as string | undefined, }) // 吸顶状态 const { isSticky, tabsHeight, titleBarHeightRef, handleScroll, handleTabsLayout, handleTitleBarLayout, } = useStickyTabs() // 模板过滤 const { filteredTemplates } = useTemplateFilter({ templates: currentCategory?.templates || [], excludeVideo: true, }) // 初始化加载 useEffect(() => { loadCategories() loadActivates() }, []) // 路由参数同步 useEffect(() => { if (params.categoryId) { selectCategoryById(params.categoryId as string) } }, [params.categoryId]) // 状态判断 const showLoading = categoriesLoading const showEmptyState = !categoriesLoading && categoriesData?.categories?.length === 0 const showEmptyTemplates = !categoriesLoading && !showEmptyState && filteredTemplates.length === 0 // 导航处理 const handlePointsPress = () => router.push('/membership' as any) const handleSearchPress = () => router.push('/searchTemplate') const handleActivityPress = (link: string) => router.push(link as any) const handleArrowPress = () => router.push({ pathname: '/channels' as any, params: selectedCategoryId ? { categoryId: selectedCategoryId } : undefined, }) const handleTemplatePress = (id: string) => router.push({ pathname: '/templateDetail' as any, params: { id }, }) return ( {/* 标题栏 */} {/* 吸顶标签导航 */} {isSticky && !showLoading && !showEmptyState && ( )} handleScroll(e.nativeEvent.contentOffset.y)} scrollEventThrottle={Platform.OS === 'ios' ? 16 : 50} > {/* 活动轮播图 */} {/* 标签导航 */} {!showLoading && !showEmptyState && ( handleTabsLayout(e.nativeEvent.layout.y, e.nativeEvent.layout.height)} style={isSticky ? { opacity: 0, height: tabsHeight } : undefined} > )} {/* 加载状态 */} {showLoading && } {/* 错误状态 */} {categoriesError && ( loadCategories()} /> )} {/* 空状态 - 分类数据为空 */} {showEmptyState && !categoriesError && ( loadCategories()} /> )} {/* 空状态 - 当前分类下没有模板 */} {showEmptyTemplates && ( loadCategories()} /> )} {/* 模板网格 */} {!showLoading && !showEmptyState && !showEmptyTemplates && ( )} ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#090A0B', }, scrollView: { flex: 1, backgroundColor: '#090A0B', }, scrollContent: { flexGrow: 1, backgroundColor: '#090A0B', }, stickyTabsWrapper: { position: 'absolute', top: 0, left: 0, right: 0, zIndex: 100, backgroundColor: '#090A0B', }, })