PeerTube/client/src/app/shared/shared-actor-image/actor-avatar.component.ts

164 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-03-02 10:16:21 +01:00
import { Component, Input, OnChanges, OnInit } from '@angular/core'
2021-04-28 11:49:34 +02:00
import { VideoChannel } from '../shared-main'
import { Account } from '../shared-main/account/account.model'
import { objectKeysTyped } from '@shared/core-utils'
2021-04-28 11:49:34 +02:00
type ActorInput = {
name: string
avatars: { width: number, url?: string, path: string }[]
2021-04-28 11:49:34 +02:00
url: string
}
export type ActorAvatarSize = '18' | '25' | '28' | '32' | '34' | '35' | '36' | '40' | '48' | '75' | '80' | '100' | '120'
2021-05-05 09:44:24 +02:00
2021-04-28 11:49:34 +02:00
@Component({
selector: 'my-actor-avatar',
styleUrls: [ './actor-avatar.component.scss' ],
templateUrl: './actor-avatar.component.html'
})
2023-03-02 10:16:21 +01:00
export class ActorAvatarComponent implements OnInit, OnChanges {
2021-08-17 14:42:53 +02:00
private _title: string
2022-06-27 11:22:21 +02:00
@Input() actor: ActorInput
@Input() actorType: 'channel' | 'account' | 'unlogged'
2021-04-28 11:49:34 +02:00
2021-09-08 10:10:51 +02:00
@Input() previewImage: string
2021-04-28 11:49:34 +02:00
2022-05-02 13:46:09 +02:00
@Input() size: ActorAvatarSize
2021-04-28 11:49:34 +02:00
// Use an external link
@Input() href: string
// Use routerLink
@Input() internalHref: string | any[]
@Input() set title (value) {
this._title = value
}
get title () {
if (this._title) return this._title
2022-06-27 11:22:21 +02:00
if (this.isAccount()) return $localize`${this.actor.name} (account page)`
if (this.isChannel()) return $localize`${this.actor.name} (channel page)`
2021-04-28 11:49:34 +02:00
return ''
}
2022-06-15 14:59:25 +02:00
classes: string[] = []
2023-03-02 10:16:21 +01:00
defaultAvatarUrl: string
avatarUrl: string
2022-06-15 14:59:25 +02:00
2023-03-02 10:16:21 +01:00
ngOnInit () {
this.buildDefaultAvatarUrl()
2021-04-28 11:49:34 +02:00
2023-03-02 10:16:21 +01:00
this.buildAvatarUrl()
2023-03-06 11:39:23 +01:00
this.buildClasses()
2021-04-28 11:49:34 +02:00
}
2023-03-02 10:16:21 +01:00
ngOnChanges () {
this.buildAvatarUrl()
2023-03-06 11:39:23 +01:00
this.buildClasses()
2021-04-28 11:49:34 +02:00
}
2023-03-02 10:16:21 +01:00
private buildClasses () {
2022-06-15 14:59:25 +02:00
this.classes = [ 'avatar' ]
2021-08-17 14:42:53 +02:00
2022-06-27 11:22:21 +02:00
if (this.size) {
this.classes.push(`avatar-${this.size}`)
}
2021-08-17 14:42:53 +02:00
2022-06-27 11:22:21 +02:00
if (this.isChannel()) {
this.classes.push('channel')
} else {
this.classes.push('account')
}
2021-08-17 14:42:53 +02:00
2022-06-27 11:22:21 +02:00
// No avatar, use actor name initial
if (this.displayActorInitial()) {
2022-06-15 14:59:25 +02:00
this.classes.push('initial')
this.classes.push(this.getColorTheme())
2021-08-17 14:42:53 +02:00
}
}
2023-03-02 10:16:21 +01:00
private buildDefaultAvatarUrl () {
this.defaultAvatarUrl = this.isChannel()
? VideoChannel.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
: Account.GET_DEFAULT_AVATAR_URL(this.getSizeNumber())
}
private buildAvatarUrl () {
if (!this.actor) {
this.avatarUrl = ''
return
}
if (this.isAccount()) {
this.avatarUrl = Account.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
return
}
if (this.isChannel()) {
this.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(this.actor, this.getSizeNumber())
return
}
this.avatarUrl = ''
}
2022-06-27 11:22:21 +02:00
displayImage () {
if (this.actorType === 'unlogged') return true
2023-03-06 11:39:23 +01:00
if (this.previewImage) return true
2022-06-27 11:22:21 +02:00
return !!(this.actor && this.avatarUrl)
}
displayActorInitial () {
2023-03-06 11:39:23 +01:00
return !this.displayImage() && this.actor && !this.avatarUrl
2022-06-27 11:22:21 +02:00
}
displayPlaceholder () {
return this.actorType !== 'unlogged' && !this.actor
}
getActorInitial () {
const name = this.actor?.name
if (!name) return ''
return name.slice(0, 1)
}
private isAccount () {
return this.actorType === 'account'
}
private isChannel () {
return this.actorType === 'channel'
2021-04-28 11:49:34 +02:00
}
2022-05-02 13:46:09 +02:00
private getSizeNumber () {
if (this.size) return +this.size
return undefined
}
2021-04-28 11:49:34 +02:00
private getColorTheme () {
2022-06-27 11:22:21 +02:00
const initialLowercase = this.getActorInitial().toLowerCase()
2021-04-28 11:49:34 +02:00
// Keep consistency with CSS
const themes = {
'0123456789abc': 'blue',
2023-05-24 15:27:15 +02:00
'def': 'green',
'ghi': 'purple',
'jkl': 'gray',
'mno': 'yellow',
'pqr': 'orange',
'stvu': 'red',
'wxyz': 'dark-blue'
2021-04-28 11:49:34 +02:00
}
const theme = objectKeysTyped(themes)
.find(chars => chars.includes(initialLowercase))
2021-04-28 11:49:34 +02:00
2022-07-28 08:45:25 +02:00
return themes[theme] || 'blue'
2021-04-28 11:49:34 +02:00
}
}