nostros/frontend/Pages/FeedNavigator/index.tsx

194 lines
7.5 KiB
TypeScript
Raw Normal View History

2023-01-13 11:51:30 +00:00
import * as React from 'react'
import { Platform, View } from 'react-native'
import type { DrawerNavigationProp } from '@react-navigation/drawer'
2023-01-13 16:36:05 +00:00
import { CardStyleInterpolators, createStackNavigator } from '@react-navigation/stack'
2023-01-16 20:06:12 +00:00
import { Appbar, Text, useTheme } from 'react-native-paper'
2023-01-15 14:22:02 +00:00
import RBSheet from 'react-native-raw-bottom-sheet'
2023-01-13 11:51:30 +00:00
import { useTranslation } from 'react-i18next'
2023-01-13 16:36:05 +00:00
import HomePage from '../HomePage'
2023-01-14 14:52:10 +00:00
import RelaysPage from '../RelaysPage'
import AboutPage from '../AboutPage'
import ProfileConfigPage from '../ProfileConfigPage'
2023-01-14 20:35:35 +00:00
import ProfilePage from '../ProfilePage'
2023-01-15 14:22:02 +00:00
import ProfileCard from '../../Components/ProfileCard'
2023-01-15 18:04:59 +00:00
import NotePage from '../NotePage'
2023-01-16 09:03:01 +00:00
import SendPage from '../SendPage'
2023-01-16 16:00:10 +00:00
import ConversationPage from '../ConversationPage'
2023-01-27 21:03:09 +00:00
import ConfigPage from '../ConfigPage'
2023-02-03 16:33:08 +00:00
import { RelayPoolContext } from '../../Contexts/RelayPoolContext'
import { AppContext } from '../../Contexts/AppContext'
2023-02-03 18:05:06 +00:00
import RelayCard from '../../Components/RelayCard'
2023-02-16 22:32:02 +00:00
import { updateAllDirectMessagesRead } from '../../Functions/DatabaseFunctions/DirectMessages'
2023-02-05 16:22:41 +00:00
import { getUnixTime } from 'date-fns'
2023-02-11 14:12:42 +00:00
import ContactsPage from '../ContactsPage'
import GroupPage from '../GroupPage'
import GroupHeaderIcon from '../../Components/GroupHeaderIcon'
2023-01-13 11:51:30 +00:00
2023-01-13 16:36:05 +00:00
export const HomeNavigator: React.FC = () => {
2023-01-13 11:51:30 +00:00
const theme = useTheme()
const { t } = useTranslation('common')
2023-02-03 16:33:08 +00:00
const { displayRelayDrawer, setDisplayrelayDrawer } = React.useContext(RelayPoolContext)
2023-02-08 16:09:13 +00:00
const { displayUserDrawer, setDisplayUserDrawer, setRefreshBottomBarAt, database } =
React.useContext(AppContext)
2023-01-16 20:06:12 +00:00
const bottomSheetRef = React.useRef<RBSheet>(null)
2023-01-15 14:22:02 +00:00
const bottomSheetProfileRef = React.useRef<RBSheet>(null)
2023-02-03 16:33:08 +00:00
const bottomSheetRelayRef = React.useRef<RBSheet>(null)
2023-01-13 11:51:30 +00:00
const Stack = React.useMemo(() => createStackNavigator(), [])
const cardStyleInterpolator = React.useMemo(
() =>
Platform.OS === 'android'
? CardStyleInterpolators.forFadeFromBottomAndroid
: CardStyleInterpolators.forHorizontalIOS,
[],
)
2023-01-15 14:22:02 +00:00
const bottomSheetStyles = React.useMemo(() => {
return {
container: {
backgroundColor: theme.colors.background,
paddingTop: 16,
paddingRight: 16,
paddingBottom: 32,
paddingLeft: 16,
2023-01-15 14:22:02 +00:00
borderTopRightRadius: 28,
2023-01-16 12:09:18 +00:00
borderTopLeftRadius: 28,
height: 'auto',
2023-01-15 14:22:02 +00:00
},
}
}, [])
2023-01-16 20:06:12 +00:00
const onPressQuestion: (pageName: string) => void = (pageName) => {
bottomSheetRef.current?.open()
}
2023-02-16 22:32:02 +00:00
const onMesssagesPressCheckAll: () => void = () => {
if (database) updateAllDirectMessagesRead(database)
setRefreshBottomBarAt(getUnixTime(new Date()))
}
const onGroupsPressCheckAll: () => void = () => {
if (database) updateAllDirectMessagesRead(database)
2023-02-05 16:22:41 +00:00
setRefreshBottomBarAt(getUnixTime(new Date()))
}
2023-02-03 16:33:08 +00:00
React.useEffect(() => {
if (displayRelayDrawer) bottomSheetRelayRef.current?.open()
}, [displayRelayDrawer])
React.useEffect(() => {
if (displayUserDrawer) bottomSheetProfileRef.current?.open()
}, [displayUserDrawer])
2023-01-13 11:51:30 +00:00
return (
<>
<Stack.Navigator
screenOptions={({ navigation }) => {
return {
detachPreviousScreen: !navigation.isFocused(),
cardStyleInterpolator,
2023-02-05 15:52:22 +00:00
header: (headerData) => {
const { navigation, route, back } = headerData
2023-02-05 16:22:41 +00:00
const routes = navigation.getState().routes
const routeState = routes[0]?.state
const history = routeState?.history ?? []
const historyKey = history[0]?.key
2023-01-13 11:51:30 +00:00
return (
<Appbar.Header>
{back ? (
<Appbar.BackAction onPress={() => navigation.goBack()} />
) : (navigation as any).openDrawer ? (
<Appbar.Action
icon='menu'
isLeading
onPress={() => (navigation as any as DrawerNavigationProp<{}>).openDrawer()}
/>
) : null}
2023-01-22 12:43:30 +00:00
<Appbar.Content
title={
route.params?.title ? route.params?.title : t(`homeNavigator.${route.name}`)
}
/>
2023-01-16 16:00:10 +00:00
{['Profile', 'Conversation'].includes(route.name) && (
2023-01-15 14:22:02 +00:00
<Appbar.Action
icon='dots-vertical'
onPress={() => {
2023-01-16 12:09:18 +00:00
const params = route?.params as { pubKey: string }
2023-02-03 16:33:08 +00:00
setDisplayUserDrawer(params?.pubKey ?? '')
2023-01-15 14:22:02 +00:00
}}
/>
)}
2023-01-16 20:06:12 +00:00
{['Relays'].includes(route.name) && (
<Appbar.Action
icon='help-circle-outline'
isLeading
onPress={() => onPressQuestion(route.name)}
/>
)}
2023-02-06 11:10:11 +00:00
{['Landing'].includes(route.name) && historyKey?.includes('messages-') && (
2023-02-16 22:32:02 +00:00
<Appbar.Action
icon='check-all'
isLeading
onPress={() => onMesssagesPressCheckAll()}
/>
)}
{['Landing'].includes(route.name) && historyKey?.includes('groups-') && (
<Appbar.Action
icon='check-all'
isLeading
onPress={() => onGroupsPressCheckAll()}
/>
2023-02-05 16:22:41 +00:00
)}
2023-02-11 19:18:22 +00:00
{['Group'].includes(route.name) && (
<GroupHeaderIcon groupId={route.params?.groupId} />
2023-02-11 14:12:42 +00:00
)}
2023-01-13 11:51:30 +00:00
</Appbar.Header>
)
},
}
}}
>
<Stack.Group>
2023-01-14 14:52:10 +00:00
<Stack.Screen name='Landing' component={HomePage} />
2023-01-20 14:09:05 +00:00
<Stack.Screen name='Note' component={NotePage} />
<Stack.Screen name='Send' component={SendPage} />
2023-01-24 18:23:33 +00:00
<Stack.Screen name='Repost' component={SendPage} />
2023-01-20 14:09:05 +00:00
<Stack.Screen name='Reply' component={SendPage} />
<Stack.Screen name='Conversation' component={ConversationPage} />
2023-02-11 19:18:22 +00:00
<Stack.Screen name='Group' component={GroupPage} />
2023-01-14 14:52:10 +00:00
</Stack.Group>
<Stack.Group>
2023-02-11 14:12:42 +00:00
<Stack.Screen name='Contacts' component={ContactsPage} />
2023-01-14 14:52:10 +00:00
<Stack.Screen name='Relays' component={RelaysPage} />
<Stack.Screen name='About' component={AboutPage} />
2023-01-27 21:03:09 +00:00
<Stack.Screen name='Config' component={ConfigPage} />
2023-01-14 14:52:10 +00:00
<Stack.Screen name='ProfileConfig' component={ProfileConfigPage} />
2023-01-14 20:35:35 +00:00
<Stack.Screen name='Profile' component={ProfilePage} />
2023-01-13 11:51:30 +00:00
</Stack.Group>
</Stack.Navigator>
2023-02-03 16:33:08 +00:00
<RBSheet
ref={bottomSheetProfileRef}
closeOnDragDown={true}
customStyles={bottomSheetStyles}
onClose={() => setDisplayUserDrawer(undefined)}
>
<ProfileCard bottomSheetRef={bottomSheetProfileRef} />
</RBSheet>
<RBSheet
ref={bottomSheetRelayRef}
closeOnDragDown={true}
customStyles={bottomSheetStyles}
onClose={() => setDisplayrelayDrawer(undefined)}
>
2023-02-03 18:05:06 +00:00
<RelayCard url={displayRelayDrawer} bottomSheetRef={bottomSheetRelayRef} />
2023-01-15 14:22:02 +00:00
</RBSheet>
2023-01-28 20:22:04 +00:00
<RBSheet ref={bottomSheetRef} closeOnDragDown={true} customStyles={bottomSheetStyles}>
2023-01-25 15:38:18 +00:00
<View>
<Text variant='headlineSmall'>{t('drawers.relaysTitle')}</Text>
<Text variant='bodyMedium'>{t('drawers.relaysDescription')}</Text>
</View>
2023-01-13 11:51:30 +00:00
</RBSheet>
</>
)
}
2023-01-13 16:36:05 +00:00
export default HomeNavigator