tooot/src/components/Hashtag.tsx

89 lines
2.6 KiB
TypeScript
Raw Permalink 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'
import React, { PropsWithChildren, useState } from 'react'
2023-04-17 23:33:40 +02:00
import { Dimensions, Pressable, Text, View } from 'react-native'
2022-12-03 23:10:20 +01:00
import Sparkline from './Sparkline'
import CustomText from './Text'
2023-04-17 23:33:40 +02:00
import { sumBy } from 'lodash'
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-10 20:19:18 +01:00
const ComponentHashtag: React.FC<PropsWithChildren & Props> = ({
hashtag,
onPress: customOnPress,
children
}) => {
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 = () => {
2023-01-26 23:07:13 +01:00
navigation.push('Tab-Shared-Hashtag', { tag_name: hashtag.name })
}
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)
2023-04-17 23:33:40 +02:00
const sum = sumBy(hashtag.history, h => parseInt(h.uses))
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',
2022-12-10 20:19:18 +01:00
alignItems: 'center',
2022-12-03 23:10:20 +01:00
justifyContent: 'space-between',
padding
}}
2021-04-09 21:43:12 +02:00
onPress={customOnPress || onPress}
>
2023-04-17 23:33:40 +02:00
<View
2022-12-03 23:10:20 +01:00
style={{
flexShrink: 1,
paddingRight: StyleConstants.Spacing.M
}}
>
2023-04-17 23:33:40 +02:00
<CustomText fontStyle='M' style={{ color: colors.primaryDefault }} numberOfLines={1}>
#{hashtag.name}
{sum ? (
<>
{' '}
<CustomText fontStyle='S' style={{ color: colors.secondary }}>
({sumBy(hashtag.history, h => parseInt(h.uses))})
</CustomText>
</>
) : null}
</CustomText>
</View>
{hashtag.history?.length ? (
<View
style={{ flexDirection: 'row', alignItems: 'center', alignSelf: 'stretch' }}
onLayout={({
nativeEvent: {
layout: { height }
}
}) => setHeight(height)}
>
<Sparkline
data={hashtag.history.map(h => parseInt(h.uses)).reverse()}
width={width}
height={height}
margin={children ? StyleConstants.Spacing.S : undefined}
2023-04-17 23:33:40 +02:00
color={!sum ? colors.disabled : undefined}
/>
{children}
</View>
) : null}
2021-01-16 00:00:31 +01:00
</Pressable>
)
}
export default ComponentHashtag