1 import { Component, Input, OnChanges } 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 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[] = []
48 if (this.isAccount()) return $localize`Account avatar`
49 if (this.isChannel()) return $localize`Channel avatar`
54 get defaultAvatarUrl () {
55 if (this.isChannel()) return VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
57 // account or unlogged
58 return Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
62 if (!this.actor) return ''
64 if (this.isAccount()) return Account.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
65 if (this.isChannel()) return VideoChannel.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
71 this.classes = [ 'avatar' ]
74 this.classes.push(`avatar-${this.size}`)
77 if (this.isChannel()) {
78 this.classes.push('channel')
80 this.classes.push('account')
83 // No avatar, use actor name initial
84 if (this.displayActorInitial()) {
85 this.classes.push('initial')
86 this.classes.push(this.getColorTheme())
91 if (this.actorType === 'unlogged') return true
93 return !!(this.actor && this.avatarUrl)
96 displayActorInitial () {
97 return this.actor && !this.avatarUrl
100 displayPlaceholder () {
101 return this.actorType !== 'unlogged' && !this.actor
105 const name = this.actor?.name
108 return name.slice(0, 1)
111 private isAccount () {
112 return this.actorType === 'account'
115 private isChannel () {
116 return this.actorType === 'channel'
119 private getSizeNumber () {
120 if (this.size) return +this.size
125 private getColorTheme () {
126 const initialLowercase = this.getActorInitial().toLowerCase()
128 // Keep consistency with CSS
130 '0123456789abc': 'blue',
140 const theme = Object.keys(themes)
141 .find(chars => chars.includes(initialLowercase))
143 return themes[theme] || 'blue'