1 import { Component, linkEvent } from "inferno";
2 import { Link } from "inferno-router";
5 CommunityModeratorView,
11 } from "lemmy-js-client";
12 import { WebSocketService, UserService } from "../services";
13 import { mdToHtml, getUnixTime, wsClient, authField } from "../utils";
14 import { CommunityForm } from "./community-form";
15 import { UserListing } from "./user-listing";
16 import { CommunityLink } from "./community-link";
17 import { BannerIconHeader } from "./banner-icon-header";
18 import { Icon } from "./icon";
19 import { i18n } from "../i18next";
21 interface SidebarProps {
22 community_view: CommunityView;
23 moderators: CommunityModeratorView[];
24 admins: UserViewSafe[];
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 <UserListing user={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">
295 onClick={linkEvent(this, this.handleEditClick)}
296 data-tippy-content={i18n.t("edit")}
297 aria-label={i18n.t("edit")}
299 <Icon icon="edit" classes="icon-inline" />
303 (!this.state.showConfirmLeaveModTeam ? (
304 <li className="list-inline-item-action">
310 this.handleShowConfirmLeaveModTeamClick
313 {i18n.t("leave_mod_team")}
318 <li className="list-inline-item-action">
319 {i18n.t("are_you_sure")}
321 <li className="list-inline-item-action">
325 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
330 <li className="list-inline-item-action">
336 this.handleCancelLeaveModTeamClick
345 <li className="list-inline-item-action">
348 onClick={linkEvent(this, this.handleDeleteClick)}
350 !community_view.community.deleted
355 !community_view.community.deleted
362 classes={`icon-inline ${
363 community_view.community.deleted && "text-danger"
372 <li className="list-inline-item">
373 {!this.props.community_view.community.removed ? (
377 onClick={linkEvent(this, this.handleModRemoveShow)}
385 onClick={linkEvent(this, this.handleModRemoveSubmit)}
393 {this.state.showRemoveDialog && (
394 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
395 <div class="form-group row">
396 <label class="col-form-label" htmlFor="remove-reason">
402 class="form-control mr-2"
403 placeholder={i18n.t("optional")}
404 value={this.state.removeReason}
405 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
408 {/* TODO hold off on expires for now */}
409 {/* <div class="form-group row"> */}
410 {/* <label class="col-form-label">Expires</label> */}
411 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
413 <div class="form-group row">
414 <button type="submit" class="btn btn-secondary">
415 {i18n.t("remove_community")}
424 handleEditClick(i: Sidebar) {
425 i.state.showEdit = true;
429 handleEditCommunity() {
430 this.state.showEdit = false;
431 this.setState(this.state);
435 this.state.showEdit = false;
436 this.setState(this.state);
439 handleDeleteClick(i: Sidebar, event: any) {
440 event.preventDefault();
441 let deleteForm: DeleteCommunity = {
442 community_id: i.props.community_view.community.id,
443 deleted: !i.props.community_view.community.deleted,
446 WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
449 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
450 i.state.showConfirmLeaveModTeam = true;
454 handleLeaveModTeamClick(i: Sidebar) {
455 let form: AddModToCommunity = {
456 user_id: UserService.Instance.user.id,
457 community_id: i.props.community_view.community.id,
461 WebSocketService.Instance.send(wsClient.addModToCommunity(form));
462 i.state.showConfirmLeaveModTeam = false;
466 handleCancelLeaveModTeamClick(i: Sidebar) {
467 i.state.showConfirmLeaveModTeam = false;
471 handleUnsubscribe(communityId: number, event: any) {
472 event.preventDefault();
473 let form: FollowCommunity = {
474 community_id: communityId,
478 WebSocketService.Instance.send(wsClient.followCommunity(form));
481 handleSubscribe(communityId: number, event: any) {
482 event.preventDefault();
483 let form: FollowCommunity = {
484 community_id: communityId,
488 WebSocketService.Instance.send(wsClient.followCommunity(form));
491 private get amCreator(): boolean {
492 return this.props.community_view.creator.id == UserService.Instance.user.id;
495 get canMod(): boolean {
497 UserService.Instance.user &&
498 this.props.moderators
499 .map(m => m.moderator.id)
500 .includes(UserService.Instance.user.id)
504 get canAdmin(): boolean {
506 UserService.Instance.user &&
509 .includes(UserService.Instance.user.id)
513 handleModRemoveShow(i: Sidebar) {
514 i.state.showRemoveDialog = true;
518 handleModRemoveReasonChange(i: Sidebar, event: any) {
519 i.state.removeReason = event.target.value;
523 handleModRemoveExpiresChange(i: Sidebar, event: any) {
524 console.log(event.target.value);
525 i.state.removeExpires = event.target.value;
529 handleModRemoveSubmit(i: Sidebar, event: any) {
530 event.preventDefault();
531 let removeForm: RemoveCommunity = {
532 community_id: i.props.community_view.community.id,
533 removed: !i.props.community_view.community.removed,
534 reason: i.state.removeReason,
535 expires: getUnixTime(i.state.removeExpires),
538 WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
540 i.state.showRemoveDialog = false;