@@ -15,11 +15,13 @@ import { ValidationService } from '../../services/messenger/validation.service';
1515import * as signalR from '@microsoft/signalr' ;
1616import { EditMessageNotification } from '../../types/models/EditMessageNotification' ;
1717import { DeleteMessageNotification } from '../../types/models/DeleteMessageNotification' ;
18- import { firstValueFrom , Subject , takeUntil } from 'rxjs' ;
18+ import { Subject , takeUntil , BehaviorSubject , firstValueFrom , distinctUntilKeyChanged } from 'rxjs' ;
1919import { DisplayNameColours } from 'src/app/types/enums/DisplayNameColours' ;
2020import { DeleteMessageCommand } from 'src/app/types/requests/DeleteMessageCommand' ;
2121import ApiBaseService from 'src/app/services/api/apiBase.service' ;
2222import { SendMessageResponse } from '../../types/responses/SendMessageResponse' ;
23+ import { ReplyStateSerivce } from 'src/app/services/states/replyState.service' ;
24+ import { Reply } from 'src/app/types/models/Reply' ;
2325
2426@Component ( {
2527 selector : 'app-chats' ,
@@ -36,7 +38,8 @@ export class ChatsComponent implements OnInit, OnDestroy {
3638 private _router : Router ,
3739 private _validationService : ValidationService ,
3840 private _apiBaseService : ApiBaseService ,
39- public _modalWindowStateService : ModalWindowStateService
41+ public _modalWindowStateService : ModalWindowStateService ,
42+ public _replyStateService : ReplyStateSerivce
4043 ) { }
4144
4245 private connectionBuilder : signalR . HubConnectionBuilder = new signalR . HubConnectionBuilder ( ) ;
@@ -72,12 +75,14 @@ export class ChatsComponent implements OnInit, OnDestroy {
7275 public messages : Message [ ] = [ ] ;
7376
7477 public messageAttachment : File | null = null ;
78+ public messageAttachmentBlobUrl : string | ArrayBuffer | null = '' ;
7579 public messageText = '' ;
76- public messageAttachmentUrl = '' ;
7780 public searchChatQuery = '' ;
7881 public searchMessagesQuery = '' ;
7982 public chatFilter = 'All chats' ;
8083
84+ public activeChatBehaviorSubject = new BehaviorSubject < Chat > ( this . activeChat ) ;
85+
8186 componentDestroyed$ : Subject < boolean > = new Subject ( ) ;
8287
8388 public get routingConstants ( ) : typeof RoutingConstants {
@@ -89,6 +94,12 @@ export class ChatsComponent implements OnInit, OnDestroy {
8994 }
9095
9196 initializeView ( ) : void {
97+ this . activeChatBehaviorSubject . pipe ( distinctUntilKeyChanged ( 'chatId' ) ) . subscribe ( ( ) => {
98+ this . _replyStateService . setReplyNull ( ) ;
99+ this . messageAttachment = null ;
100+ this . messageAttachmentBlobUrl = null ;
101+ } ) ;
102+
92103 const tokens = this . _tokensService . getTokens ( ) ;
93104
94105 if ( ! tokens ) {
@@ -144,8 +155,31 @@ export class ChatsComponent implements OnInit, OnDestroy {
144155 . catch ( ( err ) => console . error ( err . toString ( ) ) ) ;
145156 }
146157
147- imageSelected ( event : any ) {
158+ onReplyClick (
159+ messageId : string ,
160+ displayName : string ,
161+ text : string ,
162+ displayNameColour : DisplayNameColours
163+ ) {
164+ const replyEntity = new Reply ( messageId , displayName , text , displayNameColour ) ;
165+
166+ this . _replyStateService . setReply ( replyEntity ) ;
167+ this . scrollToEnd ( ) ;
168+ }
169+
170+ async imageSelected ( event : any ) {
148171 this . messageAttachment = event . target . files [ 0 ] ;
172+
173+ const reader = new FileReader ( ) ;
174+ reader . onload = ( ) => {
175+ this . messageAttachmentBlobUrl = reader . result ;
176+ } ;
177+ reader . readAsDataURL ( event . target . files [ 0 ] ) ;
178+ }
179+
180+ removeImageSelected ( ) {
181+ this . messageAttachment = null ;
182+ this . messageAttachmentBlobUrl = null ;
149183 }
150184
151185 onOpenImageClick ( imageLink : string ) : void {
@@ -275,8 +309,13 @@ export class ChatsComponent implements OnInit, OnDestroy {
275309 }
276310
277311 this . activeChatId = chatId ;
278- this . activeChat = this . chats . filter ( ( x ) => x . chatId === this . activeChatId ) [ 0 ] ;
312+ const newActiveChat = this . chats . filter ( ( x ) => x . chatId === this . activeChatId ) [ 0 ] ;
313+ this . activeChat = newActiveChat ;
314+
315+ this . activeChatBehaviorSubject . next ( newActiveChat ) ;
316+
279317 this . getChatMessages ( this . activeChatId ) ;
318+ this . scrollToEnd ( ) ;
280319 }
281320
282321 chatContainsMessages ( chat : Chat ) : boolean {
@@ -426,6 +465,11 @@ export class ChatsComponent implements OnInit, OnDestroy {
426465 sendMessageFormData . append ( 'messageText' , newMessageText ) ;
427466 sendMessageFormData . append ( 'chatId' , this . activeChatId ) ;
428467 sendMessageFormData . append ( 'messageId' , messageId ) ;
468+ sendMessageFormData . append (
469+ 'inReplayToAuthor' ,
470+ this . _replyStateService . reply ?. displayName ?? ''
471+ ) ;
472+ sendMessageFormData . append ( 'inReplayToText' , this . _replyStateService . reply ?. text ?? '' ) ;
429473
430474 if ( this . messageAttachment ) {
431475 sendMessageFormData . append ( 'Attachment' , this . messageAttachment ) ;
@@ -440,13 +484,17 @@ export class ChatsComponent implements OnInit, OnDestroy {
440484 newMessageText ,
441485 isoString ,
442486 true ,
443- tokens . userProfilePictureUrl
487+ tokens . userProfilePictureUrl ,
488+ this . _replyStateService . reply ?. displayName ?? null ,
489+ this . _replyStateService . reply ?. text ?? null
444490 ) ;
445491
446492 this . clearMessageInput ( ) ;
447493
448494 this . messages . push ( newMessage ) ;
449495
496+ this . _replyStateService . setReplyNull ( ) ;
497+
450498 const sendMessage$ = this . _messagesService . sendMessage ( sendMessageFormData ) ;
451499
452500 const response = await firstValueFrom < SendMessageResponse > ( sendMessage$ ) ;
0 commit comments