tooot/src/screens/Tabs/Public.tsx

116 lines
3.5 KiB
TypeScript
Raw Normal View History

import { HeaderRight } from '@components/Header'
2021-02-08 23:47:20 +01:00
import Timeline from '@components/Timeline'
2021-02-27 16:33:54 +01:00
import TimelineDefault from '@components/Timeline/Default'
import SegmentedControl from '@react-native-community/segmented-control'
import { createNativeStackNavigator } from '@react-navigation/native-stack'
2022-11-14 15:54:51 +01:00
import { ScreenTabsScreenProps, TabPublicStackParamList } from '@utils/navigation/navigators'
import usePopToTop from '@utils/navigation/usePopToTop'
2021-02-27 16:33:54 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useCallback, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
2022-11-14 15:54:51 +01:00
import { Dimensions } from 'react-native'
import { TabView } from 'react-native-tab-view'
2022-11-20 16:14:08 +01:00
import TabShared from './Shared'
2021-08-29 15:25:38 +02:00
const Stack = createNativeStackNavigator<TabPublicStackParamList>()
2021-01-30 01:29:15 +01:00
const TabPublic = React.memo(
2021-08-29 15:25:38 +02:00
({ navigation }: ScreenTabsScreenProps<'Tab-Public'>) => {
2021-03-28 23:31:10 +02:00
const { t, i18n } = useTranslation('screenTabs')
2022-02-17 00:09:19 +01:00
const { mode, theme } = useTheme()
const [segment, setSegment] = useState(0)
2021-02-27 16:33:54 +01:00
const pages: {
title: string
2022-01-16 23:26:05 +01:00
key: Extract<App.Pages, 'Local' | 'LocalPublic'>
2021-02-27 16:33:54 +01:00
}[] = [
{
2021-03-28 23:31:10 +02:00
title: t('tabs.public.segments.left'),
2021-02-27 16:33:54 +01:00
key: 'LocalPublic'
},
{
2021-03-28 23:31:10 +02:00
title: t('tabs.public.segments.right'),
2021-02-27 16:33:54 +01:00
key: 'Local'
}
]
const screenOptionsRoot = useMemo(
() => ({
headerTitle: () => (
<SegmentedControl
appearance={mode}
values={pages.map(p => p.title)}
selectedIndex={segment}
2022-11-14 15:54:51 +01:00
onChange={({ nativeEvent }) => setSegment(nativeEvent.selectedSegmentIndex)}
style={{ flexBasis: '65%' }}
/>
),
headerRight: () => (
<HeaderRight
2021-04-09 21:43:12 +02:00
accessibilityLabel={t('common.search.accessibilityLabel')}
accessibilityHint={t('common.search.accessibilityHint')}
content='Search'
2022-11-29 23:44:11 +01:00
onPress={() =>
2021-03-14 00:47:55 +01:00
navigation.navigate('Tab-Public', {
screen: 'Tab-Shared-Search',
params: { text: undefined }
})
2022-11-29 23:44:11 +01:00
}
/>
)
}),
2022-02-17 00:09:19 +01:00
[theme, segment, i18n.language]
)
2021-02-27 16:33:54 +01:00
const routes = pages.map(p => ({ key: p.key }))
2021-03-15 22:30:29 +01:00
const renderScene = useCallback(
({
2021-02-27 16:33:54 +01:00
route: { key: page }
}: {
route: {
2022-01-16 23:26:05 +01:00
key: Extract<App.Pages, 'Local' | 'LocalPublic'>
}
}) => {
2021-02-27 16:33:54 +01:00
const queryKey: QueryKeyTimeline = ['Timeline', { page }]
2022-01-16 23:26:05 +01:00
return (
<Timeline
queryKey={queryKey}
lookback={page}
2022-02-04 23:13:32 +01:00
customProps={{
2022-11-14 15:54:51 +01:00
renderItem: ({ item }: any) => <TimelineDefault item={item} queryKey={queryKey} />
2022-02-04 23:13:32 +01:00
}}
2022-01-16 23:26:05 +01:00
/>
2021-02-27 16:33:54 +01:00
)
},
2021-02-27 16:33:54 +01:00
[]
)
const children = useCallback(
2021-03-15 22:30:29 +01:00
() => (
<TabView
lazy
swipeEnabled
renderScene={renderScene}
renderTabBar={() => null}
onIndexChange={index => setSegment(index)}
navigationState={{ index: segment, routes }}
initialLayout={{ width: Dimensions.get('screen').width }}
/>
),
[segment]
)
2022-11-14 15:54:51 +01:00
usePopToTop()
return (
<Stack.Navigator screenOptions={{ headerShadowVisible: false }}>
2022-11-14 15:54:51 +01:00
<Stack.Screen name='Tab-Public-Root' options={screenOptionsRoot} children={children} />
2022-11-20 16:14:08 +01:00
{TabShared({ Stack })}
</Stack.Navigator>
)
2021-01-22 01:34:20 +01:00
},
() => true
)
2020-10-31 21:04:46 +01:00
2021-01-30 01:29:15 +01:00
export default TabPublic