Add ListOverflow component to prevent sub-menu overflow
[oweals/peertube.git] / client / src / app / +video-channels / video-channels.component.ts
1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
4 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
5 import { RestExtractor } from '@app/shared'
6 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
7 import { Subscription } from 'rxjs'
8 import { AuthService, Notifier } from '@app/core'
9 import { Hotkey, HotkeysService } from 'angular2-hotkeys'
10 import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { ListOverflowItem } from '@app/shared/misc/list-overflow.component'
13
14 @Component({
15   templateUrl: './video-channels.component.html',
16   styleUrls: [ './video-channels.component.scss' ]
17 })
18 export class VideoChannelsComponent implements OnInit, OnDestroy {
19   @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent
20
21   videoChannel: VideoChannel
22   hotkeys: Hotkey[]
23   links: ListOverflowItem[] = []
24   isChannelManageable = false
25
26   private routeSub: Subscription
27
28   constructor (
29     private i18n: I18n,
30     private route: ActivatedRoute,
31     private notifier: Notifier,
32     private authService: AuthService,
33     private videoChannelService: VideoChannelService,
34     private restExtractor: RestExtractor,
35     private hotkeysService: HotkeysService
36   ) { }
37
38   ngOnInit () {
39     this.routeSub = this.route.params
40                         .pipe(
41                           map(params => params[ 'videoChannelName' ]),
42                           distinctUntilChanged(),
43                           switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)),
44                           catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
45                         )
46                         .subscribe(videoChannel => {
47                           this.videoChannel = videoChannel
48
49                           if (this.authService.isLoggedIn()) {
50                             this.authService.userInformationLoaded
51                               .subscribe(() => {
52                                 const channelUserId = this.videoChannel.ownerAccount.userId
53                                 this.isChannelManageable = channelUserId && channelUserId === this.authService.getUser().id
54                               })
55                           }
56                         })
57
58     this.hotkeys = [
59       new Hotkey('S', (event: KeyboardEvent): boolean => {
60         this.subscribeButton.subscribed ?
61           this.subscribeButton.unsubscribe() :
62           this.subscribeButton.subscribe()
63         return false
64       }, undefined, this.i18n('Subscribe to the account'))
65     ]
66     if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys)
67
68     this.links = [
69       { label: this.i18n('Videos'), routerLink: 'videos' },
70       { label: this.i18n('Video playlists'), routerLink: 'video-playlists' },
71       { label: this.i18n('About'), routerLink: 'about' }
72     ]
73   }
74
75   ngOnDestroy () {
76     if (this.routeSub) this.routeSub.unsubscribe()
77
78     // Unbind hotkeys
79     if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys)
80   }
81
82   isUserLoggedIn () {
83     return this.authService.isLoggedIn()
84   }
85
86   get isManageable () {
87     if (!this.isUserLoggedIn()) return false
88     return this.videoChannel.ownerAccount.userId === this.authService.getUser().id
89   }
90
91   activateCopiedMessage () {
92     this.notifier.success(this.i18n('Username copied'))
93   }
94 }