1- import { Component , Input , OnChanges } from '@angular/core' ;
1+ import {
2+ AfterViewInit ,
3+ ChangeDetectorRef ,
4+ Component ,
5+ ElementRef ,
6+ Input ,
7+ OnChanges ,
8+ OnDestroy ,
9+ } from '@angular/core' ;
210import { Channel , User } from 'stream-chat' ;
311import { CustomTemplatesService } from '../custom-templates.service' ;
412import {
@@ -16,7 +24,9 @@ import {
1624 templateUrl : './avatar-placeholder.component.html' ,
1725 styles : [ ] ,
1826} )
19- export class AvatarPlaceholderComponent implements OnChanges {
27+ export class AvatarPlaceholderComponent
28+ implements OnChanges , AfterViewInit , OnDestroy
29+ {
2030 /**
2131 * An optional name of the image, used for fallback image or image title (if `imageUrl` is provided)
2232 */
@@ -61,7 +71,34 @@ export class AvatarPlaceholderComponent implements OnChanges {
6171 type : undefined ,
6272 initialsType : undefined ,
6373 } ;
64- constructor ( public customTemplatesService : CustomTemplatesService ) { }
74+ isVisible = true ;
75+ private mutationObserver ?: MutationObserver ;
76+ constructor (
77+ public customTemplatesService : CustomTemplatesService ,
78+ private hostElement : ElementRef < HTMLElement > ,
79+ private cdRef : ChangeDetectorRef
80+ ) { }
81+
82+ ngAfterViewInit ( ) : void {
83+ if ( this . location !== 'message-sender' ) {
84+ this . isVisible = true ;
85+ this . cdRef . detectChanges ( ) ;
86+ return ;
87+ }
88+ this . checkIfVisible ( ) ;
89+ const elementToObserve =
90+ this . hostElement . nativeElement . parentElement ?. parentElement
91+ ?. parentElement ;
92+ if ( ! elementToObserve ) {
93+ return ;
94+ }
95+ this . mutationObserver = new MutationObserver ( ( ) => {
96+ this . checkIfVisible ( ) ;
97+ } ) ;
98+ this . mutationObserver . observe ( elementToObserve , {
99+ attributeFilter : [ 'class' ] ,
100+ } ) ;
101+ }
65102
66103 ngOnChanges ( ) : void {
67104 this . context = {
@@ -75,4 +112,19 @@ export class AvatarPlaceholderComponent implements OnChanges {
75112 initialsType : this . initialsType ,
76113 } ;
77114 }
115+
116+ ngOnDestroy ( ) : void {
117+ this . mutationObserver ?. disconnect ( ) ;
118+ }
119+
120+ private checkIfVisible ( ) {
121+ const isVisible =
122+ getComputedStyle ( this . hostElement . nativeElement ) . getPropertyValue (
123+ 'visibility'
124+ ) === 'visible' ;
125+ if ( isVisible !== this . isVisible ) {
126+ this . isVisible = isVisible ;
127+ this . cdRef . detectChanges ( ) ;
128+ }
129+ }
78130}
0 commit comments