tooot/src/screens/Shared/Relationships.tsx

74 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-01-04 18:29:02 +01:00
import SegmentedControl from '@react-native-community/segmented-control'
import { useNavigation } from '@react-navigation/native'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useEffect, useState } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
2021-01-04 18:29:02 +01:00
import { Dimensions, StyleSheet, View } from 'react-native'
import { TabView } from 'react-native-tab-view'
import RelationshipsList from './Relationships/List'
2021-01-07 22:18:14 +01:00
import { SharedRelationshipsProp } from './sharedScreens'
2021-01-04 18:29:02 +01:00
2021-01-07 22:18:14 +01:00
const ScreenSharedRelationships: React.FC<SharedRelationshipsProp> = ({
2021-01-04 18:29:02 +01:00
route: {
params: { account, initialType }
}
}) => {
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('sharedRelationships')
2021-01-04 18:29:02 +01:00
const { mode } = useTheme()
const navigation = useNavigation()
const [segment, setSegment] = useState(initialType === 'following' ? 0 : 1)
useEffect(() => {
const updateHeaderRight = () =>
navigation.setOptions({
headerCenter: () => (
<View style={styles.segmentsContainer}>
<SegmentedControl
appearance={mode}
2021-01-19 01:13:45 +01:00
values={[t('heading.segments.left'), t('heading.segments.right')]}
2021-01-04 18:29:02 +01:00
selectedIndex={segment}
onChange={({ nativeEvent }) =>
setSegment(nativeEvent.selectedSegmentIndex)
}
/>
</View>
)
})
return updateHeaderRight()
2021-01-14 22:53:01 +01:00
}, [segment, mode])
2021-01-04 18:29:02 +01:00
2021-01-07 22:18:14 +01:00
const routes: {
key: SharedRelationshipsProp['route']['params']['initialType']
}[] = [{ key: 'following' }, { key: 'followers' }]
2021-01-04 18:29:02 +01:00
const renderScene = ({
route
}: {
route: {
2021-01-07 22:18:14 +01:00
key: SharedRelationshipsProp['route']['params']['initialType']
2021-01-04 18:29:02 +01:00
}
}) => {
return <RelationshipsList id={account.id} type={route.key} />
}
return (
<TabView
lazy
swipeEnabled
renderScene={renderScene}
renderTabBar={() => null}
onIndexChange={index => setSegment(index)}
2021-01-14 22:53:01 +01:00
navigationState={{ index: segment, routes }}
2021-01-16 00:00:31 +01:00
initialLayout={{ width: Dimensions.get('screen').width }}
2021-01-04 18:29:02 +01:00
/>
)
}
const styles = StyleSheet.create({
segmentsContainer: {
flexBasis: '60%'
}
})
export default React.memo(ScreenSharedRelationships, () => true)