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 } from '../utils';
13 import { CommunityForm } from './community-form';
14 import { UserListing } from './user-listing';
15 import { CommunityLink } from './community-link';
16 import { i18n } from '../i18next';
18 interface SidebarProps {
20 moderators: Array<CommunityUser>;
21 admins: Array<UserView>;
26 interface SidebarState {
28 showRemoveDialog: boolean;
30 removeExpires: string;
33 export class Sidebar extends Component<SidebarProps, SidebarState> {
34 private emptyState: SidebarState = {
36 showRemoveDialog: false,
41 constructor(props: any, context: any) {
42 super(props, context);
43 this.state = this.emptyState;
44 this.handleEditCommunity = this.handleEditCommunity.bind(this);
45 this.handleEditCancel = this.handleEditCancel.bind(this);
51 {!this.state.showEdit ? (
55 community={this.props.community}
56 onEdit={this.handleEditCommunity}
57 onCancel={this.handleEditCancel}
58 enableNsfw={this.props.enableNsfw}
68 <div class="card bg-transparent border-secondary mb-3">
69 <div class="card-header bg-transparent border-secondary">
70 {this.communityTitle()}
73 <div class="card-body">{this.subscribes()}</div>
75 <div class="card bg-transparent border-secondary mb-3">
76 <div class="card-body">
87 let community = this.props.community;
90 <span>{community.title}</span>
91 {community.removed && (
92 <small className="ml-2 text-muted font-italic">
96 {community.deleted && (
97 <small className="ml-2 text-muted font-italic">
102 <small className="ml-2 text-muted font-italic">
111 let community = this.props.community;
113 <ul class="my-1 list-inline">
115 <li className="list-inline-item badge badge-light">
116 {i18n.t('number_online', { count: this.props.online })}
119 <li className="list-inline-item badge badge-light">
120 {i18n.t('number_of_subscribers', {
121 count: community.number_of_subscribers,
124 <li className="list-inline-item badge badge-light">
125 {i18n.t('number_of_posts', {
126 count: community.number_of_posts,
129 <li className="list-inline-item badge badge-light">
130 {i18n.t('number_of_comments', {
131 count: community.number_of_comments,
134 <li className="list-inline-item">
135 <Link className="badge badge-light" to="/communities">
136 {community.category_name}
139 <li className="list-inline-item">
141 className="badge badge-light"
142 to={`/modlog/community/${this.props.community.id}`}
147 <li className="list-inline-item badge badge-light">
148 <CommunityLink community={community} realLink />
156 <ul class="list-inline small">
157 <li class="list-inline-item">{i18n.t('mods')}: </li>
158 {this.props.moderators.map(mod => (
159 <li class="list-inline-item">
165 local: mod.user_local,
166 actor_id: mod.user_actor_id,
176 let community = this.props.community;
178 <div class="d-flex flex-wrap">
180 class={`btn btn-secondary flex-fill mr-2 mb-2 ${
181 community.deleted || community.removed ? 'no-click' : ''
183 to={`/create_post?community=${community.name}`}
185 {i18n.t('create_a_post')}
187 {community.subscribed ? (
189 class="btn btn-secondary flex-fill mb-2"
191 onClick={linkEvent(community.id, this.handleUnsubscribe)}
193 {i18n.t('unsubscribe')}
197 class="btn btn-secondary flex-fill mb-2"
199 onClick={linkEvent(community.id, this.handleSubscribe)}
201 {i18n.t('subscribe')}
209 let community = this.props.community;
211 community.description && (
214 dangerouslySetInnerHTML={mdToHtml(community.description)}
221 let community = this.props.community;
224 <ul class="list-inline mb-1 text-muted font-weight-bold">
227 <li className="list-inline-item-action">
230 onClick={linkEvent(this, this.handleEditClick)}
231 data-tippy-content={i18n.t('edit')}
233 <svg class="icon icon-inline">
234 <use xlinkHref="#icon-edit"></use>
239 <li className="list-inline-item-action">
242 onClick={linkEvent(this, this.handleDeleteClick)}
244 !community.deleted ? i18n.t('delete') : i18n.t('restore')
248 class={`icon icon-inline ${
249 community.deleted && 'text-danger'
252 <use xlinkHref="#icon-trash"></use>
260 <li className="list-inline-item">
261 {!this.props.community.removed ? (
264 onClick={linkEvent(this, this.handleModRemoveShow)}
271 onClick={linkEvent(this, this.handleModRemoveSubmit)}
279 {this.state.showRemoveDialog && (
280 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
281 <div class="form-group row">
282 <label class="col-form-label" htmlFor="remove-reason">
288 class="form-control mr-2"
289 placeholder={i18n.t('optional')}
290 value={this.state.removeReason}
291 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
294 {/* TODO hold off on expires for now */}
295 {/* <div class="form-group row"> */}
296 {/* <label class="col-form-label">Expires</label> */}
297 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
299 <div class="form-group row">
300 <button type="submit" class="btn btn-secondary">
301 {i18n.t('remove_community')}
310 handleEditClick(i: Sidebar) {
311 i.state.showEdit = true;
315 handleEditCommunity() {
316 this.state.showEdit = false;
317 this.setState(this.state);
321 this.state.showEdit = false;
322 this.setState(this.state);
325 handleDeleteClick(i: Sidebar) {
326 event.preventDefault();
327 let deleteForm: DeleteCommunityForm = {
328 edit_id: i.props.community.id,
329 deleted: !i.props.community.deleted,
331 WebSocketService.Instance.deleteCommunity(deleteForm);
334 handleUnsubscribe(communityId: number) {
335 event.preventDefault();
336 let form: FollowCommunityForm = {
337 community_id: communityId,
340 WebSocketService.Instance.followCommunity(form);
343 handleSubscribe(communityId: number) {
344 event.preventDefault();
345 let form: FollowCommunityForm = {
346 community_id: communityId,
349 WebSocketService.Instance.followCommunity(form);
352 private get amCreator(): boolean {
353 return this.props.community.creator_id == UserService.Instance.user.id;
356 get canMod(): boolean {
358 UserService.Instance.user &&
359 this.props.moderators
361 .includes(UserService.Instance.user.id)
365 get canAdmin(): boolean {
367 UserService.Instance.user &&
368 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
372 handleModRemoveShow(i: Sidebar) {
373 i.state.showRemoveDialog = true;
377 handleModRemoveReasonChange(i: Sidebar, event: any) {
378 i.state.removeReason = event.target.value;
382 handleModRemoveExpiresChange(i: Sidebar, event: any) {
383 console.log(event.target.value);
384 i.state.removeExpires = event.target.value;
388 handleModRemoveSubmit(i: Sidebar) {
389 event.preventDefault();
390 let removeForm: RemoveCommunityForm = {
391 edit_id: i.props.community.id,
392 removed: !i.props.community.removed,
393 reason: i.state.removeReason,
394 expires: getUnixTime(i.state.removeExpires),
396 WebSocketService.Instance.removeCommunity(removeForm);
398 i.state.showRemoveDialog = false;