]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/sidebar.tsx
Revert "Fix lint"
[lemmy-ui.git] / src / shared / components / community / sidebar.tsx
1 import { Component, linkEvent } from "inferno";
2 import { Link } from "inferno-router";
3 import {
4   AddModToCommunity,
5   CommunityModeratorView,
6   CommunityView,
7   DeleteCommunity,
8   FollowCommunity,
9   PersonViewSafe,
10   RemoveCommunity,
11 } from "lemmy-js-client";
12 import { i18n } from "../../i18next";
13 import { UserService, WebSocketService } from "../../services";
14 import {
15   authField,
16   getUnixTime,
17   mdToHtml,
18   numToSI,
19   wsClient,
20 } from "../../utils";
21 import { BannerIconHeader } from "../common/banner-icon-header";
22 import { Icon } from "../common/icon";
23 import { CommunityForm } from "../community/community-form";
24 import { CommunityLink } from "../community/community-link";
25 import { PersonListing } from "../person/person-listing";
26
27 interface SidebarProps {
28   community_view: CommunityView;
29   moderators: CommunityModeratorView[];
30   admins: PersonViewSafe[];
31   online: number;
32   enableNsfw: boolean;
33   showIcon?: boolean;
34 }
35
36 interface SidebarState {
37   showEdit: boolean;
38   showRemoveDialog: boolean;
39   removeReason: string;
40   removeExpires: string;
41   showConfirmLeaveModTeam: boolean;
42 }
43
44 export class Sidebar extends Component<SidebarProps, SidebarState> {
45   private emptyState: SidebarState = {
46     showEdit: false,
47     showRemoveDialog: false,
48     removeReason: null,
49     removeExpires: null,
50     showConfirmLeaveModTeam: false,
51   };
52
53   constructor(props: any, context: any) {
54     super(props, context);
55     this.state = this.emptyState;
56     this.handleEditCommunity = this.handleEditCommunity.bind(this);
57     this.handleEditCancel = this.handleEditCancel.bind(this);
58   }
59
60   render() {
61     return (
62       <div>
63         {!this.state.showEdit ? (
64           this.sidebar()
65         ) : (
66           <CommunityForm
67             community_view={this.props.community_view}
68             onEdit={this.handleEditCommunity}
69             onCancel={this.handleEditCancel}
70             enableNsfw={this.props.enableNsfw}
71           />
72         )}
73       </div>
74     );
75   }
76
77   sidebar() {
78     return (
79       <div>
80         <div class="card border-secondary mb-3">
81           <div class="card-body">
82             {this.communityTitle()}
83             {this.adminButtons()}
84             {this.subscribe()}
85             {this.canPost && this.createPost()}
86           </div>
87         </div>
88         <div class="card border-secondary mb-3">
89           <div class="card-body">
90             {this.description()}
91             {this.badges()}
92             {this.mods()}
93           </div>
94         </div>
95       </div>
96     );
97   }
98
99   communityTitle() {
100     let community = this.props.community_view.community;
101     let subscribed = this.props.community_view.subscribed;
102     return (
103       <div>
104         <h5 className="mb-0">
105           {this.props.showIcon && (
106             <BannerIconHeader icon={community.icon} banner={community.banner} />
107           )}
108           <span class="mr-2">{community.title}</span>
109           {subscribed && (
110             <a
111               class="btn btn-secondary btn-sm mr-2"
112               href="#"
113               onClick={linkEvent(this, this.handleUnsubscribe)}
114             >
115               <Icon icon="check" classes="icon-inline text-success mr-1" />
116               {i18n.t("joined")}
117             </a>
118           )}
119           {community.removed && (
120             <small className="mr-2 text-muted font-italic">
121               {i18n.t("removed")}
122             </small>
123           )}
124           {community.deleted && (
125             <small className="mr-2 text-muted font-italic">
126               {i18n.t("deleted")}
127             </small>
128           )}
129           {community.nsfw && (
130             <small className="mr-2 text-muted font-italic">
131               {i18n.t("nsfw")}
132             </small>
133           )}
134         </h5>
135         <CommunityLink
136           community={community}
137           realLink
138           useApubName
139           muted
140           hideAvatar
141         />
142       </div>
143     );
144   }
145
146   badges() {
147     let community_view = this.props.community_view;
148     let counts = community_view.counts;
149     return (
150       <ul class="my-1 list-inline">
151         <li className="list-inline-item badge badge-secondary">
152           {i18n.t("number_online", {
153             count: this.props.online,
154             formattedCount: numToSI(this.props.online),
155           })}
156         </li>
157         <li
158           className="list-inline-item badge badge-secondary pointer"
159           data-tippy-content={i18n.t("active_users_in_the_last_day", {
160             count: counts.users_active_day,
161             formattedCount: counts.users_active_day,
162           })}
163         >
164           {i18n.t("number_of_users", {
165             count: counts.users_active_day,
166             formattedCount: numToSI(counts.users_active_day),
167           })}{" "}
168           / {i18n.t("day")}
169         </li>
170         <li
171           className="list-inline-item badge badge-secondary pointer"
172           data-tippy-content={i18n.t("active_users_in_the_last_week", {
173             count: counts.users_active_week,
174             formattedCount: counts.users_active_week,
175           })}
176         >
177           {i18n.t("number_of_users", {
178             count: counts.users_active_week,
179             formattedCount: numToSI(counts.users_active_week),
180           })}{" "}
181           / {i18n.t("week")}
182         </li>
183         <li
184           className="list-inline-item badge badge-secondary pointer"
185           data-tippy-content={i18n.t("active_users_in_the_last_month", {
186             count: counts.users_active_month,
187             formattedCount: counts.users_active_month,
188           })}
189         >
190           {i18n.t("number_of_users", {
191             count: counts.users_active_month,
192             formattedCount: numToSI(counts.users_active_month),
193           })}{" "}
194           / {i18n.t("month")}
195         </li>
196         <li
197           className="list-inline-item badge badge-secondary pointer"
198           data-tippy-content={i18n.t("active_users_in_the_last_six_months", {
199             count: counts.users_active_half_year,
200             formattedCount: counts.users_active_half_year,
201           })}
202         >
203           {i18n.t("number_of_users", {
204             count: counts.users_active_half_year,
205             formattedCount: numToSI(counts.users_active_half_year),
206           })}{" "}
207           / {i18n.t("number_of_months", { count: 6, formattedCount: 6 })}
208         </li>
209         <li className="list-inline-item badge badge-secondary">
210           {i18n.t("number_of_subscribers", {
211             count: counts.subscribers,
212             formattedCount: numToSI(counts.subscribers),
213           })}
214         </li>
215         <li className="list-inline-item badge badge-secondary">
216           {i18n.t("number_of_posts", {
217             count: counts.posts,
218             formattedCount: numToSI(counts.posts),
219           })}
220         </li>
221         <li className="list-inline-item badge badge-secondary">
222           {i18n.t("number_of_comments", {
223             count: counts.comments,
224             formattedCount: numToSI(counts.comments),
225           })}
226         </li>
227         <li className="list-inline-item">
228           <Link
229             className="badge badge-primary"
230             to={`/modlog/community/${this.props.community_view.community.id}`}
231           >
232             {i18n.t("modlog")}
233           </Link>
234         </li>
235       </ul>
236     );
237   }
238
239   mods() {
240     return (
241       <ul class="list-inline small">
242         <li class="list-inline-item">{i18n.t("mods")}: </li>
243         {this.props.moderators.map(mod => (
244           <li class="list-inline-item">
245             <PersonListing person={mod.moderator} />
246           </li>
247         ))}
248       </ul>
249     );
250   }
251
252   createPost() {
253     let cv = this.props.community_view;
254     return (
255       cv.subscribed && (
256         <Link
257           className={`btn btn-secondary btn-block mb-2 ${
258             cv.community.deleted || cv.community.removed ? "no-click" : ""
259           }`}
260           to={`/create_post?community_id=${cv.community.id}`}
261         >
262           {i18n.t("create_a_post")}
263         </Link>
264       )
265     );
266   }
267
268   subscribe() {
269     let community_view = this.props.community_view;
270     return (
271       <div class="mb-2">
272         {!community_view.subscribed && (
273           <a
274             class="btn btn-secondary btn-block"
275             href="#"
276             onClick={linkEvent(this, this.handleSubscribe)}
277           >
278             {i18n.t("subscribe")}
279           </a>
280         )}
281       </div>
282     );
283   }
284
285   description() {
286     let description = this.props.community_view.community.description;
287     return (
288       description && (
289         <div
290           className="md-div"
291           dangerouslySetInnerHTML={mdToHtml(description)}
292         />
293       )
294     );
295   }
296
297   adminButtons() {
298     let community_view = this.props.community_view;
299     return (
300       <>
301         <ul class="list-inline mb-1 text-muted font-weight-bold">
302           {this.canMod && (
303             <>
304               <li className="list-inline-item-action">
305                 <button
306                   class="btn btn-link text-muted d-inline-block"
307                   onClick={linkEvent(this, this.handleEditClick)}
308                   data-tippy-content={i18n.t("edit")}
309                   aria-label={i18n.t("edit")}
310                 >
311                   <Icon icon="edit" classes="icon-inline" />
312                 </button>
313               </li>
314               {!this.amTopMod &&
315                 (!this.state.showConfirmLeaveModTeam ? (
316                   <li className="list-inline-item-action">
317                     <button
318                       class="btn btn-link text-muted d-inline-block"
319                       onClick={linkEvent(
320                         this,
321                         this.handleShowConfirmLeaveModTeamClick
322                       )}
323                     >
324                       {i18n.t("leave_mod_team")}
325                     </button>
326                   </li>
327                 ) : (
328                   <>
329                     <li className="list-inline-item-action">
330                       {i18n.t("are_you_sure")}
331                     </li>
332                     <li className="list-inline-item-action">
333                       <button
334                         class="btn btn-link text-muted d-inline-block"
335                         onClick={linkEvent(this, this.handleLeaveModTeamClick)}
336                       >
337                         {i18n.t("yes")}
338                       </button>
339                     </li>
340                     <li className="list-inline-item-action">
341                       <button
342                         class="btn btn-link text-muted d-inline-block"
343                         onClick={linkEvent(
344                           this,
345                           this.handleCancelLeaveModTeamClick
346                         )}
347                       >
348                         {i18n.t("no")}
349                       </button>
350                     </li>
351                   </>
352                 ))}
353               {this.amTopMod && (
354                 <li className="list-inline-item-action">
355                   <button
356                     class="btn btn-link text-muted d-inline-block"
357                     onClick={linkEvent(this, this.handleDeleteClick)}
358                     data-tippy-content={
359                       !community_view.community.deleted
360                         ? i18n.t("delete")
361                         : i18n.t("restore")
362                     }
363                     aria-label={
364                       !community_view.community.deleted
365                         ? i18n.t("delete")
366                         : i18n.t("restore")
367                     }
368                   >
369                     <Icon
370                       icon="trash"
371                       classes={`icon-inline ${
372                         community_view.community.deleted && "text-danger"
373                       }`}
374                     />
375                   </button>
376                 </li>
377               )}
378             </>
379           )}
380           {this.canAdmin && (
381             <li className="list-inline-item">
382               {!this.props.community_view.community.removed ? (
383                 <button
384                   class="btn btn-link text-muted d-inline-block"
385                   onClick={linkEvent(this, this.handleModRemoveShow)}
386                 >
387                   {i18n.t("remove")}
388                 </button>
389               ) : (
390                 <button
391                   class="btn btn-link text-muted d-inline-block"
392                   onClick={linkEvent(this, this.handleModRemoveSubmit)}
393                 >
394                   {i18n.t("restore")}
395                 </button>
396               )}
397             </li>
398           )}
399         </ul>
400         {this.state.showRemoveDialog && (
401           <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
402             <div class="form-group row">
403               <label class="col-form-label" htmlFor="remove-reason">
404                 {i18n.t("reason")}
405               </label>
406               <input
407                 type="text"
408                 id="remove-reason"
409                 class="form-control mr-2"
410                 placeholder={i18n.t("optional")}
411                 value={this.state.removeReason}
412                 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
413               />
414             </div>
415             {/* TODO hold off on expires for now */}
416             {/* <div class="form-group row"> */}
417             {/*   <label class="col-form-label">Expires</label> */}
418             {/*   <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
419             {/* </div> */}
420             <div class="form-group row">
421               <button type="submit" class="btn btn-secondary">
422                 {i18n.t("remove_community")}
423               </button>
424             </div>
425           </form>
426         )}
427       </>
428     );
429   }
430
431   handleEditClick(i: Sidebar) {
432     i.state.showEdit = true;
433     i.setState(i.state);
434   }
435
436   handleEditCommunity() {
437     this.state.showEdit = false;
438     this.setState(this.state);
439   }
440
441   handleEditCancel() {
442     this.state.showEdit = false;
443     this.setState(this.state);
444   }
445
446   handleDeleteClick(i: Sidebar, event: any) {
447     event.preventDefault();
448     let deleteForm: DeleteCommunity = {
449       community_id: i.props.community_view.community.id,
450       deleted: !i.props.community_view.community.deleted,
451       auth: authField(),
452     };
453     WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
454   }
455
456   handleShowConfirmLeaveModTeamClick(i: Sidebar) {
457     i.state.showConfirmLeaveModTeam = true;
458     i.setState(i.state);
459   }
460
461   handleLeaveModTeamClick(i: Sidebar) {
462     let form: AddModToCommunity = {
463       person_id: UserService.Instance.myUserInfo.local_user_view.person.id,
464       community_id: i.props.community_view.community.id,
465       added: false,
466       auth: authField(),
467     };
468     WebSocketService.Instance.send(wsClient.addModToCommunity(form));
469     i.state.showConfirmLeaveModTeam = false;
470     i.setState(i.state);
471   }
472
473   handleCancelLeaveModTeamClick(i: Sidebar) {
474     i.state.showConfirmLeaveModTeam = false;
475     i.setState(i.state);
476   }
477
478   handleUnsubscribe(i: Sidebar, event: any) {
479     event.preventDefault();
480     let community_id = i.props.community_view.community.id;
481     let form: FollowCommunity = {
482       community_id,
483       follow: false,
484       auth: authField(),
485     };
486     WebSocketService.Instance.send(wsClient.followCommunity(form));
487
488     // Update myUserInfo
489     UserService.Instance.myUserInfo.follows =
490       UserService.Instance.myUserInfo.follows.filter(
491         i => i.community.id != community_id
492       );
493   }
494
495   handleSubscribe(i: Sidebar, event: any) {
496     event.preventDefault();
497     let community_id = i.props.community_view.community.id;
498     let form: FollowCommunity = {
499       community_id,
500       follow: true,
501       auth: authField(),
502     };
503     WebSocketService.Instance.send(wsClient.followCommunity(form));
504
505     // Update myUserInfo
506     UserService.Instance.myUserInfo.follows.push({
507       community: i.props.community_view.community,
508       follower: UserService.Instance.myUserInfo.local_user_view.person,
509     });
510   }
511
512   private get amTopMod(): boolean {
513     return (
514       this.props.moderators[0].moderator.id ==
515       UserService.Instance.myUserInfo.local_user_view.person.id
516     );
517   }
518
519   get canMod(): boolean {
520     return (
521       UserService.Instance.myUserInfo &&
522       this.props.moderators
523         .map(m => m.moderator.id)
524         .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
525     );
526   }
527
528   get canAdmin(): boolean {
529     return (
530       UserService.Instance.myUserInfo &&
531       this.props.admins
532         .map(a => a.person.id)
533         .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
534     );
535   }
536
537   get canPost(): boolean {
538     return (
539       !this.props.community_view.community.posting_restricted_to_mods ||
540       this.canMod ||
541       this.canAdmin
542     );
543   }
544
545   handleModRemoveShow(i: Sidebar) {
546     i.state.showRemoveDialog = true;
547     i.setState(i.state);
548   }
549
550   handleModRemoveReasonChange(i: Sidebar, event: any) {
551     i.state.removeReason = event.target.value;
552     i.setState(i.state);
553   }
554
555   handleModRemoveExpiresChange(i: Sidebar, event: any) {
556     console.log(event.target.value);
557     i.state.removeExpires = event.target.value;
558     i.setState(i.state);
559   }
560
561   handleModRemoveSubmit(i: Sidebar, event: any) {
562     event.preventDefault();
563     let removeForm: RemoveCommunity = {
564       community_id: i.props.community_view.community.id,
565       removed: !i.props.community_view.community.removed,
566       reason: i.state.removeReason,
567       expires: getUnixTime(i.state.removeExpires),
568       auth: authField(),
569     };
570     WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
571
572     i.state.showRemoveDialog = false;
573     i.setState(i.state);
574   }
575 }