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 templateUrl: './my-video-channels.component.html',
11 styleUrls: [ './my-video-channels.component.scss' ]
13 export class MyVideoChannelsComponent implements OnInit {
16 videoChannels: VideoChannel[] = []
17 videoChannelsChartData: ChartData[]
18 videoChannelsMinimumDailyViews = 0
19 videoChannelsMaximumDailyViews: number
21 channelsSearch: string
22 channelsSearchChanged = new Subject<string>()
27 private authService: AuthService,
28 private notifier: Notifier,
29 private confirmService: ConfirmService,
30 private videoChannelService: VideoChannelService,
31 private screenService: ScreenService
35 this.user = this.authService.getUser()
37 this.loadVideoChannels()
39 this.channelsSearchChanged
40 .pipe(debounceTime(500))
42 this.loadVideoChannels()
46 get isInSmallView () {
47 return this.screenService.isInSmallView()
62 min: Math.max(0, this.videoChannelsMinimumDailyViews - (3 * this.videoChannelsMaximumDailyViews / 100)),
63 max: Math.max(1, this.videoChannelsMaximumDailyViews)
83 custom: function (tooltip: any) {
85 // disable displaying the color box
86 tooltip.displayColors = false
89 label: (tooltip: any, data: any) => `${tooltip.value} views`
100 this.channelsSearch = ''
101 this.onChannelsSearchChanged()
104 onChannelsSearchChanged () {
105 this.channelsSearchChanged.next()
108 async deleteVideoChannel (videoChannel: VideoChannel) {
109 const res = await this.confirmService.confirmWithInput(
110 $localize`Do you really want to delete ${videoChannel.displayName}?
111 It will delete ${videoChannel.videosCount} videos uploaded in this channel, and you will not be able to create another
112 channel with the same name (${videoChannel.name})!`,
114 $localize`Please type the display name of the video channel (${videoChannel.displayName}) to confirm`,
116 videoChannel.displayName,
120 if (res === false) return
122 this.videoChannelService.removeVideoChannel(videoChannel)
125 this.loadVideoChannels()
126 this.notifier.success($localize`Video channel ${videoChannel.displayName} deleted.`)
129 error => this.notifier.error(error.message)
133 private loadVideoChannels () {
134 this.authService.userInformationLoaded
135 .pipe(mergeMap(() => this.videoChannelService.listAccountVideoChannels(this.user.account, null, true, this.channelsSearch)))
137 this.videoChannels = res.data
138 this.totalItems = res.total
141 this.videoChannelsChartData = this.videoChannels.map(v => ({
142 labels: v.viewsPerDay.map(day => day.date.toLocaleDateString()),
145 label: $localize`Views for the day`,
146 data: v.viewsPerDay.map(day => day.views),
148 borderColor: '#c6c6c6'
153 // chart options that depend on chart data:
154 // we don't want to skew values and have min at 0, so we define what the floor/ceiling is here
155 this.videoChannelsMinimumDailyViews = min(
156 // compute local minimum daily views for each channel, by their "views" attribute
157 this.videoChannels.map(v => minBy(
160 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute
162 this.videoChannelsMaximumDailyViews = max(
163 // compute local maximum daily views for each channel, by their "views" attribute
164 this.videoChannels.map(v => maxBy(
167 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute