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>;
24 interface SidebarState {
26 showRemoveDialog: boolean;
28 removeExpires: string;
31 export class Sidebar extends Component<SidebarProps, SidebarState> {
32 private emptyState: SidebarState = {
34 showRemoveDialog: false,
39 constructor(props: any, context: any) {
40 super(props, context);
41 this.state = this.emptyState;
42 this.handleEditCommunity = this.handleEditCommunity.bind(this);
43 this.handleEditCancel = this.handleEditCancel.bind(this);
49 {!this.state.showEdit ? (
53 community={this.props.community}
54 onEdit={this.handleEditCommunity}
55 onCancel={this.handleEditCancel}
63 let community = this.props.community;
64 let name_: string, link: string;
66 if (community.local) {
67 name_ = community.name;
68 link = `/c/${community.name}`;
70 name_ = `${community.name}@${hostname(community.actor_id)}`;
71 link = community.actor_id;
75 <div class="card border-secondary mb-3">
76 <div class="card-body">
78 <span>{community.title}</span>
79 {community.removed && (
80 <small className="ml-2 text-muted font-italic">
84 {community.deleted && (
85 <small className="ml-2 text-muted font-italic">
90 <CommunityLink community={community} realLink />
91 <ul class="list-inline mb-1 text-muted font-weight-bold">
94 <li className="list-inline-item-action">
97 onClick={linkEvent(this, this.handleEditClick)}
98 data-tippy-content={i18n.t('edit')}
100 <svg class="icon icon-inline">
101 <use xlinkHref="#icon-edit"></use>
106 <li className="list-inline-item-action">
109 onClick={linkEvent(this, this.handleDeleteClick)}
117 class={`icon icon-inline ${
118 community.deleted && 'text-danger'
121 <use xlinkHref="#icon-trash"></use>
129 <li className="list-inline-item">
130 {!this.props.community.removed ? (
133 onClick={linkEvent(this, this.handleModRemoveShow)}
140 onClick={linkEvent(this, this.handleModRemoveSubmit)}
148 {this.state.showRemoveDialog && (
149 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
150 <div class="form-group row">
151 <label class="col-form-label" htmlFor="remove-reason">
157 class="form-control mr-2"
158 placeholder={i18n.t('optional')}
159 value={this.state.removeReason}
160 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
163 {/* TODO hold off on expires for now */}
164 {/* <div class="form-group row"> */}
165 {/* <label class="col-form-label">Expires</label> */}
166 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
168 <div class="form-group row">
169 <button type="submit" class="btn btn-secondary">
170 {i18n.t('remove_community')}
175 <ul class="my-1 list-inline">
177 <li className="list-inline-item badge badge-secondary">
178 {i18n.t('number_online', { count: this.props.online })}
181 <li className="list-inline-item badge badge-secondary">
182 {i18n.t('number_of_subscribers', {
183 count: community.number_of_subscribers,
186 <li className="list-inline-item badge badge-secondary">
187 {i18n.t('number_of_posts', {
188 count: community.number_of_posts,
191 <li className="list-inline-item badge badge-secondary">
192 {i18n.t('number_of_comments', {
193 count: community.number_of_comments,
196 <li className="list-inline-item">
197 <Link className="badge badge-secondary" to="/communities">
198 {community.category_name}
201 <li className="list-inline-item">
203 className="badge badge-secondary"
204 to={`/modlog/community/${this.props.community.id}`}
210 <ul class="list-inline small">
211 <li class="list-inline-item">{i18n.t('mods')}: </li>
212 {this.props.moderators.map(mod => (
213 <li class="list-inline-item">
219 local: mod.user_local,
220 actor_id: mod.user_actor_id,
226 {/* TODO the to= needs to be able to handle community_ids as well, since they're federated */}
228 class={`btn btn-sm btn-secondary btn-block mb-3 ${
229 (community.deleted || community.removed) && 'no-click'
231 to={`/create_post?community=${community.name}`}
233 {i18n.t('create_a_post')}
236 {community.subscribed ? (
238 class="btn btn-sm btn-secondary btn-block"
239 onClick={linkEvent(community.id, this.handleUnsubscribe)}
241 {i18n.t('unsubscribe')}
245 class="btn btn-sm btn-secondary btn-block"
246 onClick={linkEvent(community.id, this.handleSubscribe)}
248 {i18n.t('subscribe')}
254 {community.description && (
255 <div class="card border-secondary">
256 <div class="card-body">
259 dangerouslySetInnerHTML={mdToHtml(community.description)}
268 handleEditClick(i: Sidebar) {
269 i.state.showEdit = true;
273 handleEditCommunity() {
274 this.state.showEdit = false;
275 this.setState(this.state);
279 this.state.showEdit = false;
280 this.setState(this.state);
283 handleDeleteClick(i: Sidebar) {
284 event.preventDefault();
285 let deleteForm: CommunityFormI = {
286 name: i.props.community.name,
287 title: i.props.community.title,
288 category_id: i.props.community.category_id,
289 edit_id: i.props.community.id,
290 deleted: !i.props.community.deleted,
291 nsfw: i.props.community.nsfw,
294 WebSocketService.Instance.editCommunity(deleteForm);
297 handleUnsubscribe(communityId: number) {
298 let form: FollowCommunityForm = {
299 community_id: communityId,
302 WebSocketService.Instance.followCommunity(form);
305 handleSubscribe(communityId: number) {
306 let form: FollowCommunityForm = {
307 community_id: communityId,
310 WebSocketService.Instance.followCommunity(form);
313 private get amCreator(): boolean {
314 return this.props.community.creator_id == UserService.Instance.user.id;
317 get canMod(): boolean {
319 UserService.Instance.user &&
320 this.props.moderators
322 .includes(UserService.Instance.user.id)
326 get canAdmin(): boolean {
328 UserService.Instance.user &&
329 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
333 handleModRemoveShow(i: Sidebar) {
334 i.state.showRemoveDialog = true;
338 handleModRemoveReasonChange(i: Sidebar, event: any) {
339 i.state.removeReason = event.target.value;
343 handleModRemoveExpiresChange(i: Sidebar, event: any) {
344 console.log(event.target.value);
345 i.state.removeExpires = event.target.value;
349 handleModRemoveSubmit(i: Sidebar) {
350 event.preventDefault();
351 let deleteForm: CommunityFormI = {
352 name: i.props.community.name,
353 title: i.props.community.title,
354 category_id: i.props.community.category_id,
355 edit_id: i.props.community.id,
356 removed: !i.props.community.removed,
357 reason: i.state.removeReason,
358 expires: getUnixTime(i.state.removeExpires),
359 nsfw: i.props.community.nsfw,
362 WebSocketService.Instance.editCommunity(deleteForm);
364 i.state.showRemoveDialog = false;