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">
176 <li className="list-inline-item badge badge-secondary">
177 {i18n.t('number_online', { count: this.props.online })}
179 <li className="list-inline-item badge badge-secondary">
180 {i18n.t('number_of_subscribers', {
181 count: community.number_of_subscribers,
184 <li className="list-inline-item badge badge-secondary">
185 {i18n.t('number_of_posts', {
186 count: community.number_of_posts,
189 <li className="list-inline-item badge badge-secondary">
190 {i18n.t('number_of_comments', {
191 count: community.number_of_comments,
194 <li className="list-inline-item">
195 <Link className="badge badge-secondary" to="/communities">
196 {community.category_name}
199 <li className="list-inline-item">
201 className="badge badge-secondary"
202 to={`/modlog/community/${this.props.community.id}`}
208 <ul class="list-inline small">
209 <li class="list-inline-item">{i18n.t('mods')}: </li>
210 {this.props.moderators.map(mod => (
211 <li class="list-inline-item">
217 local: mod.user_local,
218 actor_id: mod.user_actor_id,
224 {/* TODO the to= needs to be able to handle community_ids as well, since they're federated */}
226 class={`btn btn-sm btn-secondary btn-block mb-3 ${
227 (community.deleted || community.removed) && 'no-click'
229 to={`/create_post?community=${community.name}`}
231 {i18n.t('create_a_post')}
234 {community.subscribed ? (
236 class="btn btn-sm btn-secondary btn-block"
237 onClick={linkEvent(community.id, this.handleUnsubscribe)}
239 {i18n.t('unsubscribe')}
243 class="btn btn-sm btn-secondary btn-block"
244 onClick={linkEvent(community.id, this.handleSubscribe)}
246 {i18n.t('subscribe')}
252 {community.description && (
253 <div class="card border-secondary">
254 <div class="card-body">
257 dangerouslySetInnerHTML={mdToHtml(community.description)}
266 handleEditClick(i: Sidebar) {
267 i.state.showEdit = true;
271 handleEditCommunity() {
272 this.state.showEdit = false;
273 this.setState(this.state);
277 this.state.showEdit = false;
278 this.setState(this.state);
281 handleDeleteClick(i: Sidebar) {
282 event.preventDefault();
283 let deleteForm: CommunityFormI = {
284 name: i.props.community.name,
285 title: i.props.community.title,
286 category_id: i.props.community.category_id,
287 edit_id: i.props.community.id,
288 deleted: !i.props.community.deleted,
289 nsfw: i.props.community.nsfw,
292 WebSocketService.Instance.editCommunity(deleteForm);
295 handleUnsubscribe(communityId: number) {
296 let form: FollowCommunityForm = {
297 community_id: communityId,
300 WebSocketService.Instance.followCommunity(form);
303 handleSubscribe(communityId: number) {
304 let form: FollowCommunityForm = {
305 community_id: communityId,
308 WebSocketService.Instance.followCommunity(form);
311 private get amCreator(): boolean {
312 return this.props.community.creator_id == UserService.Instance.user.id;
315 get canMod(): boolean {
317 UserService.Instance.user &&
318 this.props.moderators
320 .includes(UserService.Instance.user.id)
324 get canAdmin(): boolean {
326 UserService.Instance.user &&
327 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
331 handleModRemoveShow(i: Sidebar) {
332 i.state.showRemoveDialog = true;
336 handleModRemoveReasonChange(i: Sidebar, event: any) {
337 i.state.removeReason = event.target.value;
341 handleModRemoveExpiresChange(i: Sidebar, event: any) {
342 console.log(event.target.value);
343 i.state.removeExpires = event.target.value;
347 handleModRemoveSubmit(i: Sidebar) {
348 event.preventDefault();
349 let deleteForm: CommunityFormI = {
350 name: i.props.community.name,
351 title: i.props.community.title,
352 category_id: i.props.community.category_id,
353 edit_id: i.props.community.id,
354 removed: !i.props.community.removed,
355 reason: i.state.removeReason,
356 expires: getUnixTime(i.state.removeExpires),
357 nsfw: i.props.community.nsfw,
360 WebSocketService.Instance.editCommunity(deleteForm);
362 i.state.showRemoveDialog = false;