Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert empty avatar placeholder #464 #481

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@
[showOnlineIndicator]="showOnlineIndicator"
></stream-avatar>
</ng-template>
<ng-container *ngIf="isVisible; else emptyPlaceholder">
<ng-container
*ngTemplateOutlet="
(customTemplatesService.avatarTemplate$ | async) || defaultAvatar;
context: context
"
></ng-container>
</ng-container>
<ng-template #emptyPlaceholder>
<div
class="str-chat__avatar"
[ngStyle]="{
width: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')',
height: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')'
}"
></div>
</ng-template>
<ng-container
*ngTemplateOutlet="
(customTemplatesService.avatarTemplate$ | async) || defaultAvatar;
context: context
"
></ng-container>
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnChanges,
OnDestroy,
} from '@angular/core';
import { Component, Input, OnChanges } from '@angular/core';
import { Channel, User } from 'stream-chat';
import { CustomTemplatesService } from '../custom-templates.service';
import {
Expand All @@ -15,7 +7,6 @@ import {
AvatarType,
DefaultStreamChatGenerics,
} from '../types';
import { ThemeService } from '../theme.service';

/**
* The `AvatarPlaceholder` component displays the [default avatar](./AvatarComponent.mdx) unless a [custom template](../services/CustomTemplatesService.mdx) is provided. This component is used by the SDK internally, you likely won't need to use it.
Expand All @@ -25,9 +16,7 @@ import { ThemeService } from '../theme.service';
templateUrl: './avatar-placeholder.component.html',
styles: [],
})
export class AvatarPlaceholderComponent
implements OnChanges, AfterViewInit, OnDestroy
{
export class AvatarPlaceholderComponent implements OnChanges {
/**
* An optional name of the image, used for fallback image or image title (if `imageUrl` is provided)
*/
Expand Down Expand Up @@ -77,37 +66,7 @@ export class AvatarPlaceholderComponent
initialsType: undefined,
showOnlineIndicator: undefined,
};
isVisible = true;
private mutationObserver?: MutationObserver;
constructor(
public customTemplatesService: CustomTemplatesService,
private hostElement: ElementRef<HTMLElement>,
private cdRef: ChangeDetectorRef,
private themeService: ThemeService
) {}

ngAfterViewInit(): void {
const elementToObserve =
this.hostElement.nativeElement.parentElement?.parentElement
?.parentElement;
if (
this.location !== 'message-sender' ||
!elementToObserve ||
!elementToObserve.classList.contains('str-chat__li') ||
this.themeService.themeVersion === '1'
) {
this.isVisible = true;
this.cdRef.detectChanges();
return;
}
this.checkIfVisible();
this.mutationObserver = new MutationObserver(() => {
this.checkIfVisible();
});
this.mutationObserver.observe(elementToObserve, {
attributeFilter: ['class'],
});
}
constructor(public customTemplatesService: CustomTemplatesService) {}

ngOnChanges(): void {
this.context = {
Expand All @@ -122,19 +81,4 @@ export class AvatarPlaceholderComponent
showOnlineIndicator: this.showOnlineIndicator,
};
}

ngOnDestroy(): void {
this.mutationObserver?.disconnect();
}

private checkIfVisible() {
const isVisible =
getComputedStyle(this.hostElement.nativeElement).getPropertyValue(
'visibility'
) === 'visible';
if (isVisible !== this.isVisible) {
this.isVisible = isVisible;
this.cdRef.detectChanges();
}
}
}
Loading