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">
128 <li className="list-inline-item badge badge-light">
129 {i18n.t('number_online', { count: this.props.online })}
131 <li className="list-inline-item badge badge-light">
132 {i18n.t('number_of_subscribers', {
133 count: community.number_of_subscribers,
136 <li className="list-inline-item badge badge-light">
137 {i18n.t('number_of_posts', {
138 count: community.number_of_posts,
141 <li className="list-inline-item badge badge-light">
142 {i18n.t('number_of_comments', {
143 count: community.number_of_comments,
146 <li className="list-inline-item">
147 <Link className="badge badge-light" to="/communities">
148 {community.category_name}
151 <li className="list-inline-item">
153 className="badge badge-light"
154 to={`/modlog/community/${this.props.community.id}`}
159 <li className="list-inline-item badge badge-light">
160 <CommunityLink community={community} realLink />
168 <ul class="list-inline small">
169 <li class="list-inline-item">{i18n.t('mods')}: </li>
170 {this.props.moderators.map(mod => (
171 <li class="list-inline-item">
175 preferred_username: mod.user_preferred_username,
178 local: mod.user_local,
179 actor_id: mod.user_actor_id,
189 let community = this.props.community;
191 <div class="d-flex flex-wrap">
193 class={`btn btn-secondary flex-fill mr-2 mb-2 ${
194 community.deleted || community.removed ? 'no-click' : ''
196 to={`/create_post?community=${community.name}`}
198 {i18n.t('create_a_post')}
200 {community.subscribed ? (
202 class="btn btn-secondary flex-fill mb-2"
204 onClick={linkEvent(community.id, this.handleUnsubscribe)}
206 {i18n.t('unsubscribe')}
210 class="btn btn-secondary flex-fill mb-2"
212 onClick={linkEvent(community.id, this.handleSubscribe)}
214 {i18n.t('subscribe')}
222 let community = this.props.community;
224 community.description && (
227 dangerouslySetInnerHTML={mdToHtml(community.description)}
234 let community = this.props.community;
237 <ul class="list-inline mb-1 text-muted font-weight-bold">
240 <li className="list-inline-item-action">
243 onClick={linkEvent(this, this.handleEditClick)}
244 data-tippy-content={i18n.t('edit')}
246 <svg class="icon icon-inline">
247 <use xlinkHref="#icon-edit"></use>
252 <li className="list-inline-item-action">
255 onClick={linkEvent(this, this.handleDeleteClick)}
257 !community.deleted ? i18n.t('delete') : i18n.t('restore')
261 class={`icon icon-inline ${
262 community.deleted && 'text-danger'
265 <use xlinkHref="#icon-trash"></use>
273 <li className="list-inline-item">
274 {!this.props.community.removed ? (
277 onClick={linkEvent(this, this.handleModRemoveShow)}
284 onClick={linkEvent(this, this.handleModRemoveSubmit)}
292 {this.state.showRemoveDialog && (
293 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
294 <div class="form-group row">
295 <label class="col-form-label" htmlFor="remove-reason">
301 class="form-control mr-2"
302 placeholder={i18n.t('optional')}
303 value={this.state.removeReason}
304 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
307 {/* TODO hold off on expires for now */}
308 {/* <div class="form-group row"> */}
309 {/* <label class="col-form-label">Expires</label> */}
310 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
312 <div class="form-group row">
313 <button type="submit" class="btn btn-secondary">
314 {i18n.t('remove_community')}
323 handleEditClick(i: Sidebar) {
324 i.state.showEdit = true;
328 handleEditCommunity() {
329 this.state.showEdit = false;
330 this.setState(this.state);
334 this.state.showEdit = false;
335 this.setState(this.state);
338 handleDeleteClick(i: Sidebar) {
339 event.preventDefault();
340 let deleteForm: DeleteCommunityForm = {
341 edit_id: i.props.community.id,
342 deleted: !i.props.community.deleted,
344 WebSocketService.Instance.deleteCommunity(deleteForm);
347 handleUnsubscribe(communityId: number) {
348 event.preventDefault();
349 let form: FollowCommunityForm = {
350 community_id: communityId,
353 WebSocketService.Instance.followCommunity(form);
356 handleSubscribe(communityId: number) {
357 event.preventDefault();
358 let form: FollowCommunityForm = {
359 community_id: communityId,
362 WebSocketService.Instance.followCommunity(form);
365 private get amCreator(): boolean {
366 return this.props.community.creator_id == UserService.Instance.user.id;
369 get canMod(): boolean {
371 UserService.Instance.user &&
372 this.props.moderators
374 .includes(UserService.Instance.user.id)
378 get canAdmin(): boolean {
380 UserService.Instance.user &&
381 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
385 handleModRemoveShow(i: Sidebar) {
386 i.state.showRemoveDialog = true;
390 handleModRemoveReasonChange(i: Sidebar, event: any) {
391 i.state.removeReason = event.target.value;
395 handleModRemoveExpiresChange(i: Sidebar, event: any) {
396 console.log(event.target.value);
397 i.state.removeExpires = event.target.value;
401 handleModRemoveSubmit(i: Sidebar) {
402 event.preventDefault();
403 let removeForm: RemoveCommunityForm = {
404 edit_id: i.props.community.id,
405 removed: !i.props.community.removed,
406 reason: i.state.removeReason,
407 expires: getUnixTime(i.state.removeExpires),
409 WebSocketService.Instance.removeCommunity(removeForm);
411 i.state.showRemoveDialog = false;