1 import { Component, Input, OnChanges, OnInit } from '@angular/core'
2 import { VideoChannel } from '../shared-main'
3 import { Account } from '../shared-main/account/account.model'
7 avatars: { width: number, url?: string, path: string }[]
11 export type ActorAvatarSize = '18' | '25' | '28' | '32' | '34' | '35' | '36' | '40' | '48' | '75' | '80' | '100' | '120'
14 selector: 'my-actor-avatar',
15 styleUrls: [ './actor-avatar.component.scss' ],
16 templateUrl: './actor-avatar.component.html'
18 export class ActorAvatarComponent implements OnInit, OnChanges {
19 private _title: string
21 @Input() actor: ActorInput
22 @Input() actorType: 'channel' | 'account' | 'unlogged'
24 @Input() previewImage: string
26 @Input() size: ActorAvatarSize
28 // Use an external link
31 @Input() internalHref: string | any[]
33 @Input() set title (value) {
38 if (this._title) return this._title
39 if (this.isAccount()) return $localize`${this.actor.name} (account page)`
40 if (this.isChannel()) return $localize`${this.actor.name} (channel page)`
45 classes: string[] = []
46 defaultAvatarUrl: string
50 this.buildDefaultAvatarUrl()
61 private buildClasses () {
62 this.classes = [ 'avatar' ]
65 this.classes.push(`avatar-${this.size}`)
68 if (this.isChannel()) {
69 this.classes.push('channel')
71 this.classes.push('account')
74 // No avatar, use actor name initial
75 if (this.displayActorInitial()) {
76 this.classes.push('initial')
77 this.classes.push(this.getColorTheme())
81 private buildDefaultAvatarUrl () {
82 this.defaultAvatarUrl = this.isChannel()
83 ? VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
84 : Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
87 private buildAvatarUrl () {
93 if (this.isAccount()) {
94 this.avatarUrl = Account.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
98 if (this.isChannel()) {
99 this.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
107 if (this.actorType === 'unlogged') return true
108 if (this.previewImage) return true
110 return !!(this.actor && this.avatarUrl)
113 displayActorInitial () {
114 return !this.displayImage() && this.actor && !this.avatarUrl
117 displayPlaceholder () {
118 return this.actorType !== 'unlogged' && !this.actor
122 const name = this.actor?.name
125 return name.slice(0, 1)
128 private isAccount () {
129 return this.actorType === 'account'
132 private isChannel () {
133 return this.actorType === 'channel'
136 private getSizeNumber () {
137 if (this.size) return +this.size
142 private getColorTheme () {
143 const initialLowercase = this.getActorInitial().toLowerCase()
145 // Keep consistency with CSS
147 '0123456789abc': 'blue',
157 const theme = Object.keys(themes)
158 .find(chars => chars.includes(initialLowercase))
160 return themes[theme] || 'blue'