diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index 3c3b2725..900a12ec 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -1,31 +1,30 @@ -import { myAuth, showAvatars } from "@utils/app"; +import { showAvatars } from "@utils/app"; import { isBrowser } from "@utils/browser"; -import { numToSI, poll } from "@utils/helpers"; +import { numToSI } from "@utils/helpers"; import { amAdmin, canCreateCommunity } from "@utils/roles"; import { Component, createRef, linkEvent } from "inferno"; import { NavLink } from "inferno-router"; +import { GetSiteResponse } from "lemmy-js-client"; +import { donateLemmyUrl } from "../../config"; import { - GetReportCountResponse, - GetSiteResponse, - GetUnreadCountResponse, - GetUnreadRegistrationApplicationCountResponse, -} from "lemmy-js-client"; -import { donateLemmyUrl, updateUnreadCountsInterval } from "../../config"; -import { I18NextService, UserService } from "../../services"; -import { HttpService, RequestState } from "../../services/HttpService"; + I18NextService, + UserService, + UnreadCounterService, +} from "../../services"; import { toast } from "../../toast"; import { Icon } from "../common/icon"; import { PictrsImage } from "../common/pictrs-image"; +import { Subscription } from "rxjs"; interface NavbarProps { siteRes?: GetSiteResponse; } interface NavbarState { - unreadInboxCountRes: RequestState; - unreadReportCountRes: RequestState; - unreadApplicationCountRes: RequestState; onSiteBanner?(url: string): any; + unreadInboxCount: number; + unreadReportCount: number; + unreadApplicationCount: number; } function handleCollapseClick(i: Navbar) { @@ -44,13 +43,17 @@ function handleLogOut(i: Navbar) { } export class Navbar extends Component { - state: NavbarState = { - unreadInboxCountRes: { state: "empty" }, - unreadReportCountRes: { state: "empty" }, - unreadApplicationCountRes: { state: "empty" }, - }; collapseButtonRef = createRef(); mobileMenuRef = createRef(); + unreadInboxCountSubscription: Subscription; + unreadReportCountSubscription: Subscription; + unreadApplicationCountSubscription: Subscription; + + state: NavbarState = { + unreadInboxCount: 0, + unreadReportCount: 0, + unreadApplicationCount: 0, + }; constructor(props: any, context: any) { super(props, context); @@ -63,7 +66,18 @@ export class Navbar extends Component { if (isBrowser()) { // On the first load, check the unreads this.requestNotificationPermission(); - this.fetchUnreads(); + this.unreadInboxCountSubscription = + UnreadCounterService.Instance.unreadInboxCountSubject.subscribe( + unreadInboxCount => this.setState({ unreadInboxCount }), + ); + this.unreadReportCountSubscription = + UnreadCounterService.Instance.unreadReportCountSubject.subscribe( + unreadReportCount => this.setState({ unreadReportCount }), + ); + this.unreadApplicationCountSubscription = + UnreadCounterService.Instance.unreadApplicationCountSubject.subscribe( + unreadApplicationCount => this.setState({ unreadApplicationCount }), + ); this.requestNotificationPermission(); document.addEventListener("mouseup", this.handleOutsideMenuClick); @@ -72,6 +86,9 @@ export class Navbar extends Component { componentWillUnmount() { document.removeEventListener("mouseup", this.handleOutsideMenuClick); + this.unreadInboxCountSubscription.unsubscribe(); + this.unreadReportCountSubscription.unsubscribe(); + this.unreadApplicationCountSubscription.unsubscribe(); } // TODO class active corresponding to current pages @@ -103,34 +120,34 @@ export class Navbar extends Component { to="/inbox" className="p-1 nav-link border-0 nav-messages" title={I18NextService.i18n.t("unread_messages", { - count: Number(this.state.unreadApplicationCountRes.state), - formattedCount: numToSI(this.unreadInboxCount), + count: Number(this.state.unreadInboxCount), + formattedCount: numToSI(this.state.unreadInboxCount), })} onMouseUp={linkEvent(this, handleCollapseClick)} > - {this.unreadInboxCount > 0 && ( + {this.state.unreadInboxCount > 0 && ( - {numToSI(this.unreadInboxCount)} + {numToSI(this.state.unreadInboxCount)} )} - {this.moderatesSomething && ( + {UserService.Instance.moderatesSomething && (
  • - {this.unreadReportCount > 0 && ( + {this.state.unreadReportCount > 0 && ( - {numToSI(this.unreadReportCount)} + {numToSI(this.state.unreadReportCount)} )} @@ -144,16 +161,18 @@ export class Navbar extends Component { title={I18NextService.i18n.t( "unread_registration_applications", { - count: Number(this.unreadApplicationCount), - formattedCount: numToSI(this.unreadApplicationCount), + count: Number(this.state.unreadApplicationCount), + formattedCount: numToSI( + this.state.unreadApplicationCount, + ), }, )} onMouseUp={linkEvent(this, handleCollapseClick)} > - {this.unreadApplicationCount > 0 && ( + {this.state.unreadApplicationCount > 0 && ( - {numToSI(this.unreadApplicationCount)} + {numToSI(this.state.unreadApplicationCount)} )} @@ -268,46 +287,48 @@ export class Navbar extends Component { className="nav-link d-inline-flex align-items-center d-md-inline-block" to="/inbox" title={I18NextService.i18n.t("unread_messages", { - count: Number(this.unreadInboxCount), - formattedCount: numToSI(this.unreadInboxCount), + count: Number(this.state.unreadInboxCount), + formattedCount: numToSI(this.state.unreadInboxCount), })} onMouseUp={linkEvent(this, handleCollapseClick)} > {I18NextService.i18n.t("unread_messages", { - count: Number(this.unreadInboxCount), - formattedCount: numToSI(this.unreadInboxCount), + count: Number(this.state.unreadInboxCount), + formattedCount: numToSI(this.state.unreadInboxCount), })} - {this.unreadInboxCount > 0 && ( + {this.state.unreadInboxCount > 0 && ( - {numToSI(this.unreadInboxCount)} + {numToSI(this.state.unreadInboxCount)} )}
  • - {this.moderatesSomething && ( + {UserService.Instance.moderatesSomething && (