import { Module } from '@ringcentral-integration/commons/lib/di' ; import { formatNumber } from '@ringcentral-integration/commons/lib/formatNumber' ; import { FormatDateTimeOptions } from '@ringcentral-integration/commons/modules/DateTimeFormatV2' ; import { Attachment } from '@ringcentral-integration/commons/modules/MessageSenderV2' ; import { RcUIModuleV2 , UIFunctions , UIProps , } from '@ringcentral-integration/core' ; import { ConversationContainerProps , ConversationPanelProps , Deps , OnLogConversationOptions , } from './ConversationUI.interface' ; @ Module ( { name : 'ConversationUI' , deps : [ 'AppFeatures' , 'Brand' , 'Locale' , 'DateTimeFormat' , 'RegionSettings' , 'Conversations' , 'RateLimiter' , 'ConnectivityMonitor' , 'MessageStore' , 'RouterInteraction' , { dep : 'ConversationLogger' , optional : true } , { dep : 'ContactMatcher' , optional : true } , { dep : 'ConversationUIOptions' , optional : true } , ] , } ) export class ConversationUI < T > extends RcUIModuleV2 < Deps & T > { constructor ( deps ) { super ( { deps , } ) ; } getUIProps ( { params , enableContactFallback = false , showGroupNumberName = false , supportAttachment = false , perPage = 20 , inputExpandable , } ) { const { brand , locale , conversationLogger , dateTimeFormat , contactMatcher , regionSettings , conversations , rateLimiter , connectivityMonitor , appFeatures , } = this . _deps ; const disableLinks = rateLimiter . throttling || ! connectivityMonitor . connectivity ; const showSpinner = ! ( dateTimeFormat . ready && ( ! contactMatcher || contactMatcher . ready ) && regionSettings . ready && conversations . ready && rateLimiter . ready && connectivityMonitor . ready && ( ! conversationLogger || conversationLogger . ready ) ) ; const currentConversation = conversations . currentConversation ; const hasInputContent = conversations . messageText . length > 0 || ( conversations . attachments && conversations . attachments . length > 0 ) ; return { brand : brand . name , enableContactFallback , showGroupNumberName , supportAttachment , currentLocale : locale . currentLocale , conversationId : params . conversationId , sendButtonDisabled : conversations . pushing || disableLinks || ! hasInputContent || showSpinner , areaCode : regionSettings . areaCode , countryCode : regionSettings . countryCode , showSpinner , recipients : currentConversation . recipients , messages : currentConversation . messages , messageText : conversations . messageText , attachments : conversations . attachments , conversation : currentConversation , disableLinks , autoLog : ! ! conversationLogger ?. autoLog , perPage , loadingNextPage : conversations . loadingOldMessages , inputExpandable , enableCDC : appFeatures . isCDCEnabled , } ; } getUIFunctions ( { dateTimeFormatter , onLogConversation , conversationsPath = '/messages' , renderExtraButton , } ) { const { contactMatcher , dateTimeFormat , routerInteraction , conversationLogger , regionSettings , conversations , messageStore , } = this . _deps ; let getMatcherContactName ; let getMatcherContactList ; let getMatcherContactNameList ; if ( contactMatcher && contactMatcher . ready ) { getMatcherContactList = ( phoneNumber ) => { const matcherNames = contactMatcher . dataMapping [ phoneNumber ] ; if ( matcherNames ?. length > 0 ) { return matcherNames . map ( ( matcher ) => `${ matcher . name } | ${ matcher . phoneNumbers [ 0 ] . phoneType }` ) ; } return [ ] ; } ; getMatcherContactNameList = ( phoneNumber ) => { const matcherNames = contactMatcher . dataMapping [ phoneNumber ] ; if ( matcherNames ?. length > 0 ) { return matcherNames . map ( ( matcher ) => matcher . name ) ; } return [ ] ; } ; getMatcherContactName = ( phoneNumber ) => { const matcherNames = getMatcherContactNameList ( phoneNumber ) ; return matcherNames ?. length > 0 ? matcherNames . join ( '&' ) : null ; } ; } return { replyToReceivers : ( text , attachments ? ) => conversations . replyToReceivers ( text , attachments ) , unloadConversation : ( ) => conversations . unloadConversation ( ) , loadConversation : ( id ) => conversations . loadConversation ( id ) , updateMessageText : ( text ) => conversations . updateMessageText ( text ) , addAttachment : ( attachment ) => conversations . addAttachment ( attachment ) , removeAttachment : ( attachment ) => conversations . removeAttachment ( attachment ) , dateTimeFormatter : dateTimeFormatter ?? ( ( options ) => dateTimeFormat . formatDateTime ( options ) ) , formatPhone : ( phoneNumber ) => formatNumber ( { phoneNumber , areaCode : regionSettings . areaCode , countryCode : regionSettings . countryCode , } ) , getMatcherContactName , getMatcherContactList , getMatcherContactNameList , onLogConversation : onLogConversation || ( conversationLogger && ( async ( { redirect = true , ... options } ) => { await conversationLogger . logConversation ( { ... options , redirect , } ) ; } ) ) , goBack ( ) { routerInteraction . push ( conversationsPath ) ; } , readMessages ( id ) { messageStore . readMessages ( id ) ; } , loadPreviousMessages ( ) { conversations . fetchOldMessages ( ) ; } , renderExtraButton , } ; } }