1 import { ChartData } from 'chart.js'
2 import { max, maxBy, min, minBy } from 'lodash-es'
3 import { Subject } from 'rxjs'
4 import { debounceTime, mergeMap } from 'rxjs/operators'
5 import { Component, OnInit } from '@angular/core'
6 import { AuthService, ConfirmService, Notifier, ScreenService, User } from '@app/core'
7 import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
10 selector: 'my-account-video-channels',
11 templateUrl: './my-account-video-channels.component.html',
12 styleUrls: [ './my-account-video-channels.component.scss' ]
14 export class MyAccountVideoChannelsComponent implements OnInit {
17 videoChannels: VideoChannel[] = []
18 videoChannelsChartData: ChartData[]
19 videoChannelsMinimumDailyViews = 0
20 videoChannelsMaximumDailyViews: number
22 channelsSearch: string
23 channelsSearchChanged = new Subject<string>()
28 private authService: AuthService,
29 private notifier: Notifier,
30 private confirmService: ConfirmService,
31 private videoChannelService: VideoChannelService,
32 private screenService: ScreenService
36 this.user = this.authService.getUser()
38 this.loadVideoChannels()
40 this.channelsSearchChanged
41 .pipe(debounceTime(500))
43 this.loadVideoChannels()
47 get isInSmallView () {
48 return this.screenService.isInSmallView()
63 min: Math.max(0, this.videoChannelsMinimumDailyViews - (3 * this.videoChannelsMaximumDailyViews / 100)),
64 max: Math.max(1, this.videoChannelsMaximumDailyViews)
84 custom: function (tooltip: any) {
86 // disable displaying the color box
87 tooltip.displayColors = false
90 label: (tooltip: any, data: any) => `${tooltip.value} views`
101 this.channelsSearch = ''
102 this.onChannelsSearchChanged()
105 onChannelsSearchChanged () {
106 this.channelsSearchChanged.next()
109 async deleteVideoChannel (videoChannel: VideoChannel) {
110 const res = await this.confirmService.confirmWithInput(
111 $localize`Do you really want to delete ${videoChannel.displayName}?
112 It will delete ${videoChannel.videosCount} videos uploaded in this channel, and you will not be able to create another
113 channel with the same name (${videoChannel.name})!`,
115 $localize`Please type the display name of the video channel (${videoChannel.displayName}) to confirm`,
119 if (res === false) return
121 this.videoChannelService.removeVideoChannel(videoChannel)
124 this.loadVideoChannels()
125 this.notifier.success($localize`Video channel ${videoChannel.displayName} deleted.`)
128 error => this.notifier.error(error.message)
132 private loadVideoChannels () {
133 this.authService.userInformationLoaded
134 .pipe(mergeMap(() => this.videoChannelService.listAccountVideoChannels(this.user.account, null, true, this.channelsSearch)))
136 this.videoChannels = res.data
137 this.totalItems = res.total
140 this.videoChannelsChartData = this.videoChannels.map(v => ({
141 labels: v.viewsPerDay.map(day => day.date.toLocaleDateString()),
144 label: $localize`Views for the day`,
145 data: v.viewsPerDay.map(day => day.views),
147 borderColor: '#c6c6c6'
152 // chart options that depend on chart data:
153 // we don't want to skew values and have min at 0, so we define what the floor/ceiling is here
154 this.videoChannelsMinimumDailyViews = min(
155 // compute local minimum daily views for each channel, by their "views" attribute
156 this.videoChannels.map(v => minBy(
159 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute
161 this.videoChannelsMaximumDailyViews = max(
162 // compute local maximum daily views for each channel, by their "views" attribute
163 this.videoChannels.map(v => maxBy(
166 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute