import React, { useState, useEffect, useCallback, useRef } from 'react'; import { View, Text, ScrollView, TouchableOpacity, FlatList, Image, NativeSyntheticEvent, NativeScrollEvent, Dimensions, } from 'react-native'; import { useNavigation, CompositeNavigationProp, } from '@react-navigation/native'; import { BottomTabNavigationProp } from '@react-navigation/bottom-tabs'; import { StackNavigationProp } from '@react-navigation/stack'; import { getStyles } from './homeScreen.styles'; import { ProviderCard, SearchBar } from '@components'; import { useAppTheme } from '@theme'; import { getProvidersApi } from '../../../api/deliveryApi'; import { Provider } from '../../../interfaces'; import { AppStackParamList } from '../../../navigation/appStack'; import { MainTabParamList } from '../../../navigation/mainTabNavigator'; type NavProp = CompositeNavigationProp< BottomTabNavigationProp, StackNavigationProp >; const { width } = Dimensions.get('window'); const BANNER_STEP = width - 32 + 12; // card width + margin const CATEGORIES = [ { key: 'all', icon: '🏠', label: 'All' }, { key: 'Food', icon: '🍔', label: 'Food' }, { key: 'Groceries', icon: '🛒', label: 'Grocery' }, { key: 'Pharmacy', icon: '💊', label: 'Pharmacy' }, { key: 'Meat', icon: '🥩', label: 'Meat' }, { key: 'Flowers', icon: '💐', label: 'Flowers' }, { key: 'More', icon: '📦', label: 'More' }, ]; const PROMOS = [ { key: 'promo1', colors: ['#05824C', '#0AA25E'], eyebrow: 'Limited time', title: '50% OFF', subtitle: 'On your first order, up to ₹150', cta: 'Order now', }, { key: 'promo2', colors: ['#7B3FF2', '#9B6BF5'], eyebrow: 'Free delivery', title: '₹0 Delivery', subtitle: 'On orders above ₹299 today', cta: 'Explore', }, { key: 'promo3', colors: ['#E85D3D', '#F0805F'], eyebrow: 'Pharmacy', title: 'Meds in 20 min', subtitle: 'Genuine medicines, fast delivery', cta: 'Shop now', }, ]; export const HomeScreen: React.FC = () => { const { colors } = useAppTheme(); const styles = getStyles(colors); const navigation = useNavigation(); const [providers, setProviders] = useState([]); const [selectedCategory, setSelectedCategory] = useState('all'); const [activeBanner, setActiveBanner] = useState(0); useEffect(() => { getProvidersApi().then(setProviders); }, []); const filteredProviders = selectedCategory === 'all' ? providers : providers.filter(p => p.tag === selectedCategory); const handleSearchFocus = useCallback(() => { navigation.navigate('SearchScreen'); }, [navigation]); const handleBannerScroll = useCallback( (e: NativeSyntheticEvent) => { const index = Math.round(e.nativeEvent.contentOffset.x / BANNER_STEP); setActiveBanner(index); }, [], ); return ( {/* Top bar */} 📍 Deliver to Koramangala 4th Block 🔔 {/* Search */} {}} placeholder="Search providers or items..." onFocus={handleSearchFocus} /> {/* Promo carousel */} item.key} showsHorizontalScrollIndicator={false} contentContainerStyle={styles.bannerListContent} snapToInterval={BANNER_STEP} decelerationRate="fast" onScroll={handleBannerScroll} scrollEventThrottle={16} renderItem={({ item }) => ( {item.eyebrow} {item.title} {item.subtitle} {item.cta} )} /> {PROMOS.map((_, i) => ( ))} {/* Categories */} item.key} showsHorizontalScrollIndicator={false} contentContainerStyle={styles.chipRow} renderItem={({ item }) => { const isSelected = selectedCategory === item.key; return ( { setSelectedCategory(item.key); if (item.key !== 'all') { navigation.navigate('ProviderListScreen', { category: item.key, }); } }} > {item.icon} {item.label} ); }} /> {/* Section header */} {selectedCategory === 'all' ? 'Popular near you' : selectedCategory} {filteredProviders.length} place {filteredProviders.length === 1 ? '' : 's'} delivering to you See all {/* Provider list */} {filteredProviders.length === 0 && ( 🍽️ No providers in this category yet )} {filteredProviders.map(provider => ( navigation.navigate('ProviderDetailsScreen', { providerId: provider.id, }) } /> ))} ); };