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}`}
184 <ul class="list-inline small">
185 <li class="list-inline-item">{i18n.t('mods')}: </li>
186 {this.props.moderators.map(mod => (
187 <li class="list-inline-item">
191 preferred_username: mod.user_preferred_username,
194 local: mod.user_local,
195 actor_id: mod.user_actor_id,
205 let community = this.props.community;
207 community.subscribed && (
209 className={`btn btn-secondary btn-block mb-2 ${
210 community.deleted || community.removed ? 'no-click' : ''
212 to={`/create_post?community=${community.name}`}
214 {i18n.t('create_a_post')}
221 let community = this.props.community;
224 {!community.subscribed && (
226 class="btn btn-secondary btn-block"
228 onClick={linkEvent(community.id, this.handleSubscribe)}
230 {i18n.t('subscribe')}
238 let community = this.props.community;
240 community.description && (
243 dangerouslySetInnerHTML={mdToHtml(community.description)}
250 let community = this.props.community;
253 <ul class="list-inline mb-1 text-muted font-weight-bold">
256 <li className="list-inline-item-action">
259 onClick={linkEvent(this, this.handleEditClick)}
260 data-tippy-content={i18n.t('edit')}
262 <svg class="icon icon-inline">
263 <use xlinkHref="#icon-edit"></use>
268 (!this.state.showConfirmLeaveModTeam ? (
269 <li className="list-inline-item-action">
274 this.handleShowConfirmLeaveModTeamClick
277 {i18n.t('leave_mod_team')}
282 <li className="list-inline-item-action">
283 {i18n.t('are_you_sure')}
285 <li className="list-inline-item-action">
288 onClick={linkEvent(this, this.handleLeaveModTeamClick)}
293 <li className="list-inline-item-action">
298 this.handleCancelLeaveModTeamClick
307 <li className="list-inline-item-action">
310 onClick={linkEvent(this, this.handleDeleteClick)}
312 !community.deleted ? i18n.t('delete') : i18n.t('restore')
316 class={`icon icon-inline ${
317 community.deleted && 'text-danger'
320 <use xlinkHref="#icon-trash"></use>
328 <li className="list-inline-item">
329 {!this.props.community.removed ? (
332 onClick={linkEvent(this, this.handleModRemoveShow)}
339 onClick={linkEvent(this, this.handleModRemoveSubmit)}
347 {this.state.showRemoveDialog && (
348 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
349 <div class="form-group row">
350 <label class="col-form-label" htmlFor="remove-reason">
356 class="form-control mr-2"
357 placeholder={i18n.t('optional')}
358 value={this.state.removeReason}
359 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
362 {/* TODO hold off on expires for now */}
363 {/* <div class="form-group row"> */}
364 {/* <label class="col-form-label">Expires</label> */}
365 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
367 <div class="form-group row">
368 <button type="submit" class="btn btn-secondary">
369 {i18n.t('remove_community')}
378 handleEditClick(i: Sidebar) {
379 i.state.showEdit = true;
383 handleEditCommunity() {
384 this.state.showEdit = false;
385 this.setState(this.state);
389 this.state.showEdit = false;
390 this.setState(this.state);
393 handleDeleteClick(i: Sidebar, event: any) {
394 event.preventDefault();
395 let deleteForm: DeleteCommunityForm = {
396 edit_id: i.props.community.id,
397 deleted: !i.props.community.deleted,
399 WebSocketService.Instance.deleteCommunity(deleteForm);
402 handleShowConfirmLeaveModTeamClick(i: Sidebar) {
403 i.state.showConfirmLeaveModTeam = true;
407 handleLeaveModTeamClick(i: Sidebar) {
408 let form: AddModToCommunityForm = {
409 user_id: UserService.Instance.user.id,
410 community_id: i.props.community.id,
413 WebSocketService.Instance.addModToCommunity(form);
414 i.state.showConfirmLeaveModTeam = false;
418 handleCancelLeaveModTeamClick(i: Sidebar) {
419 i.state.showConfirmLeaveModTeam = false;
423 handleUnsubscribe(communityId: number, event: any) {
424 event.preventDefault();
425 let form: FollowCommunityForm = {
426 community_id: communityId,
429 WebSocketService.Instance.followCommunity(form);
432 handleSubscribe(communityId: number, event: any) {
433 event.preventDefault();
434 let form: FollowCommunityForm = {
435 community_id: communityId,
438 WebSocketService.Instance.followCommunity(form);
441 private get amCreator(): boolean {
442 return this.props.community.creator_id == UserService.Instance.user.id;
445 get canMod(): boolean {
447 UserService.Instance.user &&
448 this.props.moderators
450 .includes(UserService.Instance.user.id)
454 get canAdmin(): boolean {
456 UserService.Instance.user &&
457 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
461 handleModRemoveShow(i: Sidebar) {
462 i.state.showRemoveDialog = true;
466 handleModRemoveReasonChange(i: Sidebar, event: any) {
467 i.state.removeReason = event.target.value;
471 handleModRemoveExpiresChange(i: Sidebar, event: any) {
472 console.log(event.target.value);
473 i.state.removeExpires = event.target.value;
477 handleModRemoveSubmit(i: Sidebar, event: any) {
478 event.preventDefault();
479 let removeForm: RemoveCommunityForm = {
480 edit_id: i.props.community.id,
481 removed: !i.props.community.removed,
482 reason: i.state.removeReason,
483 expires: getUnixTime(i.state.removeExpires),
485 WebSocketService.Instance.removeCommunity(removeForm);
487 i.state.showRemoveDialog = false;