import { myAuth, showAvatars } from "@utils/app"; import { isBrowser } from "@utils/browser"; import { numToSI, poll } from "@utils/helpers"; import { amAdmin, canCreateCommunity } from "@utils/roles"; import { Component, createRef, linkEvent } from "inferno"; import { NavLink } from "inferno-router"; 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"; import { toast } from "../../toast"; import { Icon } from "../common/icon"; import { PictrsImage } from "../common/pictrs-image"; interface NavbarProps { siteRes?: GetSiteResponse; } interface NavbarState { unreadInboxCountRes: RequestState; unreadReportCountRes: RequestState; unreadApplicationCountRes: RequestState; onSiteBanner?(url: string): any; } function handleCollapseClick(i: Navbar) { if ( i.collapseButtonRef.current?.attributes && i.collapseButtonRef.current?.attributes.getNamedItem("aria-expanded") ?.value === "true" ) { i.collapseButtonRef.current?.click(); } } function handleLogOut(i: Navbar) { UserService.Instance.logout(); handleCollapseClick(i); } export class Navbar extends Component { state: NavbarState = { unreadInboxCountRes: { state: "empty" }, unreadReportCountRes: { state: "empty" }, unreadApplicationCountRes: { state: "empty" }, }; collapseButtonRef = createRef(); mobileMenuRef = createRef(); constructor(props: any, context: any) { super(props, context); this.handleOutsideMenuClick = this.handleOutsideMenuClick.bind(this); } async componentDidMount() { // Subscribe to jwt changes if (isBrowser()) { // On the first load, check the unreads this.requestNotificationPermission(); this.fetchUnreads(); this.requestNotificationPermission(); document.addEventListener("mouseup", this.handleOutsideMenuClick); } } componentWillUnmount() { document.removeEventListener("mouseup", this.handleOutsideMenuClick); } // TODO class active corresponding to current pages render() { const siteView = this.props.siteRes?.site_view; const person = UserService.Instance.myUserInfo?.local_user_view.person; return (
); } handleOutsideMenuClick(event: MouseEvent) { if (!this.mobileMenuRef.current?.contains(event.target as Node | null)) { handleCollapseClick(this); } } get moderatesSomething(): boolean { const mods = UserService.Instance.myUserInfo?.moderates; const moderatesS = (mods && mods.length > 0) || false; return amAdmin() || moderatesS; } fetchUnreads() { poll(async () => { if (window.document.visibilityState !== "hidden") { const auth = myAuth(); if (auth) { this.setState({ unreadInboxCountRes: await HttpService.client.getUnreadCount({ auth, }), }); if (this.moderatesSomething) { this.setState({ unreadReportCountRes: await HttpService.client.getReportCount({ auth, }), }); } if (amAdmin()) { this.setState({ unreadApplicationCountRes: await HttpService.client.getUnreadRegistrationApplicationCount({ auth, }), }); } } } }, updateUnreadCountsInterval); } get unreadInboxCount(): number { if (this.state.unreadInboxCountRes.state == "success") { const data = this.state.unreadInboxCountRes.data; return data.replies + data.mentions + data.private_messages; } else { return 0; } } get unreadReportCount(): number { if (this.state.unreadReportCountRes.state == "success") { const data = this.state.unreadReportCountRes.data; return ( data.post_reports + data.comment_reports + (data.private_message_reports ?? 0) ); } else { return 0; } } get unreadApplicationCount(): number { if (this.state.unreadApplicationCountRes.state == "success") { const data = this.state.unreadApplicationCountRes.data; return data.registration_applications; } else { return 0; } } get currentLocation() { return this.context.router.history.location.pathname; } requestNotificationPermission() { if (UserService.Instance.myUserInfo) { document.addEventListener("DOMContentLoaded", function () { if (!Notification) { toast(I18NextService.i18n.t("notifications_error"), "danger"); return; } if (Notification.permission !== "granted") Notification.requestPermission(); }); } } }