2022-09-18 16:49:18 +02:00
|
|
|
import Icon from '@components/Icon'
|
2022-05-07 00:52:32 +02:00
|
|
|
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'
|
|
|
|
import React, { useCallback, 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
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-09-18 16:49:18 +02:00
|
|
|
const { emojisState } = useContext(EmojisContext)
|
|
|
|
const { colors, mode } = useTheme()
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
const addEmoji = (shortcode: string) => {
|
|
|
|
if (!emojisState.targetProps) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputValue = emojisState.targetProps.value
|
|
|
|
const maxLength = emojisState.targetProps.maxLength
|
|
|
|
const selectionRange = emojisState.targetProps.selectionRange || {
|
|
|
|
start: emojisState.targetProps.value.length,
|
|
|
|
end: emojisState.targetProps.value.length
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputValue?.length) {
|
|
|
|
const contentFront = inputValue.slice(0, selectionRange.start)
|
|
|
|
const contentRear = inputValue.slice(selectionRange.end)
|
|
|
|
|
|
|
|
const whiteSpaceRear = /\s/g.test(contentRear.slice(-1))
|
|
|
|
|
|
|
|
const newTextWithSpace = ` ${shortcode}${whiteSpaceRear ? '' : ' '}`
|
|
|
|
emojisState.targetProps.setValue(
|
|
|
|
[contentFront, newTextWithSpace, contentRear].join('').slice(0, maxLength)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
emojisState.targetProps.setValue(`${shortcode} `.slice(0, maxLength))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
const listItem = useCallback(
|
|
|
|
({ index, item }: { item: Mastodon.Emoji[]; index: number }) => {
|
|
|
|
return (
|
2022-05-07 00:52:32 +02:00
|
|
|
<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={() => {
|
2022-09-18 01:02:25 +02:00
|
|
|
addEmoji(`:${emoji.shortcode}:`)
|
2022-02-13 22:14:16 +01:00
|
|
|
dispatch(countInstanceEmoji(emoji))
|
|
|
|
}}
|
2022-09-18 16:49:18 +02:00
|
|
|
style={{ padding: StyleConstants.Spacing.S }}
|
2021-05-09 21:59:03 +02:00
|
|
|
>
|
|
|
|
<FastImage
|
2022-09-18 01:02:25 +02:00
|
|
|
accessibilityLabel={t('common:customEmoji.accessibilityLabel', {
|
|
|
|
emoji: emoji.shortcode
|
|
|
|
})}
|
2021-05-09 21:59:03 +02:00
|
|
|
accessibilityHint={t(
|
|
|
|
'screenCompose:content.root.footer.emojis.accessibilityHint'
|
|
|
|
)}
|
|
|
|
source={{ uri }}
|
2022-09-18 16:49:18 +02:00
|
|
|
style={{ width: 32, height: 32 }}
|
2021-05-09 21:59:03 +02:00
|
|
|
/>
|
|
|
|
</Pressable>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
},
|
2022-09-18 01:02:25 +02:00
|
|
|
[emojisState.targetProps]
|
2021-05-09 21:59:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const listRef = useRef<SectionList>(null)
|
|
|
|
useEffect(() => {
|
|
|
|
const tagEmojis = findNodeHandle(listRef.current)
|
2022-09-18 01:02:25 +02:00
|
|
|
if (emojisState.targetProps) {
|
|
|
|
layoutAnimation()
|
2021-05-09 21:59:03 +02:00
|
|
|
tagEmojis && AccessibilityInfo.setAccessibilityFocus(tagEmojis)
|
|
|
|
}
|
2022-09-18 01:02:25 +02:00
|
|
|
}, [emojisState.targetProps])
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-09-18 16:49:18 +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 01:02:25 +02:00
|
|
|
return emojisState.targetProps ? (
|
2022-09-18 16:49:18 +02:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
paddingBottom: StyleConstants.Spacing.Global.PagePadding,
|
|
|
|
backgroundColor: colors.backgroundDefault
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
padding: StyleConstants.Spacing.Global.PagePadding,
|
|
|
|
paddingBottom: StyleConstants.Spacing.S
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
borderBottomWidth: 1,
|
|
|
|
borderBottomColor: colors.border,
|
|
|
|
alignSelf: 'stretch',
|
|
|
|
justifyContent: 'center',
|
|
|
|
paddingRight: StyleConstants.Spacing.S
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name='Search' size={StyleConstants.Font.Size.L} color={colors.secondary} />
|
|
|
|
</View>
|
|
|
|
<TextInput
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
borderBottomWidth: 1,
|
|
|
|
borderBottomColor: colors.border,
|
|
|
|
...StyleConstants.FontStyle.M,
|
|
|
|
color: colors.primaryDefault,
|
|
|
|
paddingVertical: StyleConstants.Spacing.S
|
|
|
|
}}
|
|
|
|
onChangeText={setSearch}
|
|
|
|
autoCapitalize='none'
|
|
|
|
clearButtonMode='always'
|
|
|
|
keyboardAppearance={mode}
|
|
|
|
autoCorrect={false}
|
|
|
|
spellCheck={false}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<SectionList
|
|
|
|
accessible
|
|
|
|
ref={listRef}
|
|
|
|
horizontal
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
sections={
|
|
|
|
search.length
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
title: 'Search result',
|
|
|
|
data: chunk(
|
|
|
|
emojisState.emojis
|
|
|
|
.filter(e => e.type !== 'frequent')
|
|
|
|
.flatMap(e =>
|
|
|
|
e.data.flatMap(e => e).filter(emoji => emoji.shortcode.includes(search))
|
|
|
|
),
|
|
|
|
2
|
|
|
|
)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
: emojisState.emojis
|
|
|
|
}
|
|
|
|
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>
|
2021-05-09 21:59:03 +02:00
|
|
|
) : null
|
|
|
|
},
|
|
|
|
() => true
|
|
|
|
)
|
|
|
|
|
|
|
|
export default EmojisList
|