import React from 'react'; import { Text, View, TouchableOpacity, ScrollView, Image, } from 'react-native'; import { DrawerContentComponentProps } from '@react-navigation/drawer'; import Icon from 'react-native-vector-icons/Ionicons'; import { menuItems } from '@mock-data'; import { useTheme } from '@theme'; import { DrawerItemProps } from '@interfaces'; import { route } from '@utils'; import { getStyles } from './customDrawerContent.style'; import { useAppDispatch, useAppSelector, logout, RootState } from '@store'; const DrawerItem = ({ label, iconName, focused, onPress }: DrawerItemProps) => { const { theme: colors } = useTheme(); const styles = getStyles(colors); return ( {focused && } {label} ); }; export const CustomDrawerContent = (props: DrawerContentComponentProps) => { const { state, navigation } = props; const { theme: colors } = useTheme(); const styles = getStyles(colors); const dispatch = useAppDispatch(); const userData = useAppSelector((state: RootState) => state.auth.user_data); const handleLogout = async () => { try { dispatch(logout()); } catch (e) { console.error(e); } navigation.reset({ index: 0, routes: [{ name: 'AuthStack' }], }); }; const activeRouteName = state.routeNames[state.index]; // User details const fullName = userData?.full_name || `${userData?.firstname || ''} ${userData?.lastname || ''}`.trim() || 'User'; const email = userData?.email || 'N/A'; const isAdmin = userData?.admin === '1'; const roleText = isAdmin ? 'Administrator' : 'Staff Member'; const isActive = userData?.active === '1'; const hasProfileImage = userData?.profile_image && userData.profile_image.startsWith('http'); const initials = fullName .split(' ') .filter(Boolean) .map(n => n[0]) .join('') .substring(0, 2) .toUpperCase() || 'U'; return ( {/* ── Drawer Header (User Profile) ── */} navigation.navigate('home', { screen: route.profile })}> {hasProfileImage ? ( ) : ( {initials} )} {isActive && } {fullName} {email} {roleText} {/* ── Navigation Menu List ── */} {menuItems.map(item => { // Find currently active nested tab route let activeTabName = ''; try { let currentRoute = state.routes[state.index]; if (currentRoute.name === 'home' && currentRoute.state) { const nestedIndex = currentRoute.state.index ?? 0; activeTabName = currentRoute.state.routes[nestedIndex].name; } else { activeTabName = currentRoute.name; } } catch (e) { activeTabName = ''; } const isFocused = (item.route === 'home' && activeTabName === route.dashboard) || (item.route !== 'home' && activeTabName === item.route); return ( { if (item.route === 'home') { navigation.navigate('home', { screen: route.dashboard }); } else { navigation.navigate('home', { screen: item.route }); } }} /> ); })} {/* ── Drawer Footer (Logout) ── */} Sign Out ); };