1 import { filter, first, map, tap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router'
4 import { getParameterByName } from '../shared/misc/utils'
5 import { AuthService, ServerService, Notifier } from '@app/core'
6 import { of } from 'rxjs'
7 import { ServerConfig } from '@shared/models'
10 selector: 'my-header',
11 templateUrl: './header.component.html',
12 styleUrls: [ './header.component.scss' ]
15 export class HeaderComponent implements OnInit {
18 private serverConfig: ServerConfig
21 private router: Router,
22 private route: ActivatedRoute,
23 private auth: AuthService,
24 private serverService: ServerService,
25 private authService: AuthService,
26 private notifier: Notifier
32 filter(e => e instanceof NavigationEnd),
33 map(() => getParameterByName('search', window.location.href))
35 .subscribe(searchQuery => this.searchValue = searchQuery || '')
37 this.serverConfig = this.serverService.getTmpConfig()
38 this.serverService.getConfig().subscribe(
39 config => this.serverConfig = config,
41 err => this.notifier.error(err.message)
46 if (this.isUserLoggedIn()) {
47 return [ '/videos/upload' ]
48 } else if (this.isRegistrationAllowed()) {
51 return [ '/login', { fromUpload: true } ]
56 const queryParams: Params = {}
58 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
59 Object.assign(queryParams, this.route.snapshot.queryParams)
62 Object.assign(queryParams, { search: this.searchValue })
64 const o = this.auth.isLoggedIn()
65 ? this.loadUserLanguagesIfNeeded(queryParams)
68 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
72 return this.authService.isLoggedIn()
75 isRegistrationAllowed () {
76 return this.serverConfig.signup.allowed &&
77 this.serverConfig.signup.allowedForCurrentIP
80 private loadUserLanguagesIfNeeded (queryParams: any) {
81 if (queryParams && queryParams.languageOneOf) return of(queryParams)
83 return this.auth.userInformationLoaded
86 tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))