]> Untitled Git - lemmy.git/blob - ui/src/components/navbar.tsx
Merge branch 'master' into arrudaricardo-issue-#814
[lemmy.git] / ui / src / components / navbar.tsx
1 import { Component, linkEvent, createRef, RefObject } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
5 import { WebSocketService, UserService } from '../services';
6 import {
7   UserOperation,
8   GetRepliesForm,
9   GetRepliesResponse,
10   GetUserMentionsForm,
11   GetUserMentionsResponse,
12   GetPrivateMessagesForm,
13   PrivateMessagesResponse,
14   SortType,
15   SearchType,
16   GetSiteResponse,
17   Comment,
18   CommentResponse,
19   PrivateMessage,
20   UserView,
21   PrivateMessageResponse,
22   WebSocketJsonResponse,
23   SearchForm,
24 } from '../interfaces';
25 import {
26   wsJsonToRes,
27   pictrsAvatarThumbnail,
28   showAvatars,
29   fetchLimit,
30   isCommentType,
31   toast,
32   messageToastify,
33   md,
34 } from '../utils';
35 import { version } from '../version';
36 import { i18n } from '../i18next';
37
38 interface NavbarState {
39   isLoggedIn: boolean;
40   expanded: boolean;
41   replies: Array<Comment>;
42   mentions: Array<Comment>;
43   messages: Array<PrivateMessage>;
44   unreadCount: number;
45   siteName: string;
46   admins: Array<UserView>;
47   searchParam: string;
48   toggleSearch: boolean;
49 }
50
51 export class Navbar extends Component<any, NavbarState> {
52   private wsSub: Subscription;
53   private userSub: Subscription;
54   private searchTextField: RefObject<HTMLInputElement>;
55   emptyState: NavbarState = {
56     isLoggedIn: UserService.Instance.user !== undefined,
57     unreadCount: 0,
58     replies: [],
59     mentions: [],
60     messages: [],
61     expanded: false,
62     siteName: undefined,
63     admins: [],
64     searchParam: '',
65     toggleSearch: false,
66   };
67
68   constructor(props: any, context: any) {
69     super(props, context);
70     this.state = this.emptyState;
71
72     // Subscribe to user changes
73     this.userSub = UserService.Instance.sub.subscribe(user => {
74       this.state.isLoggedIn = user.user !== undefined;
75       if (this.state.isLoggedIn) {
76         this.state.unreadCount = user.user.unreadCount;
77         this.requestNotificationPermission();
78       }
79       this.setState(this.state);
80     });
81
82     this.wsSub = WebSocketService.Instance.subject
83       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
84       .subscribe(
85         msg => this.parseMessage(msg),
86         err => console.error(err),
87         () => console.log('complete')
88       );
89
90     if (this.state.isLoggedIn) {
91       this.requestNotificationPermission();
92       // TODO couldn't get re-logging in to re-fetch unreads
93       this.fetchUnreads();
94     }
95
96     WebSocketService.Instance.getSite();
97
98     this.searchTextField = createRef();
99   }
100
101   handleSearchParam(i: Navbar, event: any) {
102     i.state.searchParam = event.target.value;
103     i.setState(i.state);
104   }
105
106   updateUrl() {
107     const searchParam = this.state.searchParam;
108     this.setState({ searchParam: '' });
109     this.setState({ toggleSearch: false });
110     if (searchParam === '') {
111       this.context.router.history.push(`/search/`);
112     } else {
113       this.context.router.history.push(
114         `/search/q/${searchParam}/type/all/sort/topall/page/1`
115       );
116     }
117   }
118
119   handleSearchSubmit(i: Navbar, event: any) {
120     event.preventDefault();
121     i.updateUrl();
122   }
123
124   handleSearchBtn(i: Navbar, event: any) {
125     event.preventDefault();
126     i.setState({ toggleSearch: true });
127
128     i.searchTextField.current.focus();
129     const offsetWidth = i.searchTextField.current.offsetWidth;
130     if (i.state.searchParam && offsetWidth > 100) {
131       i.updateUrl();
132     }
133   }
134
135   handleSearchBlur(i: Navbar, event: any) {
136     if (!(event.relatedTarget && event.relatedTarget.name !== 'search-btn')) {
137       i.state.toggleSearch = false;
138       i.setState(i.state);
139     }
140   }
141
142   render() {
143     return this.navbar();
144   }
145
146   componentWillUnmount() {
147     this.wsSub.unsubscribe();
148     this.userSub.unsubscribe();
149   }
150
151   // TODO class active corresponding to current page
152   navbar() {
153     return (
154       <nav class="container-fluid navbar navbar-expand-md navbar-light shadow p-0 px-3">
155         <Link title={version} class="navbar-brand" to="/">
156           {this.state.siteName}
157         </Link>
158         {this.state.isLoggedIn && (
159           <Link
160             class="ml-auto p-0 navbar-toggler nav-link"
161             to="/inbox"
162             title={i18n.t('inbox')}
163           >
164             <svg class="icon">
165               <use xlinkHref="#icon-bell"></use>
166             </svg>
167             {this.state.unreadCount > 0 && (
168               <span class="ml-1 badge badge-light">
169                 {this.state.unreadCount}
170               </span>
171             )}
172           </Link>
173         )}
174         <button
175           class="navbar-toggler"
176           type="button"
177           aria-label="menu"
178           onClick={linkEvent(this, this.expandNavbar)}
179           data-tippy-content={i18n.t('expand_here')}
180         >
181           <span class="navbar-toggler-icon"></span>
182         </button>
183         <div
184           className={`${!this.state.expanded && 'collapse'} navbar-collapse`}
185         >
186           <ul class="navbar-nav mr-auto">
187             <li class="nav-item">
188               <Link
189                 class="nav-link"
190                 to="/communities"
191                 title={i18n.t('communities')}
192               >
193                 {i18n.t('communities')}
194               </Link>
195             </li>
196             <li class="nav-item">
197               <Link
198                 class="nav-link"
199                 to={{
200                   pathname: '/create_post',
201                   state: { prevPath: this.currentLocation },
202                 }}
203                 title={i18n.t('create_post')}
204               >
205                 {i18n.t('create_post')}
206               </Link>
207             </li>
208             <li class="nav-item">
209               <Link
210                 class="nav-link"
211                 to="/create_community"
212                 title={i18n.t('create_community')}
213               >
214                 {i18n.t('create_community')}
215               </Link>
216             </li>
217             <li className="nav-item">
218               <Link
219                 class="nav-link"
220                 to="/sponsors"
221                 title={i18n.t('donate_to_lemmy')}
222               >
223                 <svg class="icon">
224                   <use xlinkHref="#icon-coffee"></use>
225                 </svg>
226               </Link>
227             </li>
228           </ul>
229           {!this.context.router.history.location.pathname.match(
230             /^\/search/
231           ) && (
232             <div class="nav-item my-2">
233               <form
234                 class="form-inline"
235                 onSubmit={linkEvent(this, this.handleSearchSubmit)}
236               >
237                 <input
238                   class={`form-control mr-0 search-input ${
239                     this.state.toggleSearch ? 'show-input' : 'hide-input'
240                   }`}
241                   onInput={linkEvent(this, this.handleSearchParam)}
242                   value={this.state.searchParam}
243                   ref={this.searchTextField}
244                   type="text"
245                   placeholder={i18n.t('search')}
246                   onBlur={linkEvent(this, this.handleSearchBlur)}
247                 ></input>
248                 <div class="mx-sm-2">
249                   <button
250                     name="search-btn"
251                     onClick={linkEvent(this, this.handleSearchBtn)}
252                     class="btn btn-secondary"
253                   >
254                     {i18n.t('search')}
255                   </button>
256                 </div>
257               </form>
258             </div>
259           )}
260           <ul class="navbar-nav ml-2">
261             {this.canAdmin && (
262               <li className="nav-item mt-1">
263                 <Link
264                   class="nav-link"
265                   to={`/admin`}
266                   title={i18n.t('admin_settings')}
267                 >
268                   <svg class="icon">
269                     <use xlinkHref="#icon-settings"></use>
270                   </svg>
271                 </Link>
272               </li>
273             )}
274             {this.state.isLoggedIn ? (
275               <>
276                 <li className="nav-item mt-1">
277                   <Link class="nav-link" to="/inbox" title={i18n.t('inbox')}>
278                     <svg class="icon">
279                       <use xlinkHref="#icon-bell"></use>
280                     </svg>
281                     {this.state.unreadCount > 0 && (
282                       <span class="ml-1 badge badge-light">
283                         {this.state.unreadCount}
284                       </span>
285                     )}
286                   </Link>
287                 </li>
288                 <li className="nav-item">
289                   <Link
290                     class="nav-link"
291                     to={`/u/${UserService.Instance.user.username}`}
292                     title={i18n.t('settings')}
293                   >
294                     <span>
295                       {UserService.Instance.user.avatar && showAvatars() && (
296                         <img
297                           src={pictrsAvatarThumbnail(
298                             UserService.Instance.user.avatar
299                           )}
300                           height="32"
301                           width="32"
302                           class="rounded-circle mr-2"
303                         />
304                       )}
305                       {UserService.Instance.user.username}
306                     </span>
307                   </Link>
308                 </li>
309               </>
310             ) : (
311               <Link
312                 class="nav-link"
313                 to="/login"
314                 title={i18n.t('login_sign_up')}
315               >
316                 {i18n.t('login_sign_up')}
317               </Link>
318             )}
319           </ul>
320         </div>
321       </nav>
322     );
323   }
324
325   expandNavbar(i: Navbar) {
326     i.state.expanded = !i.state.expanded;
327     i.setState(i.state);
328   }
329
330   parseMessage(msg: WebSocketJsonResponse) {
331     let res = wsJsonToRes(msg);
332     if (msg.error) {
333       if (msg.error == 'not_logged_in') {
334         UserService.Instance.logout();
335         location.reload();
336       }
337       return;
338     } else if (msg.reconnect) {
339       this.fetchUnreads();
340     } else if (res.op == UserOperation.GetReplies) {
341       let data = res.data as GetRepliesResponse;
342       let unreadReplies = data.replies.filter(r => !r.read);
343
344       this.state.replies = unreadReplies;
345       this.state.unreadCount = this.calculateUnreadCount();
346       this.setState(this.state);
347       this.sendUnreadCount();
348     } else if (res.op == UserOperation.GetUserMentions) {
349       let data = res.data as GetUserMentionsResponse;
350       let unreadMentions = data.mentions.filter(r => !r.read);
351
352       this.state.mentions = unreadMentions;
353       this.state.unreadCount = this.calculateUnreadCount();
354       this.setState(this.state);
355       this.sendUnreadCount();
356     } else if (res.op == UserOperation.GetPrivateMessages) {
357       let data = res.data as PrivateMessagesResponse;
358       let unreadMessages = data.messages.filter(r => !r.read);
359
360       this.state.messages = unreadMessages;
361       this.state.unreadCount = this.calculateUnreadCount();
362       this.setState(this.state);
363       this.sendUnreadCount();
364     } else if (res.op == UserOperation.CreateComment) {
365       let data = res.data as CommentResponse;
366
367       if (this.state.isLoggedIn) {
368         if (data.recipient_ids.includes(UserService.Instance.user.id)) {
369           this.state.replies.push(data.comment);
370           this.state.unreadCount++;
371           this.setState(this.state);
372           this.sendUnreadCount();
373           this.notify(data.comment);
374         }
375       }
376     } else if (res.op == UserOperation.CreatePrivateMessage) {
377       let data = res.data as PrivateMessageResponse;
378
379       if (this.state.isLoggedIn) {
380         if (data.message.recipient_id == UserService.Instance.user.id) {
381           this.state.messages.push(data.message);
382           this.state.unreadCount++;
383           this.setState(this.state);
384           this.sendUnreadCount();
385           this.notify(data.message);
386         }
387       }
388     } else if (res.op == UserOperation.GetSite) {
389       let data = res.data as GetSiteResponse;
390
391       if (data.site && !this.state.siteName) {
392         this.state.siteName = data.site.name;
393         this.state.admins = data.admins;
394         WebSocketService.Instance.site = data.site;
395         WebSocketService.Instance.admins = data.admins;
396
397         this.setState(this.state);
398       }
399     }
400   }
401
402   fetchUnreads() {
403     if (this.state.isLoggedIn) {
404       let repliesForm: GetRepliesForm = {
405         sort: SortType[SortType.New],
406         unread_only: true,
407         page: 1,
408         limit: fetchLimit,
409       };
410
411       let userMentionsForm: GetUserMentionsForm = {
412         sort: SortType[SortType.New],
413         unread_only: true,
414         page: 1,
415         limit: fetchLimit,
416       };
417
418       let privateMessagesForm: GetPrivateMessagesForm = {
419         unread_only: true,
420         page: 1,
421         limit: fetchLimit,
422       };
423
424       if (this.currentLocation !== '/inbox') {
425         WebSocketService.Instance.getReplies(repliesForm);
426         WebSocketService.Instance.getUserMentions(userMentionsForm);
427         WebSocketService.Instance.getPrivateMessages(privateMessagesForm);
428       }
429     }
430   }
431
432   get currentLocation() {
433     return this.context.router.history.location.pathname;
434   }
435
436   sendUnreadCount() {
437     UserService.Instance.user.unreadCount = this.state.unreadCount;
438     UserService.Instance.sub.next({
439       user: UserService.Instance.user,
440     });
441   }
442
443   calculateUnreadCount(): number {
444     return (
445       this.state.replies.filter(r => !r.read).length +
446       this.state.mentions.filter(r => !r.read).length +
447       this.state.messages.filter(r => !r.read).length
448     );
449   }
450
451   get canAdmin(): boolean {
452     return (
453       UserService.Instance.user &&
454       this.state.admins.map(a => a.id).includes(UserService.Instance.user.id)
455     );
456   }
457
458   requestNotificationPermission() {
459     if (UserService.Instance.user) {
460       document.addEventListener('DOMContentLoaded', function () {
461         if (!Notification) {
462           toast(i18n.t('notifications_error'), 'danger');
463           return;
464         }
465
466         if (Notification.permission !== 'granted')
467           Notification.requestPermission();
468       });
469     }
470   }
471
472   notify(reply: Comment | PrivateMessage) {
473     let creator_name = reply.creator_name;
474     let creator_avatar = reply.creator_avatar
475       ? reply.creator_avatar
476       : `${window.location.protocol}//${window.location.host}/static/assets/apple-touch-icon.png`;
477     let link = isCommentType(reply)
478       ? `/post/${reply.post_id}/comment/${reply.id}`
479       : `/inbox`;
480     let htmlBody = md.render(reply.content);
481     let body = reply.content; // Unfortunately the notifications API can't do html
482
483     messageToastify(
484       creator_name,
485       creator_avatar,
486       htmlBody,
487       link,
488       this.context.router
489     );
490
491     if (Notification.permission !== 'granted') Notification.requestPermission();
492     else {
493       var notification = new Notification(reply.creator_name, {
494         icon: creator_avatar,
495         body: body,
496       });
497
498       notification.onclick = () => {
499         event.preventDefault();
500         this.context.router.history.push(link);
501       };
502     }
503   }
504 }