]>
Commit | Line | Data |
---|---|---|
bc6f8863 | 1 | import { Subject, Subscription } from 'rxjs' |
2f1548fd | 2 | import { filter } from 'rxjs/operators' |
51a83970 | 3 | import { Component, EventEmitter, Output, OnDestroy, OnInit, ViewChild } from '@angular/core' |
67ed6552 | 4 | import { NavigationEnd, Router } from '@angular/router' |
51a83970 | 5 | import { Notifier, PeerTubeSocket, ScreenService } from '@app/core' |
67ed6552 C |
6 | import { UserNotificationService } from '@app/shared/shared-main' |
7 | import { 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 | 14 | export 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() | |
1378c0d3 C |
39 | .subscribe({ |
40 | next: result => { | |
4de99f22 | 41 | this.unreadNotifications = result |
41d71344 C |
42 | this.subscribeToNotifications() |
43 | }, | |
2f1548fd | 44 | |
1378c0d3 C |
45 | error: 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 | } |