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 })}
153 className="list-inline-item badge badge-secondary pointer"
154 data-tippy-content={`${i18n.t('number_of_users', {
155 count: counts.users_active_day,
156 })} ${i18n.t('active_in_the_last')} ${i18n.t('day')}`}
158 {i18n.t('number_of_users', {
159 count: counts.users_active_day,
164 className="list-inline-item badge badge-secondary pointer"
165 data-tippy-content={`${i18n.t('number_of_users', {
166 count: counts.users_active_week,
167 })} ${i18n.t('active_in_the_last')} ${i18n.t('week')}`}
169 {i18n.t('number_of_users', {
170 count: counts.users_active_week,
175 className="list-inline-item badge badge-secondary pointer"
176 data-tippy-content={`${i18n.t('number_of_users', {
177 count: counts.users_active_month,
178 })} ${i18n.t('active_in_the_last')} ${i18n.t('month')}`}
180 {i18n.t('number_of_users', {
181 count: counts.users_active_month,
186 className="list-inline-item badge badge-secondary pointer"
187 data-tippy-content={`${i18n.t('number_of_users', {
188 count: counts.users_active_half_year,
189 })} ${i18n.t('active_in_the_last')} ${i18n.t('number_of_months', {
193 {i18n.t('number_of_users', {
194 count: counts.users_active_half_year,
196 / {i18n.t('number_of_months', { count: 6 })}
198 <li className="list-inline-item badge badge-secondary">
199 {i18n.t('number_of_subscribers', {
200 count: counts.subscribers,
203 <li className="list-inline-item badge badge-secondary">
204 {i18n.t('number_of_posts', {
208 <li className="list-inline-item badge badge-secondary">
209 {i18n.t('number_of_comments', {
210 count: counts.comments,
213 <li className="list-inline-item">
214 <Link className="badge badge-secondary" to="/communities">
215 {community_view.category.name}
218 <li className="list-inline-item">
220 className="badge badge-secondary"
221 to={`/modlog/community/${this.props.community_view.community.id}`}
232 <ul class="list-inline small">
233 <li class="list-inline-item">{i18n.t('mods')}: </li>
234 {this.props.moderators.map(mod => (
235 <li class="list-inline-item">
236 <UserListing user={mod.moderator} />
244 let community_view = this.props.community_view;
246 community_view.subscribed && (
248 className={`btn btn-secondary btn-block mb-2 ${
249 community_view.community.deleted || community_view.community.removed
253 to={`/create_post?community_id=${community_view.community.id}`}
255 {i18n.t('create_a_post')}
262 let community_view = this.props.community_view;
265 {!community_view.subscribed && (
267 class="btn btn-secondary btn-block"
270 community_view.community.id,
274 {i18n.t('subscribe')}
282 let description = this.props.community_view.community.description;
287 dangerouslySetInnerHTML={mdToHtml(description)}
294 let community_view = this.props.community_view;
297 <ul class="list-inline mb-1 text-muted font-weight-bold">
300 <li className="list-inline-item-action">
303 onClick={linkEvent(this, this.handleEditClick)}
304 data-tippy-content={i18n.t('edit')}
306 <svg class="icon icon-inline">
307 <use xlinkHref="#icon-edit"></use>
312 (!this.state.showConfirmLeaveModTeam ? (
313 <li className="list-inline-item-action">
318 this.handleShowConfirmLeaveModTeamClick
321 {i18n.t('leave_mod_team')}
326 <li className="list-inline-item-action">
327 {i18n.t('are_you_sure')}
329 <li className="list-inline-item-action">
332 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
337 <li className="list-inline-item-action">
342 this.handleCancelLeaveModTeamClick
351 <li className="list-inline-item-action">
354 onClick={linkEvent(this, this.handleDeleteClick)}
356 !community_view.community.deleted
362 class={`icon icon-inline ${
363 community_view.community.deleted && 'text-danger'
366 <use xlinkHref="#icon-trash"></use>
374 <li className="list-inline-item">
375 {!this.props.community_view.community.removed ? (
378 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;