index.tsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Text, View, TouchableWithoutFeedback } from 'react-native';
  2. import { SvgProps } from 'react-native-svg';
  3. import { NAVIGATION_PAGES } from '../../types';
  4. import MapIcon from '../../../assets/icons/bottom-navigation/map.svg';
  5. import TravellersIcon from '../../../assets/icons/bottom-navigation/travellers.svg';
  6. import GlobeIcon from '../../../assets/icons/bottom-navigation/globe-solid.svg';
  7. import MenuIcon from '../../../assets/icons/menu.svg';
  8. import MessagesIcon from '../../../assets/icons/bottom-navigation/messages.svg';
  9. import { Colors } from '../../theme';
  10. import { styles } from './style';
  11. import BlinkingDot from '../BlinkingDot';
  12. import { useNotification } from 'src/contexts/NotificationContext';
  13. import MessagesDot from '../MessagesDot';
  14. import { storage, StoreType } from 'src/storage';
  15. import { WarningModal } from '../WarningModal';
  16. import { useState } from 'react';
  17. import { useMessagesStore } from 'src/stores/unreadMessagesStore';
  18. const getTabIcon = (routeName: string) => {
  19. switch (routeName) {
  20. case NAVIGATION_PAGES.IN_APP_MAP_TAB:
  21. return MapIcon;
  22. case NAVIGATION_PAGES.IN_APP_TRAVELLERS_TAB:
  23. return TravellersIcon;
  24. case NAVIGATION_PAGES.IN_APP_TRAVELS_TAB:
  25. return GlobeIcon;
  26. case NAVIGATION_PAGES.IN_APP_MESSAGES_TAB:
  27. return MessagesIcon;
  28. case NAVIGATION_PAGES.MENU_DRAWER:
  29. return MenuIcon;
  30. default:
  31. return null;
  32. }
  33. };
  34. const TabBarButton = ({
  35. label,
  36. onPress,
  37. focused,
  38. navigation
  39. }: {
  40. label: string;
  41. onPress: () => void;
  42. focused: boolean;
  43. navigation: any;
  44. }) => {
  45. const IconComponent: React.FC<SvgProps> | null = getTabIcon(label);
  46. const token = storage.get('token', StoreType.STRING);
  47. const { isNotificationActive } = useNotification();
  48. const unreadMessagesCount = useMessagesStore((state) => state.unreadMessagesCount);
  49. const [isWarningModalVisible, setIsWarningModalVisible] = useState(false);
  50. let currentColor = focused ? Colors.DARK_BLUE : Colors.LIGHT_GRAY;
  51. return (
  52. <TouchableWithoutFeedback
  53. onPress={() => {
  54. if (label === NAVIGATION_PAGES.MENU_DRAWER) {
  55. (navigation as any)?.openDrawer();
  56. } else if (label === NAVIGATION_PAGES.IN_APP_MESSAGES_TAB && !token) {
  57. setIsWarningModalVisible(true);
  58. } else {
  59. onPress();
  60. }
  61. }}
  62. >
  63. <View style={styles.buttonStyle}>
  64. <View style={{ alignItems: 'center' }}>
  65. {IconComponent && <IconComponent width={24} height={24} fill={currentColor} />}
  66. {label === NAVIGATION_PAGES.IN_APP_TRAVELLERS_TAB && isNotificationActive && (
  67. <BlinkingDot diameter={8} />
  68. )}
  69. {label === NAVIGATION_PAGES.IN_APP_MESSAGES_TAB && unreadMessagesCount > 0 && (
  70. <MessagesDot messagesCount={unreadMessagesCount} />
  71. )}
  72. <Text style={[styles.labelStyle, { color: currentColor }]}>{label}</Text>
  73. </View>
  74. <WarningModal
  75. type={'unauthorized'}
  76. isVisible={isWarningModalVisible}
  77. onClose={() => setIsWarningModalVisible(false)}
  78. />
  79. </View>
  80. </TouchableWithoutFeedback>
  81. );
  82. };
  83. export default TabBarButton;