tooot/src/components/Emojis/List.tsx

211 lines
6.5 KiB
TypeScript
Raw Normal View History

2022-09-20 22:23:01 +02:00
import { emojis } from '@components/Emojis'
2022-09-18 16:49:18 +02:00
import Icon from '@components/Icon'
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'
2022-09-18 16:49:18 +02:00
import { chunk } from 'lodash'
2022-09-18 23:28:14 +02:00
import React, { useContext, useEffect, useRef, useState } from 'react'
2021-05-09 21:59:03 +02:00
import { useTranslation } from 'react-i18next'
2022-09-18 16:49:18 +02:00
import {
AccessibilityInfo,
findNodeHandle,
Pressable,
SectionList,
TextInput,
View
} from 'react-native'
2021-05-09 21:59:03 +02:00
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
2022-09-18 23:28:14 +02:00
const EmojisList = () => {
const dispatch = useAppDispatch()
const { reduceMotionEnabled } = useAccessibility()
const { t } = useTranslation()
2021-05-09 21:59:03 +02:00
2022-09-18 23:28:14 +02:00
const { emojisState } = useContext(EmojisContext)
const { colors, mode } = useTheme()
2021-05-09 21:59:03 +02:00
2022-09-18 23:28:14 +02:00
const addEmoji = (shortcode: string) => {
if (emojisState.targetIndex === -1) {
return
}
2022-09-18 23:28:14 +02:00
const {
value: [value, setValue],
selection: [selection, setSelection],
ref,
maxLength
} = emojisState.inputProps[emojisState.targetIndex]
2022-09-18 23:28:14 +02:00
const contentFront = value.slice(0, selection.start)
2022-09-19 22:22:52 +02:00
const contentRear = value.slice(selection.end || selection.start)
const spaceFront = value.length === 0 || /\s/g.test(contentFront.slice(-1)) ? '' : ' '
2022-09-18 23:28:14 +02:00
const spaceRear = /\s/g.test(contentRear[0]) ? '' : ' '
2022-09-18 23:28:14 +02:00
setValue(
[contentFront, spaceFront, shortcode, spaceRear, contentRear].join('').slice(0, maxLength)
)
const addedLength = spaceFront.length + shortcode.length + spaceRear.length
setSelection({ start: selection.start + addedLength })
ref?.current?.setNativeProps({
selection: { start: selection.start + addedLength }
})
}
const listItem = ({ index, item }: { item: Mastodon.Emoji[]; index: number }) => {
return (
<View
key={index}
style={{
flex: 1,
flexWrap: 'wrap',
marginTop: StyleConstants.Spacing.M,
marginRight: StyleConstants.Spacing.S
}}
>
{item.map(emoji => {
const uri = reduceMotionEnabled ? emoji.static_url : emoji.url
if (validUrl.isHttpsUri(uri)) {
return (
<Pressable
key={emoji.shortcode}
onPress={() => {
addEmoji(`:${emoji.shortcode}:`)
dispatch(countInstanceEmoji(emoji))
}}
style={{ padding: StyleConstants.Spacing.S }}
>
<FastImage
accessibilityLabel={t('common:customEmoji.accessibilityLabel', {
emoji: emoji.shortcode
})}
accessibilityHint={t(
'screenCompose:content.root.footer.emojis.accessibilityHint'
)}
source={{ uri }}
style={{ width: 32, height: 32 }}
/>
</Pressable>
)
} else {
return null
}
})}
</View>
2021-05-09 21:59:03 +02:00
)
2022-09-18 23:28:14 +02:00
}
2021-05-09 21:59:03 +02:00
2022-09-18 23:28:14 +02:00
const listRef = useRef<SectionList>(null)
useEffect(() => {
const tagEmojis = findNodeHandle(listRef.current)
if (emojisState.targetIndex !== -1) {
layoutAnimation()
tagEmojis && AccessibilityInfo.setAccessibilityFocus(tagEmojis)
}
}, [emojisState.targetIndex])
2021-05-09 21:59:03 +02:00
2022-09-18 23:28:14 +02:00
const [search, setSearch] = useState('')
const searchLength = useRef(0)
useEffect(() => {
if (
(search.length === 0 && searchLength.current === 1) ||
(search.length === 1 && searchLength.current === 0)
) {
layoutAnimation()
}
searchLength.current = search.length
}, [search.length, searchLength.current])
2022-09-18 16:49:18 +02:00
2022-09-18 23:28:14 +02:00
return emojisState.targetIndex !== -1 ? (
<View
style={{
paddingBottom: StyleConstants.Spacing.Global.PagePadding,
backgroundColor: colors.backgroundDefault
}}
>
2022-09-18 16:49:18 +02:00
<View
style={{
2022-09-18 23:28:14 +02:00
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding,
paddingVertical: StyleConstants.Spacing.S
2022-09-18 16:49:18 +02:00
}}
>
<View
style={{
2022-09-18 23:28:14 +02:00
borderBottomWidth: 1,
borderBottomColor: colors.border,
alignSelf: 'stretch',
justifyContent: 'center',
paddingRight: StyleConstants.Spacing.S
2022-09-18 16:49:18 +02:00
}}
>
2022-09-18 23:28:14 +02:00
<Icon name='Search' size={StyleConstants.Font.Size.L} color={colors.secondary} />
2022-09-18 16:49:18 +02:00
</View>
2022-09-18 23:28:14 +02:00
<TextInput
style={{
flex: 1,
borderBottomWidth: 1,
borderBottomColor: colors.border,
...StyleConstants.FontStyle.M,
color: colors.primaryDefault,
paddingVertical: StyleConstants.Spacing.S
2022-09-18 16:49:18 +02:00
}}
2022-09-18 23:28:14 +02:00
onChangeText={setSearch}
autoCapitalize='none'
clearButtonMode='always'
keyboardAppearance={mode}
autoCorrect={false}
spellCheck={false}
2022-09-18 16:49:18 +02:00
/>
</View>
2022-09-18 23:28:14 +02:00
<SectionList
accessible
ref={listRef}
horizontal
keyboardShouldPersistTaps='always'
sections={
search.length
? [
{
title: 'Search result',
2022-09-20 22:23:01 +02:00
data: emojis.current
? chunk(
emojis.current
.filter(e => e.type !== 'frequent')
.flatMap(e =>
e.data.flatMap(e => e).filter(emoji => emoji.shortcode.includes(search))
),
2
)
: []
2022-09-18 23:28:14 +02:00
}
]
2022-09-20 22:23:01 +02:00
: emojis.current || []
2022-09-18 23:28:14 +02:00
}
keyExtractor={item => item[0]?.shortcode}
renderSectionHeader={({ section: { title } }) => (
<CustomText fontStyle='S' style={{ position: 'absolute', color: colors.secondary }}>
{title}
</CustomText>
)}
renderItem={listItem}
windowSize={4}
contentContainerStyle={{
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding,
minHeight: 32 * 2 + StyleConstants.Spacing.M * 3
}}
/>
</View>
) : null
}
2021-05-09 21:59:03 +02:00
export default EmojisList