mirror of
https://github.com/tooot-app/app
synced 2025-02-18 04:40:57 +01:00
Add hashtag sparkline
This commit is contained in:
parent
7421ffd1bc
commit
8a7e78485d
@ -3,38 +3,61 @@ import { StackNavigationProp } from '@react-navigation/stack'
|
|||||||
import { TabLocalStackParamList } from '@utils/navigation/navigators'
|
import { TabLocalStackParamList } from '@utils/navigation/navigators'
|
||||||
import { StyleConstants } from '@utils/styles/constants'
|
import { StyleConstants } from '@utils/styles/constants'
|
||||||
import { useTheme } from '@utils/styles/ThemeManager'
|
import { useTheme } from '@utils/styles/ThemeManager'
|
||||||
import React, { useCallback } from 'react'
|
import { sumBy } from 'lodash'
|
||||||
import { Pressable } from 'react-native'
|
import React, { useCallback, useState } from 'react'
|
||||||
|
import { Dimensions, Pressable, View } from 'react-native'
|
||||||
|
import Sparkline from './Sparkline'
|
||||||
import CustomText from './Text'
|
import CustomText from './Text'
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
hashtag: Mastodon.Tag
|
hashtag: Mastodon.Tag
|
||||||
onPress?: () => void
|
onPress?: () => void
|
||||||
origin?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ComponentHashtag: React.FC<Props> = ({
|
const ComponentHashtag: React.FC<Props> = ({ hashtag, onPress: customOnPress }) => {
|
||||||
hashtag,
|
|
||||||
onPress: customOnPress,
|
|
||||||
origin
|
|
||||||
}) => {
|
|
||||||
const { colors } = useTheme()
|
const { colors } = useTheme()
|
||||||
const navigation =
|
const navigation = useNavigation<StackNavigationProp<TabLocalStackParamList>>()
|
||||||
useNavigation<StackNavigationProp<TabLocalStackParamList>>()
|
|
||||||
|
|
||||||
const onPress = useCallback(() => {
|
const onPress = useCallback(() => {
|
||||||
navigation.push('Tab-Shared-Hashtag', { hashtag: hashtag.name })
|
navigation.push('Tab-Shared-Hashtag', { hashtag: hashtag.name })
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const padding = StyleConstants.Spacing.S * 1.5
|
||||||
|
const width = Dimensions.get('window').width / 4
|
||||||
|
const [height, setHeight] = useState<number>(0)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Pressable
|
<Pressable
|
||||||
accessibilityRole='button'
|
accessibilityRole='button'
|
||||||
style={{ padding: StyleConstants.Spacing.S * 1.5 }}
|
style={{
|
||||||
|
flex: 1,
|
||||||
|
flexDirection: 'row',
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
padding
|
||||||
|
}}
|
||||||
onPress={customOnPress || onPress}
|
onPress={customOnPress || onPress}
|
||||||
|
onLayout={({
|
||||||
|
nativeEvent: {
|
||||||
|
layout: { height }
|
||||||
|
}
|
||||||
|
}) => setHeight(height - padding * 2 - 1)}
|
||||||
>
|
>
|
||||||
<CustomText fontStyle='M' style={{ color: colors.primaryDefault }}>
|
<CustomText
|
||||||
|
fontStyle='M'
|
||||||
|
style={{
|
||||||
|
flexShrink: 1,
|
||||||
|
color: colors.primaryDefault,
|
||||||
|
paddingRight: StyleConstants.Spacing.M
|
||||||
|
}}
|
||||||
|
numberOfLines={1}
|
||||||
|
>
|
||||||
#{hashtag.name}
|
#{hashtag.name}
|
||||||
</CustomText>
|
</CustomText>
|
||||||
|
<Sparkline
|
||||||
|
data={hashtag.history.map(h => parseInt(h.uses)).reverse()}
|
||||||
|
width={width}
|
||||||
|
height={height}
|
||||||
|
/>
|
||||||
</Pressable>
|
</Pressable>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
86
src/components/Sparkline.tsx
Normal file
86
src/components/Sparkline.tsx
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
import { useTheme } from '@utils/styles/ThemeManager'
|
||||||
|
import { maxBy, minBy } from 'lodash'
|
||||||
|
import React from 'react'
|
||||||
|
import Svg, { G, Path } from 'react-native-svg'
|
||||||
|
|
||||||
|
export interface Props {
|
||||||
|
data: number[]
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
margin?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const Sparkline: React.FC<Props> = ({ data, width, height, margin = 0 }) => {
|
||||||
|
const { colors } = useTheme()
|
||||||
|
|
||||||
|
const dataToPoints = ({
|
||||||
|
data,
|
||||||
|
width,
|
||||||
|
height
|
||||||
|
}: {
|
||||||
|
data: number[]
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
}): { x: number; y: number }[] => {
|
||||||
|
const max = maxBy(data) || 0
|
||||||
|
const min = minBy(data) || 0
|
||||||
|
const len = data.length
|
||||||
|
|
||||||
|
const vfactor = (height - margin * 2) / (max - min || 2)
|
||||||
|
const hfactor = (width - margin * 2) / (len - (len > 1 ? 1 : 0))
|
||||||
|
|
||||||
|
return data.map((d, i) => ({
|
||||||
|
x: i * hfactor + margin,
|
||||||
|
y: (max === min ? 1 : max - d) * vfactor + margin
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
const points = dataToPoints({ data, width, height })
|
||||||
|
const divisor = 0.25
|
||||||
|
let prev: { x: number; y: number }
|
||||||
|
const curve = (p: { x: number; y: number }) => {
|
||||||
|
let res
|
||||||
|
if (!prev) {
|
||||||
|
res = [p.x, p.y]
|
||||||
|
} else {
|
||||||
|
const len = (p.x - prev.x) * divisor
|
||||||
|
res = [
|
||||||
|
'C',
|
||||||
|
prev.x + len, // x1
|
||||||
|
prev.y, // y1
|
||||||
|
p.x - len, // x2
|
||||||
|
p.y, // y2
|
||||||
|
p.x, // x
|
||||||
|
p.y // y
|
||||||
|
]
|
||||||
|
}
|
||||||
|
prev = p
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
const linePoints = points.map(p => curve(p)).reduce((a, b) => a.concat(b))
|
||||||
|
const closePolyPoints = [
|
||||||
|
'L' + points[points.length - 1].x,
|
||||||
|
height - margin,
|
||||||
|
margin,
|
||||||
|
height - margin,
|
||||||
|
margin,
|
||||||
|
points[0].y
|
||||||
|
]
|
||||||
|
const fillPoints = linePoints.concat(closePolyPoints)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Svg height='auto' width={width}>
|
||||||
|
<G>
|
||||||
|
<Path d={'M' + fillPoints.join(' ')} fill={colors.blue} fillOpacity={0.1} />
|
||||||
|
<Path
|
||||||
|
d={'M' + linePoints.join(' ')}
|
||||||
|
stroke={colors.blue}
|
||||||
|
strokeWidth={1}
|
||||||
|
strokeLinejoin='round'
|
||||||
|
strokeLinecap='round'
|
||||||
|
/>
|
||||||
|
</G>
|
||||||
|
</Svg>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Sparkline
|
@ -46,7 +46,7 @@ const ComposeRootSuggestion: React.FC<Props> = ({ item }) => {
|
|||||||
return item.acct ? (
|
return item.acct ? (
|
||||||
<ComponentAccount account={item} props={{ onPress }} />
|
<ComponentAccount account={item} props={{ onPress }} />
|
||||||
) : (
|
) : (
|
||||||
<ComponentHashtag hashtag={item} onPress={onPress} origin='suggestion' />
|
<ComponentHashtag hashtag={item} onPress={onPress} />
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ const TabSharedSearch: React.FC<TabSharedStackScreenProps<'Tab-Shared-Search'>>
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}, [])
|
}, [mode])
|
||||||
|
|
||||||
const mapKeyToTranslations = {
|
const mapKeyToTranslations = {
|
||||||
accounts: t('shared.search.sections.accounts'),
|
accounts: t('shared.search.sections.accounts'),
|
||||||
@ -183,7 +183,7 @@ const TabSharedSearch: React.FC<TabSharedStackScreenProps<'Tab-Shared-Search'>>
|
|||||||
case 'accounts':
|
case 'accounts':
|
||||||
return <ComponentAccount account={item} />
|
return <ComponentAccount account={item} />
|
||||||
case 'hashtags':
|
case 'hashtags':
|
||||||
return <ComponentHashtag hashtag={item} origin='search' />
|
return <ComponentHashtag hashtag={item} />
|
||||||
case 'statuses':
|
case 'statuses':
|
||||||
return <TimelineDefault item={item} disableDetails />
|
return <TimelineDefault item={item} disableDetails />
|
||||||
default:
|
default:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user