import React, { useEffect } from 'react'; import { eventEmitter, EventType } from '../../services/EventEmitter'; import { clearMessages, downloadFile, getDownloadFileStatus, getDownloadFileProgress, getMessages, loadMoreMessages, replyMessage, sendMessage, } from '../../store/messages/messagesSlice'; import { IMessage } from '../../store/messages/types'; import { IPeer } from '../../store/peer/types'; import { StyleProp, StyleSheet, TextStyle, View, ViewStyle } from 'react-native'; import { MessagesView } from './MessagesView'; import { BubbleProps, MessageTextProps, SendProps, TimeProps } from 'react-native-gifted-chat' import { useAppDispatch, useAppSelector } from '../../store/hooks'; export interface IProps { conversation: IPeer; messages: IMessage[]; style?: ViewStyle; alwaysShowSend?: boolean; onMessageLongPressed?: (context: any, message: IMessage) => void; renderSendingExtraView?: () => React.ReactNode; renderMessageCustomView?: (currentMessage: IMessage) => React.ReactNode; renderSystemMessage?: (currentMessage: IMessage) => React.ReactNode; renderMessageImage?: (currentMessage: IMessage) => React.ReactNode; renderChatActions?: () => React.ReactNode; renderAvatar?: (currentMessage: IMessage) => React.ReactNode; onSend?: (message: IMessage) => void; onInputTextChanged?: (text: string) => void; renderSendButton?: (sendProps: SendProps) => React.ReactNode; renderMessageText?: (messageText: MessageTextProps) => React.ReactNode; dateCustomStyle?: StyleProp; renderCustomTime?: (timeProps: TimeProps) => React.ReactNode; renderBubbleContainer?: (bubbleProps: BubbleProps) => React.ReactNode; } /** * @module * @name Messages * @public * @description This guide explains how to retrieve messages from a conversation (P2P or Bubble) and how to send new ones. *

*/ export const MessagesContainer: React.FunctionComponent = (props: IProps) => { const dispatch = useAppDispatch(); const loading = useAppSelector((state) => state.messages.loading); useEffect(() => { dispatch(getMessages(props.conversation.jId)); const onFileDownloadProgressUpdated = eventEmitter.addListener( EventType.FileDownloadProgressUpdated, (eventData: number) => { dispatch(getDownloadFileProgress(eventData)) } ); const onFileDownloadFinished = eventEmitter.addListener( EventType.FileDownloadFinished, (eventData: string) => { dispatch(getDownloadFileStatus(eventData)); } ); const onThumpnailUpdated = eventEmitter.addListener(EventType.ThumpnailUpdated, () => { dispatch(loadMoreMessages()); }); return () => { dispatch(clearMessages()); onFileDownloadProgressUpdated.remove(); onFileDownloadFinished.remove(); onThumpnailUpdated.remove(); }; }, []); const onLoadMore = () => { dispatch(loadMoreMessages()); } const onSendMessage = (message: IMessage, urgency: string, attachedFileUri: string[]) =>{ dispatch(sendMessage({message, urgency, attachedFileUri})); } const onFileDownload = (fileDescriptorId: string)=> { dispatch(downloadFile(fileDescriptorId)); } const onReplayMessage = (message:IMessage) => { dispatch(replyMessage(message)) } return ( ) }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#ffffff', height: 'auto', } });