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(community.id, 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"
261 community_view.community.id,
265 {i18n.t("subscribe")}
273 let description = this.props.community_view.community.description;
278 dangerouslySetInnerHTML={mdToHtml(description)}
285 let community_view = this.props.community_view;
288 <ul class="list-inline mb-1 text-muted font-weight-bold">
291 <li className="list-inline-item-action">
293 class="btn btn-link text-muted d-inline-block"
294 onClick={linkEvent(this, this.handleEditClick)}
295 data-tippy-content={i18n.t("edit")}
296 aria-label={i18n.t("edit")}
298 <Icon icon="edit" classes="icon-inline" />
302 (!this.state.showConfirmLeaveModTeam ? (
303 <li className="list-inline-item-action">
305 class="btn btn-link text-muted d-inline-block"
308 this.handleShowConfirmLeaveModTeamClick
311 {i18n.t("leave_mod_team")}
316 <li className="list-inline-item-action">
317 {i18n.t("are_you_sure")}
319 <li className="list-inline-item-action">
321 class="btn btn-link text-muted d-inline-block"
322 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
327 <li className="list-inline-item-action">
329 class="btn btn-link text-muted d-inline-block"
332 this.handleCancelLeaveModTeamClick
341 <li className="list-inline-item-action">
343 class="btn btn-link text-muted d-inline-block"
344 onClick={linkEvent(this, this.handleDeleteClick)}
346 !community_view.community.deleted
351 !community_view.community.deleted
358 classes={`icon-inline ${
359 community_view.community.deleted && "text-danger"
368 <li className="list-inline-item">
369 {!this.props.community_view.community.removed ? (
371 class="btn btn-link text-muted d-inline-block"
372 onClick={linkEvent(this, this.handleModRemoveShow)}
378 class="btn btn-link text-muted d-inline-block"
379 onClick={linkEvent(this, this.handleModRemoveSubmit)}
387 {this.state.showRemoveDialog && (
388 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
389 <div class="form-group row">
390 <label class="col-form-label" htmlFor="remove-reason">
396 class="form-control mr-2"
397 placeholder={i18n.t("optional")}
398 value={this.state.removeReason}
399 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
402 {/* TODO hold off on expires for now */}
403 {/* <div class="form-group row"> */}
404 {/* <label class="col-form-label">Expires</label> */}
405 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
407 <div class="form-group row">
408 <button type="submit" class="btn btn-secondary">
409 {i18n.t("remove_community")}
418 handleEditClick(i: Sidebar) {
419 i.state.showEdit = true;
423 handleEditCommunity() {
424 this.state.showEdit = false;
425 this.setState(this.state);
429 this.state.showEdit = false;
430 this.setState(this.state);
433 handleDeleteClick(i: Sidebar, event: any) {
434 event.preventDefault();
435 let deleteForm: DeleteCommunity = {
436 community_id: i.props.community_view.community.id,
437 deleted: !i.props.community_view.community.deleted,
440 WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
443 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
444 i.state.showConfirmLeaveModTeam = true;
448 handleLeaveModTeamClick(i: Sidebar) {
449 let form: AddModToCommunity = {
450 person_id: UserService.Instance.localUserView.person.id,
451 community_id: i.props.community_view.community.id,
455 WebSocketService.Instance.send(wsClient.addModToCommunity(form));
456 i.state.showConfirmLeaveModTeam = false;
460 handleCancelLeaveModTeamClick(i: Sidebar) {
461 i.state.showConfirmLeaveModTeam = false;
465 handleUnsubscribe(communityId: number, event: any) {
466 event.preventDefault();
467 let form: FollowCommunity = {
468 community_id: communityId,
472 WebSocketService.Instance.send(wsClient.followCommunity(form));
475 handleSubscribe(communityId: number, event: any) {
476 event.preventDefault();
477 let form: FollowCommunity = {
478 community_id: communityId,
482 WebSocketService.Instance.send(wsClient.followCommunity(form));
485 private get amTopMod(): boolean {
487 this.props.moderators[0].moderator.id ==
488 UserService.Instance.localUserView.person.id
492 get canMod(): boolean {
494 UserService.Instance.localUserView &&
495 this.props.moderators
496 .map(m => m.moderator.id)
497 .includes(UserService.Instance.localUserView.person.id)
501 get canAdmin(): boolean {
503 UserService.Instance.localUserView &&
505 .map(a => a.person.id)
506 .includes(UserService.Instance.localUserView.person.id)
510 handleModRemoveShow(i: Sidebar) {
511 i.state.showRemoveDialog = true;
515 handleModRemoveReasonChange(i: Sidebar, event: any) {
516 i.state.removeReason = event.target.value;
520 handleModRemoveExpiresChange(i: Sidebar, event: any) {
521 console.log(event.target.value);
522 i.state.removeExpires = event.target.value;
526 handleModRemoveSubmit(i: Sidebar, event: any) {
527 event.preventDefault();
528 let removeForm: RemoveCommunity = {
529 community_id: i.props.community_view.community.id,
530 removed: !i.props.community_view.community.removed,
531 reason: i.state.removeReason,
532 expires: getUnixTime(i.state.removeExpires),
535 WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
537 i.state.showRemoveDialog = false;