Skip to content

Commit 23e5c16

Browse files
authored
Merge pull request #445 from GetStream/fix-message-list-reset
fix: Don't reset scroll stat on channel update
2 parents 33c12ec + d12a7dd commit 23e5c16

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

projects/stream-chat-angular/src/lib/date-parser.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class DateParserService {
2222
/**
2323
* Return a user-friendly string representation of the date (year, month and date)
2424
* @param date
25-
* @returns
25+
* @returns The parsed date
2626
*/
2727
parseDate(date: Date) {
2828
if (this.customDateParser) {
@@ -34,7 +34,7 @@ export class DateParserService {
3434
/**
3535
* Return a user-friendly string representation of the date and time
3636
* @param date
37-
* @returns
37+
* @returns The parsed date
3838
*/
3939
parseDateTime(date: Date) {
4040
if (this.customDateTimeParser) {

projects/stream-chat-angular/src/lib/message-list/message-list.component.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@angular/core/testing';
88
import { By } from '@angular/platform-browser';
99
import { TranslateModule } from '@ngx-translate/core';
10-
import { of } from 'rxjs';
10+
import { BehaviorSubject, of } from 'rxjs';
1111
import { Channel } from 'stream-chat';
1212
import { AvatarPlaceholderComponent } from '../avatar-placeholder/avatar-placeholder.component';
1313
import { AvatarComponent } from '../avatar/avatar.component';
@@ -26,6 +26,7 @@ import { StreamI18nService } from '../stream-i18n.service';
2626
import { DefaultStreamChatGenerics } from '../types';
2727
import { ImageLoadService } from './image-load.service';
2828
import { MessageListComponent } from './message-list.component';
29+
import { take } from 'rxjs/operators';
2930

3031
describe('MessageListComponent', () => {
3132
let component: MessageListComponent;
@@ -489,7 +490,9 @@ describe('MessageListComponent', () => {
489490

490491
it('should get unread message information from "message.new" event if an older message list is displayed', () => {
491492
let channel!: Channel<DefaultStreamChatGenerics>;
492-
channelServiceMock.activeChannel$.subscribe((c) => (channel = c!));
493+
channelServiceMock.activeChannel$
494+
.pipe(take(1))
495+
.subscribe((c) => (channel = c!));
493496
// Simulate message set change
494497
channel.state.latestMessages = [];
495498
channelServiceMock.activeChannelMessages$.next(
@@ -1011,4 +1014,15 @@ describe('MessageListComponent', () => {
10111014

10121015
expect(queryDateSeparators().length).toBe(0);
10131016
});
1017+
1018+
it(`shouldn't reset the scroll state if active channel is updated`, () => {
1019+
spyOn<any>(component, 'resetScrollState');
1020+
channelServiceMock.activeChannel$.next(
1021+
(
1022+
channelServiceMock.activeChannel$ as any as BehaviorSubject<any>
1023+
).getValue()
1024+
);
1025+
1026+
expect(component['resetScrollState']).not.toHaveBeenCalled();
1027+
});
10141028
});

projects/stream-chat-angular/src/lib/message-list/message-list.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export class MessageListComponent
106106
UserResponse<DefaultStreamChatGenerics>[]
107107
>;
108108
private isLatestMessageInList = true;
109+
private channelId?: string;
109110

110111
constructor(
111112
private channelService: ChannelService,
@@ -115,7 +116,10 @@ export class MessageListComponent
115116
) {
116117
this.subscriptions.push(
117118
this.channelService.activeChannel$.subscribe((channel) => {
118-
this.resetScrollState();
119+
if (this.channelId !== channel?.id) {
120+
this.resetScrollState();
121+
this.channelId = channel?.id;
122+
}
119123
const capabilites = channel?.data?.own_capabilities as string[];
120124
if (capabilites) {
121125
this.enabledMessageActions = capabilites;
@@ -400,6 +404,7 @@ export class MessageListComponent
400404
tap((messages) => {
401405
this.isLoading = false;
402406
if (messages.length === 0) {
407+
this.resetScrollState();
403408
return;
404409
}
405410
const currentLatestMessage = messages[messages.length - 1];

0 commit comments

Comments
 (0)