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 { i18n } from '../i18next';
16 interface SidebarProps {
18 moderators: Array<CommunityUser>;
19 admins: Array<UserView>;
23 interface SidebarState {
25 showRemoveDialog: boolean;
27 removeExpires: string;
30 export class Sidebar extends Component<SidebarProps, SidebarState> {
31 private emptyState: SidebarState = {
33 showRemoveDialog: false,
38 constructor(props: any, context: any) {
39 super(props, context);
40 this.state = this.emptyState;
41 this.handleEditCommunity = this.handleEditCommunity.bind(this);
42 this.handleEditCancel = this.handleEditCancel.bind(this);
48 {!this.state.showEdit ? (
52 community={this.props.community}
53 onEdit={this.handleEditCommunity}
54 onCancel={this.handleEditCancel}
62 let community = this.props.community;
63 let name_: string, link: string;
65 if (community.local) {
66 name_ = community.name;
67 link = `/c/${community.name}`;
69 name_ = `${hostname(community.actor_id)}/${community.name}`;
70 link = community.actor_id;
74 <div class="card border-secondary mb-3">
75 <div class="card-body">
77 <span>{community.title}</span>
78 {community.removed && (
79 <small className="ml-2 text-muted font-italic">
83 {community.deleted && (
84 <small className="ml-2 text-muted font-italic">
90 <Link className="text-muted" to={link}>
94 <a className="text-muted" href={link} target="_blank">
98 <ul class="list-inline mb-1 text-muted font-weight-bold">
101 <li className="list-inline-item-action">
104 onClick={linkEvent(this, this.handleEditClick)}
105 data-tippy-content={i18n.t('edit')}
107 <svg class="icon icon-inline">
108 <use xlinkHref="#icon-edit"></use>
113 <li className="list-inline-item-action">
116 onClick={linkEvent(this, this.handleDeleteClick)}
124 class={`icon icon-inline ${
125 community.deleted && 'text-danger'
128 <use xlinkHref="#icon-trash"></use>
136 <li className="list-inline-item">
137 {!this.props.community.removed ? (
140 onClick={linkEvent(this, this.handleModRemoveShow)}
147 onClick={linkEvent(this, this.handleModRemoveSubmit)}
155 {this.state.showRemoveDialog && (
156 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
157 <div class="form-group row">
158 <label class="col-form-label" htmlFor="remove-reason">
164 class="form-control mr-2"
165 placeholder={i18n.t('optional')}
166 value={this.state.removeReason}
167 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
170 {/* TODO hold off on expires for now */}
171 {/* <div class="form-group row"> */}
172 {/* <label class="col-form-label">Expires</label> */}
173 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
175 <div class="form-group row">
176 <button type="submit" class="btn btn-secondary">
177 {i18n.t('remove_community')}
182 <ul class="my-1 list-inline">
183 <li className="list-inline-item badge badge-secondary">
184 {i18n.t('number_online', { count: this.props.online })}
186 <li className="list-inline-item badge badge-secondary">
187 {i18n.t('number_of_subscribers', {
188 count: community.number_of_subscribers,
191 <li className="list-inline-item badge badge-secondary">
192 {i18n.t('number_of_posts', {
193 count: community.number_of_posts,
196 <li className="list-inline-item badge badge-secondary">
197 {i18n.t('number_of_comments', {
198 count: community.number_of_comments,
201 <li className="list-inline-item">
202 <Link className="badge badge-secondary" to="/communities">
203 {community.category_name}
206 <li className="list-inline-item">
208 className="badge badge-secondary"
209 to={`/modlog/community/${this.props.community.id}`}
215 <ul class="list-inline small">
216 <li class="list-inline-item">{i18n.t('mods')}: </li>
217 {this.props.moderators.map(mod => (
218 <li class="list-inline-item">
224 local: mod.user_local,
225 actor_id: mod.user_actor_id,
231 {/* TODO the to= needs to be able to handle community_ids as well, since they're federated */}
233 class={`btn btn-sm btn-secondary btn-block mb-3 ${
234 (community.deleted || community.removed) && 'no-click'
236 to={`/create_post?community=${community.name}`}
238 {i18n.t('create_a_post')}
241 {community.subscribed ? (
243 class="btn btn-sm btn-secondary btn-block"
244 onClick={linkEvent(community.id, this.handleUnsubscribe)}
246 {i18n.t('unsubscribe')}
250 class="btn btn-sm btn-secondary btn-block"
251 onClick={linkEvent(community.id, this.handleSubscribe)}
253 {i18n.t('subscribe')}
259 {community.description && (
260 <div class="card border-secondary">
261 <div class="card-body">
264 dangerouslySetInnerHTML={mdToHtml(community.description)}
273 handleEditClick(i: Sidebar) {
274 i.state.showEdit = true;
278 handleEditCommunity() {
279 this.state.showEdit = false;
280 this.setState(this.state);
284 this.state.showEdit = false;
285 this.setState(this.state);
288 handleDeleteClick(i: Sidebar) {
289 event.preventDefault();
290 let deleteForm: CommunityFormI = {
291 name: i.props.community.name,
292 title: i.props.community.title,
293 category_id: i.props.community.category_id,
294 edit_id: i.props.community.id,
295 deleted: !i.props.community.deleted,
296 nsfw: i.props.community.nsfw,
299 WebSocketService.Instance.editCommunity(deleteForm);
302 handleUnsubscribe(communityId: number) {
303 let form: FollowCommunityForm = {
304 community_id: communityId,
307 WebSocketService.Instance.followCommunity(form);
310 handleSubscribe(communityId: number) {
311 let form: FollowCommunityForm = {
312 community_id: communityId,
315 WebSocketService.Instance.followCommunity(form);
318 private get amCreator(): boolean {
319 return this.props.community.creator_id == UserService.Instance.user.id;
322 get canMod(): boolean {
324 UserService.Instance.user &&
325 this.props.moderators
327 .includes(UserService.Instance.user.id)
331 get canAdmin(): boolean {
333 UserService.Instance.user &&
334 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
338 handleModRemoveShow(i: Sidebar) {
339 i.state.showRemoveDialog = true;
343 handleModRemoveReasonChange(i: Sidebar, event: any) {
344 i.state.removeReason = event.target.value;
348 handleModRemoveExpiresChange(i: Sidebar, event: any) {
349 console.log(event.target.value);
350 i.state.removeExpires = event.target.value;
354 handleModRemoveSubmit(i: Sidebar) {
355 event.preventDefault();
356 let deleteForm: CommunityFormI = {
357 name: i.props.community.name,
358 title: i.props.community.title,
359 category_id: i.props.community.category_id,
360 edit_id: i.props.community.id,
361 removed: !i.props.community.removed,
362 reason: i.state.removeReason,
363 expires: getUnixTime(i.state.removeExpires),
364 nsfw: i.props.community.nsfw,
367 WebSocketService.Instance.editCommunity(deleteForm);
369 i.state.showRemoveDialog = false;