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";
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";
27 interface SidebarProps {
28 community_view: CommunityView;
29 moderators: CommunityModeratorView[];
30 admins: PersonViewSafe[];
36 interface SidebarState {
38 showRemoveDialog: boolean;
40 removeExpires: string;
41 showConfirmLeaveModTeam: boolean;
44 export class Sidebar extends Component<SidebarProps, SidebarState> {
45 private emptyState: SidebarState = {
47 showRemoveDialog: false,
50 showConfirmLeaveModTeam: false,
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);
63 {!this.state.showEdit ? (
67 community_view={this.props.community_view}
68 onEdit={this.handleEditCommunity}
69 onCancel={this.handleEditCancel}
70 enableNsfw={this.props.enableNsfw}
80 <div class="card border-secondary mb-3">
81 <div class="card-body">
82 {this.communityTitle()}
88 <div class="card border-secondary mb-3">
89 <div class="card-body">
100 let community = this.props.community_view.community;
101 let subscribed = this.props.community_view.subscribed;
104 <h5 className="mb-0">
105 {this.props.showIcon && (
106 <BannerIconHeader icon={community.icon} banner={community.banner} />
108 <span class="mr-2">{community.title}</span>
111 class="btn btn-secondary btn-sm mr-2"
113 onClick={linkEvent(this, this.handleUnsubscribe)}
115 <Icon icon="check" classes="icon-inline text-success mr-1" />
119 {community.removed && (
120 <small className="mr-2 text-muted font-italic">
124 {community.deleted && (
125 <small className="mr-2 text-muted font-italic">
130 <small className="mr-2 text-muted font-italic">
136 community={community}
147 let community_view = this.props.community_view;
148 let counts = community_view.counts;
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),
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,
164 {i18n.t("number_of_users", {
165 count: counts.users_active_day,
166 formattedCount: numToSI(counts.users_active_day),
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,
177 {i18n.t("number_of_users", {
178 count: counts.users_active_week,
179 formattedCount: numToSI(counts.users_active_week),
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,
190 {i18n.t("number_of_users", {
191 count: counts.users_active_month,
192 formattedCount: numToSI(counts.users_active_month),
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,
203 {i18n.t("number_of_users", {
204 count: counts.users_active_half_year,
205 formattedCount: numToSI(counts.users_active_half_year),
207 / {i18n.t("number_of_months", { count: 6, formattedCount: 6 })}
209 <li className="list-inline-item badge badge-secondary">
210 {i18n.t("number_of_subscribers", {
211 count: counts.subscribers,
212 formattedCount: numToSI(counts.subscribers),
215 <li className="list-inline-item badge badge-secondary">
216 {i18n.t("number_of_posts", {
218 formattedCount: numToSI(counts.posts),
221 <li className="list-inline-item badge badge-secondary">
222 {i18n.t("number_of_comments", {
223 count: counts.comments,
224 formattedCount: numToSI(counts.comments),
227 <li className="list-inline-item">
229 className="badge badge-secondary"
230 to={`/modlog/community/${this.props.community_view.community.id}`}
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} />
253 let community_view = this.props.community_view;
255 community_view.subscribed && (
257 className={`btn btn-secondary btn-block mb-2 ${
258 community_view.community.deleted || community_view.community.removed
262 to={`/create_post?community_id=${community_view.community.id}`}
264 {i18n.t("create_a_post")}
271 let community_view = this.props.community_view;
274 {!community_view.subscribed && (
276 class="btn btn-secondary btn-block"
278 onClick={linkEvent(this, this.handleSubscribe)}
280 {i18n.t("subscribe")}
288 let description = this.props.community_view.community.description;
293 dangerouslySetInnerHTML={mdToHtml(description)}
300 let community_view = this.props.community_view;
303 <ul class="list-inline mb-1 text-muted font-weight-bold">
306 <li className="list-inline-item-action">
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")}
313 <Icon icon="edit" classes="icon-inline" />
317 (!this.state.showConfirmLeaveModTeam ? (
318 <li className="list-inline-item-action">
320 class="btn btn-link text-muted d-inline-block"
323 this.handleShowConfirmLeaveModTeamClick
326 {i18n.t("leave_mod_team")}
331 <li className="list-inline-item-action">
332 {i18n.t("are_you_sure")}
334 <li className="list-inline-item-action">
336 class="btn btn-link text-muted d-inline-block"
337 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
342 <li className="list-inline-item-action">
344 class="btn btn-link text-muted d-inline-block"
347 this.handleCancelLeaveModTeamClick
356 <li className="list-inline-item-action">
358 class="btn btn-link text-muted d-inline-block"
359 onClick={linkEvent(this, this.handleDeleteClick)}
361 !community_view.community.deleted
366 !community_view.community.deleted
373 classes={`icon-inline ${
374 community_view.community.deleted && "text-danger"
383 <li className="list-inline-item">
384 {!this.props.community_view.community.removed ? (
386 class="btn btn-link text-muted d-inline-block"
387 onClick={linkEvent(this, this.handleModRemoveShow)}
393 class="btn btn-link text-muted d-inline-block"
394 onClick={linkEvent(this, this.handleModRemoveSubmit)}
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">
411 class="form-control mr-2"
412 placeholder={i18n.t("optional")}
413 value={this.state.removeReason}
414 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
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)} /> */}
422 <div class="form-group row">
423 <button type="submit" class="btn btn-secondary">
424 {i18n.t("remove_community")}
433 handleEditClick(i: Sidebar) {
434 i.state.showEdit = true;
438 handleEditCommunity() {
439 this.state.showEdit = false;
440 this.setState(this.state);
444 this.state.showEdit = false;
445 this.setState(this.state);
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,
455 WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
458 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
459 i.state.showConfirmLeaveModTeam = true;
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,
470 WebSocketService.Instance.send(wsClient.addModToCommunity(form));
471 i.state.showConfirmLeaveModTeam = false;
475 handleCancelLeaveModTeamClick(i: Sidebar) {
476 i.state.showConfirmLeaveModTeam = false;
480 handleUnsubscribe(i: Sidebar, event: any) {
481 event.preventDefault();
482 let community_id = i.props.community_view.community.id;
483 let form: FollowCommunity = {
488 WebSocketService.Instance.send(wsClient.followCommunity(form));
491 UserService.Instance.myUserInfo.follows =
492 UserService.Instance.myUserInfo.follows.filter(
493 i => i.community.id != community_id
497 handleSubscribe(i: Sidebar, event: any) {
498 event.preventDefault();
499 let community_id = i.props.community_view.community.id;
500 let form: FollowCommunity = {
505 WebSocketService.Instance.send(wsClient.followCommunity(form));
508 UserService.Instance.myUserInfo.follows.push({
509 community: i.props.community_view.community,
510 follower: UserService.Instance.myUserInfo.local_user_view.person,
514 private get amTopMod(): boolean {
516 this.props.moderators[0].moderator.id ==
517 UserService.Instance.myUserInfo.local_user_view.person.id
521 get canMod(): boolean {
523 UserService.Instance.myUserInfo &&
524 this.props.moderators
525 .map(m => m.moderator.id)
526 .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
530 get canAdmin(): boolean {
532 UserService.Instance.myUserInfo &&
534 .map(a => a.person.id)
535 .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
539 handleModRemoveShow(i: Sidebar) {
540 i.state.showRemoveDialog = true;
544 handleModRemoveReasonChange(i: Sidebar, event: any) {
545 i.state.removeReason = event.target.value;
549 handleModRemoveExpiresChange(i: Sidebar, event: any) {
550 console.log(event.target.value);
551 i.state.removeExpires = event.target.value;
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),
564 WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
566 i.state.showRemoveDialog = false;