Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289import {useCallback, useState} from 'react'import {Pressable, TextInput, useWindowDimensions} from 'react-native'import { useFocusedInputHandler, useKeyboardHandler, useReanimatedKeyboardAnimation,} from 'react-native-keyboard-controller'import Animated, { measure, runOnJS, useAnimatedProps, useAnimatedRef, useAnimatedStyle, useSharedValue,} from 'react-native-reanimated'import {useSafeAreaInsets} from 'react-native-safe-area-context'import {GlassContainer} from 'expo-glass-effect'import {type $Typed, type ChatBskyConvoDefs} from '@atproto/api'import {useLingui} from '@lingui/react/macro'import {countGraphemes} from 'unicode-segmenter/grapheme'
import {HITSLOP_10, MAX_DM_GRAPHEME_LENGTH} from '#/lib/constants'import {useHaptics} from '#/lib/haptics'import {useEmail} from '#/state/email-verification'import { useMessageDraft, useSaveMessageDraft,} from '#/state/messages/message-drafts'import {atoms as a, platform, tokens, useTheme} from '#/alf'import {useMessageReplies} from '#/components/dms/MessageReplies'import {GlassView} from '#/components/GlassView'import {PaperPlaneVertical_Filled_Stroke2_Corner1_Rounded as PaperPlaneIcon} from '#/components/icons/PaperPlane'import {Loader} from '#/components/Loader'import * as Toast from '#/components/Toast'import {IS_ANDROID, IS_IOS, IS_WEB} from '#/env'import {ComposerContainer} from './MessageComposer'import { type MessageEmbedState, useExtractEmbedFromFacets,} from './MessageInputEmbed'
const AnimatedTextInput = Animated.createAnimatedComponent(TextInput)
const MIN_HEIGHT = 40
export function MessageInput({ textInputId, onSendMessage, messageEmbed, setEmbed, children, loading = false,}: { textInputId?: string onSendMessage: ( message: string, embed?: MessageEmbedState, replyTo?: $Typed<ChatBskyConvoDefs.MessageView>, ) => Promise<void> | void messageEmbed: MessageEmbedState | undefined setEmbed: (embedUrl: string | undefined) => void children?: React.ReactNode loading?: boolean}) { const {t: l} = useLingui() const t = useTheme() const playHaptic = useHaptics() const {getDraft, clearDraft} = useMessageDraft() const {replyTo, clearReply} = useMessageReplies()
// Input layout const {top: topInset} = useSafeAreaInsets() const {height: windowHeight} = useWindowDimensions() const {height: keyboardHeight} = useReanimatedKeyboardAnimation() const maxHeight = useSharedValue<undefined | number>(undefined) const isInputScrollable = useSharedValue(false)
const [message, setMessage] = useState(getDraft) const inputRef = useAnimatedRef<TextInput>() const [shouldEnforceClear, setShouldEnforceClear] = useState(false)
const {needsEmailVerification} = useEmail() const editable = !needsEmailVerification && !loading
useSaveMessageDraft(message) useExtractEmbedFromFacets(message, setEmbed)
const onSubmit = useCallback(() => { if (!editable) { return } if (!messageEmbed && message.trim() === '') { return } if (countGraphemes(message) > MAX_DM_GRAPHEME_LENGTH) { Toast.show(l`Message is too long`, { type: 'error', }) return } clearDraft() playHaptic() // Capture the embed before clearing - the deferred send below reads it. const embed = messageEmbed setEmbed(undefined) setMessage('') // Capture the reply before clearing - the deferred send below reads it. const reply = replyTo clearReply() if (IS_IOS) { setShouldEnforceClear(true) } if (IS_WEB) { // Pressing the send button causes the text input to lose focus, so we need to // re-focus it after sending setTimeout(() => { inputRef.current?.focus() }, 100) }
requestAnimationFrame(() => { void onSendMessage( message, embed, reply ? {...reply, $type: 'chat.bsky.convo.defs#messageView'} : undefined, ) }) }, [ editable, messageEmbed, message, clearDraft, onSendMessage, playHaptic, setEmbed, inputRef, l, replyTo, clearReply, ])
useFocusedInputHandler( { onChangeText: () => { 'worklet' const measurement = measure(inputRef) if (!measurement) return
const max = windowHeight - -keyboardHeight.get() - topInset - 150 const availableSpace = max - measurement.height
maxHeight.set(max) isInputScrollable.set(availableSpace < 30) }, }, [windowHeight, topInset], )
const animatedStyle = useAnimatedStyle(() => ({ maxHeight: maxHeight.get(), }))
const animatedProps = useAnimatedProps(() => ({ scrollEnabled: isInputScrollable.get(), }))
const submitDisabled = !editable || (!messageEmbed && message.trim().length === 0)
const blur = useCallback(() => { inputRef.current?.blur() }, [inputRef])
useKeyboardHandler({ onEnd: evt => { 'worklet' // small hack: interactive dismiss on Android sometimes doesn't blur the input if (IS_ANDROID && evt.progress === 0) { runOnJS(blur)() } }, })
return ( <ComposerContainer> <GlassContainer style={[a.flex_row, a.align_end, a.gap_sm]} spacing={tokens.space.xs}> <GlassView isInteractive glassEffectStyle="regular" style={[a.flex_1, a.rounded_xl, {minHeight: MIN_HEIGHT}]} tintColor={t.palette.contrast_50} fallbackStyle={[t.atoms.bg_contrast_50]}> {children} <AnimatedTextInput nativeID={textInputId} accessibilityLabel={l`Message input field`} accessibilityHint={l`Type your message here`} placeholder={l`Message`} placeholderTextColor={t.palette.contrast_500} value={message} onChange={evt => { // bit of a hack: iOS automatically accepts autocomplete suggestions when you tap anywhere on the screen // including the button we just pressed - and this overrides clearing the input! so we watch for the // next change and double make sure the input is cleared. It should *always* send an onChange event after // clearing via setMessage('') that happens in onSubmit() // -sfn if (IS_IOS && shouldEnforceClear) { setShouldEnforceClear(false) setMessage('') return } const text = evt.nativeEvent.text setMessage(text) }} multiline={true} style={[ {flexBasis: 'auto', minHeight: MIN_HEIGHT}, a.flex_shrink_0, a.flex_grow, a.text_md, a.px_lg, t.atoms.text, platform({ android: {paddingTop: 2, paddingBottom: 3}, ios: {paddingTop: 10, paddingBottom: 5}, }), animatedStyle, ]} verticalAlign="middle" keyboardAppearance={t.scheme} submitBehavior="newline" ref={inputRef} hitSlop={HITSLOP_10} animatedProps={animatedProps} editable={editable} /> </GlassView> <GlassView isInteractive glassEffectStyle="regular" style={[a.rounded_full]} tintColor={ submitDisabled ? t.palette.contrast_100 : t.palette.primary_500 } fallbackStyle={{ backgroundColor: submitDisabled ? t.palette.contrast_100 : t.palette.primary_500, }}> <Pressable accessibilityRole="button" accessibilityLabel={ loading ? l({message: 'Loading chat…', context: 'placeholder'}) : l({message: 'Message', context: 'action'}) } accessibilityHint="" hitSlop={HITSLOP_10} style={[ a.rounded_full, a.align_center, a.justify_center, { height: MIN_HEIGHT, width: MIN_HEIGHT, }, ]} onPress={onSubmit} disabled={submitDisabled}> {loading ? ( <Loader size="md" fill={t.palette.white} style={[a.mb_2xs]} /> ) : ( <PaperPlaneIcon size="md" fill={t.palette.white} style={[a.mb_2xs]} /> )} </Pressable> </GlassView> </GlassContainer> </ComposerContainer> )}