tooot/src/screens/Tabs.tsx

150 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-04-09 21:43:12 +02:00
import GracefullyImage from '@components/GracefullyImage'
2021-01-30 01:29:15 +01:00
import haptics from '@components/haptics'
import Icon from '@components/Icon'
import {
BottomTabNavigationOptions,
createBottomTabNavigator
} from '@react-navigation/bottom-tabs'
import { NavigatorScreenParams } from '@react-navigation/native'
2021-03-02 01:17:06 +01:00
import { StackScreenProps } from '@react-navigation/stack'
2021-02-11 23:42:13 +01:00
import { getPreviousTab } from '@utils/slices/contextsSlice'
2021-03-04 01:03:53 +01:00
import {
getInstanceAccount,
getInstanceActive
} from '@utils/slices/instancesSlice'
2021-05-09 21:59:03 +02:00
import {
getVersionUpdate,
retriveVersionLatest
} from '@utils/slices/versionSlice'
2021-01-30 01:29:15 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2021-05-09 21:59:03 +02:00
import React, { useCallback, useEffect, useMemo } from 'react'
import { Platform } from 'react-native'
import { useDispatch, useSelector } from 'react-redux'
2021-01-30 01:29:15 +01:00
import TabLocal from './Tabs/Local'
import TabMe from './Tabs/Me'
import TabNotifications from './Tabs/Notifications'
import TabPublic from './Tabs/Public'
export type ScreenTabsParamList = {
'Tab-Local': NavigatorScreenParams<Nav.TabLocalStackParamList>
'Tab-Public': NavigatorScreenParams<Nav.TabPublicStackParamList>
2021-01-31 03:09:35 +01:00
'Tab-Compose': NavigatorScreenParams<Nav.ScreenComposeStackParamList>
2021-01-30 01:29:15 +01:00
'Tab-Notifications': NavigatorScreenParams<Nav.TabNotificationsStackParamList>
'Tab-Me': NavigatorScreenParams<Nav.TabMeStackParamList>
}
export type ScreenTabsProp = StackScreenProps<
Nav.RootStackParamList,
'Screen-Tabs'
>
const Tab = createBottomTabNavigator<Nav.ScreenTabsStackParamList>()
2021-02-10 00:40:44 +01:00
const ScreenTabs = React.memo(
({ navigation }: ScreenTabsProp) => {
2021-03-02 01:17:06 +01:00
const { mode, theme } = useTheme()
const instanceActive = useSelector(getInstanceActive)
2021-03-04 01:03:53 +01:00
const instanceAccount = useSelector(
getInstanceAccount,
(prev, next) => prev?.avatarStatic === next?.avatarStatic
2021-02-28 17:41:21 +01:00
)
2021-02-10 00:40:44 +01:00
const screenOptions = useCallback(
({ route }): BottomTabNavigationOptions => ({
headerShown: false,
tabBarActiveTintColor: theme.primaryDefault,
tabBarInactiveTintColor: theme.secondary,
tabBarShowLabel: false,
...(Platform.OS === 'android' && { tabBarHideOnKeyboard: true }),
tabBarStyle: { display: instanceActive !== -1 ? 'flex' : 'none' },
2021-02-10 00:40:44 +01:00
tabBarIcon: ({
focused,
color,
size
}: {
focused: boolean
color: string
size: number
}) => {
switch (route.name) {
case 'Tab-Local':
return <Icon name='Home' size={size} color={color} />
case 'Tab-Public':
return <Icon name='Globe' size={size} color={color} />
case 'Tab-Compose':
return <Icon name='Plus' size={size} color={color} />
case 'Tab-Notifications':
return <Icon name='Bell' size={size} color={color} />
case 'Tab-Me':
2021-04-09 21:43:12 +02:00
return (
<GracefullyImage
key={instanceAccount?.avatarStatic}
uri={{ original: instanceAccount?.avatarStatic }}
dimension={{
width: size,
height: size
2021-03-02 01:17:06 +01:00
}}
2021-02-10 00:40:44 +01:00
style={{
borderRadius: size,
2021-04-09 21:43:12 +02:00
overflow: 'hidden',
2021-02-10 00:40:44 +01:00
borderWidth: focused ? 2 : 0,
borderColor: focused ? theme.secondary : color
}}
/>
)
default:
return <Icon name='AlertOctagon' size={size} color={color} />
}
2021-01-30 01:29:15 +01:00
}
2021-02-10 00:40:44 +01:00
}),
2021-04-09 21:43:12 +02:00
[instanceAccount?.avatarStatic, instanceActive]
2021-02-10 00:40:44 +01:00
)
2021-02-10 00:40:44 +01:00
const composeListeners = useMemo(
() => ({
tabPress: (e: any) => {
2021-01-30 01:29:15 +01:00
e.preventDefault()
2021-02-20 19:12:44 +01:00
haptics('Light')
navigation.navigate('Screen-Compose')
2021-01-30 01:29:15 +01:00
}
2021-02-10 00:40:44 +01:00
}),
2021-02-20 19:12:44 +01:00
[]
2021-02-10 00:40:44 +01:00
)
const composeComponent = useCallback(() => null, [])
2021-02-20 19:12:44 +01:00
const previousTab = useSelector(getPreviousTab, () => true)
2021-05-09 21:59:03 +02:00
const versionUpdate = useSelector(getVersionUpdate)
const dispatch = useDispatch()
useEffect(() => {
dispatch(retriveVersionLatest())
}, [])
const tabMeOptions = useMemo(() => {
if (versionUpdate) {
return { tabBarBadge: 1 }
}
}, [versionUpdate])
2021-02-10 00:40:44 +01:00
return (
<Tab.Navigator
2021-02-20 19:12:44 +01:00
initialRouteName={instanceActive !== -1 ? previousTab : 'Tab-Me'}
2021-02-10 00:40:44 +01:00
screenOptions={screenOptions}
>
2021-02-20 19:12:44 +01:00
<Tab.Screen name='Tab-Local' component={TabLocal} />
2021-02-10 00:40:44 +01:00
<Tab.Screen name='Tab-Public' component={TabPublic} />
<Tab.Screen
name='Tab-Compose'
component={composeComponent}
listeners={composeListeners}
/>
2021-03-01 00:28:14 +01:00
<Tab.Screen name='Tab-Notifications' component={TabNotifications} />
2021-05-09 21:59:03 +02:00
<Tab.Screen name='Tab-Me' component={TabMe} options={tabMeOptions} />
2021-02-10 00:40:44 +01:00
</Tab.Navigator>
)
},
() => true
)
2021-01-30 01:29:15 +01:00
export default ScreenTabs