tooot/src/components/Emojis.tsx

138 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-05-09 21:59:03 +02:00
import EmojisButton from '@components/Emojis/Button'
import EmojisList from '@components/Emojis/List'
import { useAccessibility } from '@utils/accessibility/AccessibilityManager'
import { useEmojisQuery } from '@utils/queryHooks/emojis'
2022-02-13 22:14:16 +01:00
import { getInstanceFrequentEmojis } from '@utils/slices/instancesSlice'
2021-05-09 21:59:03 +02:00
import { chunk, forEach, groupBy, sortBy } from 'lodash'
2022-09-23 00:21:41 +02:00
import React, { createRef, PropsWithChildren, useEffect, useReducer, useState } from 'react'
2022-02-13 22:14:16 +01:00
import { useTranslation } from 'react-i18next'
2022-09-23 00:21:41 +02:00
import { Keyboard, KeyboardAvoidingView, View } from 'react-native'
2021-05-09 21:59:03 +02:00
import FastImage from 'react-native-fast-image'
2022-09-18 23:28:14 +02:00
import { Edge, SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'
2022-02-13 22:14:16 +01:00
import { useSelector } from 'react-redux'
2022-09-20 22:23:01 +02:00
import EmojisContext, { Emojis, emojisReducer, EmojisState } from './Emojis/helpers/EmojisContext'
2021-05-09 21:59:03 +02:00
const prefetchEmojis = (
2022-02-13 22:14:16 +01:00
sortedEmojis: {
title: string
data: Pick<Mastodon.Emoji, 'shortcode' | 'url' | 'static_url'>[][]
}[],
2021-05-09 21:59:03 +02:00
reduceMotionEnabled: boolean
) => {
const prefetches: { uri: string }[] = []
let requestedIndex = 0
sortedEmojis.forEach(sorted => {
sorted.data.forEach(emojis =>
emojis.forEach(emoji => {
if (requestedIndex > 40) {
return
}
prefetches.push({
uri: reduceMotionEnabled ? emoji.static_url : emoji.url
})
requestedIndex++
})
)
})
try {
FastImage.preload(prefetches)
} catch {}
}
export type Props = {
inputProps: EmojisState['inputProps']
2022-09-18 23:28:14 +02:00
customButton?: boolean
customEdges?: Edge[]
customBehavior?: 'height' | 'padding' | 'position'
2021-05-09 21:59:03 +02:00
}
2022-09-20 22:23:01 +02:00
export const emojis: Emojis = createRef()
2022-08-07 01:18:10 +02:00
const ComponentEmojis: React.FC<Props & PropsWithChildren> = ({
children,
inputProps,
2022-09-18 23:28:14 +02:00
customButton = false,
customEdges = ['bottom'],
customBehavior
2021-05-09 21:59:03 +02:00
}) => {
const { reduceMotionEnabled } = useAccessibility()
2022-09-20 22:23:01 +02:00
const [emojisState, emojisDispatch] = useReducer(emojisReducer, { inputProps, targetIndex: -1 })
2021-05-09 21:59:03 +02:00
useEffect(() => {
emojisDispatch({ type: 'input', payload: inputProps })
}, [inputProps])
2021-05-09 21:59:03 +02:00
2022-02-13 22:14:16 +01:00
const { t } = useTranslation()
const { data } = useEmojisQuery({})
2022-02-13 22:14:16 +01:00
const frequentEmojis = useSelector(getInstanceFrequentEmojis, () => true)
2021-05-09 21:59:03 +02:00
useEffect(() => {
if (data && data.length) {
2022-09-20 22:23:01 +02:00
let sortedEmojis: NonNullable<Emojis['current']> = []
forEach(groupBy(sortBy(data, ['category', 'shortcode']), 'category'), (value, key) =>
2022-09-23 00:21:41 +02:00
sortedEmojis.push({ title: key, data: chunk(value, 5) })
2021-05-09 21:59:03 +02:00
)
2022-02-13 22:14:16 +01:00
if (frequentEmojis.length) {
sortedEmojis.unshift({
title: t('componentEmojis:frequentUsed'),
data: chunk(
frequentEmojis.map(e => e.emoji),
2022-09-23 00:21:41 +02:00
5
2022-09-18 16:49:18 +02:00
),
type: 'frequent'
2022-02-13 22:14:16 +01:00
})
}
2022-09-20 22:23:01 +02:00
emojis.current = sortedEmojis
2021-05-09 21:59:03 +02:00
prefetchEmojis(sortedEmojis, reduceMotionEnabled)
}
}, [data, reduceMotionEnabled])
const insets = useSafeAreaInsets()
const [keyboardShown, setKeyboardShown] = useState(false)
useEffect(() => {
const showSubscription = Keyboard.addListener('keyboardWillShow', () => {
2022-09-18 23:28:14 +02:00
const anyInputHasFocus = inputProps.filter(props => props.isFocused.current).length
2022-09-18 16:49:18 +02:00
if (anyInputHasFocus) {
2022-09-18 23:28:14 +02:00
emojisDispatch({ type: 'target', payload: -1 })
2022-09-18 16:49:18 +02:00
}
setKeyboardShown(true)
})
const hideSubscription = Keyboard.addListener('keyboardWillHide', () => {
setKeyboardShown(false)
})
return () => {
showSubscription.remove()
hideSubscription.remove()
}
2022-09-18 16:49:18 +02:00
}, [inputProps])
2021-05-09 21:59:03 +02:00
return (
2022-09-18 23:28:14 +02:00
<KeyboardAvoidingView style={{ flex: 1 }} behavior={customBehavior}>
<SafeAreaView style={{ flex: 1 }} edges={customEdges}>
<View style={{ flex: 1 }}>
<EmojisContext.Provider value={{ emojisState, emojisDispatch }}>
2022-09-18 23:28:14 +02:00
{children}
<View
style={[
2022-09-23 00:21:41 +02:00
{ position: 'absolute', bottom: 0, width: '100%' },
2022-09-19 22:22:52 +02:00
{
marginBottom: keyboardShown && emojisState.targetIndex === -1 ? insets.bottom : 0
}
]}
2022-09-18 23:28:14 +02:00
children={
emojisState.targetIndex !== -1 ? (
<EmojisList />
) : customButton ? null : (
<EmojisButton />
)
}
/>
</EmojisContext.Provider>
</View>
</SafeAreaView>
</KeyboardAvoidingView>
2021-05-09 21:59:03 +02:00
)
}
2021-05-12 15:40:55 +02:00
export { ComponentEmojis, EmojisButton, EmojisList }