]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/sidebar.tsx
Remove active in the last (#419)
[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.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-secondary"
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 community_view = this.props.community_view;
254     return (
255       community_view.subscribed && (
256         <Link
257           className={`btn btn-secondary btn-block mb-2 ${
258             community_view.community.deleted || community_view.community.removed
259               ? "no-click"
260               : ""
261           }`}
262           to={`/create_post?community_id=${community_view.community.id}`}
263         >
264           {i18n.t("create_a_post")}
265         </Link>
266       )
267     );
268   }
269
270   subscribe() {
271     let community_view = this.props.community_view;
272     return (
273       <div class="mb-2">
274         {!community_view.subscribed && (
275           <a
276             class="btn btn-secondary btn-block"
277             href="#"
278             onClick={linkEvent(this, this.handleSubscribe)}
279           >
280             {i18n.t("subscribe")}
281           </a>
282         )}
283       </div>
284     );
285   }
286
287   description() {
288     let description = this.props.community_view.community.description;
289     return (
290       description && (
291         <div
292           className="md-div"
293           dangerouslySetInnerHTML={mdToHtml(description)}
294         />
295       )
296     );
297   }
298
299   adminButtons() {
300     let community_view = this.props.community_view;
301     return (
302       <>
303         <ul class="list-inline mb-1 text-muted font-weight-bold">
304           {this.canMod && (
305             <>
306               <li className="list-inline-item-action">
307                 <button
308                   class="btn btn-link text-muted d-inline-block"
309                   onClick={linkEvent(this, this.handleEditClick)}
310                   data-tippy-content={i18n.t("edit")}
311                   aria-label={i18n.t("edit")}
312                 >
313                   <Icon icon="edit" classes="icon-inline" />
314                 </button>
315               </li>
316               {!this.amTopMod &&
317                 (!this.state.showConfirmLeaveModTeam ? (
318                   <li className="list-inline-item-action">
319                     <button
320                       class="btn btn-link text-muted d-inline-block"
321                       onClick={linkEvent(
322                         this,
323                         this.handleShowConfirmLeaveModTeamClick
324                       )}
325                     >
326                       {i18n.t("leave_mod_team")}
327                     </button>
328                   </li>
329                 ) : (
330                   <>
331                     <li className="list-inline-item-action">
332                       {i18n.t("are_you_sure")}
333                     </li>
334                     <li className="list-inline-item-action">
335                       <button
336                         class="btn btn-link text-muted d-inline-block"
337                         onClick={linkEvent(this, this.handleLeaveModTeamClick)}
338                       >
339                         {i18n.t("yes")}
340                       </button>
341                     </li>
342                     <li className="list-inline-item-action">
343                       <button
344                         class="btn btn-link text-muted d-inline-block"
345                         onClick={linkEvent(
346                           this,
347                           this.handleCancelLeaveModTeamClick
348                         )}
349                       >
350                         {i18n.t("no")}
351                       </button>
352                     </li>
353                   </>
354                 ))}
355               {this.amTopMod && (
356                 <li className="list-inline-item-action">
357                   <button
358                     class="btn btn-link text-muted d-inline-block"
359                     onClick={linkEvent(this, this.handleDeleteClick)}
360                     data-tippy-content={
361                       !community_view.community.deleted
362                         ? i18n.t("delete")
363                         : i18n.t("restore")
364                     }
365                     aria-label={
366                       !community_view.community.deleted
367                         ? i18n.t("delete")
368                         : i18n.t("restore")
369                     }
370                   >
371                     <Icon
372                       icon="trash"
373                       classes={`icon-inline ${
374                         community_view.community.deleted && "text-danger"
375                       }`}
376                     />
377                   </button>
378                 </li>
379               )}
380             </>
381           )}
382           {this.canAdmin && (
383             <li className="list-inline-item">
384               {!this.props.community_view.community.removed ? (
385                 <button
386                   class="btn btn-link text-muted d-inline-block"
387                   onClick={linkEvent(this, this.handleModRemoveShow)}
388                 >
389                   {i18n.t("remove")}
390                 </button>
391               ) : (
392                 <button
393                   class="btn btn-link text-muted d-inline-block"
394                   onClick={linkEvent(this, this.handleModRemoveSubmit)}
395                 >
396                   {i18n.t("restore")}
397                 </button>
398               )}
399             </li>
400           )}
401         </ul>
402         {this.state.showRemoveDialog && (
403           <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
404             <div class="form-group row">
405               <label class="col-form-label" htmlFor="remove-reason">
406                 {i18n.t("reason")}
407               </label>
408               <input
409                 type="text"
410                 id="remove-reason"
411                 class="form-control mr-2"
412                 placeholder={i18n.t("optional")}
413                 value={this.state.removeReason}
414                 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
415               />
416             </div>
417             {/* TODO hold off on expires for now */}
418             {/* <div class="form-group row"> */}
419             {/*   <label class="col-form-label">Expires</label> */}
420             {/*   <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
421             {/* </div> */}
422             <div class="form-group row">
423               <button type="submit" class="btn btn-secondary">
424                 {i18n.t("remove_community")}
425               </button>
426             </div>
427           </form>
428         )}
429       </>
430     );
431   }
432
433   handleEditClick(i: Sidebar) {
434     i.state.showEdit = true;
435     i.setState(i.state);
436   }
437
438   handleEditCommunity() {
439     this.state.showEdit = false;
440     this.setState(this.state);
441   }
442
443   handleEditCancel() {
444     this.state.showEdit = false;
445     this.setState(this.state);
446   }
447
448   handleDeleteClick(i: Sidebar, event: any) {
449     event.preventDefault();
450     let deleteForm: DeleteCommunity = {
451       community_id: i.props.community_view.community.id,
452       deleted: !i.props.community_view.community.deleted,
453       auth: authField(),
454     };
455     WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
456   }
457
458   handleShowConfirmLeaveModTeamClick(i: Sidebar) {
459     i.state.showConfirmLeaveModTeam = true;
460     i.setState(i.state);
461   }
462
463   handleLeaveModTeamClick(i: Sidebar) {
464     let form: AddModToCommunity = {
465       person_id: UserService.Instance.myUserInfo.local_user_view.person.id,
466       community_id: i.props.community_view.community.id,
467       added: false,
468       auth: authField(),
469     };
470     WebSocketService.Instance.send(wsClient.addModToCommunity(form));
471     i.state.showConfirmLeaveModTeam = false;
472     i.setState(i.state);
473   }
474
475   handleCancelLeaveModTeamClick(i: Sidebar) {
476     i.state.showConfirmLeaveModTeam = false;
477     i.setState(i.state);
478   }
479
480   handleUnsubscribe(i: Sidebar, event: any) {
481     event.preventDefault();
482     let community_id = i.props.community_view.community.id;
483     let form: FollowCommunity = {
484       community_id,
485       follow: false,
486       auth: authField(),
487     };
488     WebSocketService.Instance.send(wsClient.followCommunity(form));
489
490     // Update myUserInfo
491     UserService.Instance.myUserInfo.follows =
492       UserService.Instance.myUserInfo.follows.filter(
493         i => i.community.id != community_id
494       );
495   }
496
497   handleSubscribe(i: Sidebar, event: any) {
498     event.preventDefault();
499     let community_id = i.props.community_view.community.id;
500     let form: FollowCommunity = {
501       community_id,
502       follow: true,
503       auth: authField(),
504     };
505     WebSocketService.Instance.send(wsClient.followCommunity(form));
506
507     // Update myUserInfo
508     UserService.Instance.myUserInfo.follows.push({
509       community: i.props.community_view.community,
510       follower: UserService.Instance.myUserInfo.local_user_view.person,
511     });
512   }
513
514   private get amTopMod(): boolean {
515     return (
516       this.props.moderators[0].moderator.id ==
517       UserService.Instance.myUserInfo.local_user_view.person.id
518     );
519   }
520
521   get canMod(): boolean {
522     return (
523       UserService.Instance.myUserInfo &&
524       this.props.moderators
525         .map(m => m.moderator.id)
526         .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
527     );
528   }
529
530   get canAdmin(): boolean {
531     return (
532       UserService.Instance.myUserInfo &&
533       this.props.admins
534         .map(a => a.person.id)
535         .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
536     );
537   }
538
539   handleModRemoveShow(i: Sidebar) {
540     i.state.showRemoveDialog = true;
541     i.setState(i.state);
542   }
543
544   handleModRemoveReasonChange(i: Sidebar, event: any) {
545     i.state.removeReason = event.target.value;
546     i.setState(i.state);
547   }
548
549   handleModRemoveExpiresChange(i: Sidebar, event: any) {
550     console.log(event.target.value);
551     i.state.removeExpires = event.target.value;
552     i.setState(i.state);
553   }
554
555   handleModRemoveSubmit(i: Sidebar, event: any) {
556     event.preventDefault();
557     let removeForm: RemoveCommunity = {
558       community_id: i.props.community_view.community.id,
559       removed: !i.props.community_view.community.removed,
560       reason: i.state.removeReason,
561       expires: getUnixTime(i.state.removeExpires),
562       auth: authField(),
563     };
564     WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
565
566     i.state.showRemoveDialog = false;
567     i.setState(i.state);
568   }
569 }