Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Improve comment component performance

Chocobozzz8dc8b9a42ffc
diff --git a/client/src/app/+video-watch/shared/comment/video-comment-add.component.ts b/client/src/app/+video-watch/shared/comment/video-comment-add.component.ts
index 7b0d21fe54..d39931f618 100644
--- a/client/src/app/+video-watch/shared/comment/video-comment-add.component.ts
+++ b/client/src/app/+video-watch/shared/comment/video-comment-add.component.ts
@@ -1,6 +1,7 @@
import { getLocaleDirection, NgClass } from '@angular/common'
import {
booleanAttribute,
+ ChangeDetectorRef,
Component,
ElementRef,
inject,
@@ -49,6 +50,7 @@ import { Observable } from 'rxjs'
]
})
export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
+ private cd = inject(ChangeDetectorRef)
protected formReactiveService = inject(FormReactiveService)
private notifier = inject(Notifier)
private videoCommentService = inject(VideoCommentService)
@@ -157,12 +159,16 @@ export class VideoCommentAddComponent extends FormReactive implements OnChanges,
this.addingComment = false
this.commentCreated.emit(comment)
this.form.reset()
+
+ this.cd.markForCheck()
},
error: err => {
this.addingComment = false
this.notifier.handleError(err)
+
+ this.cd.markForCheck()
}
})
}
diff --git a/client/src/app/+video-watch/shared/comment/video-comment.component.html b/client/src/app/+video-watch/shared/comment/video-comment.component.html
index 9c0ef7d58a..b0ca900d82 100644
--- a/client/src/app/+video-watch/shared/comment/video-comment.component.html
+++ b/client/src/app/+video-watch/shared/comment/video-comment.component.html
@@ -44,12 +44,15 @@
myTimestampRouteTransformer
></div>
- <div class="comment-actions">
+ <div class="comment-actions" #userCommentActionsEl>
<button (click)="onWantToReply()" class="comment-action-reply border-0 p-0" i18n>Reply</button>
- <my-user-moderation-dropdown
- [prependActions]="prependModerationActions" [buttonStyled]="false"
- buttonSize="small" [account]="commentAccount" [user]="commentUser" i18n-label label="Options" placement="bottom-left auto"
- ></my-user-moderation-dropdown>
+
+ @defer (on viewport(userCommentActionsEl); prefetch on idle) {
+ <my-user-moderation-dropdown
+ [prependActions]="prependModerationActions" [buttonStyled]="false"
+ buttonSize="small" [account]="commentAccount" [user]="commentUser" i18n-label label="Options" placement="bottom-left auto"
+ ></my-user-moderation-dropdown>
+ }
</div>
}
@@ -106,7 +109,7 @@
</div>
}
-@if (prependModerationActions) {
+@defer (when showCommentReportModal) {
<my-comment-report #commentReportModal [comment]="comment()"></my-comment-report>
}
diff --git a/client/src/app/+video-watch/shared/comment/video-comment.component.ts b/client/src/app/+video-watch/shared/comment/video-comment.component.ts
index 5ccf3ac030..d6a9945b92 100644
--- a/client/src/app/+video-watch/shared/comment/video-comment.component.ts
+++ b/client/src/app/+video-watch/shared/comment/video-comment.component.ts
@@ -1,5 +1,17 @@
import { NgClass } from '@angular/common'
-import { Component, OnChanges, OnInit, TemplateRef, inject, input, model, output, viewChild, ChangeDetectionStrategy } from '@angular/core'
+import {
+ ChangeDetectionStrategy,
+ ChangeDetectorRef,
+ Component,
+ OnChanges,
+ TemplateRef,
+ effect,
+ inject,
+ input,
+ model,
+ output,
+ viewChild
+} from '@angular/core'
import { RouterLink } from '@angular/router'
import { MarkdownService, Notifier, ScreenService, UserService } from '@app/core'
import { AuthService } from '@app/core/auth'
@@ -22,7 +34,7 @@ import { VideoCommentAddComponent } from './video-comment-add.component'
selector: 'my-video-comment',
templateUrl: './video-comment.component.html',
styleUrls: [ './video-comment.component.scss' ],
- changeDetection: ChangeDetectionStrategy.Eager,
+ changeDetection: ChangeDetectionStrategy.OnPush,
imports: [
NgClass,
ActorAvatarComponent,
@@ -35,7 +47,8 @@ import { VideoCommentAddComponent } from './video-comment-add.component'
FromNowPipe
]
})
-export class VideoCommentComponent implements OnInit, OnChanges {
+export class VideoCommentComponent implements OnChanges {
+ private cd = inject(ChangeDetectorRef)
private markdownService = inject(MarkdownService)
private authService = inject(AuthService)
private userService = inject(UserService)
@@ -73,16 +86,22 @@ export class VideoCommentComponent implements OnInit, OnChanges {
commentUser: User
private replyModalRef: NgbModalRef
+ showCommentReportModal = false
+
get user () {
return this.authService.getUser()
}
- ngOnInit () {
- this.init()
+ constructor () {
+ // Open the report modal as soon as it has been loaded (it may be deferred)
+ effect(() => {
+ const modal = this.commentReportModal()
+ if (modal) modal.show()
+ })
}
ngOnChanges () {
- this.init()
+ this.reinit()
this.toggleMobileReplyModalIfNeeded()
}
@@ -201,14 +220,17 @@ export class VideoCommentComponent implements OnInit, OnChanges {
if (user.hasRight(UserRight.MANAGE_USERS)) {
this.userService.getUserWithCache(account.userId)
.subscribe({
- next: user => this.commentUser = user,
+ next: user => {
+ this.commentUser = user
+ this.cd.markForCheck()
+ },
error: err => this.notifier.handleError(err)
})
}
}
- private async init () {
+ private async reinit () {
// Before HTML rendering restore line feed for markdown list compatibility
const commentText = this.comment().text.replace(/<br.?\/?>/g, '\r\n')
const html = await this.markdownService.textMarkdownToHTML({ markdown: commentText, withHtml: true, withEmoji: true })
@@ -263,10 +285,12 @@ export class VideoCommentComponent implements OnInit, OnChanges {
isHeader: true
})
}
+
+ this.cd.markForCheck()
}
private showReportModal () {
- this.commentReportModal().show()
+ this.showCommentReportModal = true
}
private closeReplyModal () {
diff --git a/client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts b/client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
index 89da369253..e695a17411 100644
--- a/client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
+++ b/client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
@@ -1,4 +1,4 @@
-import { Component, OnChanges, OnInit, inject, input, output, viewChild, ChangeDetectionStrategy } from '@angular/core'
+import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnChanges, OnInit, inject, input, output, viewChild } from '@angular/core'
import { AuthService, ConfirmService, HooksService, Notifier, ServerService, UserService } from '@app/core'
import { BulkRemoveCommentsOfBody, User, UserRight } from '@peertube/peertube-models'
import { Account } from '../shared-main/account/account.model'
@@ -26,6 +26,7 @@ export type UserModerationDisplayType = {
imports: [ UserBanModalComponent, ActionDropdownComponent ]
})
export class UserModerationDropdownComponent implements OnInit, OnChanges {
+ private cd = inject(ChangeDetectorRef)
private authService = inject(AuthService)
private notifier = inject(Notifier)
private confirmService = inject(ConfirmService)
@@ -85,7 +86,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
onUserBanned ({ muted }: { muted: boolean }) {
if (muted) this.account().mutedByInstance = true
- this.userChanged.emit()
+ this.emitUserChanged()
}
async unbanUser (user: User) {
@@ -96,7 +97,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
.subscribe({
next: () => {
this.notifier.success($localize`User ${user.username} unbanned.`)
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -119,7 +120,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
.subscribe({
next: () => {
this.notifier.success($localize`User ${user.username} deleted.`)
- this.userDeleted.emit()
+ this.emitUserDeleted()
},
error: err => this.notifier.handleError(err)
@@ -131,7 +132,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
.subscribe({
next: () => {
this.notifier.success($localize`User ${user.username} email set as verified`)
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -156,7 +157,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
this.account().mutedByUser = true
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -170,7 +171,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
this.account().mutedByUser = false
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -184,7 +185,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`${host} muted.`)
this.account().mutedServerByUser = true
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -198,7 +199,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`${host} unmuted.`)
this.account().mutedServerByUser = false
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -212,7 +213,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`Account ${account.nameWithHost} muted by your platform.`)
this.account().mutedByInstance = true
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -226,7 +227,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`Account ${account.nameWithHost} unmuted by your platform.`)
this.account().mutedByInstance = false
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -240,7 +241,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`Instance ${host} muted by the instance.`)
this.account().mutedServerByInstance = true
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -254,7 +255,7 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
this.account().mutedServerByInstance = false
- this.userChanged.emit()
+ this.emitUserChanged()
},
error: err => this.notifier.handleError(err)
@@ -280,6 +281,17 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
return [ '/admin', 'overview', 'users', 'update', user.id ]
}
+ // The parent may use the OnPush strategy, so we need to notify it that we emitted an event asynchronously
+ private emitUserChanged () {
+ this.userChanged.emit()
+ this.cd.markForCheck()
+ }
+
+ private emitUserDeleted () {
+ this.userDeleted.emit()
+ this.cd.markForCheck()
+ }
+
private isMyUser (user: User) {
return this.authService.getUser().id === user?.id
}
@@ -303,6 +315,8 @@ export class UserModerationDropdownComponent implements OnInit, OnChanges {
if (instanceModerationActions.length !== 0) userActions.push(...instanceModerationActions)
this.userActions = await this.hooks.wrapObject(userActions, 'moderation', 'filter:user-moderation.actions.create.result')
+
+ this.cd.markForCheck()
}
private buildMyAccountModerationActions () {