tooot/src/components/Emojis/List.tsx

125 lines
3.7 KiB
TypeScript
Raw Normal View History

import CustomText from '@components/Text'
2022-04-30 23:47:52 +02:00
import { useAppDispatch } from '@root/store'
2021-05-09 21:59:03 +02:00
import { useAccessibility } from '@utils/accessibility/AccessibilityManager'
2022-02-13 22:14:16 +01:00
import { countInstanceEmoji } from '@utils/slices/instancesSlice'
2021-05-09 21:59:03 +02:00
import { StyleConstants } from '@utils/styles/constants'
import layoutAnimation from '@utils/styles/layoutAnimation'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useCallback, useContext, useEffect, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import {
AccessibilityInfo,
findNodeHandle,
Pressable,
SectionList,
View
} from 'react-native'
import FastImage from 'react-native-fast-image'
import validUrl from 'valid-url'
2021-05-12 15:40:55 +02:00
import EmojisContext from './helpers/EmojisContext'
2021-05-09 21:59:03 +02:00
const EmojisList = React.memo(
() => {
2022-04-30 23:47:52 +02:00
const dispatch = useAppDispatch()
2021-05-09 21:59:03 +02:00
const { reduceMotionEnabled } = useAccessibility()
const { t } = useTranslation()
const { emojisState, emojisDispatch } = useContext(EmojisContext)
2022-02-12 14:51:01 +01:00
const { colors } = useTheme()
2021-05-09 21:59:03 +02:00
const listHeader = useCallback(
({ section: { title } }) => (
<CustomText
fontStyle='S'
style={{ position: 'absolute', color: colors.secondary }}
>
{title}
</CustomText>
2021-05-09 21:59:03 +02:00
),
[]
)
const listItem = useCallback(
({ index, item }: { item: Mastodon.Emoji[]; index: number }) => {
return (
<View
key={index}
style={{
flex: 1,
flexWrap: 'wrap',
marginTop: StyleConstants.Spacing.M,
marginRight: StyleConstants.Spacing.S
}}
>
2021-05-09 21:59:03 +02:00
{item.map(emoji => {
const uri = reduceMotionEnabled ? emoji.static_url : emoji.url
if (validUrl.isHttpsUri(uri)) {
return (
<Pressable
key={emoji.shortcode}
2022-02-13 22:14:16 +01:00
onPress={() => {
2021-05-09 21:59:03 +02:00
emojisDispatch({
type: 'shortcode',
payload: `:${emoji.shortcode}:`
})
2022-02-13 22:14:16 +01:00
dispatch(countInstanceEmoji(emoji))
}}
2021-05-09 21:59:03 +02:00
>
<FastImage
accessibilityLabel={t(
'common:customEmoji.accessibilityLabel',
{
emoji: emoji.shortcode
}
)}
accessibilityHint={t(
'screenCompose:content.root.footer.emojis.accessibilityHint'
)}
source={{ uri }}
style={{
width: 32,
height: 32,
padding: StyleConstants.Spacing.S,
margin: StyleConstants.Spacing.S
}}
2021-05-09 21:59:03 +02:00
/>
</Pressable>
)
} else {
return null
}
})}
</View>
)
},
[]
)
const listRef = useRef<SectionList>(null)
useEffect(() => {
layoutAnimation()
const tagEmojis = findNodeHandle(listRef.current)
if (emojisState.active) {
tagEmojis && AccessibilityInfo.setAccessibilityFocus(tagEmojis)
}
}, [emojisState.active])
return emojisState.active ? (
<SectionList
accessible
ref={listRef}
horizontal
keyboardShouldPersistTaps='always'
sections={emojisState.emojis}
keyExtractor={item => item[0].shortcode}
renderSectionHeader={listHeader}
renderItem={listItem}
windowSize={4}
/>
) : null
},
() => true
)
export default EmojisList