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">
304 onClick={linkEvent(this, this.handleEditClick)}
305 data-tippy-content={i18n.t('edit')}
306 aria-label={i18n.t('edit')}
308 <svg class="icon icon-inline">
309 <use xlinkHref="#icon-edit"></use>
314 (!this.state.showConfirmLeaveModTeam ? (
315 <li className="list-inline-item-action">
321 this.handleShowConfirmLeaveModTeamClick
324 {i18n.t('leave_mod_team')}
329 <li className="list-inline-item-action">
330 {i18n.t('are_you_sure')}
332 <li className="list-inline-item-action">
336 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
341 <li className="list-inline-item-action">
347 this.handleCancelLeaveModTeamClick
356 <li className="list-inline-item-action">
359 onClick={linkEvent(this, this.handleDeleteClick)}
361 !community_view.community.deleted
366 !community_view.community.deleted
372 class={`icon icon-inline ${
373 community_view.community.deleted && 'text-danger'
376 <use xlinkHref="#icon-trash"></use>
384 <li className="list-inline-item">
385 {!this.props.community_view.community.removed ? (
389 onClick={linkEvent(this, this.handleModRemoveShow)}
397 onClick={linkEvent(this, this.handleModRemoveSubmit)}
405 {this.state.showRemoveDialog && (
406 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
407 <div class="form-group row">
408 <label class="col-form-label" htmlFor="remove-reason">
414 class="form-control mr-2"
415 placeholder={i18n.t('optional')}
416 value={this.state.removeReason}
417 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
420 {/* TODO hold off on expires for now */}
421 {/* <div class="form-group row"> */}
422 {/* <label class="col-form-label">Expires</label> */}
423 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
425 <div class="form-group row">
426 <button type="submit" class="btn btn-secondary">
427 {i18n.t('remove_community')}
436 handleEditClick(i: Sidebar) {
437 i.state.showEdit = true;
441 handleEditCommunity() {
442 this.state.showEdit = false;
443 this.setState(this.state);
447 this.state.showEdit = false;
448 this.setState(this.state);
451 handleDeleteClick(i: Sidebar, event: any) {
452 event.preventDefault();
453 let deleteForm: DeleteCommunity = {
454 community_id: i.props.community_view.community.id,
455 deleted: !i.props.community_view.community.deleted,
458 WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
461 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
462 i.state.showConfirmLeaveModTeam = true;
466 handleLeaveModTeamClick(i: Sidebar) {
467 let form: AddModToCommunity = {
468 user_id: UserService.Instance.user.id,
469 community_id: i.props.community_view.community.id,
473 WebSocketService.Instance.send(wsClient.addModToCommunity(form));
474 i.state.showConfirmLeaveModTeam = false;
478 handleCancelLeaveModTeamClick(i: Sidebar) {
479 i.state.showConfirmLeaveModTeam = false;
483 handleUnsubscribe(communityId: number, event: any) {
484 event.preventDefault();
485 let form: FollowCommunity = {
486 community_id: communityId,
490 WebSocketService.Instance.send(wsClient.followCommunity(form));
493 handleSubscribe(communityId: number, event: any) {
494 event.preventDefault();
495 let form: FollowCommunity = {
496 community_id: communityId,
500 WebSocketService.Instance.send(wsClient.followCommunity(form));
503 private get amCreator(): boolean {
504 return this.props.community_view.creator.id == UserService.Instance.user.id;
507 get canMod(): boolean {
509 UserService.Instance.user &&
510 this.props.moderators
511 .map(m => m.moderator.id)
512 .includes(UserService.Instance.user.id)
516 get canAdmin(): boolean {
518 UserService.Instance.user &&
521 .includes(UserService.Instance.user.id)
525 handleModRemoveShow(i: Sidebar) {
526 i.state.showRemoveDialog = true;
530 handleModRemoveReasonChange(i: Sidebar, event: any) {
531 i.state.removeReason = event.target.value;
535 handleModRemoveExpiresChange(i: Sidebar, event: any) {
536 console.log(event.target.value);
537 i.state.removeExpires = event.target.value;
541 handleModRemoveSubmit(i: Sidebar, event: any) {
542 event.preventDefault();
543 let removeForm: RemoveCommunity = {
544 community_id: i.props.community_view.community.id,
545 removed: !i.props.community_view.community.removed,
546 reason: i.state.removeReason,
547 expires: getUnixTime(i.state.removeExpires),
550 WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
552 i.state.showRemoveDialog = false;