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,
54 import { i18n } from '../i18next';
57 community: CommunityI;
59 communityName: string;
60 moderators: Array<CommunityUser>;
61 admins: Array<UserView>;
65 comments: Array<Comment>;
72 interface CommunityProps {
84 export class Community extends Component<any, State> {
85 private subscription: Subscription;
86 private emptyState: State = {
95 number_of_subscribers: null,
96 number_of_posts: null,
97 number_of_comments: null,
104 last_refreshed_at: null,
105 creator_actor_id: null,
110 communityId: Number(this.props.match.params.id),
111 communityName: this.props.match.params.name,
116 dataType: getDataTypeFromProps(this.props),
117 sort: getSortTypeFromProps(this.props),
118 page: getPageFromProps(this.props),
122 creator_id: undefined,
123 published: undefined,
124 creator_name: undefined,
125 number_of_users: undefined,
126 number_of_posts: undefined,
127 number_of_comments: undefined,
128 number_of_communities: undefined,
129 enable_downvotes: undefined,
130 open_registration: undefined,
131 enable_nsfw: undefined,
134 creator_preferred_username: undefined,
138 constructor(props: any, context: any) {
139 super(props, context);
141 this.state = this.emptyState;
142 this.handleSortChange = this.handleSortChange.bind(this);
143 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
145 this.subscription = WebSocketService.Instance.subject
146 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
148 msg => this.parseMessage(msg),
149 err => console.error(err),
150 () => console.log('complete')
153 let form: GetCommunityForm = {
154 id: this.state.communityId ? this.state.communityId : null,
155 name: this.state.communityName ? this.state.communityName : null,
157 WebSocketService.Instance.getCommunity(form);
158 WebSocketService.Instance.getSite();
161 componentWillUnmount() {
162 this.subscription.unsubscribe();
165 static getDerivedStateFromProps(props: any): CommunityProps {
167 dataType: getDataTypeFromProps(props),
168 sort: getSortTypeFromProps(props),
169 page: getPageFromProps(props),
173 componentDidUpdate(_: any, lastState: State) {
175 lastState.dataType !== this.state.dataType ||
176 lastState.sort !== this.state.sort ||
177 lastState.page !== this.state.page
179 this.setState({ loading: true });
184 get documentTitle(): string {
185 if (this.state.community.title) {
186 return `${this.state.community.title} - ${this.state.site.name}`;
192 get favIcon(): string {
193 return this.state.site.icon ? this.state.site.icon : favIconUrl;
198 <div class="container">
199 <Helmet title={this.documentTitle}>
207 {this.state.loading ? (
209 <svg class="icon icon-spinner spin">
210 <use xlinkHref="#icon-spinner"></use>
215 <div class="col-12 col-md-8">
216 {this.communityInfo()}
221 <div class="col-12 col-md-4">
223 community={this.state.community}
224 moderators={this.state.moderators}
225 admins={this.state.admins}
226 online={this.state.online}
227 enableNsfw={this.state.site.enable_nsfw}
237 return this.state.dataType == DataType.Post ? (
239 posts={this.state.posts}
241 sort={this.state.sort}
242 enableDownvotes={this.state.site.enable_downvotes}
243 enableNsfw={this.state.site.enable_nsfw}
247 nodes={commentsToFlatNodes(this.state.comments)}
249 sortType={this.state.sort}
251 enableDownvotes={this.state.site.enable_downvotes}
260 banner={this.state.community.banner}
261 icon={this.state.community.icon}
263 <h5 class="mb-0">{this.state.community.title}</h5>
265 community={this.state.community}
281 type_={this.state.dataType}
282 onChange={this.handleDataTypeChange}
286 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
289 href={`/feeds/c/${this.state.communityName}.xml?sort=${
290 SortType[this.state.sort]
296 <svg class="icon text-muted small">
297 <use xlinkHref="#icon-rss">#</use>
307 {this.state.page > 1 && (
309 class="btn btn-secondary mr-1"
310 onClick={linkEvent(this, this.prevPage)}
315 {this.state.posts.length > 0 && (
317 class="btn btn-secondary"
318 onClick={linkEvent(this, this.nextPage)}
327 nextPage(i: Community) {
328 i.updateUrl({ page: i.state.page + 1 });
329 window.scrollTo(0, 0);
332 prevPage(i: Community) {
333 i.updateUrl({ page: i.state.page - 1 });
334 window.scrollTo(0, 0);
337 handleSortChange(val: SortType) {
338 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
339 window.scrollTo(0, 0);
342 handleDataTypeChange(val: DataType) {
343 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
344 window.scrollTo(0, 0);
347 updateUrl(paramUpdates: UrlParams) {
349 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
351 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
352 const page = paramUpdates.page || this.state.page;
353 this.props.history.push(
354 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
359 if (this.state.dataType == DataType.Post) {
360 let getPostsForm: GetPostsForm = {
361 page: this.state.page,
363 sort: SortType[this.state.sort],
364 type_: ListingType[ListingType.Community],
365 community_id: this.state.community.id,
367 WebSocketService.Instance.getPosts(getPostsForm);
369 let getCommentsForm: GetCommentsForm = {
370 page: this.state.page,
372 sort: SortType[this.state.sort],
373 type_: ListingType[ListingType.Community],
374 community_id: this.state.community.id,
376 WebSocketService.Instance.getComments(getCommentsForm);
380 parseMessage(msg: WebSocketJsonResponse) {
382 let res = wsJsonToRes(msg);
384 toast(i18n.t(msg.error), 'danger');
385 this.context.router.history.push('/');
387 } else if (msg.reconnect) {
389 } else if (res.op == UserOperation.GetCommunity) {
390 let data = res.data as GetCommunityResponse;
391 this.state.community = data.community;
392 this.state.moderators = data.moderators;
393 this.state.online = data.online;
394 this.setState(this.state);
397 res.op == UserOperation.EditCommunity ||
398 res.op == UserOperation.DeleteCommunity ||
399 res.op == UserOperation.RemoveCommunity
401 let data = res.data as CommunityResponse;
402 this.state.community = data.community;
403 this.setState(this.state);
404 } else if (res.op == UserOperation.FollowCommunity) {
405 let data = res.data as CommunityResponse;
406 this.state.community.subscribed = data.community.subscribed;
407 this.state.community.number_of_subscribers =
408 data.community.number_of_subscribers;
409 this.setState(this.state);
410 } else if (res.op == UserOperation.GetPosts) {
411 let data = res.data as GetPostsResponse;
412 this.state.posts = data.posts;
413 this.state.loading = false;
414 this.setState(this.state);
417 res.op == UserOperation.EditPost ||
418 res.op == UserOperation.DeletePost ||
419 res.op == UserOperation.RemovePost ||
420 res.op == UserOperation.LockPost ||
421 res.op == UserOperation.StickyPost
423 let data = res.data as PostResponse;
424 editPostFindRes(data, this.state.posts);
425 this.setState(this.state);
426 } else if (res.op == UserOperation.CreatePost) {
427 let data = res.data as PostResponse;
428 this.state.posts.unshift(data.post);
429 this.setState(this.state);
430 } else if (res.op == UserOperation.CreatePostLike) {
431 let data = res.data as PostResponse;
432 createPostLikeFindRes(data, this.state.posts);
433 this.setState(this.state);
434 } else if (res.op == UserOperation.AddModToCommunity) {
435 let data = res.data as AddModToCommunityResponse;
436 this.state.moderators = data.moderators;
437 this.setState(this.state);
438 } else if (res.op == UserOperation.BanFromCommunity) {
439 let data = res.data as BanFromCommunityResponse;
442 .filter(p => p.creator_id == data.user.id)
443 .forEach(p => (p.banned = data.banned));
445 this.setState(this.state);
446 } else if (res.op == UserOperation.GetComments) {
447 let data = res.data as GetCommentsResponse;
448 this.state.comments = data.comments;
449 this.state.loading = false;
450 this.setState(this.state);
452 res.op == UserOperation.EditComment ||
453 res.op == UserOperation.DeleteComment ||
454 res.op == UserOperation.RemoveComment
456 let data = res.data as CommentResponse;
457 editCommentRes(data, this.state.comments);
458 this.setState(this.state);
459 } else if (res.op == UserOperation.CreateComment) {
460 let data = res.data as CommentResponse;
462 // Necessary since it might be a user reply
463 if (data.recipient_ids.length == 0) {
464 this.state.comments.unshift(data.comment);
465 this.setState(this.state);
467 } else if (res.op == UserOperation.SaveComment) {
468 let data = res.data as CommentResponse;
469 saveCommentRes(data, this.state.comments);
470 this.setState(this.state);
471 } else if (res.op == UserOperation.CreateCommentLike) {
472 let data = res.data as CommentResponse;
473 createCommentLikeRes(data, this.state.comments);
474 this.setState(this.state);
475 } else if (res.op == UserOperation.GetSite) {
476 let data = res.data as GetSiteResponse;
477 this.state.site = data.site;
478 this.state.admins = data.admins;
479 this.setState(this.state);