import React from 'react' import { Dimensions, Keyboard, NativeSyntheticEvent, Pressable, TextInput, TextInputContentSizeChangeEventData, View, } from 'react-native' import {useSafeAreaInsets} from 'react-native-safe-area-context' import {msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import Graphemer from 'graphemer' import {HITSLOP_10, MAX_DM_GRAPHEME_LENGTH} from '#/lib/constants' import {useHaptics} from '#/lib/haptics' import * as Toast from '#/view/com/util/Toast' import {atoms as a, useTheme} from '#/alf' import {PaperPlane_Stroke2_Corner0_Rounded as PaperPlane} from '#/components/icons/PaperPlane' export function MessageInput({ onSendMessage, scrollToEnd, }: { onSendMessage: (message: string) => void scrollToEnd: () => void }) { const {_} = useLingui() const t = useTheme() const playHaptic = useHaptics() const [message, setMessage] = React.useState('') const [maxHeight, setMaxHeight] = React.useState() const [isInputScrollable, setIsInputScrollable] = React.useState(false) const {top: topInset} = useSafeAreaInsets() const inputRef = React.useRef(null) const onSubmit = React.useCallback(() => { if (message.trim() === '') { return } if (new Graphemer().countGraphemes(message) > MAX_DM_GRAPHEME_LENGTH) { Toast.show(_(msg`Message is too long`)) return } onSendMessage(message.trimEnd()) playHaptic() setMessage('') setTimeout(() => { inputRef.current?.focus() }, 100) }, [message, onSendMessage, playHaptic, _]) const onInputLayout = React.useCallback( (e: NativeSyntheticEvent) => { const keyboardHeight = Keyboard.metrics()?.height ?? 0 const windowHeight = Dimensions.get('window').height const max = windowHeight - keyboardHeight - topInset - 100 const availableSpace = max - e.nativeEvent.contentSize.height setMaxHeight(max) setIsInputScrollable(availableSpace < 30) scrollToEnd() }, [scrollToEnd, topInset], ) return ( ) }