PeerTube/client/src/app/menu/notification.component.ts

110 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-01-06 15:59:17 +01:00
import { Subject, Subscription } from 'rxjs'
2019-01-08 11:26:41 +01:00
import { filter } from 'rxjs/operators'
import { Component, EventEmitter, Output, OnDestroy, OnInit, ViewChild } from '@angular/core'
2020-06-23 14:10:17 +02:00
import { NavigationEnd, Router } from '@angular/router'
import { Notifier, PeerTubeSocket, ScreenService } from '@app/core'
2020-06-23 14:10:17 +02:00
import { UserNotificationService } from '@app/shared/shared-main'
import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
2019-01-08 11:26:41 +01:00
@Component({
selector: 'my-notification',
templateUrl: './notification.component.html',
styleUrls: [ './notification.component.scss' ]
2019-01-08 11:26:41 +01:00
})
export class NotificationComponent implements OnInit, OnDestroy {
2019-07-24 16:05:59 +02:00
@ViewChild('popover', { static: true }) popover: NgbPopover
2020-01-06 15:59:17 +01:00
@Output() navigate = new EventEmitter<HTMLAnchorElement>()
2019-01-08 11:26:41 +01:00
unreadNotifications = 0
2019-02-20 10:16:04 +01:00
loaded = false
opened = false
2019-01-08 11:26:41 +01:00
2020-01-06 15:59:17 +01:00
markAllAsReadSubject = new Subject<boolean>()
2019-01-08 11:26:41 +01:00
private notificationSub: Subscription
private routeSub: Subscription
constructor (
private userNotificationService: UserNotificationService,
private screenService: ScreenService,
private peertubeSocket: PeerTubeSocket,
2019-01-08 11:26:41 +01:00
private notifier: Notifier,
private router: Router
2019-02-15 15:52:18 +01:00
) {
}
2019-01-08 11:26:41 +01:00
ngOnInit () {
this.userNotificationService.countUnreadNotifications()
2021-08-17 11:27:47 +02:00
.subscribe({
next: result => {
2023-02-14 10:03:18 +01:00
this.unreadNotifications = result
2019-02-15 15:52:18 +01:00
this.subscribeToNotifications()
},
2019-01-08 11:26:41 +01:00
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
2019-01-08 11:26:41 +01:00
this.routeSub = this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(() => this.closePopover())
}
ngOnDestroy () {
if (this.notificationSub) this.notificationSub.unsubscribe()
if (this.routeSub) this.routeSub.unsubscribe()
}
get isInMobileView () {
return this.screenService.isInMobileView()
}
2019-01-08 11:26:41 +01:00
closePopover () {
this.popover.close()
}
onPopoverShown () {
this.opened = true
2023-11-07 11:19:17 +01:00
document.querySelector('nav').scrollTo(0, 0) // Reset menu scroll to easy lock
2024-02-22 10:12:04 +01:00
// eslint-disable-next-line @typescript-eslint/unbound-method
2023-11-07 11:19:17 +01:00
document.querySelector('nav').addEventListener('scroll', this.onMenuScrollEvent)
}
2019-02-20 10:16:04 +01:00
onPopoverHidden () {
this.loaded = false
this.opened = false
2024-02-22 10:12:04 +01:00
// eslint-disable-next-line @typescript-eslint/unbound-method
2023-11-07 11:19:17 +01:00
document.querySelector('nav').removeEventListener('scroll', this.onMenuScrollEvent)
}
// Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
onMenuScrollEvent () {
2023-11-07 11:19:17 +01:00
document.querySelector('nav').scrollTo(0, 0)
2019-02-20 10:16:04 +01:00
}
onNotificationLoaded () {
this.loaded = true
}
onNavigate (link: HTMLAnchorElement) {
this.closePopover()
this.navigate.emit(link)
}
markAllAsRead () {
2020-01-06 15:59:17 +01:00
this.markAllAsReadSubject.next(true)
}
2019-02-15 15:52:18 +01:00
private async subscribeToNotifications () {
const obs = await this.peertubeSocket.getMyNotificationsSocket()
2019-02-15 15:52:18 +01:00
this.notificationSub = obs.subscribe(data => {
if (data.type === 'new') return this.unreadNotifications++
if (data.type === 'read') return this.unreadNotifications--
if (data.type === 'read-all') return this.unreadNotifications = 0
})
2019-01-08 11:26:41 +01:00
}
}