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';
5 import { DataType } from '../interfaces';
8 Community as CommunityI,
20 AddModToCommunityResponse,
21 BanFromCommunityResponse,
26 WebSocketJsonResponse,
29 } from 'lemmy-js-client';
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=${this.state.sort}`}
295 <svg class="icon text-muted small">
296 <use xlinkHref="#icon-rss">#</use>
306 {this.state.page > 1 && (
308 class="btn btn-secondary mr-1"
309 onClick={linkEvent(this, this.prevPage)}
314 {this.state.posts.length > 0 && (
316 class="btn btn-secondary"
317 onClick={linkEvent(this, this.nextPage)}
326 nextPage(i: Community) {
327 i.updateUrl({ page: i.state.page + 1 });
328 window.scrollTo(0, 0);
331 prevPage(i: Community) {
332 i.updateUrl({ page: i.state.page - 1 });
333 window.scrollTo(0, 0);
336 handleSortChange(val: SortType) {
337 this.updateUrl({ sort: val, page: 1 });
338 window.scrollTo(0, 0);
341 handleDataTypeChange(val: DataType) {
342 this.updateUrl({ dataType: DataType[val], page: 1 });
343 window.scrollTo(0, 0);
346 updateUrl(paramUpdates: UrlParams) {
347 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
348 const sortStr = paramUpdates.sort || this.state.sort;
349 const page = paramUpdates.page || this.state.page;
350 this.props.history.push(
351 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
356 if (this.state.dataType == DataType.Post) {
357 let getPostsForm: GetPostsForm = {
358 page: this.state.page,
360 sort: this.state.sort,
361 type_: ListingType.Community,
362 community_id: this.state.community.id,
364 WebSocketService.Instance.getPosts(getPostsForm);
366 let getCommentsForm: GetCommentsForm = {
367 page: this.state.page,
369 sort: this.state.sort,
370 type_: ListingType.Community,
371 community_id: this.state.community.id,
373 WebSocketService.Instance.getComments(getCommentsForm);
377 parseMessage(msg: WebSocketJsonResponse) {
379 let res = wsJsonToRes(msg);
381 toast(i18n.t(msg.error), 'danger');
382 this.context.router.history.push('/');
384 } else if (msg.reconnect) {
386 } else if (res.op == UserOperation.GetCommunity) {
387 let data = res.data as GetCommunityResponse;
388 this.state.community = data.community;
389 this.state.moderators = data.moderators;
390 this.state.online = data.online;
391 this.setState(this.state);
394 res.op == UserOperation.EditCommunity ||
395 res.op == UserOperation.DeleteCommunity ||
396 res.op == UserOperation.RemoveCommunity
398 let data = res.data as CommunityResponse;
399 this.state.community = data.community;
400 this.setState(this.state);
401 } else if (res.op == UserOperation.FollowCommunity) {
402 let data = res.data as CommunityResponse;
403 this.state.community.subscribed = data.community.subscribed;
404 this.state.community.number_of_subscribers =
405 data.community.number_of_subscribers;
406 this.setState(this.state);
407 } else if (res.op == UserOperation.GetPosts) {
408 let data = res.data as GetPostsResponse;
409 this.state.posts = data.posts;
410 this.state.loading = false;
411 this.setState(this.state);
414 res.op == UserOperation.EditPost ||
415 res.op == UserOperation.DeletePost ||
416 res.op == UserOperation.RemovePost ||
417 res.op == UserOperation.LockPost ||
418 res.op == UserOperation.StickyPost
420 let data = res.data as PostResponse;
421 editPostFindRes(data, this.state.posts);
422 this.setState(this.state);
423 } else if (res.op == UserOperation.CreatePost) {
424 let data = res.data as PostResponse;
425 this.state.posts.unshift(data.post);
426 notifyPost(data.post, this.context.router);
427 this.setState(this.state);
428 } else if (res.op == UserOperation.CreatePostLike) {
429 let data = res.data as PostResponse;
430 createPostLikeFindRes(data, this.state.posts);
431 this.setState(this.state);
432 } else if (res.op == UserOperation.AddModToCommunity) {
433 let data = res.data as AddModToCommunityResponse;
434 this.state.moderators = data.moderators;
435 this.setState(this.state);
436 } else if (res.op == UserOperation.BanFromCommunity) {
437 let data = res.data as BanFromCommunityResponse;
440 .filter(p => p.creator_id == data.user.id)
441 .forEach(p => (p.banned = data.banned));
443 this.setState(this.state);
444 } else if (res.op == UserOperation.GetComments) {
445 let data = res.data as GetCommentsResponse;
446 this.state.comments = data.comments;
447 this.state.loading = false;
448 this.setState(this.state);
450 res.op == UserOperation.EditComment ||
451 res.op == UserOperation.DeleteComment ||
452 res.op == UserOperation.RemoveComment
454 let data = res.data as CommentResponse;
455 editCommentRes(data, this.state.comments);
456 this.setState(this.state);
457 } else if (res.op == UserOperation.CreateComment) {
458 let data = res.data as CommentResponse;
460 // Necessary since it might be a user reply
461 if (data.recipient_ids.length == 0) {
462 this.state.comments.unshift(data.comment);
463 this.setState(this.state);
465 } else if (res.op == UserOperation.SaveComment) {
466 let data = res.data as CommentResponse;
467 saveCommentRes(data, this.state.comments);
468 this.setState(this.state);
469 } else if (res.op == UserOperation.CreateCommentLike) {
470 let data = res.data as CommentResponse;
471 createCommentLikeRes(data, this.state.comments);
472 this.setState(this.state);
473 } else if (res.op == UserOperation.GetSite) {
474 let data = res.data as GetSiteResponse;
475 this.state.site = data.site;
476 this.state.admins = data.admins;
477 this.setState(this.state);