import React from 'react'; import { TouchableOpacity, View, Text } from 'react-native'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; import { NavigatorScreenParams } from '@react-navigation/native'; import { DrawerNavigationProp } from '@react-navigation/drawer'; import Icon from 'react-native-vector-icons/Ionicons'; import { route, RouteParams } from '@utils'; import { DashboardScreen, AddLeadScreen, ProfileScreen, NotificationScreen, EditProfileScreen, } from '@features'; import { LeadsStack, LeadsStackParamList } from './leadsStack'; import { CustomersStack, CustomersStackParamList } from './customersStack'; import { useTheme } from '@theme'; import { useAppSelector, RootState } from '@store'; import { getStyles } from './tabStack.styles'; export type DashboardStackParamList = { dashboardList: undefined; notifications: undefined; }; export type AddLeadStackParamList = { addLeadForm: undefined; }; export type ProfileStackParamList = { profileForm: undefined; editProfile: undefined; }; export type TabStackParamList = { dashboard: NavigatorScreenParams | undefined; leads: NavigatorScreenParams | undefined; addLead: NavigatorScreenParams | undefined; customers: NavigatorScreenParams | undefined; profile: NavigatorScreenParams | undefined; }; const Tab = createBottomTabNavigator(); const DashboardStackNav = createNativeStackNavigator(); const DashboardStack = () => { const { theme: colors } = useTheme(); const styles = getStyles(colors); const userData = useAppSelector((state: RootState) => state.auth.user_data); const { items: notificationItems, hasFetched } = useAppSelector((state: RootState) => state.notifications); // Calculate unread count from actual items if they've been fetched, otherwise fallback to userData's initial count const unreadCount = hasFetched ? notificationItems.filter(n => String(n.isread) === '0').length : parseInt(userData?.total_unread_notifications ?? '0', 10); return ( ({ headerTitle: 'Convex CRM', headerLeft: () => ( { const parentNav = navigation.getParent>(); if (parentNav) { parentNav.openDrawer(); } else { (navigation as any).openDrawer?.(); } }}> ), headerRight: () => ( { // Navigate to notification screen if exists, or profile/other suitable route navigation.navigate(route.notifications); }}> {unreadCount > 0 ? ( {unreadCount > 99 ? '99+' : unreadCount} ) : null} ), })} /> ); }; const AddLeadStackNav = createNativeStackNavigator(); const AddLeadStack = () => { const { theme: colors } = useTheme(); const styles = getStyles(colors); return ( ({ headerTitle: 'Add Lead', headerLeft: () => ( { const parentNav = navigation.getParent>(); if (parentNav) { parentNav.openDrawer(); } else { (navigation as any).openDrawer?.(); } }}> ), })} /> ); }; const ProfileStackNav = createNativeStackNavigator(); const ProfileStack = () => { const { theme: colors } = useTheme(); const styles = getStyles(colors); return ( ({ headerTitle: 'Profile', headerLeft: () => ( { const parentNav = navigation.getParent>(); if (parentNav) { parentNav.openDrawer(); } else { (navigation as any).openDrawer?.(); } }}> ), headerRight: () => ( navigation.navigate(route.editProfile)} style={{ marginRight: 8, backgroundColor: colors.icon, width: 32, height: 32, borderRadius: 16, justifyContent: 'center', alignItems: 'center', shadowColor: colors.icon, shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, elevation: 3, }} activeOpacity={0.7} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}> ), })} /> ); }; export const TabStack = () => { const { theme: colors } = useTheme(); const styles = getStyles(colors); return ( ({ tabBarIcon: ({ focused, color }) => { if (tabRoute.name === route.addLead) { return ( ); } let iconName = 'square'; if (tabRoute.name === route.dashboard) { iconName = focused ? 'grid' : 'grid-outline'; } else if (tabRoute.name === route.leads) { iconName = focused ? 'funnel' : 'funnel-outline'; } else if (tabRoute.name === route.customers) { iconName = focused ? 'people' : 'people-outline'; } else if (tabRoute.name === route.profile) { iconName = focused ? 'person' : 'person-outline'; } return ( ); }, tabBarActiveTintColor: colors.icon, tabBarInactiveTintColor: colors.textMuted, tabBarStyle: styles.tabBar, tabBarLabelStyle: styles.tabBarLabel, headerShown: false, })}> ({ tabPress: (e) => { e.preventDefault(); navigation.navigate(route.dashboard, { screen: 'dashboardList', }); }, })} /> ({ tabPress: (e) => { e.preventDefault(); navigation.navigate(route.leads, { screen: route.leadsList, }); }, })} /> ({ tabPress: (e) => { e.preventDefault(); navigation.navigate(route.addLead, { screen: 'addLeadForm', }); }, })} /> ({ tabPress: (e) => { e.preventDefault(); navigation.navigate(route.customers, { screen: route.customersList, }); }, })} /> ({ tabPress: (e) => { e.preventDefault(); navigation.navigate(route.profile, { screen: 'profileForm', }); }, })} /> ); };