import { Component, linkEvent } from "inferno"; import { NavLink } from "inferno-router"; import { CommentResponse, GetReportCount, GetReportCountResponse, GetSiteResponse, GetUnreadCount, GetUnreadCountResponse, GetUnreadRegistrationApplicationCount, GetUnreadRegistrationApplicationCountResponse, PrivateMessageResponse, UserOperation, wsJsonToRes, wsUserOp, } from "lemmy-js-client"; import { Subscription } from "rxjs"; import { i18n } from "../../i18next"; import { UserService, WebSocketService } from "../../services"; import { amAdmin, canCreateCommunity, donateLemmyUrl, isBrowser, myAuth, notifyComment, notifyPrivateMessage, numToSI, showAvatars, toast, wsClient, wsSubscribe, } from "../../utils"; import { Icon } from "../common/icon"; import { PictrsImage } from "../common/pictrs-image"; interface NavbarProps { siteRes: GetSiteResponse; } interface NavbarState { expanded: boolean; unreadInboxCount: number; unreadReportCount: number; unreadApplicationCount: number; showDropdown: boolean; onSiteBanner?(url: string): any; } export class Navbar extends Component { private wsSub: Subscription; private userSub: Subscription; private unreadInboxCountSub: Subscription; private unreadReportCountSub: Subscription; private unreadApplicationCountSub: Subscription; state: NavbarState = { unreadInboxCount: 0, unreadReportCount: 0, unreadApplicationCount: 0, expanded: false, showDropdown: false, }; subscription: any; constructor(props: any, context: any) { super(props, context); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); } componentDidMount() { // Subscribe to jwt changes if (isBrowser()) { // On the first load, check the unreads let auth = myAuth(false); if (auth && UserService.Instance.myUserInfo) { this.requestNotificationPermission(); WebSocketService.Instance.send( wsClient.userJoin({ auth, }) ); this.fetchUnreads(); } this.requestNotificationPermission(); // Subscribe to unread count changes this.unreadInboxCountSub = UserService.Instance.unreadInboxCountSub.subscribe(res => { this.setState({ unreadInboxCount: res }); }); // Subscribe to unread report count changes this.unreadReportCountSub = UserService.Instance.unreadReportCountSub.subscribe(res => { this.setState({ unreadReportCount: res }); }); // Subscribe to unread application count this.unreadApplicationCountSub = UserService.Instance.unreadApplicationCountSub.subscribe(res => { this.setState({ unreadApplicationCount: res }); }); } } componentWillUnmount() { this.wsSub.unsubscribe(); this.userSub.unsubscribe(); this.unreadInboxCountSub.unsubscribe(); this.unreadReportCountSub.unsubscribe(); this.unreadApplicationCountSub.unsubscribe(); } render() { return this.navbar(); } // TODO class active corresponding to current page navbar() { let siteView = this.props.siteRes.site_view; let person = UserService.Instance.myUserInfo?.local_user_view.person; return ( ); } get moderatesSomething(): boolean { let mods = UserService.Instance.myUserInfo?.moderates; let moderatesS = (mods && mods.length > 0) || false; return amAdmin() || moderatesS; } handleToggleExpandNavbar(i: Navbar) { i.setState({ expanded: !i.state.expanded }); } handleHideExpandNavbar(i: Navbar) { i.setState({ expanded: false, showDropdown: false }); } handleLogoutClick(i: Navbar) { i.setState({ showDropdown: false, expanded: false }); UserService.Instance.logout(); } handleToggleDropdown(i: Navbar) { i.setState({ showDropdown: !i.state.showDropdown }); } parseMessage(msg: any) { let op = wsUserOp(msg); console.log(msg); if (msg.error) { if (msg.error == "not_logged_in") { UserService.Instance.logout(); } return; } else if (msg.reconnect) { console.log(i18n.t("websocket_reconnected")); let auth = myAuth(false); if (UserService.Instance.myUserInfo && auth) { WebSocketService.Instance.send( wsClient.userJoin({ auth, }) ); this.fetchUnreads(); } } else if (op == UserOperation.GetUnreadCount) { let data = wsJsonToRes(msg); this.setState({ unreadInboxCount: data.replies + data.mentions + data.private_messages, }); this.sendUnreadCount(); } else if (op == UserOperation.GetReportCount) { let data = wsJsonToRes(msg); this.setState({ unreadReportCount: data.post_reports + data.comment_reports + (data.private_message_reports ?? 0), }); this.sendReportUnread(); } else if (op == UserOperation.GetUnreadRegistrationApplicationCount) { let data = wsJsonToRes(msg); this.setState({ unreadApplicationCount: data.registration_applications }); this.sendApplicationUnread(); } else if (op == UserOperation.CreateComment) { let data = wsJsonToRes(msg); let mui = UserService.Instance.myUserInfo; if ( mui && data.recipient_ids.includes(mui.local_user_view.local_user.id) ) { this.setState({ unreadInboxCount: this.state.unreadInboxCount + 1, }); this.sendUnreadCount(); notifyComment(data.comment_view, this.context.router); } } else if (op == UserOperation.CreatePrivateMessage) { let data = wsJsonToRes(msg); if ( data.private_message_view.recipient.id == UserService.Instance.myUserInfo?.local_user_view.person.id ) { this.setState({ unreadInboxCount: this.state.unreadInboxCount + 1, }); this.sendUnreadCount(); notifyPrivateMessage(data.private_message_view, this.context.router); } } } fetchUnreads() { console.log("Fetching inbox unreads..."); let auth = myAuth(); if (auth) { let unreadForm: GetUnreadCount = { auth, }; WebSocketService.Instance.send(wsClient.getUnreadCount(unreadForm)); console.log("Fetching reports..."); let reportCountForm: GetReportCount = { auth, }; WebSocketService.Instance.send(wsClient.getReportCount(reportCountForm)); if (amAdmin()) { console.log("Fetching applications..."); let applicationCountForm: GetUnreadRegistrationApplicationCount = { auth, }; WebSocketService.Instance.send( wsClient.getUnreadRegistrationApplicationCount(applicationCountForm) ); } } } get currentLocation() { return this.context.router.history.location.pathname; } sendUnreadCount() { UserService.Instance.unreadInboxCountSub.next(this.state.unreadInboxCount); } sendReportUnread() { UserService.Instance.unreadReportCountSub.next( this.state.unreadReportCount ); } sendApplicationUnread() { UserService.Instance.unreadApplicationCountSub.next( this.state.unreadApplicationCount ); } requestNotificationPermission() { if (UserService.Instance.myUserInfo) { document.addEventListener("DOMContentLoaded", function () { if (!Notification) { toast(i18n.t("notifications_error"), "danger"); return; } if (Notification.permission !== "granted") Notification.requestPermission(); }); } } }