1 import { Component, linkEvent, createRef, RefObject } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { WebSocketService, UserService } from '../services';
10 GetUserMentionsResponse,
11 GetPrivateMessagesForm,
12 PrivateMessagesResponse,
18 PrivateMessageResponse,
19 WebSocketJsonResponse,
20 } from 'lemmy-js-client';
23 pictrsAvatarThumbnail,
34 import { i18n } from '../i18next';
36 interface NavbarProps {
37 site: GetSiteResponse;
40 interface NavbarState {
45 messages: PrivateMessage[];
48 toggleSearch: boolean;
49 onSiteBanner?(url: string): any;
52 export class Navbar extends Component<NavbarProps, NavbarState> {
53 private wsSub: Subscription;
54 private userSub: Subscription;
55 private unreadCountSub: Subscription;
56 private searchTextField: RefObject<HTMLInputElement>;
57 emptyState: NavbarState = {
58 isLoggedIn: !!this.props.site.my_user,
69 constructor(props: any, context: any) {
70 super(props, context);
71 this.state = this.emptyState;
73 this.parseMessage = this.parseMessage.bind(this);
74 this.subscription = wsSubscribe(this.parseMessage);
77 // TODO this needs some work
78 UserService.Instance.user = this.props.site.my_user;
79 i18n.changeLanguage(getLanguage());
80 if (UserService.Instance.user) {
81 setTheme(UserService.Instance.user.theme);
84 // if (!!this.props.site.my_user) {
85 // UserService.Instance.this.props.site.my_user);
86 // // UserService.Instance.user = this.props.site.my_user;
88 // UserService.Instance.setUser(undefined);
93 // Subscribe to jwt changes
95 this.searchTextField = createRef();
96 console.log(`isLoggedIn = ${this.state.isLoggedIn}`);
98 // On the first load, check the unreads
99 if (this.state.isLoggedIn == false) {
100 // setTheme(data.my_user.theme, true);
101 // i18n.changeLanguage(getLanguage());
102 // i18n.changeLanguage('de');
104 this.requestNotificationPermission();
105 WebSocketService.Instance.userJoin();
109 this.userSub = UserService.Instance.jwtSub.subscribe(res => {
111 if (res !== undefined) {
112 this.requestNotificationPermission();
113 WebSocketService.Instance.getSite();
115 this.setState({ isLoggedIn: false });
119 // Subscribe to unread count changes
120 this.unreadCountSub = UserService.Instance.unreadCountSub.subscribe(
122 this.setState({ unreadCount: res });
128 handleSearchParam(i: Navbar, event: any) {
129 i.state.searchParam = event.target.value;
134 const searchParam = this.state.searchParam;
135 this.setState({ searchParam: '' });
136 this.setState({ toggleSearch: false });
137 if (searchParam === '') {
138 this.context.router.history.push(`/search/`);
140 this.context.router.history.push(
141 `/search/q/${searchParam}/type/All/sort/TopAll/page/1`
146 handleSearchSubmit(i: Navbar, event: any) {
147 event.preventDefault();
151 handleSearchBtn(i: Navbar, event: any) {
152 event.preventDefault();
153 i.setState({ toggleSearch: true });
155 i.searchTextField.current.focus();
156 const offsetWidth = i.searchTextField.current.offsetWidth;
157 if (i.state.searchParam && offsetWidth > 100) {
162 handleSearchBlur(i: Navbar, event: any) {
163 if (!(event.relatedTarget && event.relatedTarget.name !== 'search-btn')) {
164 i.state.toggleSearch = false;
170 return this.navbar();
173 componentWillUnmount() {
174 this.wsSub.unsubscribe();
175 this.userSub.unsubscribe();
176 this.unreadCountSub.unsubscribe();
179 // TODO class active corresponding to current page
181 let user = UserService.Instance.user;
183 <nav class="navbar navbar-expand-lg navbar-light shadow-sm p-0 px-3">
184 <div class="container">
185 {this.props.site.site && (
187 title={this.props.site.version}
188 className="d-flex align-items-center navbar-brand mr-md-3"
191 {this.props.site.site.icon && showAvatars() && (
193 src={pictrsAvatarThumbnail(this.props.site.site.icon)}
196 class="rounded-circle mr-2"
199 {this.props.site.site.name}
202 {this.state.isLoggedIn && (
204 className="ml-auto p-0 navbar-toggler nav-link border-0"
206 title={i18n.t('inbox')}
209 <use xlinkHref="#icon-bell"></use>
211 {this.state.unreadCount > 0 && (
212 <span class="mx-1 badge badge-light">
213 {this.state.unreadCount}
219 class="navbar-toggler border-0 p-1"
222 onClick={linkEvent(this, this.expandNavbar)}
223 data-tippy-content={i18n.t('expand_here')}
225 <span class="navbar-toggler-icon"></span>
228 className={`${!this.state.expanded && 'collapse'} navbar-collapse`}
230 <ul class="navbar-nav my-2 mr-auto">
231 <li class="nav-item">
235 title={i18n.t('communities')}
237 {i18n.t('communities')}
240 <li class="nav-item">
244 pathname: '/create_post',
245 state: { prevPath: this.currentLocation },
247 title={i18n.t('create_post')}
249 {i18n.t('create_post')}
252 <li class="nav-item">
255 to="/create_community"
256 title={i18n.t('create_community')}
258 {i18n.t('create_community')}
261 <li className="nav-item">
265 title={i18n.t('donate_to_lemmy')}
268 <use xlinkHref="#icon-coffee"></use>
273 <ul class="navbar-nav my-2">
275 <li className="nav-item">
279 title={i18n.t('admin_settings')}
282 <use xlinkHref="#icon-settings"></use>
288 {!this.context.router.history.location.pathname.match(
293 onSubmit={linkEvent(this, this.handleSearchSubmit)}
296 class={`form-control mr-0 search-input ${
297 this.state.toggleSearch ? 'show-input' : 'hide-input'
299 onInput={linkEvent(this, this.handleSearchParam)}
300 value={this.state.searchParam}
301 ref={this.searchTextField}
303 placeholder={i18n.t('search')}
304 onBlur={linkEvent(this, this.handleSearchBlur)}
308 onClick={linkEvent(this, this.handleSearchBtn)}
309 class="px-1 btn btn-link"
310 style="color: var(--gray)"
313 <use xlinkHref="#icon-search"></use>
318 {this.state.isLoggedIn ? (
320 <ul class="navbar-nav my-2">
321 <li className="nav-item">
325 title={i18n.t('inbox')}
328 <use xlinkHref="#icon-bell"></use>
330 {this.state.unreadCount > 0 && (
331 <span class="ml-1 badge badge-light">
332 {this.state.unreadCount}
338 <ul class="navbar-nav">
339 <li className="nav-item">
342 to={`/u/${user.name}`}
343 title={i18n.t('settings')}
346 {user.avatar && showAvatars() && (
348 src={pictrsAvatarThumbnail(user.avatar)}
351 class="rounded-circle mr-2"
354 {user.preferred_username
355 ? user.preferred_username
363 <ul class="navbar-nav my-2">
364 <li className="ml-2 nav-item">
366 className="btn btn-success"
368 title={i18n.t('login_sign_up')}
370 {i18n.t('login_sign_up')}
381 expandNavbar(i: Navbar) {
382 i.state.expanded = !i.state.expanded;
386 parseMessage(msg: WebSocketJsonResponse) {
387 let res = wsJsonToRes(msg);
389 if (msg.error == 'not_logged_in') {
390 UserService.Instance.logout();
394 } else if (msg.reconnect) {
395 WebSocketService.Instance.userJoin();
397 } else if (res.op == UserOperation.GetReplies) {
398 let data = res.data as GetRepliesResponse;
399 let unreadReplies = data.replies.filter(r => !r.read);
401 this.state.replies = unreadReplies;
402 this.state.unreadCount = this.calculateUnreadCount();
403 this.setState(this.state);
404 this.sendUnreadCount();
405 } else if (res.op == UserOperation.GetUserMentions) {
406 let data = res.data as GetUserMentionsResponse;
407 let unreadMentions = data.mentions.filter(r => !r.read);
409 this.state.mentions = unreadMentions;
410 this.state.unreadCount = this.calculateUnreadCount();
411 this.setState(this.state);
412 this.sendUnreadCount();
413 } else if (res.op == UserOperation.GetPrivateMessages) {
414 let data = res.data as PrivateMessagesResponse;
415 let unreadMessages = data.messages.filter(r => !r.read);
417 this.state.messages = unreadMessages;
418 this.state.unreadCount = this.calculateUnreadCount();
419 this.setState(this.state);
420 this.sendUnreadCount();
421 } else if (res.op == UserOperation.GetSite) {
422 // This is only called on a successful login
423 let data = res.data as GetSiteResponse;
424 UserService.Instance.user = data.my_user;
425 setTheme(UserService.Instance.user.theme);
426 i18n.changeLanguage(getLanguage());
427 this.state.isLoggedIn = true;
428 this.setState(this.state);
429 } else if (res.op == UserOperation.CreateComment) {
430 let data = res.data as CommentResponse;
432 if (this.state.isLoggedIn) {
433 if (data.recipient_ids.includes(UserService.Instance.user.id)) {
434 this.state.replies.push(data.comment);
435 this.state.unreadCount++;
436 this.setState(this.state);
437 this.sendUnreadCount();
438 notifyComment(data.comment, this.context.router);
441 } else if (res.op == UserOperation.CreatePrivateMessage) {
442 let data = res.data as PrivateMessageResponse;
444 if (this.state.isLoggedIn) {
445 if (data.message.recipient_id == UserService.Instance.user.id) {
446 this.state.messages.push(data.message);
447 this.state.unreadCount++;
448 this.setState(this.state);
449 this.sendUnreadCount();
450 notifyPrivateMessage(data.message, this.context.router);
457 console.log('Fetching unreads...');
458 let repliesForm: GetRepliesForm = {
465 let userMentionsForm: GetUserMentionsForm = {
472 let privateMessagesForm: GetPrivateMessagesForm = {
478 if (this.currentLocation !== '/inbox') {
479 WebSocketService.Instance.getReplies(repliesForm);
480 WebSocketService.Instance.getUserMentions(userMentionsForm);
481 WebSocketService.Instance.getPrivateMessages(privateMessagesForm);
485 get currentLocation() {
486 return this.context.router.history.location.pathname;
490 UserService.Instance.unreadCountSub.next(this.state.unreadCount);
493 calculateUnreadCount(): number {
495 this.state.replies.filter(r => !r.read).length +
496 this.state.mentions.filter(r => !r.read).length +
497 this.state.messages.filter(r => !r.read).length
501 get canAdmin(): boolean {
503 UserService.Instance.user &&
504 this.props.site.admins
506 .includes(UserService.Instance.user.id)
510 requestNotificationPermission() {
511 if (UserService.Instance.user) {
512 document.addEventListener('DOMContentLoaded', function () {
514 toast(i18n.t('notifications_error'), 'danger');
518 if (Notification.permission !== 'granted')
519 Notification.requestPermission();