1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
7 Community as CommunityI,
20 AddModToCommunityResponse,
21 BanFromCommunityResponse,
26 WebSocketJsonResponse,
29 } from '../interfaces';
30 import { WebSocketService } from '../services';
31 import { PostListings } from './post-listings';
32 import { CommentNodes } from './comment-nodes';
33 import { SortSelect } from './sort-select';
34 import { DataTypeSelect } from './data-type-select';
35 import { Sidebar } from './sidebar';
36 import { CommunityLink } from './community-link';
37 import { BannerIconHeader } from './banner-icon-header';
48 createPostLikeFindRes,
55 import { i18n } from '../i18next';
58 community: CommunityI;
60 communityName: string;
61 moderators: Array<CommunityUser>;
62 admins: Array<UserView>;
66 comments: Array<Comment>;
73 interface CommunityProps {
85 export class Community extends Component<any, State> {
86 private subscription: Subscription;
87 private emptyState: State = {
96 number_of_subscribers: null,
97 number_of_posts: null,
98 number_of_comments: null,
105 last_refreshed_at: null,
106 creator_actor_id: null,
111 communityId: Number(this.props.match.params.id),
112 communityName: this.props.match.params.name,
117 dataType: getDataTypeFromProps(this.props),
118 sort: getSortTypeFromProps(this.props),
119 page: getPageFromProps(this.props),
123 creator_id: undefined,
124 published: undefined,
125 creator_name: undefined,
126 number_of_users: undefined,
127 number_of_posts: undefined,
128 number_of_comments: undefined,
129 number_of_communities: undefined,
130 enable_downvotes: undefined,
131 open_registration: undefined,
132 enable_nsfw: undefined,
135 creator_preferred_username: undefined,
139 constructor(props: any, context: any) {
140 super(props, context);
142 this.state = this.emptyState;
143 this.handleSortChange = this.handleSortChange.bind(this);
144 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
146 this.subscription = WebSocketService.Instance.subject
147 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
149 msg => this.parseMessage(msg),
150 err => console.error(err),
151 () => console.log('complete')
154 let form: GetCommunityForm = {
155 id: this.state.communityId ? this.state.communityId : null,
156 name: this.state.communityName ? this.state.communityName : null,
158 WebSocketService.Instance.getCommunity(form);
159 WebSocketService.Instance.getSite();
162 componentWillUnmount() {
163 this.subscription.unsubscribe();
166 static getDerivedStateFromProps(props: any): CommunityProps {
168 dataType: getDataTypeFromProps(props),
169 sort: getSortTypeFromProps(props),
170 page: getPageFromProps(props),
174 componentDidUpdate(_: any, lastState: State) {
176 lastState.dataType !== this.state.dataType ||
177 lastState.sort !== this.state.sort ||
178 lastState.page !== this.state.page
180 this.setState({ loading: true });
185 get documentTitle(): string {
186 if (this.state.community.title) {
187 return `${this.state.community.title} - ${this.state.site.name}`;
193 get favIcon(): string {
194 return this.state.site.icon ? this.state.site.icon : favIconUrl;
199 <div class="container">
200 <Helmet title={this.documentTitle}>
208 {this.state.loading ? (
210 <svg class="icon icon-spinner spin">
211 <use xlinkHref="#icon-spinner"></use>
216 <div class="col-12 col-md-8">
217 {this.communityInfo()}
222 <div class="col-12 col-md-4">
224 community={this.state.community}
225 moderators={this.state.moderators}
226 admins={this.state.admins}
227 online={this.state.online}
228 enableNsfw={this.state.site.enable_nsfw}
238 return this.state.dataType == DataType.Post ? (
240 posts={this.state.posts}
242 sort={this.state.sort}
243 enableDownvotes={this.state.site.enable_downvotes}
244 enableNsfw={this.state.site.enable_nsfw}
248 nodes={commentsToFlatNodes(this.state.comments)}
250 sortType={this.state.sort}
252 enableDownvotes={this.state.site.enable_downvotes}
261 banner={this.state.community.banner}
262 icon={this.state.community.icon}
264 <h5 class="mb-0">{this.state.community.title}</h5>
266 community={this.state.community}
282 type_={this.state.dataType}
283 onChange={this.handleDataTypeChange}
287 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
290 href={`/feeds/c/${this.state.communityName}.xml?sort=${
291 SortType[this.state.sort]
297 <svg class="icon text-muted small">
298 <use xlinkHref="#icon-rss">#</use>
308 {this.state.page > 1 && (
310 class="btn btn-secondary mr-1"
311 onClick={linkEvent(this, this.prevPage)}
316 {this.state.posts.length > 0 && (
318 class="btn btn-secondary"
319 onClick={linkEvent(this, this.nextPage)}
328 nextPage(i: Community) {
329 i.updateUrl({ page: i.state.page + 1 });
330 window.scrollTo(0, 0);
333 prevPage(i: Community) {
334 i.updateUrl({ page: i.state.page - 1 });
335 window.scrollTo(0, 0);
338 handleSortChange(val: SortType) {
339 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
340 window.scrollTo(0, 0);
343 handleDataTypeChange(val: DataType) {
344 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
345 window.scrollTo(0, 0);
348 updateUrl(paramUpdates: UrlParams) {
350 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
352 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
353 const page = paramUpdates.page || this.state.page;
354 this.props.history.push(
355 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
360 if (this.state.dataType == DataType.Post) {
361 let getPostsForm: GetPostsForm = {
362 page: this.state.page,
364 sort: SortType[this.state.sort],
365 type_: ListingType[ListingType.Community],
366 community_id: this.state.community.id,
368 WebSocketService.Instance.getPosts(getPostsForm);
370 let getCommentsForm: GetCommentsForm = {
371 page: this.state.page,
373 sort: SortType[this.state.sort],
374 type_: ListingType[ListingType.Community],
375 community_id: this.state.community.id,
377 WebSocketService.Instance.getComments(getCommentsForm);
381 parseMessage(msg: WebSocketJsonResponse) {
383 let res = wsJsonToRes(msg);
385 toast(i18n.t(msg.error), 'danger');
386 this.context.router.history.push('/');
388 } else if (msg.reconnect) {
390 } else if (res.op == UserOperation.GetCommunity) {
391 let data = res.data as GetCommunityResponse;
392 this.state.community = data.community;
393 this.state.moderators = data.moderators;
394 this.state.online = data.online;
395 this.setState(this.state);
398 res.op == UserOperation.EditCommunity ||
399 res.op == UserOperation.DeleteCommunity ||
400 res.op == UserOperation.RemoveCommunity
402 let data = res.data as CommunityResponse;
403 this.state.community = data.community;
404 this.setState(this.state);
405 } else if (res.op == UserOperation.FollowCommunity) {
406 let data = res.data as CommunityResponse;
407 this.state.community.subscribed = data.community.subscribed;
408 this.state.community.number_of_subscribers =
409 data.community.number_of_subscribers;
410 this.setState(this.state);
411 } else if (res.op == UserOperation.GetPosts) {
412 let data = res.data as GetPostsResponse;
413 this.state.posts = data.posts;
414 this.state.loading = false;
415 this.setState(this.state);
418 res.op == UserOperation.EditPost ||
419 res.op == UserOperation.DeletePost ||
420 res.op == UserOperation.RemovePost ||
421 res.op == UserOperation.LockPost ||
422 res.op == UserOperation.StickyPost
424 let data = res.data as PostResponse;
425 editPostFindRes(data, this.state.posts);
426 this.setState(this.state);
427 } else if (res.op == UserOperation.CreatePost) {
428 let data = res.data as PostResponse;
429 this.state.posts.unshift(data.post);
430 notifyPost(data.post, this.context.router);
431 this.setState(this.state);
432 } else if (res.op == UserOperation.CreatePostLike) {
433 let data = res.data as PostResponse;
434 createPostLikeFindRes(data, this.state.posts);
435 this.setState(this.state);
436 } else if (res.op == UserOperation.AddModToCommunity) {
437 let data = res.data as AddModToCommunityResponse;
438 this.state.moderators = data.moderators;
439 this.setState(this.state);
440 } else if (res.op == UserOperation.BanFromCommunity) {
441 let data = res.data as BanFromCommunityResponse;
444 .filter(p => p.creator_id == data.user.id)
445 .forEach(p => (p.banned = data.banned));
447 this.setState(this.state);
448 } else if (res.op == UserOperation.GetComments) {
449 let data = res.data as GetCommentsResponse;
450 this.state.comments = data.comments;
451 this.state.loading = false;
452 this.setState(this.state);
454 res.op == UserOperation.EditComment ||
455 res.op == UserOperation.DeleteComment ||
456 res.op == UserOperation.RemoveComment
458 let data = res.data as CommentResponse;
459 editCommentRes(data, this.state.comments);
460 this.setState(this.state);
461 } else if (res.op == UserOperation.CreateComment) {
462 let data = res.data as CommentResponse;
464 // Necessary since it might be a user reply
465 if (data.recipient_ids.length == 0) {
466 this.state.comments.unshift(data.comment);
467 this.setState(this.state);
469 } else if (res.op == UserOperation.SaveComment) {
470 let data = res.data as CommentResponse;
471 saveCommentRes(data, this.state.comments);
472 this.setState(this.state);
473 } else if (res.op == UserOperation.CreateCommentLike) {
474 let data = res.data as CommentResponse;
475 createCommentLikeRes(data, this.state.comments);
476 this.setState(this.state);
477 } else if (res.op == UserOperation.GetSite) {
478 let data = res.data as GetSiteResponse;
479 this.state.site = data.site;
480 this.state.admins = data.admins;
481 this.setState(this.state);