tooot/src/Index.tsx

142 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-10-23 09:22:17 +02:00
import 'react-native-gesture-handler'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { NavigationContainer } from '@react-navigation/native'
import { enableScreens } from 'react-native-screens'
import React, { useEffect } from 'react'
2020-11-29 18:08:31 +01:00
import { StatusBar } from 'react-native'
import Toast from 'react-native-toast-message'
2020-11-28 17:07:30 +01:00
import { Feather } from '@expo/vector-icons'
2020-10-23 09:22:17 +02:00
2020-12-13 14:04:25 +01:00
import ScreenLocal from '@screens/Local'
import ScreenPublic from '@screens/Public'
import ScreenNotifications from '@screens/Notifications'
import ScreenMe from '@screens/Me'
2020-10-23 09:22:17 +02:00
2020-12-13 14:04:25 +01:00
import { themes } from '@utils/styles/themes'
import { useTheme } from '@utils/styles/ThemeManager'
import getCurrentTab from '@utils/getCurrentTab'
import { toastConfig } from '@components/toast'
2020-11-30 00:24:53 +01:00
import { useTranslation } from 'react-i18next'
import { useDispatch, useSelector } from 'react-redux'
import {
getLocalToken,
getLocalUrl,
updateLocal
} from './utils/slices/instancesSlice'
2020-11-23 00:07:32 +01:00
2020-10-26 00:27:53 +01:00
enableScreens()
2020-11-24 00:18:47 +01:00
const Tab = createBottomTabNavigator<RootStackParamList>()
export type RootStackParamList = {
'Screen-Local': undefined
'Screen-Public': { publicTab: boolean }
'Screen-Post': undefined
'Screen-Notifications': undefined
'Screen-Me': undefined
}
2020-10-23 09:22:17 +02:00
2020-10-31 21:04:46 +01:00
export const Index: React.FC = () => {
const dispatch = useDispatch()
2020-12-13 21:09:21 +01:00
const localInstance = useSelector(getLocalUrl)
const localToken = useSelector(getLocalToken)
2020-11-30 00:24:53 +01:00
const { i18n } = useTranslation()
2020-11-23 00:07:32 +01:00
const { mode, theme } = useTheme()
2020-11-29 18:08:31 +01:00
enum barStyle {
light = 'dark-content',
dark = 'light-content'
}
2020-11-23 00:07:32 +01:00
useEffect(() => {
if (localInstance && localToken) {
dispatch(updateLocal({ url: localInstance, token: localToken }))
}
}, [])
2020-10-23 09:22:17 +02:00
return (
2020-11-29 18:08:31 +01:00
<>
<StatusBar barStyle={barStyle[mode]} />
2020-11-30 00:24:53 +01:00
<NavigationContainer theme={themes[mode]} key={i18n.language}>
2020-11-29 18:08:31 +01:00
<Tab.Navigator
initialRouteName={localInstance ? 'Screen-Local' : 'Screen-Public'}
2020-11-29 18:08:31 +01:00
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
2020-12-13 14:04:25 +01:00
let name: any
2020-12-13 21:09:21 +01:00
let updateColor: string = color
2020-11-29 18:08:31 +01:00
switch (route.name) {
case 'Screen-Local':
name = 'home'
break
case 'Screen-Public':
name = 'globe'
2020-12-13 21:09:21 +01:00
!focused && (updateColor = theme.secondary)
2020-11-29 18:08:31 +01:00
break
case 'Screen-Post':
name = 'plus'
break
case 'Screen-Notifications':
name = 'bell'
break
case 'Screen-Me':
2020-12-01 00:44:28 +01:00
name = focused ? 'meh' : 'smile'
2020-12-13 21:09:21 +01:00
!focused && (updateColor = theme.secondary)
2020-11-29 18:08:31 +01:00
break
default:
name = 'alert-octagon'
break
}
2020-12-13 21:09:21 +01:00
return <Feather name={name} size={size} color={updateColor} />
2020-11-15 23:33:01 +01:00
}
})}
2020-11-29 18:08:31 +01:00
tabBarOptions={{
activeTintColor: theme.primary,
2020-12-13 21:09:21 +01:00
inactiveTintColor: localInstance ? theme.secondary : theme.disabled,
2020-11-29 18:08:31 +01:00
showLabel: false
}}
2020-11-22 00:46:23 +01:00
>
2020-12-13 21:09:21 +01:00
<Tab.Screen
name='Screen-Local'
component={ScreenLocal}
listeners={{
tabPress: e => {
if (!localInstance) {
e.preventDefault()
}
}
}}
/>
2020-11-29 18:08:31 +01:00
<Tab.Screen name='Screen-Public' component={ScreenPublic} />
<Tab.Screen
name='Screen-Post'
listeners={({ navigation, route }) => ({
tabPress: e => {
e.preventDefault()
2020-12-13 21:09:21 +01:00
localInstance &&
navigation.navigate(getCurrentTab(navigation), {
screen: 'Screen-Shared-Compose'
})
2020-11-29 18:08:31 +01:00
}
})}
>
2020-12-13 23:38:37 +01:00
{() => null}
2020-11-29 18:08:31 +01:00
</Tab.Screen>
<Tab.Screen
name='Screen-Notifications'
component={ScreenNotifications}
2020-12-13 21:09:21 +01:00
listeners={{
tabPress: e => {
if (!localInstance) {
e.preventDefault()
}
}
}}
2020-11-29 18:08:31 +01:00
/>
<Tab.Screen name='Screen-Me' component={ScreenMe} />
</Tab.Navigator>
2020-11-29 18:08:31 +01:00
<Toast ref={(ref: any) => Toast.setRef(ref)} config={toastConfig} />
</NavigationContainer>
</>
2020-10-23 09:22:17 +02:00
)
}