Unsubscribe from the router
[oweals/peertube.git] / client / src / app / +my-account / my-account.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ServerService } from '@app/core'
3 import { NavigationStart, Router } from '@angular/router'
4 import { filter } from 'rxjs/operators'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
6 import { Subscription } from 'rxjs'
7
8 @Component({
9   selector: 'my-my-account',
10   templateUrl: './my-account.component.html',
11   styleUrls: [ './my-account.component.scss' ]
12 })
13 export class MyAccountComponent implements OnInit, OnDestroy {
14
15   libraryLabel = ''
16
17   private routeSub: Subscription
18
19   constructor (
20     private serverService: ServerService,
21     private router: Router,
22     private i18n: I18n
23   ) {}
24
25   ngOnInit () {
26     this.updateLibraryLabel(this.router.url)
27
28     this.routeSub = this.router.events
29         .pipe(filter(event => event instanceof NavigationStart))
30         .subscribe((event: NavigationStart) => this.updateLibraryLabel(event.url))
31   }
32
33   ngOnDestroy () {
34     if (this.routeSub) this.routeSub.unsubscribe()
35   }
36
37   isVideoImportEnabled () {
38     const importConfig = this.serverService.getConfig().import.videos
39
40     return importConfig.http.enabled || importConfig.torrent.enabled
41   }
42
43   private updateLibraryLabel (url: string) {
44     const [ path ] = url.split('?')
45
46     if (path === '/my-account/video-channels') {
47       this.libraryLabel = this.i18n('Channels')
48     } else if (path === '/my-account/videos') {
49       this.libraryLabel = this.i18n('Videos')
50     } else if (path === '/my-account/subscriptions') {
51       this.libraryLabel = this.i18n('Subscriptions')
52     } else if (path === '/my-account/video-imports') {
53       this.libraryLabel = this.i18n('Video imports')
54     } else {
55       this.libraryLabel = ''
56     }
57   }
58 }