import React, { useMemo, useCallback, useEffect } from 'react' import { StyleSheet, StyleProp, ViewStyle, TextStyle, } from 'react-native' import { Text } from 'react-native-gesture-handler' import Animated, { useSharedValue, useAnimatedStyle, withTiming } from 'react-native-reanimated' import { Color } from './Color' import { TouchableOpacity, TouchableOpacityProps } from './components/TouchableOpacity' import { TEST_ID } from './Constant' import { useColorScheme } from './hooks/useColorScheme' import { IMessage } from './Models' import { getColorSchemeStyle } from './styles' export interface SendProps { text?: string label?: string containerStyle?: StyleProp textStyle?: StyleProp children?: React.ReactNode /** Always show send button, even when text is empty */ isSendButtonAlwaysVisible?: boolean /** Text is optional, allow sending empty messages (useful for media-only messages) */ isTextOptional?: boolean sendButtonProps?: Partial onSend?( messages: Partial | Partial[], shouldResetInputToolbar: boolean, ): void } export const Send = ({ text, containerStyle, children, textStyle, label = 'Send', isSendButtonAlwaysVisible = false, isTextOptional = false, sendButtonProps, onSend, }: SendProps) => { const colorScheme = useColorScheme() const opacity = useSharedValue(0) const handleOnPress = useCallback(() => { const trimmedText = text?.trim() ?? '' const message = { text: trimmedText } as Partial if (onSend && (trimmedText.length || isTextOptional)) onSend(message, true) }, [text, onSend, isTextOptional]) const isVisible = useMemo( () => isSendButtonAlwaysVisible || !!text?.trim().length, [isSendButtonAlwaysVisible, text] ) useEffect(() => { opacity.value = withTiming(isVisible ? 1 : 0, { duration: 200 }) }, [isVisible, opacity]) const animatedStyle = useAnimatedStyle(() => ({ opacity: opacity.value, }), [opacity]) return ( { children || {label} } ) } const styles = StyleSheet.create({ container: { justifyContent: 'flex-end', }, touchable: { justifyContent: 'flex-end', }, text: { color: Color.defaultBlue, fontWeight: '600', fontSize: 17, backgroundColor: Color.backgroundTransparent, paddingVertical: 10, paddingHorizontal: 10, }, text_dark: { color: '#4da6ff', }, })