1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
7 CommunityForm as CommunityFormI,
9 } from '../interfaces';
10 import { WebSocketService, UserService } from '../services';
11 import { mdToHtml, getUnixTime, hostname } from '../utils';
12 import { CommunityForm } from './community-form';
13 import { UserListing } from './user-listing';
14 import { CommunityLink } from './community-link';
15 import { i18n } from '../i18next';
17 interface SidebarProps {
19 moderators: Array<CommunityUser>;
20 admins: Array<UserView>;
25 interface SidebarState {
27 showRemoveDialog: boolean;
29 removeExpires: string;
32 export class Sidebar extends Component<SidebarProps, SidebarState> {
33 private emptyState: SidebarState = {
35 showRemoveDialog: false,
40 constructor(props: any, context: any) {
41 super(props, context);
42 this.state = this.emptyState;
43 this.handleEditCommunity = this.handleEditCommunity.bind(this);
44 this.handleEditCancel = this.handleEditCancel.bind(this);
50 {!this.state.showEdit ? (
54 community={this.props.community}
55 onEdit={this.handleEditCommunity}
56 onCancel={this.handleEditCancel}
57 enableNsfw={this.props.enableNsfw}
65 let community = this.props.community;
66 let name_: string, link: string;
68 if (community.local) {
69 name_ = community.name;
70 link = `/c/${community.name}`;
72 name_ = `${community.name}@${hostname(community.actor_id)}`;
73 link = community.actor_id;
77 <div class="card border-secondary mb-3">
78 <div class="card-body">
80 <span>{community.title}</span>
81 {community.removed && (
82 <small className="ml-2 text-muted font-italic">
86 {community.deleted && (
87 <small className="ml-2 text-muted font-italic">
92 <CommunityLink community={community} realLink />
93 <ul class="list-inline mb-1 text-muted font-weight-bold">
96 <li className="list-inline-item-action">
99 onClick={linkEvent(this, this.handleEditClick)}
100 data-tippy-content={i18n.t('edit')}
102 <svg class="icon icon-inline">
103 <use xlinkHref="#icon-edit"></use>
108 <li className="list-inline-item-action">
111 onClick={linkEvent(this, this.handleDeleteClick)}
119 class={`icon icon-inline ${
120 community.deleted && 'text-danger'
123 <use xlinkHref="#icon-trash"></use>
131 <li className="list-inline-item">
132 {!this.props.community.removed ? (
135 onClick={linkEvent(this, this.handleModRemoveShow)}
142 onClick={linkEvent(this, this.handleModRemoveSubmit)}
150 {this.state.showRemoveDialog && (
151 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
152 <div class="form-group row">
153 <label class="col-form-label" htmlFor="remove-reason">
159 class="form-control mr-2"
160 placeholder={i18n.t('optional')}
161 value={this.state.removeReason}
162 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
165 {/* TODO hold off on expires for now */}
166 {/* <div class="form-group row"> */}
167 {/* <label class="col-form-label">Expires</label> */}
168 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
170 <div class="form-group row">
171 <button type="submit" class="btn btn-secondary">
172 {i18n.t('remove_community')}
177 <ul class="my-1 list-inline">
179 <li className="list-inline-item badge badge-secondary">
180 {i18n.t('number_online', { count: this.props.online })}
183 <li className="list-inline-item badge badge-secondary">
184 {i18n.t('number_of_subscribers', {
185 count: community.number_of_subscribers,
188 <li className="list-inline-item badge badge-secondary">
189 {i18n.t('number_of_posts', {
190 count: community.number_of_posts,
193 <li className="list-inline-item badge badge-secondary">
194 {i18n.t('number_of_comments', {
195 count: community.number_of_comments,
198 <li className="list-inline-item">
199 <Link className="badge badge-secondary" to="/communities">
200 {community.category_name}
203 <li className="list-inline-item">
205 className="badge badge-secondary"
206 to={`/modlog/community/${this.props.community.id}`}
212 <ul class="list-inline small">
213 <li class="list-inline-item">{i18n.t('mods')}: </li>
214 {this.props.moderators.map(mod => (
215 <li class="list-inline-item">
221 local: mod.user_local,
222 actor_id: mod.user_actor_id,
228 {/* TODO the to= needs to be able to handle community_ids as well, since they're federated */}
230 class={`btn btn-sm btn-secondary btn-block mb-3 ${
231 (community.deleted || community.removed) && 'no-click'
233 to={`/create_post?community=${community.name}`}
235 {i18n.t('create_a_post')}
238 {community.subscribed ? (
240 class="btn btn-sm btn-secondary btn-block"
241 onClick={linkEvent(community.id, this.handleUnsubscribe)}
243 {i18n.t('unsubscribe')}
247 class="btn btn-sm btn-secondary btn-block"
248 onClick={linkEvent(community.id, this.handleSubscribe)}
250 {i18n.t('subscribe')}
256 {community.description && (
257 <div class="card border-secondary">
258 <div class="card-body">
261 dangerouslySetInnerHTML={mdToHtml(community.description)}
270 handleEditClick(i: Sidebar) {
271 i.state.showEdit = true;
275 handleEditCommunity() {
276 this.state.showEdit = false;
277 this.setState(this.state);
281 this.state.showEdit = false;
282 this.setState(this.state);
285 handleDeleteClick(i: Sidebar) {
286 event.preventDefault();
287 let deleteForm: CommunityFormI = {
288 name: i.props.community.name,
289 title: i.props.community.title,
290 category_id: i.props.community.category_id,
291 edit_id: i.props.community.id,
292 deleted: !i.props.community.deleted,
293 nsfw: i.props.community.nsfw,
296 WebSocketService.Instance.editCommunity(deleteForm);
299 handleUnsubscribe(communityId: number) {
300 let form: FollowCommunityForm = {
301 community_id: communityId,
304 WebSocketService.Instance.followCommunity(form);
307 handleSubscribe(communityId: number) {
308 let form: FollowCommunityForm = {
309 community_id: communityId,
312 WebSocketService.Instance.followCommunity(form);
315 private get amCreator(): boolean {
316 return this.props.community.creator_id == UserService.Instance.user.id;
319 get canMod(): boolean {
321 UserService.Instance.user &&
322 this.props.moderators
324 .includes(UserService.Instance.user.id)
328 get canAdmin(): boolean {
330 UserService.Instance.user &&
331 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
335 handleModRemoveShow(i: Sidebar) {
336 i.state.showRemoveDialog = true;
340 handleModRemoveReasonChange(i: Sidebar, event: any) {
341 i.state.removeReason = event.target.value;
345 handleModRemoveExpiresChange(i: Sidebar, event: any) {
346 console.log(event.target.value);
347 i.state.removeExpires = event.target.value;
351 handleModRemoveSubmit(i: Sidebar) {
352 event.preventDefault();
353 let deleteForm: CommunityFormI = {
354 name: i.props.community.name,
355 title: i.props.community.title,
356 category_id: i.props.community.category_id,
357 edit_id: i.props.community.id,
358 removed: !i.props.community.removed,
359 reason: i.state.removeReason,
360 expires: getUnixTime(i.state.removeExpires),
361 nsfw: i.props.community.nsfw,
364 WebSocketService.Instance.editCommunity(deleteForm);
366 i.state.showRemoveDialog = false;