diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index d80de80e..475eeeaf 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -16,6 +16,7 @@ import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; import PasswordInput from "../common/password-input"; import TotpModal from "../common/totp-modal"; +import { UnreadCounterService } from "../../services"; interface LoginProps { prev?: string; @@ -55,6 +56,8 @@ async function handleLoginSuccess(i: Login, loginRes: LoginResponse) { : i.props.history.action === "PUSH" ? i.props.history.back() : i.props.history.replace("/"); + + UnreadCounterService.Instance.updateAll(); } async function handleLoginSubmit(i: Login, event: any) { diff --git a/src/shared/components/person/inbox.tsx b/src/shared/components/person/inbox.tsx index 247850c7..d1237e7b 100644 --- a/src/shared/components/person/inbox.tsx +++ b/src/shared/components/person/inbox.tsx @@ -795,7 +795,7 @@ export class Inbox extends Component { limit, }), }); - UnreadCounterService.Instance.update(); + UnreadCounterService.Instance.updateInboxCounts(); } async handleSortChange(val: CommentSortType) { diff --git a/src/shared/components/person/registration-applications.tsx b/src/shared/components/person/registration-applications.tsx index 4525fa5a..2aca3cad 100644 --- a/src/shared/components/person/registration-applications.tsx +++ b/src/shared/components/person/registration-applications.tsx @@ -23,6 +23,7 @@ import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; import { Paginator } from "../common/paginator"; import { RegistrationApplication } from "../common/registration-application"; +import { UnreadCounterService } from "../../services"; enum UnreadOrAll { Unread, @@ -243,6 +244,10 @@ export class RegistrationApplications extends Component< approveRes.data.registration_application, s.appsRes.data.registration_applications, ); + if (this.state.unreadOrAll === UnreadOrAll.Unread) { + this.refetch(); + UnreadCounterService.Instance.updateApplications(); + } } return s; }); diff --git a/src/shared/components/person/reports.tsx b/src/shared/components/person/reports.tsx index 122fa234..915107c1 100644 --- a/src/shared/components/person/reports.tsx +++ b/src/shared/components/person/reports.tsx @@ -46,6 +46,7 @@ import { Spinner } from "../common/icon"; import { Paginator } from "../common/paginator"; import { PostReport } from "../post/post-report"; import { PrivateMessageReport } from "../private_message/private-message-report"; +import { UnreadCounterService } from "../../services"; enum UnreadOrAll { Unread, @@ -610,16 +611,28 @@ export class Reports extends Component { async handleResolveCommentReport(form: ResolveCommentReport) { const res = await HttpService.client.resolveCommentReport(form); this.findAndUpdateCommentReport(res); + if (this.state.unreadOrAll === UnreadOrAll.Unread) { + this.refetch(); + UnreadCounterService.Instance.updateReports(); + } } async handleResolvePostReport(form: ResolvePostReport) { const res = await HttpService.client.resolvePostReport(form); this.findAndUpdatePostReport(res); + if (this.state.unreadOrAll === UnreadOrAll.Unread) { + this.refetch(); + UnreadCounterService.Instance.updateReports(); + } } async handleResolvePrivateMessageReport(form: ResolvePrivateMessageReport) { const res = await HttpService.client.resolvePrivateMessageReport(form); this.findAndUpdatePrivateMessageReport(res); + if (this.state.unreadOrAll === UnreadOrAll.Unread) { + this.refetch(); + UnreadCounterService.Instance.updateReports(); + } } findAndUpdateCommentReport(res: RequestState) { diff --git a/src/shared/services/UnreadCounterService.ts b/src/shared/services/UnreadCounterService.ts index dacf72c4..d847f801 100644 --- a/src/shared/services/UnreadCounterService.ts +++ b/src/shared/services/UnreadCounterService.ts @@ -32,27 +32,36 @@ export class UnreadCounterService { constructor() { if (isBrowser()) { - poll(this.update, updateUnreadCountsInterval); + poll(async () => this.updateAll(), updateUnreadCountsInterval); } } - public update = async () => { + private get shouldUpdate() { if (window.document.visibilityState === "hidden") { - return; + return false; } if (!myAuth()) { - return; + return false; } - const unreadCountRes = await HttpService.client.getUnreadCount(); - if (unreadCountRes.state === "success") { - this.unreadPrivateMessages = unreadCountRes.data.private_messages; - this.unreadReplies = unreadCountRes.data.replies; - this.unreadMentions = unreadCountRes.data.mentions; - this.unreadInboxCountSubject.next( - this.unreadPrivateMessages + this.unreadReplies + this.unreadMentions, - ); + return true; + } + + public async updateInboxCounts() { + if (this.shouldUpdate) { + const unreadCountRes = await HttpService.client.getUnreadCount(); + if (unreadCountRes.state === "success") { + this.unreadPrivateMessages = unreadCountRes.data.private_messages; + this.unreadReplies = unreadCountRes.data.replies; + this.unreadMentions = unreadCountRes.data.mentions; + this.unreadInboxCountSubject.next( + this.unreadPrivateMessages + this.unreadReplies + this.unreadMentions, + ); + } } - if (UserService.Instance.moderatesSomething) { + } + + public async updateReports() { + if (this.shouldUpdate && UserService.Instance.moderatesSomething) { const reportCountRes = await HttpService.client.getReportCount({}); if (reportCountRes.state === "success") { this.commentReportCount = reportCountRes.data.comment_reports ?? 0; @@ -66,7 +75,10 @@ export class UnreadCounterService { ); } } - if (amAdmin()) { + } + + public async updateApplications() { + if (this.shouldUpdate && amAdmin()) { const unreadApplicationsRes = await HttpService.client.getUnreadRegistrationApplicationCount(); if (unreadApplicationsRes.state === "success") { @@ -75,7 +87,13 @@ export class UnreadCounterService { ); } } - }; + } + + public async updateAll() { + this.updateInboxCounts(); + this.updateReports(); + this.updateApplications(); + } static get Instance() { return this.#instance ?? (this.#instance = new this());