1 import { Component, linkEvent } from "inferno";
2 import { Link } from "inferno-router";
5 CommunityModeratorView,
11 } from "lemmy-js-client";
12 import { i18n } from "../../i18next";
13 import { UserService, WebSocketService } from "../../services";
14 import { authField, getUnixTime, mdToHtml, wsClient } from "../../utils";
15 import { BannerIconHeader } from "../common/banner-icon-header";
16 import { Icon } from "../common/icon";
17 import { CommunityForm } from "../community/community-form";
18 import { CommunityLink } from "../community/community-link";
19 import { PersonListing } from "../person/person-listing";
21 interface SidebarProps {
22 community_view: CommunityView;
23 moderators: CommunityModeratorView[];
24 admins: PersonViewSafe[];
30 interface SidebarState {
32 showRemoveDialog: boolean;
34 removeExpires: string;
35 showConfirmLeaveModTeam: boolean;
38 export class Sidebar extends Component<SidebarProps, SidebarState> {
39 private emptyState: SidebarState = {
41 showRemoveDialog: false,
44 showConfirmLeaveModTeam: false,
47 constructor(props: any, context: any) {
48 super(props, context);
49 this.state = this.emptyState;
50 this.handleEditCommunity = this.handleEditCommunity.bind(this);
51 this.handleEditCancel = this.handleEditCancel.bind(this);
57 {!this.state.showEdit ? (
61 community_view={this.props.community_view}
62 onEdit={this.handleEditCommunity}
63 onCancel={this.handleEditCancel}
64 enableNsfw={this.props.enableNsfw}
74 <div class="card border-secondary mb-3">
75 <div class="card-body">
76 {this.communityTitle()}
82 <div class="card border-secondary mb-3">
83 <div class="card-body">
94 let community = this.props.community_view.community;
95 let subscribed = this.props.community_view.subscribed;
99 {this.props.showIcon && (
100 <BannerIconHeader icon={community.icon} banner={community.banner} />
102 <span class="mr-2">{community.title}</span>
105 class="btn btn-secondary btn-sm mr-2"
107 onClick={linkEvent(this, this.handleUnsubscribe)}
109 <Icon icon="check" classes="icon-inline text-success mr-1" />
113 {community.removed && (
114 <small className="mr-2 text-muted font-italic">
118 {community.deleted && (
119 <small className="mr-2 text-muted font-italic">
124 <small className="mr-2 text-muted font-italic">
130 community={community}
141 let community_view = this.props.community_view;
142 let counts = community_view.counts;
144 <ul class="my-1 list-inline">
145 <li className="list-inline-item badge badge-secondary">
146 {i18n.t("number_online", { count: this.props.online })}
149 className="list-inline-item badge badge-secondary pointer"
150 data-tippy-content={`${i18n.t("number_of_users", {
151 count: counts.users_active_day,
152 })} ${i18n.t("active_in_the_last")} ${i18n.t("day")}`}
154 {i18n.t("number_of_users", {
155 count: counts.users_active_day,
160 className="list-inline-item badge badge-secondary pointer"
161 data-tippy-content={`${i18n.t("number_of_users", {
162 count: counts.users_active_week,
163 })} ${i18n.t("active_in_the_last")} ${i18n.t("week")}`}
165 {i18n.t("number_of_users", {
166 count: counts.users_active_week,
171 className="list-inline-item badge badge-secondary pointer"
172 data-tippy-content={`${i18n.t("number_of_users", {
173 count: counts.users_active_month,
174 })} ${i18n.t("active_in_the_last")} ${i18n.t("month")}`}
176 {i18n.t("number_of_users", {
177 count: counts.users_active_month,
182 className="list-inline-item badge badge-secondary pointer"
183 data-tippy-content={`${i18n.t("number_of_users", {
184 count: counts.users_active_half_year,
185 })} ${i18n.t("active_in_the_last")} ${i18n.t("number_of_months", {
189 {i18n.t("number_of_users", {
190 count: counts.users_active_half_year,
192 / {i18n.t("number_of_months", { count: 6 })}
194 <li className="list-inline-item badge badge-secondary">
195 {i18n.t("number_of_subscribers", {
196 count: counts.subscribers,
199 <li className="list-inline-item badge badge-secondary">
200 {i18n.t("number_of_posts", {
204 <li className="list-inline-item badge badge-secondary">
205 {i18n.t("number_of_comments", {
206 count: counts.comments,
209 <li className="list-inline-item">
211 className="badge badge-secondary"
212 to={`/modlog/community/${this.props.community_view.community.id}`}
223 <ul class="list-inline small">
224 <li class="list-inline-item">{i18n.t("mods")}: </li>
225 {this.props.moderators.map(mod => (
226 <li class="list-inline-item">
227 <PersonListing person={mod.moderator} />
235 let community_view = this.props.community_view;
237 community_view.subscribed && (
239 className={`btn btn-secondary btn-block mb-2 ${
240 community_view.community.deleted || community_view.community.removed
244 to={`/create_post?community_id=${community_view.community.id}`}
246 {i18n.t("create_a_post")}
253 let community_view = this.props.community_view;
256 {!community_view.subscribed && (
258 class="btn btn-secondary btn-block"
260 onClick={linkEvent(this, this.handleSubscribe)}
262 {i18n.t("subscribe")}
270 let description = this.props.community_view.community.description;
275 dangerouslySetInnerHTML={mdToHtml(description)}
282 let community_view = this.props.community_view;
285 <ul class="list-inline mb-1 text-muted font-weight-bold">
288 <li className="list-inline-item-action">
290 class="btn btn-link text-muted d-inline-block"
291 onClick={linkEvent(this, this.handleEditClick)}
292 data-tippy-content={i18n.t("edit")}
293 aria-label={i18n.t("edit")}
295 <Icon icon="edit" classes="icon-inline" />
299 (!this.state.showConfirmLeaveModTeam ? (
300 <li className="list-inline-item-action">
302 class="btn btn-link text-muted d-inline-block"
305 this.handleShowConfirmLeaveModTeamClick
308 {i18n.t("leave_mod_team")}
313 <li className="list-inline-item-action">
314 {i18n.t("are_you_sure")}
316 <li className="list-inline-item-action">
318 class="btn btn-link text-muted d-inline-block"
319 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
324 <li className="list-inline-item-action">
326 class="btn btn-link text-muted d-inline-block"
329 this.handleCancelLeaveModTeamClick
338 <li className="list-inline-item-action">
340 class="btn btn-link text-muted d-inline-block"
341 onClick={linkEvent(this, this.handleDeleteClick)}
343 !community_view.community.deleted
348 !community_view.community.deleted
355 classes={`icon-inline ${
356 community_view.community.deleted && "text-danger"
365 <li className="list-inline-item">
366 {!this.props.community_view.community.removed ? (
368 class="btn btn-link text-muted d-inline-block"
369 onClick={linkEvent(this, this.handleModRemoveShow)}
375 class="btn btn-link text-muted d-inline-block"
376 onClick={linkEvent(this, this.handleModRemoveSubmit)}
384 {this.state.showRemoveDialog && (
385 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
386 <div class="form-group row">
387 <label class="col-form-label" htmlFor="remove-reason">
393 class="form-control mr-2"
394 placeholder={i18n.t("optional")}
395 value={this.state.removeReason}
396 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
399 {/* TODO hold off on expires for now */}
400 {/* <div class="form-group row"> */}
401 {/* <label class="col-form-label">Expires</label> */}
402 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
404 <div class="form-group row">
405 <button type="submit" class="btn btn-secondary">
406 {i18n.t("remove_community")}
415 handleEditClick(i: Sidebar) {
416 i.state.showEdit = true;
420 handleEditCommunity() {
421 this.state.showEdit = false;
422 this.setState(this.state);
426 this.state.showEdit = false;
427 this.setState(this.state);
430 handleDeleteClick(i: Sidebar, event: any) {
431 event.preventDefault();
432 let deleteForm: DeleteCommunity = {
433 community_id: i.props.community_view.community.id,
434 deleted: !i.props.community_view.community.deleted,
437 WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
440 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
441 i.state.showConfirmLeaveModTeam = true;
445 handleLeaveModTeamClick(i: Sidebar) {
446 let form: AddModToCommunity = {
447 person_id: UserService.Instance.myUserInfo.local_user_view.person.id,
448 community_id: i.props.community_view.community.id,
452 WebSocketService.Instance.send(wsClient.addModToCommunity(form));
453 i.state.showConfirmLeaveModTeam = false;
457 handleCancelLeaveModTeamClick(i: Sidebar) {
458 i.state.showConfirmLeaveModTeam = false;
462 handleUnsubscribe(i: Sidebar, event: any) {
463 event.preventDefault();
464 let community_id = i.props.community_view.community.id;
465 let form: FollowCommunity = {
470 WebSocketService.Instance.send(wsClient.followCommunity(form));
473 UserService.Instance.myUserInfo.follows =
474 UserService.Instance.myUserInfo.follows.filter(
475 i => i.community.id != community_id
479 handleSubscribe(i: Sidebar, event: any) {
480 event.preventDefault();
481 let community_id = i.props.community_view.community.id;
482 let form: FollowCommunity = {
487 WebSocketService.Instance.send(wsClient.followCommunity(form));
490 UserService.Instance.myUserInfo.follows.push({
491 community: i.props.community_view.community,
492 follower: UserService.Instance.myUserInfo.local_user_view.person,
496 private get amTopMod(): boolean {
498 this.props.moderators[0].moderator.id ==
499 UserService.Instance.myUserInfo.local_user_view.person.id
503 get canMod(): boolean {
505 UserService.Instance.myUserInfo &&
506 this.props.moderators
507 .map(m => m.moderator.id)
508 .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
512 get canAdmin(): boolean {
514 UserService.Instance.myUserInfo &&
516 .map(a => a.person.id)
517 .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
521 handleModRemoveShow(i: Sidebar) {
522 i.state.showRemoveDialog = true;
526 handleModRemoveReasonChange(i: Sidebar, event: any) {
527 i.state.removeReason = event.target.value;
531 handleModRemoveExpiresChange(i: Sidebar, event: any) {
532 console.log(event.target.value);
533 i.state.removeExpires = event.target.value;
537 handleModRemoveSubmit(i: Sidebar, event: any) {
538 event.preventDefault();
539 let removeForm: RemoveCommunity = {
540 community_id: i.props.community_view.community.id,
541 removed: !i.props.community_view.community.removed,
542 reason: i.state.removeReason,
543 expires: getUnixTime(i.state.removeExpires),
546 WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
548 i.state.showRemoveDialog = false;