1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
10 AddModToCommunityForm,
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 {
23 categories: Category[];
24 moderators: CommunityUser[];
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={this.props.community}
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;
100 {this.props.showIcon && (
101 <BannerIconHeader icon={community.icon} banner={community.banner} />
103 <span class="mr-2">{community.title}</span>
104 {community.subscribed && (
106 class="btn btn-secondary btn-sm mr-2"
108 onClick={linkEvent(community.id, this.handleUnsubscribe)}
110 <svg class="text-success mr-1 icon icon-inline">
111 <use xlinkHref="#icon-check"></use>
116 {community.removed && (
117 <small className="mr-2 text-muted font-italic">
121 {community.deleted && (
122 <small className="mr-2 text-muted font-italic">
127 <small className="mr-2 text-muted font-italic">
133 community={community}
144 let community = this.props.community;
146 <ul class="my-1 list-inline">
147 <li className="list-inline-item badge badge-secondary">
148 {i18n.t('number_online', { count: this.props.online })}
150 <li className="list-inline-item badge badge-secondary">
151 {i18n.t('number_of_subscribers', {
152 count: community.number_of_subscribers,
155 <li className="list-inline-item badge badge-secondary">
156 {i18n.t('number_of_posts', {
157 count: community.number_of_posts,
160 <li className="list-inline-item badge badge-secondary">
161 {i18n.t('number_of_comments', {
162 count: community.number_of_comments,
165 <li className="list-inline-item">
166 <Link className="badge badge-secondary" to="/communities">
167 {community.category_name}
170 <li className="list-inline-item">
172 className="badge badge-secondary"
173 to={`/modlog/community/${this.props.community.id}`}
178 <li className="list-inline-item badge badge-secondary">
179 <CommunityLink community={community} realLink />
187 <ul class="list-inline small">
188 <li class="list-inline-item">{i18n.t('mods')}: </li>
189 {this.props.moderators.map(mod => (
190 <li class="list-inline-item">
194 preferred_username: mod.user_preferred_username,
197 local: mod.user_local,
198 actor_id: mod.user_actor_id,
208 let community = this.props.community;
210 community.subscribed && (
212 className={`btn btn-secondary btn-block mb-2 ${
213 community.deleted || community.removed ? 'no-click' : ''
215 to={`/create_post?community=${community.name}`}
217 {i18n.t('create_a_post')}
224 let community = this.props.community;
227 {!community.subscribed && (
229 class="btn btn-secondary btn-block"
231 onClick={linkEvent(community.id, this.handleSubscribe)}
233 {i18n.t('subscribe')}
241 let community = this.props.community;
243 community.description && (
246 dangerouslySetInnerHTML={mdToHtml(community.description)}
253 let community = this.props.community;
256 <ul class="list-inline mb-1 text-muted font-weight-bold">
259 <li className="list-inline-item-action">
262 onClick={linkEvent(this, this.handleEditClick)}
263 data-tippy-content={i18n.t('edit')}
265 <svg class="icon icon-inline">
266 <use xlinkHref="#icon-edit"></use>
271 (!this.state.showConfirmLeaveModTeam ? (
272 <li className="list-inline-item-action">
277 this.handleShowConfirmLeaveModTeamClick
280 {i18n.t('leave_mod_team')}
285 <li className="list-inline-item-action">
286 {i18n.t('are_you_sure')}
288 <li className="list-inline-item-action">
291 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
296 <li className="list-inline-item-action">
301 this.handleCancelLeaveModTeamClick
310 <li className="list-inline-item-action">
313 onClick={linkEvent(this, this.handleDeleteClick)}
315 !community.deleted ? i18n.t('delete') : i18n.t('restore')
319 class={`icon icon-inline ${
320 community.deleted && 'text-danger'
323 <use xlinkHref="#icon-trash"></use>
331 <li className="list-inline-item">
332 {!this.props.community.removed ? (
335 onClick={linkEvent(this, this.handleModRemoveShow)}
342 onClick={linkEvent(this, this.handleModRemoveSubmit)}
350 {this.state.showRemoveDialog && (
351 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
352 <div class="form-group row">
353 <label class="col-form-label" htmlFor="remove-reason">
359 class="form-control mr-2"
360 placeholder={i18n.t('optional')}
361 value={this.state.removeReason}
362 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
365 {/* TODO hold off on expires for now */}
366 {/* <div class="form-group row"> */}
367 {/* <label class="col-form-label">Expires</label> */}
368 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
370 <div class="form-group row">
371 <button type="submit" class="btn btn-secondary">
372 {i18n.t('remove_community')}
381 handleEditClick(i: Sidebar) {
382 i.state.showEdit = true;
386 handleEditCommunity() {
387 this.state.showEdit = false;
388 this.setState(this.state);
392 this.state.showEdit = false;
393 this.setState(this.state);
396 handleDeleteClick(i: Sidebar, event: any) {
397 event.preventDefault();
398 let deleteForm: DeleteCommunityForm = {
399 edit_id: i.props.community.id,
400 deleted: !i.props.community.deleted,
402 WebSocketService.Instance.deleteCommunity(deleteForm);
405 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
406 i.state.showConfirmLeaveModTeam = true;
410 handleLeaveModTeamClick(i: Sidebar) {
411 let form: AddModToCommunityForm = {
412 user_id: UserService.Instance.user.id,
413 community_id: i.props.community.id,
416 WebSocketService.Instance.addModToCommunity(form);
417 i.state.showConfirmLeaveModTeam = false;
421 handleCancelLeaveModTeamClick(i: Sidebar) {
422 i.state.showConfirmLeaveModTeam = false;
426 handleUnsubscribe(communityId: number, event: any) {
427 event.preventDefault();
428 let form: FollowCommunityForm = {
429 community_id: communityId,
432 WebSocketService.Instance.followCommunity(form);
435 handleSubscribe(communityId: number, event: any) {
436 event.preventDefault();
437 let form: FollowCommunityForm = {
438 community_id: communityId,
441 WebSocketService.Instance.followCommunity(form);
444 private get amCreator(): boolean {
445 return this.props.community.creator_id == UserService.Instance.user.id;
448 get canMod(): boolean {
450 UserService.Instance.user &&
451 this.props.moderators
453 .includes(UserService.Instance.user.id)
457 get canAdmin(): boolean {
459 UserService.Instance.user &&
460 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
464 handleModRemoveShow(i: Sidebar) {
465 i.state.showRemoveDialog = true;
469 handleModRemoveReasonChange(i: Sidebar, event: any) {
470 i.state.removeReason = event.target.value;
474 handleModRemoveExpiresChange(i: Sidebar, event: any) {
475 console.log(event.target.value);
476 i.state.removeExpires = event.target.value;
480 handleModRemoveSubmit(i: Sidebar, event: any) {
481 event.preventDefault();
482 let removeForm: RemoveCommunityForm = {
483 edit_id: i.props.community.id,
484 removed: !i.props.community.removed,
485 reason: i.state.removeReason,
486 expires: getUnixTime(i.state.removeExpires),
488 WebSocketService.Instance.removeCommunity(removeForm);
490 i.state.showRemoveDialog = false;