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, hostname } from '../utils';
13 import { CommunityForm } from './community-form';
14 import { UserListing } from './user-listing';
15 import { CommunityLink } from './community-link';
16 import { i18n } from '../i18next';
18 interface SidebarProps {
20 moderators: Array<CommunityUser>;
21 admins: Array<UserView>;
26 interface SidebarState {
28 showRemoveDialog: boolean;
30 removeExpires: string;
33 export class Sidebar extends Component<SidebarProps, SidebarState> {
34 private emptyState: SidebarState = {
36 showRemoveDialog: false,
41 constructor(props: any, context: any) {
42 super(props, context);
43 this.state = this.emptyState;
44 this.handleEditCommunity = this.handleEditCommunity.bind(this);
45 this.handleEditCancel = this.handleEditCancel.bind(this);
51 {!this.state.showEdit ? (
55 community={this.props.community}
56 onEdit={this.handleEditCommunity}
57 onCancel={this.handleEditCancel}
58 enableNsfw={this.props.enableNsfw}
66 let community = this.props.community;
67 let name_: string, link: string;
69 if (community.local) {
70 name_ = community.name;
71 link = `/c/${community.name}`;
73 name_ = `${community.name}@${hostname(community.actor_id)}`;
74 link = community.actor_id;
78 <div class="card border-secondary mb-3">
79 <div class="card-body">
81 <span>{community.title}</span>
82 {community.removed && (
83 <small className="ml-2 text-muted font-italic">
87 {community.deleted && (
88 <small className="ml-2 text-muted font-italic">
93 <CommunityLink community={community} realLink />
94 <ul class="list-inline mb-1 text-muted font-weight-bold">
97 <li className="list-inline-item-action">
100 onClick={linkEvent(this, this.handleEditClick)}
101 data-tippy-content={i18n.t('edit')}
103 <svg class="icon icon-inline">
104 <use xlinkHref="#icon-edit"></use>
109 <li className="list-inline-item-action">
112 onClick={linkEvent(this, this.handleDeleteClick)}
120 class={`icon icon-inline ${
121 community.deleted && 'text-danger'
124 <use xlinkHref="#icon-trash"></use>
132 <li className="list-inline-item">
133 {!this.props.community.removed ? (
136 onClick={linkEvent(this, this.handleModRemoveShow)}
143 onClick={linkEvent(this, this.handleModRemoveSubmit)}
151 {this.state.showRemoveDialog && (
152 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
153 <div class="form-group row">
154 <label class="col-form-label" htmlFor="remove-reason">
160 class="form-control mr-2"
161 placeholder={i18n.t('optional')}
162 value={this.state.removeReason}
163 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
166 {/* TODO hold off on expires for now */}
167 {/* <div class="form-group row"> */}
168 {/* <label class="col-form-label">Expires</label> */}
169 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
171 <div class="form-group row">
172 <button type="submit" class="btn btn-secondary">
173 {i18n.t('remove_community')}
178 <ul class="my-1 list-inline">
180 <li className="list-inline-item badge badge-secondary">
181 {i18n.t('number_online', { count: this.props.online })}
184 <li className="list-inline-item badge badge-secondary">
185 {i18n.t('number_of_subscribers', {
186 count: community.number_of_subscribers,
189 <li className="list-inline-item badge badge-secondary">
190 {i18n.t('number_of_posts', {
191 count: community.number_of_posts,
194 <li className="list-inline-item badge badge-secondary">
195 {i18n.t('number_of_comments', {
196 count: community.number_of_comments,
199 <li className="list-inline-item">
200 <Link className="badge badge-secondary" to="/communities">
201 {community.category_name}
204 <li className="list-inline-item">
206 className="badge badge-secondary"
207 to={`/modlog/community/${this.props.community.id}`}
213 <ul class="list-inline small">
214 <li class="list-inline-item">{i18n.t('mods')}: </li>
215 {this.props.moderators.map(mod => (
216 <li class="list-inline-item">
222 local: mod.user_local,
223 actor_id: mod.user_actor_id,
229 {/* TODO the to= needs to be able to handle community_ids as well, since they're federated */}
231 class={`btn btn-sm btn-secondary btn-block mb-3 ${
232 (community.deleted || community.removed) && 'no-click'
234 to={`/create_post?community=${community.name}`}
236 {i18n.t('create_a_post')}
239 {community.subscribed ? (
241 class="btn btn-sm btn-secondary btn-block"
242 onClick={linkEvent(community.id, this.handleUnsubscribe)}
244 {i18n.t('unsubscribe')}
248 class="btn btn-sm btn-secondary btn-block"
249 onClick={linkEvent(community.id, this.handleSubscribe)}
251 {i18n.t('subscribe')}
257 {community.description && (
258 <div class="card border-secondary">
259 <div class="card-body">
262 dangerouslySetInnerHTML={mdToHtml(community.description)}
271 handleEditClick(i: Sidebar) {
272 i.state.showEdit = true;
276 handleEditCommunity() {
277 this.state.showEdit = false;
278 this.setState(this.state);
282 this.state.showEdit = false;
283 this.setState(this.state);
286 handleDeleteClick(i: Sidebar) {
287 event.preventDefault();
288 let deleteForm: DeleteCommunityForm = {
289 edit_id: i.props.community.id,
290 deleted: !i.props.community.deleted,
292 WebSocketService.Instance.deleteCommunity(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 removeForm: RemoveCommunityForm = {
350 edit_id: i.props.community.id,
351 removed: !i.props.community.removed,
352 reason: i.state.removeReason,
353 expires: getUnixTime(i.state.removeExpires),
355 WebSocketService.Instance.removeCommunity(removeForm);
357 i.state.showRemoveDialog = false;