]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/menu/notification.component.ts
User dropdown and notifications popover improvements (#3344)
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / notification.component.ts
CommitLineData
bc6f8863 1import { Subject, Subscription } from 'rxjs'
2f1548fd 2import { filter } from 'rxjs/operators'
51a83970 3import { Component, EventEmitter, Output, OnDestroy, OnInit, ViewChild } from '@angular/core'
67ed6552 4import { NavigationEnd, Router } from '@angular/router'
51a83970 5import { Notifier, PeerTubeSocket, ScreenService } from '@app/core'
67ed6552
C
6import { UserNotificationService } from '@app/shared/shared-main'
7import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
2f1548fd
C
8
9@Component({
51a83970
K
10 selector: 'my-notification',
11 templateUrl: './notification.component.html',
12 styleUrls: [ './notification.component.scss' ]
2f1548fd 13})
51a83970 14export class NotificationComponent implements OnInit, OnDestroy {
f36da21e 15 @ViewChild('popover', { static: true }) popover: NgbPopover
bc6f8863 16
30d55e75 17 @Output() navigate = new EventEmitter<HTMLAnchorElement>()
2f1548fd
C
18
19 unreadNotifications = 0
b28e4e5e 20 loaded = false
51a83970 21 opened = false
2f1548fd 22
bc6f8863
C
23 markAllAsReadSubject = new Subject<boolean>()
24
2f1548fd
C
25 private notificationSub: Subscription
26 private routeSub: Subscription
27
28 constructor (
29 private userNotificationService: UserNotificationService,
51a83970 30 private screenService: ScreenService,
a5cf76af 31 private peertubeSocket: PeerTubeSocket,
2f1548fd
C
32 private notifier: Notifier,
33 private router: Router
41d71344
C
34 ) {
35 }
2f1548fd
C
36
37 ngOnInit () {
38 this.userNotificationService.countUnreadNotifications()
41d71344
C
39 .subscribe(
40 result => {
41 this.unreadNotifications = Math.min(result, 99) // Limit number to 99
42 this.subscribeToNotifications()
43 },
2f1548fd 44
41d71344
C
45 err => this.notifier.error(err.message)
46 )
2f1548fd
C
47
48 this.routeSub = this.router.events
49 .pipe(filter(event => event instanceof NavigationEnd))
50 .subscribe(() => this.closePopover())
51 }
52
53 ngOnDestroy () {
54 if (this.notificationSub) this.notificationSub.unsubscribe()
55 if (this.routeSub) this.routeSub.unsubscribe()
56 }
57
51a83970
K
58 get isInMobileView () {
59 return this.screenService.isInMobileView()
60 }
61
2f1548fd
C
62 closePopover () {
63 this.popover.close()
64 }
65
51a83970
K
66 onPopoverShown () {
67 this.opened = true
68
69 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
70 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
71 }
72
b28e4e5e
C
73 onPopoverHidden () {
74 this.loaded = false
51a83970
K
75 this.opened = false
76
77 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
78 }
79
80 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
81 onMenuScrollEvent () {
82 document.querySelector('menu').scrollTo(0, 0)
b28e4e5e
C
83 }
84
85 onNotificationLoaded () {
86 this.loaded = true
87 }
88
30d55e75 89 onNavigate (link: HTMLAnchorElement) {
51a83970 90 this.closePopover()
30d55e75
K
91 this.navigate.emit(link)
92 }
93
10475dea 94 markAllAsRead () {
bc6f8863 95 this.markAllAsReadSubject.next(true)
10475dea
RK
96 }
97
41d71344 98 private async subscribeToNotifications () {
a5cf76af 99 const obs = await this.peertubeSocket.getMyNotificationsSocket()
41d71344
C
100
101 this.notificationSub = obs.subscribe(data => {
102 if (data.type === 'new') return this.unreadNotifications++
103 if (data.type === 'read') return this.unreadNotifications--
104 if (data.type === 'read-all') return this.unreadNotifications = 0
105 })
2f1548fd 106 }
2f1548fd 107}