import React, { useMemo, useCallback } from 'react' import { StyleSheet, StyleProp, ViewStyle, TextStyle, View, } from 'react-native' import { Text } from 'react-native-gesture-handler' import { LinkParser, LinkMatcher, LinkType } from './linkParser' import { LeftRightStyle, IMessage } from './Models' export type MessageTextProps = { position?: 'left' | 'right' currentMessage: TMessage containerStyle?: LeftRightStyle textStyle?: LeftRightStyle linkStyle?: LeftRightStyle customTextStyle?: StyleProp onPress?: ( message: TMessage, url: string, type: LinkType ) => void // Link parser options matchers?: LinkMatcher[] email?: boolean phone?: boolean url?: boolean hashtag?: boolean mention?: boolean hashtagUrl?: string mentionUrl?: string stripPrefix?: boolean } export function MessageText({ currentMessage, position = 'left', containerStyle, textStyle, linkStyle: linkStyleProp, customTextStyle, onPress: onPressProp, matchers, email = true, phone = true, url = true, hashtag = false, mention = false, hashtagUrl, mentionUrl, stripPrefix = false, }: MessageTextProps) { const linkStyle = useMemo(() => StyleSheet.flatten([ styles.link, linkStyleProp?.[position], ]), [position, linkStyleProp]) const style = useMemo(() => [ styles[`text_${position}`], textStyle?.[position], customTextStyle, ], [position, textStyle, customTextStyle]) const handlePress = useCallback((url: string, type: LinkType) => { onPressProp?.(currentMessage, url, type) }, [onPressProp, currentMessage]) return ( ) } const styles = StyleSheet.create({ container: { marginVertical: 5, marginHorizontal: 10, }, text: { fontSize: 16, lineHeight: 20, }, text_left: { color: 'black', }, text_right: { color: 'white', }, link: { textDecorationLine: 'underline', }, })