1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
5 CommunityModeratorView,
12 } from 'lemmy-js-client';
13 import { WebSocketService, UserService } from '../services';
14 import { mdToHtml, getUnixTime, wsClient, authField } from '../utils';
15 import { CommunityForm } from './community-form';
16 import { UserListing } from './user-listing';
17 import { CommunityLink } from './community-link';
18 import { BannerIconHeader } from './banner-icon-header';
19 import { i18n } from '../i18next';
21 interface SidebarProps {
22 community_view: CommunityView;
23 categories: Category[];
24 moderators: CommunityModeratorView[];
25 admins: UserViewSafe[];
31 interface SidebarState {
33 showRemoveDialog: boolean;
35 removeExpires: string;
36 showConfirmLeaveModTeam: boolean;
39 export class Sidebar extends Component<SidebarProps, SidebarState> {
40 private emptyState: SidebarState = {
42 showRemoveDialog: false,
45 showConfirmLeaveModTeam: false,
48 constructor(props: any, context: any) {
49 super(props, context);
50 this.state = this.emptyState;
51 this.handleEditCommunity = this.handleEditCommunity.bind(this);
52 this.handleEditCancel = this.handleEditCancel.bind(this);
58 {!this.state.showEdit ? (
62 categories={this.props.categories}
63 community_view={this.props.community_view}
64 onEdit={this.handleEditCommunity}
65 onCancel={this.handleEditCancel}
66 enableNsfw={this.props.enableNsfw}
76 <div class="card border-secondary mb-3">
77 <div class="card-body">
78 {this.communityTitle()}
84 <div class="card border-secondary mb-3">
85 <div class="card-body">
96 let community = this.props.community_view.community;
97 let subscribed = this.props.community_view.subscribed;
100 <h5 className="mb-0">
101 {this.props.showIcon && (
102 <BannerIconHeader icon={community.icon} banner={community.banner} />
104 <span class="mr-2">{community.title}</span>
107 class="btn btn-secondary btn-sm mr-2"
109 onClick={linkEvent(community.id, this.handleUnsubscribe)}
111 <svg class="text-success mr-1 icon icon-inline">
112 <use xlinkHref="#icon-check"></use>
117 {community.removed && (
118 <small className="mr-2 text-muted font-italic">
122 {community.deleted && (
123 <small className="mr-2 text-muted font-italic">
128 <small className="mr-2 text-muted font-italic">
134 community={community}
145 let community_view = this.props.community_view;
146 let counts = community_view.counts;
148 <ul class="my-1 list-inline">
149 <li className="list-inline-item badge badge-secondary">
150 {i18n.t('number_online', { count: this.props.online })}
152 <li className="list-inline-item badge badge-secondary">
153 {i18n.t('number_of_users', {
154 count: counts.users_active_day,
158 <li className="list-inline-item badge badge-secondary">
159 {i18n.t('number_of_users', {
160 count: counts.users_active_week,
164 <li className="list-inline-item badge badge-secondary">
165 {i18n.t('number_of_users', {
166 count: counts.users_active_month,
170 <li className="list-inline-item badge badge-secondary">
171 {i18n.t('number_of_users', {
172 count: counts.users_active_half_year,
174 / {i18n.t('number_of_months', { count: 6 })}
176 <li className="list-inline-item badge badge-secondary">
177 {i18n.t('number_of_subscribers', {
178 count: counts.subscribers,
181 <li className="list-inline-item badge badge-secondary">
182 {i18n.t('number_of_posts', {
186 <li className="list-inline-item badge badge-secondary">
187 {i18n.t('number_of_comments', {
188 count: counts.comments,
191 <li className="list-inline-item">
192 <Link className="badge badge-secondary" to="/communities">
193 {community_view.category.name}
196 <li className="list-inline-item">
198 className="badge badge-secondary"
199 to={`/modlog/community/${this.props.community_view.community.id}`}
210 <ul class="list-inline small">
211 <li class="list-inline-item">{i18n.t('mods')}: </li>
212 {this.props.moderators.map(mod => (
213 <li class="list-inline-item">
214 <UserListing user={mod.moderator} />
222 let community_view = this.props.community_view;
224 community_view.subscribed && (
226 className={`btn btn-secondary btn-block mb-2 ${
227 community_view.community.deleted || community_view.community.removed
231 to={`/create_post?community_id=${community_view.community.id}`}
233 {i18n.t('create_a_post')}
240 let community_view = this.props.community_view;
243 {!community_view.subscribed && (
245 class="btn btn-secondary btn-block"
248 community_view.community.id,
252 {i18n.t('subscribe')}
260 let description = this.props.community_view.community.description;
265 dangerouslySetInnerHTML={mdToHtml(description)}
272 let community_view = this.props.community_view;
275 <ul class="list-inline mb-1 text-muted font-weight-bold">
278 <li className="list-inline-item-action">
281 onClick={linkEvent(this, this.handleEditClick)}
282 data-tippy-content={i18n.t('edit')}
284 <svg class="icon icon-inline">
285 <use xlinkHref="#icon-edit"></use>
290 (!this.state.showConfirmLeaveModTeam ? (
291 <li className="list-inline-item-action">
296 this.handleShowConfirmLeaveModTeamClick
299 {i18n.t('leave_mod_team')}
304 <li className="list-inline-item-action">
305 {i18n.t('are_you_sure')}
307 <li className="list-inline-item-action">
310 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
315 <li className="list-inline-item-action">
320 this.handleCancelLeaveModTeamClick
329 <li className="list-inline-item-action">
332 onClick={linkEvent(this, this.handleDeleteClick)}
334 !community_view.community.deleted
340 class={`icon icon-inline ${
341 community_view.community.deleted && 'text-danger'
344 <use xlinkHref="#icon-trash"></use>
352 <li className="list-inline-item">
353 {!this.props.community_view.community.removed ? (
356 onClick={linkEvent(this, this.handleModRemoveShow)}
363 onClick={linkEvent(this, this.handleModRemoveSubmit)}
371 {this.state.showRemoveDialog && (
372 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
373 <div class="form-group row">
374 <label class="col-form-label" htmlFor="remove-reason">
380 class="form-control mr-2"
381 placeholder={i18n.t('optional')}
382 value={this.state.removeReason}
383 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
386 {/* TODO hold off on expires for now */}
387 {/* <div class="form-group row"> */}
388 {/* <label class="col-form-label">Expires</label> */}
389 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
391 <div class="form-group row">
392 <button type="submit" class="btn btn-secondary">
393 {i18n.t('remove_community')}
402 handleEditClick(i: Sidebar) {
403 i.state.showEdit = true;
407 handleEditCommunity() {
408 this.state.showEdit = false;
409 this.setState(this.state);
413 this.state.showEdit = false;
414 this.setState(this.state);
417 handleDeleteClick(i: Sidebar, event: any) {
418 event.preventDefault();
419 let deleteForm: DeleteCommunity = {
420 community_id: i.props.community_view.community.id,
421 deleted: !i.props.community_view.community.deleted,
424 WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
427 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
428 i.state.showConfirmLeaveModTeam = true;
432 handleLeaveModTeamClick(i: Sidebar) {
433 let form: AddModToCommunity = {
434 user_id: UserService.Instance.user.id,
435 community_id: i.props.community_view.community.id,
439 WebSocketService.Instance.send(wsClient.addModToCommunity(form));
440 i.state.showConfirmLeaveModTeam = false;
444 handleCancelLeaveModTeamClick(i: Sidebar) {
445 i.state.showConfirmLeaveModTeam = false;
449 handleUnsubscribe(communityId: number, event: any) {
450 event.preventDefault();
451 let form: FollowCommunity = {
452 community_id: communityId,
456 WebSocketService.Instance.send(wsClient.followCommunity(form));
459 handleSubscribe(communityId: number, event: any) {
460 event.preventDefault();
461 let form: FollowCommunity = {
462 community_id: communityId,
466 WebSocketService.Instance.send(wsClient.followCommunity(form));
469 private get amCreator(): boolean {
470 return this.props.community_view.creator.id == UserService.Instance.user.id;
473 get canMod(): boolean {
475 UserService.Instance.user &&
476 this.props.moderators
477 .map(m => m.moderator.id)
478 .includes(UserService.Instance.user.id)
482 get canAdmin(): boolean {
484 UserService.Instance.user &&
487 .includes(UserService.Instance.user.id)
491 handleModRemoveShow(i: Sidebar) {
492 i.state.showRemoveDialog = true;
496 handleModRemoveReasonChange(i: Sidebar, event: any) {
497 i.state.removeReason = event.target.value;
501 handleModRemoveExpiresChange(i: Sidebar, event: any) {
502 console.log(event.target.value);
503 i.state.removeExpires = event.target.value;
507 handleModRemoveSubmit(i: Sidebar, event: any) {
508 event.preventDefault();
509 let removeForm: RemoveCommunity = {
510 community_id: i.props.community_view.community.id,
511 removed: !i.props.community_view.community.removed,
512 reason: i.state.removeReason,
513 expires: getUnixTime(i.state.removeExpires),
516 WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
518 i.state.showRemoveDialog = false;