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 bg-transparent border-secondary mb-3">
77 <div class="card-header bg-transparent border-secondary">
78 {this.communityTitle()}
81 <div class="card-body">{this.subscribes()}</div>
83 <div class="card bg-transparent border-secondary mb-3">
84 <div class="card-body">
95 let community = this.props.community;
99 {this.props.showIcon && (
100 <BannerIconHeader icon={community.icon} banner={community.banner} />
102 <span>{community.title}</span>
103 {community.removed && (
104 <small className="ml-2 text-muted font-italic">
108 {community.deleted && (
109 <small className="ml-2 text-muted font-italic">
114 <small className="ml-2 text-muted font-italic">
120 community={community}
131 let community = this.props.community;
133 <ul class="my-1 list-inline">
134 <li className="list-inline-item badge badge-light">
135 {i18n.t('number_online', { count: this.props.online })}
137 <li className="list-inline-item badge badge-light">
138 {i18n.t('number_of_subscribers', {
139 count: community.number_of_subscribers,
142 <li className="list-inline-item badge badge-light">
143 {i18n.t('number_of_posts', {
144 count: community.number_of_posts,
147 <li className="list-inline-item badge badge-light">
148 {i18n.t('number_of_comments', {
149 count: community.number_of_comments,
152 <li className="list-inline-item">
153 <Link className="badge badge-light" to="/communities">
154 {community.category_name}
157 <li className="list-inline-item">
159 className="badge badge-light"
160 to={`/modlog/community/${this.props.community.id}`}
165 <li className="list-inline-item badge badge-light">
166 <CommunityLink community={community} realLink />
174 <ul class="list-inline small">
175 <li class="list-inline-item">{i18n.t('mods')}: </li>
176 {this.props.moderators.map(mod => (
177 <li class="list-inline-item">
181 preferred_username: mod.user_preferred_username,
184 local: mod.user_local,
185 actor_id: mod.user_actor_id,
195 let community = this.props.community;
197 <div class="d-flex flex-wrap">
199 className={`btn btn-secondary flex-fill mr-2 mb-2 ${
200 community.deleted || community.removed ? 'no-click' : ''
202 to={`/create_post?community=${community.name}`}
204 {i18n.t('create_a_post')}
206 {community.subscribed ? (
208 class="btn btn-secondary flex-fill mb-2"
210 onClick={linkEvent(community.id, this.handleUnsubscribe)}
212 {i18n.t('unsubscribe')}
216 class="btn btn-secondary flex-fill mb-2"
218 onClick={linkEvent(community.id, this.handleSubscribe)}
220 {i18n.t('subscribe')}
228 let community = this.props.community;
230 community.description && (
233 dangerouslySetInnerHTML={mdToHtml(community.description)}
240 let community = this.props.community;
243 <ul class="list-inline mb-1 text-muted font-weight-bold">
246 <li className="list-inline-item-action">
249 onClick={linkEvent(this, this.handleEditClick)}
250 data-tippy-content={i18n.t('edit')}
252 <svg class="icon icon-inline">
253 <use xlinkHref="#icon-edit"></use>
258 (!this.state.showConfirmLeaveModTeam ? (
259 <li className="list-inline-item-action">
264 this.handleShowConfirmLeaveModTeamClick
267 {i18n.t('leave_mod_team')}
272 <li className="list-inline-item-action">
273 {i18n.t('are_you_sure')}
275 <li className="list-inline-item-action">
278 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
283 <li className="list-inline-item-action">
288 this.handleCancelLeaveModTeamClick
297 <li className="list-inline-item-action">
300 onClick={linkEvent(this, this.handleDeleteClick)}
302 !community.deleted ? i18n.t('delete') : i18n.t('restore')
306 class={`icon icon-inline ${
307 community.deleted && 'text-danger'
310 <use xlinkHref="#icon-trash"></use>
318 <li className="list-inline-item">
319 {!this.props.community.removed ? (
322 onClick={linkEvent(this, this.handleModRemoveShow)}
329 onClick={linkEvent(this, this.handleModRemoveSubmit)}
337 {this.state.showRemoveDialog && (
338 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
339 <div class="form-group row">
340 <label class="col-form-label" htmlFor="remove-reason">
346 class="form-control mr-2"
347 placeholder={i18n.t('optional')}
348 value={this.state.removeReason}
349 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
352 {/* TODO hold off on expires for now */}
353 {/* <div class="form-group row"> */}
354 {/* <label class="col-form-label">Expires</label> */}
355 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
357 <div class="form-group row">
358 <button type="submit" class="btn btn-secondary">
359 {i18n.t('remove_community')}
368 handleEditClick(i: Sidebar) {
369 i.state.showEdit = true;
373 handleEditCommunity() {
374 this.state.showEdit = false;
375 this.setState(this.state);
379 this.state.showEdit = false;
380 this.setState(this.state);
383 handleDeleteClick(i: Sidebar) {
384 event.preventDefault();
385 let deleteForm: DeleteCommunityForm = {
386 edit_id: i.props.community.id,
387 deleted: !i.props.community.deleted,
389 WebSocketService.Instance.deleteCommunity(deleteForm);
392 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
393 i.state.showConfirmLeaveModTeam = true;
397 handleLeaveModTeamClick(i: Sidebar) {
398 let form: AddModToCommunityForm = {
399 user_id: UserService.Instance.user.id,
400 community_id: i.props.community.id,
403 WebSocketService.Instance.addModToCommunity(form);
404 i.state.showConfirmLeaveModTeam = false;
408 handleCancelLeaveModTeamClick(i: Sidebar) {
409 i.state.showConfirmLeaveModTeam = false;
413 handleUnsubscribe(communityId: number) {
414 event.preventDefault();
415 let form: FollowCommunityForm = {
416 community_id: communityId,
419 WebSocketService.Instance.followCommunity(form);
422 handleSubscribe(communityId: number) {
423 event.preventDefault();
424 let form: FollowCommunityForm = {
425 community_id: communityId,
428 WebSocketService.Instance.followCommunity(form);
431 private get amCreator(): boolean {
432 return this.props.community.creator_id == UserService.Instance.user.id;
435 get canMod(): boolean {
437 UserService.Instance.user &&
438 this.props.moderators
440 .includes(UserService.Instance.user.id)
444 get canAdmin(): boolean {
446 UserService.Instance.user &&
447 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
451 handleModRemoveShow(i: Sidebar) {
452 i.state.showRemoveDialog = true;
456 handleModRemoveReasonChange(i: Sidebar, event: any) {
457 i.state.removeReason = event.target.value;
461 handleModRemoveExpiresChange(i: Sidebar, event: any) {
462 console.log(event.target.value);
463 i.state.removeExpires = event.target.value;
467 handleModRemoveSubmit(i: Sidebar) {
468 event.preventDefault();
469 let removeForm: RemoveCommunityForm = {
470 edit_id: i.props.community.id,
471 removed: !i.props.community.removed,
472 reason: i.state.removeReason,
473 expires: getUnixTime(i.state.removeExpires),
475 WebSocketService.Instance.removeCommunity(removeForm);
477 i.state.showRemoveDialog = false;