import * as React from 'react'; import { Dimensions, View } from 'react-native'; import { gCustomMessageCardEventType } from '../../chat'; import { userInfoFromMessage } from '../../chat/utils'; import { useColors } from '../../hook'; import { useI18nContext } from '../../i18n'; import { ChatCustomMessageBody, ChatFileMessageBody, ChatMessage, ChatMessageType, ChatTextMessageBody, ChatVoiceMessageBody, } from '../../rename.chat'; import { usePaletteContext } from '../../theme'; import { IconButtonMemo } from '../../ui/Button'; import { Icon } from '../../ui/Image'; import { SingleLineText } from '../../ui/Text'; import { MessageDefaultImage } from './MessageListItem'; import { getImageThumbUrl, getVideoThumbUrl } from './MessageListItem.hooks'; /** * Message Input Quote View Component properties. */ export type MessageInputQuoteViewProps = { showQuote: boolean; onDel: () => void; msg?: ChatMessage; }; /** * Message Input Quote View Component. */ export const MessageInputQuoteView = (props: MessageInputQuoteViewProps) => { const { showQuote, onDel, msg: propsMsg } = props; const { tr } = useI18nContext(); const { colors } = usePaletteContext(); const { getColor } = useColors({ quote: { light: colors.neutral[9], dark: colors.neutral[3], }, quote_del: { light: colors.neutral[3], dark: colors.neutral[7], }, t1: { light: colors.neutralSpecial[5], dark: colors.neutralSpecial[6], }, t2: { light: colors.neutral[5], dark: colors.neutral[6], }, }); const bodyType = React.useRef( propsMsg?.body.type ).current; const [thumbUrl, setThumbUrl] = React.useState(); const getContent = (msg: ChatMessage) => { let maxWidth = Dimensions.get('window').width; if (msg.body.type === ChatMessageType.TXT) { maxWidth = maxWidth * 0.9; const body = msg.body as ChatTextMessageBody; const content = body.content; return ( {content} ); } else if (msg.body.type === ChatMessageType.FILE) { maxWidth = maxWidth * 0.8; const body = msg.body as ChatFileMessageBody; return ( {tr('_uikit_chat_input_quote_file')} {body.displayName.substring(0)} ); } else if (msg.body.type === ChatMessageType.COMBINE) { maxWidth = maxWidth * 0.8; return ( {tr('_uikit_msg_record')} ); } else if (msg.body.type === ChatMessageType.IMAGE) { maxWidth = maxWidth * 0.6; return ( {tr('picture')} ); } else if (msg.body.type === ChatMessageType.VIDEO) { maxWidth = maxWidth * 0.6; return ( {tr('video')} ); } else if (msg.body.type === ChatMessageType.VOICE) { maxWidth = maxWidth * 0.6; const body = msg.body as ChatVoiceMessageBody; const second = Math.floor(body.duration ?? 0); return ( {tr('voice')} {tr(` ${second}"`)} ); } else if (msg.body.type === ChatMessageType.CUSTOM) { const body = msg.body as ChatCustomMessageBody; const event = body.event; if (event === gCustomMessageCardEventType) { const cardParams = body.params as { uid: string; nickname: string; avatar: string; }; maxWidth = maxWidth * 0.8; return ( {tr('card')} {cardParams.nickname ?? cardParams.uid} ); } } return null; }; const getContentThumb = React.useCallback(async (msg: ChatMessage) => { if (msg.body.type === ChatMessageType.IMAGE) { const ret = await getImageThumbUrl(msg); setThumbUrl(ret); } else if (msg.body.type === ChatMessageType.VIDEO) { const ret = await getVideoThumbUrl(msg); setThumbUrl(ret); } }, []); const getUserName = (msg: ChatMessage) => { const user = userInfoFromMessage(msg); return user?.userName ?? user?.userId ?? msg.from; }; React.useEffect(() => { if (propsMsg) { getContentThumb(propsMsg); } }, [getContentThumb, propsMsg]); if (showQuote !== true || propsMsg === undefined) { return null; } return ( {tr('you')} {tr('_uikit_chat_input_quote_title_1')} {getUserName(propsMsg)} {getContent(propsMsg)} {thumbUrl ? ( <> ) : null} ); };