tooot/src/components/Hashtag.tsx

65 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import { useNavigation } from '@react-navigation/native'
import { StackNavigationProp } from '@react-navigation/stack'
2022-04-29 23:57:18 +02:00
import { TabLocalStackParamList } from '@utils/navigation/navigators'
2021-01-16 00:00:31 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2022-12-03 23:10:20 +01:00
import React, { useCallback, useState } from 'react'
import { Dimensions, Pressable } from 'react-native'
2022-12-03 23:10:20 +01:00
import Sparkline from './Sparkline'
import CustomText from './Text'
2021-01-16 00:00:31 +01:00
export interface Props {
2021-01-24 02:25:43 +01:00
hashtag: Mastodon.Tag
onPress?: () => void
2021-01-16 00:00:31 +01:00
}
2022-12-03 23:10:20 +01:00
const ComponentHashtag: React.FC<Props> = ({ hashtag, onPress: customOnPress }) => {
2022-02-12 14:51:01 +01:00
const { colors } = useTheme()
2022-12-03 23:10:20 +01:00
const navigation = useNavigation<StackNavigationProp<TabLocalStackParamList>>()
2021-01-24 02:25:43 +01:00
const onPress = useCallback(() => {
2021-01-30 01:29:15 +01:00
navigation.push('Tab-Shared-Hashtag', { hashtag: hashtag.name })
2021-01-24 02:25:43 +01:00
}, [])
2021-01-16 00:00:31 +01:00
const padding = StyleConstants.Spacing.Global.PagePadding
2022-12-03 23:10:20 +01:00
const width = Dimensions.get('window').width / 4
const [height, setHeight] = useState<number>(0)
2021-01-16 00:00:31 +01:00
return (
2021-04-09 21:43:12 +02:00
<Pressable
accessibilityRole='button'
2022-12-03 23:10:20 +01:00
style={{
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
padding
}}
2021-04-09 21:43:12 +02:00
onPress={customOnPress || onPress}
2022-12-03 23:10:20 +01:00
onLayout={({
nativeEvent: {
layout: { height }
}
}) => setHeight(height - padding * 2 - 1)}
2021-04-09 21:43:12 +02:00
>
2022-12-03 23:10:20 +01:00
<CustomText
fontStyle='M'
style={{
flexShrink: 1,
color: colors.primaryDefault,
paddingRight: StyleConstants.Spacing.M
}}
numberOfLines={1}
>
2021-01-24 02:25:43 +01:00
#{hashtag.name}
</CustomText>
2022-12-03 23:10:20 +01:00
<Sparkline
data={hashtag.history.map(h => parseInt(h.uses)).reverse()}
width={width}
height={height}
/>
2021-01-16 00:00:31 +01:00
</Pressable>
)
}
export default ComponentHashtag