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 } 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;
147 <ul class="my-1 list-inline">
148 <li className="list-inline-item badge badge-secondary">
149 {i18n.t('number_online', { count: this.props.online })}
151 <li className="list-inline-item badge badge-secondary">
152 {i18n.t('number_of_subscribers', {
153 count: community_view.counts.subscribers,
156 <li className="list-inline-item badge badge-secondary">
157 {i18n.t('number_of_posts', {
158 count: community_view.counts.posts,
161 <li className="list-inline-item badge badge-secondary">
162 {i18n.t('number_of_comments', {
163 count: community_view.counts.comments,
166 <li className="list-inline-item">
167 <Link className="badge badge-secondary" to="/communities">
168 {community_view.category.name}
171 <li className="list-inline-item">
173 className="badge badge-secondary"
174 to={`/modlog/community/${this.props.community_view.community.id}`}
185 <ul class="list-inline small">
186 <li class="list-inline-item">{i18n.t('mods')}: </li>
187 {this.props.moderators.map(mod => (
188 <li class="list-inline-item">
189 <UserListing user={mod.moderator} />
197 let community_view = this.props.community_view;
199 community_view.subscribed && (
201 className={`btn btn-secondary btn-block mb-2 ${
202 community_view.community.deleted || community_view.community.removed
206 to={`/create_post?community_id=${community_view.community.id}`}
208 {i18n.t('create_a_post')}
215 let community_view = this.props.community_view;
218 {!community_view.subscribed && (
220 class="btn btn-secondary btn-block"
223 community_view.community.id,
227 {i18n.t('subscribe')}
235 let description = this.props.community_view.community.description;
240 dangerouslySetInnerHTML={mdToHtml(description)}
247 let community_view = this.props.community_view;
250 <ul class="list-inline mb-1 text-muted font-weight-bold">
253 <li className="list-inline-item-action">
256 onClick={linkEvent(this, this.handleEditClick)}
257 data-tippy-content={i18n.t('edit')}
259 <svg class="icon icon-inline">
260 <use xlinkHref="#icon-edit"></use>
265 (!this.state.showConfirmLeaveModTeam ? (
266 <li className="list-inline-item-action">
271 this.handleShowConfirmLeaveModTeamClick
274 {i18n.t('leave_mod_team')}
279 <li className="list-inline-item-action">
280 {i18n.t('are_you_sure')}
282 <li className="list-inline-item-action">
285 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
290 <li className="list-inline-item-action">
295 this.handleCancelLeaveModTeamClick
304 <li className="list-inline-item-action">
307 onClick={linkEvent(this, this.handleDeleteClick)}
309 !community_view.community.deleted
315 class={`icon icon-inline ${
316 community_view.community.deleted && 'text-danger'
319 <use xlinkHref="#icon-trash"></use>
327 <li className="list-inline-item">
328 {!this.props.community_view.community.removed ? (
331 onClick={linkEvent(this, this.handleModRemoveShow)}
338 onClick={linkEvent(this, this.handleModRemoveSubmit)}
346 {this.state.showRemoveDialog && (
347 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
348 <div class="form-group row">
349 <label class="col-form-label" htmlFor="remove-reason">
355 class="form-control mr-2"
356 placeholder={i18n.t('optional')}
357 value={this.state.removeReason}
358 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
361 {/* TODO hold off on expires for now */}
362 {/* <div class="form-group row"> */}
363 {/* <label class="col-form-label">Expires</label> */}
364 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
366 <div class="form-group row">
367 <button type="submit" class="btn btn-secondary">
368 {i18n.t('remove_community')}
377 handleEditClick(i: Sidebar) {
378 i.state.showEdit = true;
382 handleEditCommunity() {
383 this.state.showEdit = false;
384 this.setState(this.state);
388 this.state.showEdit = false;
389 this.setState(this.state);
392 handleDeleteClick(i: Sidebar, event: any) {
393 event.preventDefault();
394 let deleteForm: DeleteCommunity = {
395 edit_id: i.props.community_view.community.id,
396 deleted: !i.props.community_view.community.deleted,
397 auth: UserService.Instance.authField(),
399 WebSocketService.Instance.client.deleteCommunity(deleteForm);
402 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
403 i.state.showConfirmLeaveModTeam = true;
407 handleLeaveModTeamClick(i: Sidebar) {
408 let form: AddModToCommunity = {
409 user_id: UserService.Instance.user.id,
410 community_id: i.props.community_view.community.id,
412 auth: UserService.Instance.authField(),
414 WebSocketService.Instance.client.addModToCommunity(form);
415 i.state.showConfirmLeaveModTeam = false;
419 handleCancelLeaveModTeamClick(i: Sidebar) {
420 i.state.showConfirmLeaveModTeam = false;
424 handleUnsubscribe(communityId: number, event: any) {
425 event.preventDefault();
426 let form: FollowCommunity = {
427 community_id: communityId,
429 auth: UserService.Instance.authField(),
431 WebSocketService.Instance.client.followCommunity(form);
434 handleSubscribe(communityId: number, event: any) {
435 event.preventDefault();
436 let form: FollowCommunity = {
437 community_id: communityId,
439 auth: UserService.Instance.authField(),
441 WebSocketService.Instance.client.followCommunity(form);
444 private get amCreator(): boolean {
445 return this.props.community_view.creator.id == UserService.Instance.user.id;
448 get canMod(): boolean {
450 UserService.Instance.user &&
451 this.props.moderators
452 .map(m => m.moderator.id)
453 .includes(UserService.Instance.user.id)
457 get canAdmin(): boolean {
459 UserService.Instance.user &&
462 .includes(UserService.Instance.user.id)
466 handleModRemoveShow(i: Sidebar) {
467 i.state.showRemoveDialog = true;
471 handleModRemoveReasonChange(i: Sidebar, event: any) {
472 i.state.removeReason = event.target.value;
476 handleModRemoveExpiresChange(i: Sidebar, event: any) {
477 console.log(event.target.value);
478 i.state.removeExpires = event.target.value;
482 handleModRemoveSubmit(i: Sidebar, event: any) {
483 event.preventDefault();
484 let removeForm: RemoveCommunity = {
485 edit_id: i.props.community_view.community.id,
486 removed: !i.props.community_view.community.removed,
487 reason: i.state.removeReason,
488 expires: getUnixTime(i.state.removeExpires),
489 auth: UserService.Instance.authField(),
491 WebSocketService.Instance.client.removeCommunity(removeForm);
493 i.state.showRemoveDialog = false;