1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
10 } from '../interfaces';
11 import { WebSocketService, UserService } from '../services';
12 import { mdToHtml, getUnixTime, pictrsAvatarThumbnail } from '../utils';
13 import { CommunityForm } from './community-form';
14 import { UserListing } from './user-listing';
15 import { CommunityLink } from './community-link';
16 import { BannerIconHeader } from './banner-icon-header';
17 import { i18n } from '../i18next';
19 interface SidebarProps {
21 moderators: Array<CommunityUser>;
22 admins: Array<UserView>;
28 interface SidebarState {
30 showRemoveDialog: boolean;
32 removeExpires: string;
35 export class Sidebar extends Component<SidebarProps, SidebarState> {
36 private emptyState: SidebarState = {
38 showRemoveDialog: false,
43 constructor(props: any, context: any) {
44 super(props, context);
45 this.state = this.emptyState;
46 this.handleEditCommunity = this.handleEditCommunity.bind(this);
47 this.handleEditCancel = this.handleEditCancel.bind(this);
53 {!this.state.showEdit ? (
57 community={this.props.community}
58 onEdit={this.handleEditCommunity}
59 onCancel={this.handleEditCancel}
60 enableNsfw={this.props.enableNsfw}
70 <div class="card bg-transparent border-secondary mb-3">
71 <div class="card-header bg-transparent border-secondary">
72 {this.communityTitle()}
75 <div class="card-body">{this.subscribes()}</div>
77 <div class="card bg-transparent border-secondary mb-3">
78 <div class="card-body">
89 let community = this.props.community;
93 {this.props.showIcon && (
94 <BannerIconHeader icon={community.icon} banner={community.banner} />
96 <span>{community.title}</span>
97 {community.removed && (
98 <small className="ml-2 text-muted font-italic">
102 {community.deleted && (
103 <small className="ml-2 text-muted font-italic">
108 <small className="ml-2 text-muted font-italic">
114 community={community}
125 let community = this.props.community;
127 <ul class="my-1 list-inline">
129 <li className="list-inline-item badge badge-light">
130 {i18n.t('number_online', { count: this.props.online })}
133 <li className="list-inline-item badge badge-light">
134 {i18n.t('number_of_subscribers', {
135 count: community.number_of_subscribers,
138 <li className="list-inline-item badge badge-light">
139 {i18n.t('number_of_posts', {
140 count: community.number_of_posts,
143 <li className="list-inline-item badge badge-light">
144 {i18n.t('number_of_comments', {
145 count: community.number_of_comments,
148 <li className="list-inline-item">
149 <Link className="badge badge-light" to="/communities">
150 {community.category_name}
153 <li className="list-inline-item">
155 className="badge badge-light"
156 to={`/modlog/community/${this.props.community.id}`}
161 <li className="list-inline-item badge badge-light">
162 <CommunityLink community={community} realLink />
170 <ul class="list-inline small">
171 <li class="list-inline-item">{i18n.t('mods')}: </li>
172 {this.props.moderators.map(mod => (
173 <li class="list-inline-item">
177 preferred_username: mod.user_preferred_username,
180 local: mod.user_local,
181 actor_id: mod.user_actor_id,
191 let community = this.props.community;
193 <div class="d-flex flex-wrap">
195 class={`btn btn-secondary flex-fill mr-2 mb-2 ${
196 community.deleted || community.removed ? 'no-click' : ''
198 to={`/create_post?community=${community.name}`}
200 {i18n.t('create_a_post')}
202 {community.subscribed ? (
204 class="btn btn-secondary flex-fill mb-2"
206 onClick={linkEvent(community.id, this.handleUnsubscribe)}
208 {i18n.t('unsubscribe')}
212 class="btn btn-secondary flex-fill mb-2"
214 onClick={linkEvent(community.id, this.handleSubscribe)}
216 {i18n.t('subscribe')}
224 let community = this.props.community;
226 community.description && (
229 dangerouslySetInnerHTML={mdToHtml(community.description)}
236 let community = this.props.community;
239 <ul class="list-inline mb-1 text-muted font-weight-bold">
242 <li className="list-inline-item-action">
245 onClick={linkEvent(this, this.handleEditClick)}
246 data-tippy-content={i18n.t('edit')}
248 <svg class="icon icon-inline">
249 <use xlinkHref="#icon-edit"></use>
254 <li className="list-inline-item-action">
257 onClick={linkEvent(this, this.handleDeleteClick)}
259 !community.deleted ? i18n.t('delete') : i18n.t('restore')
263 class={`icon icon-inline ${
264 community.deleted && 'text-danger'
267 <use xlinkHref="#icon-trash"></use>
275 <li className="list-inline-item">
276 {!this.props.community.removed ? (
279 onClick={linkEvent(this, this.handleModRemoveShow)}
286 onClick={linkEvent(this, this.handleModRemoveSubmit)}
294 {this.state.showRemoveDialog && (
295 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
296 <div class="form-group row">
297 <label class="col-form-label" htmlFor="remove-reason">
303 class="form-control mr-2"
304 placeholder={i18n.t('optional')}
305 value={this.state.removeReason}
306 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
309 {/* TODO hold off on expires for now */}
310 {/* <div class="form-group row"> */}
311 {/* <label class="col-form-label">Expires</label> */}
312 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
314 <div class="form-group row">
315 <button type="submit" class="btn btn-secondary">
316 {i18n.t('remove_community')}
325 handleEditClick(i: Sidebar) {
326 i.state.showEdit = true;
330 handleEditCommunity() {
331 this.state.showEdit = false;
332 this.setState(this.state);
336 this.state.showEdit = false;
337 this.setState(this.state);
340 handleDeleteClick(i: Sidebar) {
341 event.preventDefault();
342 let deleteForm: DeleteCommunityForm = {
343 edit_id: i.props.community.id,
344 deleted: !i.props.community.deleted,
346 WebSocketService.Instance.deleteCommunity(deleteForm);
349 handleUnsubscribe(communityId: number) {
350 event.preventDefault();
351 let form: FollowCommunityForm = {
352 community_id: communityId,
355 WebSocketService.Instance.followCommunity(form);
358 handleSubscribe(communityId: number) {
359 event.preventDefault();
360 let form: FollowCommunityForm = {
361 community_id: communityId,
364 WebSocketService.Instance.followCommunity(form);
367 private get amCreator(): boolean {
368 return this.props.community.creator_id == UserService.Instance.user.id;
371 get canMod(): boolean {
373 UserService.Instance.user &&
374 this.props.moderators
376 .includes(UserService.Instance.user.id)
380 get canAdmin(): boolean {
382 UserService.Instance.user &&
383 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
387 handleModRemoveShow(i: Sidebar) {
388 i.state.showRemoveDialog = true;
392 handleModRemoveReasonChange(i: Sidebar, event: any) {
393 i.state.removeReason = event.target.value;
397 handleModRemoveExpiresChange(i: Sidebar, event: any) {
398 console.log(event.target.value);
399 i.state.removeExpires = event.target.value;
403 handleModRemoveSubmit(i: Sidebar) {
404 event.preventDefault();
405 let removeForm: RemoveCommunityForm = {
406 edit_id: i.props.community.id,
407 removed: !i.props.community.removed,
408 reason: i.state.removeReason,
409 expires: getUnixTime(i.state.removeExpires),
411 WebSocketService.Instance.removeCommunity(removeForm);
413 i.state.showRemoveDialog = false;