1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { DataType, InitialFetchRequest } from '../interfaces';
15 AddModToCommunityResponse,
16 BanFromCommunityResponse,
21 WebSocketJsonResponse,
24 ListCategoriesResponse,
25 } from 'lemmy-js-client';
26 import { UserService, WebSocketService } from '../services';
27 import { PostListings } from './post-listings';
28 import { CommentNodes } from './comment-nodes';
29 import { HtmlTags } from './html-tags';
30 import { SortSelect } from './sort-select';
31 import { DataTypeSelect } from './data-type-select';
32 import { Sidebar } from './sidebar';
33 import { CommunityLink } from './community-link';
34 import { BannerIconHeader } from './banner-icon-header';
45 createPostLikeFindRes,
56 import { i18n } from '../i18next';
59 communityRes: GetCommunityResponse;
60 siteRes: GetSiteResponse;
62 communityName: string;
63 communityLoading: boolean;
64 postsLoading: boolean;
65 commentsLoading: boolean;
71 categories: Category[];
74 interface CommunityProps {
86 export class Community extends Component<any, State> {
87 private isoData = setIsoData(this.context);
88 private subscription: Subscription;
89 private emptyState: State = {
90 communityRes: undefined,
91 communityId: Number(this.props.match.params.id),
92 communityName: this.props.match.params.name,
93 communityLoading: true,
95 commentsLoading: true,
98 dataType: getDataTypeFromProps(this.props),
99 sort: getSortTypeFromProps(this.props),
100 page: getPageFromProps(this.props),
101 siteRes: this.isoData.site,
105 constructor(props: any, context: any) {
106 super(props, context);
108 this.state = this.emptyState;
109 this.handleSortChange = this.handleSortChange.bind(this);
110 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
112 this.parseMessage = this.parseMessage.bind(this);
113 this.subscription = wsSubscribe(this.parseMessage);
115 // Only fetch the data if coming from another route
116 if (this.isoData.path == this.context.router.route.match.url) {
117 this.state.communityRes = this.isoData.routeData[0];
118 if (this.state.dataType == DataType.Post) {
119 this.state.posts = this.isoData.routeData[1].posts;
121 this.state.comments = this.isoData.routeData[1].comments;
123 this.state.categories = this.isoData.routeData[2].categories;
124 this.state.communityLoading = false;
125 this.state.postsLoading = false;
126 this.state.commentsLoading = false;
128 this.fetchCommunity();
130 WebSocketService.Instance.listCategories();
136 let form: GetCommunityForm = {
137 id: this.state.communityId ? this.state.communityId : null,
138 name: this.state.communityName ? this.state.communityName : null,
140 WebSocketService.Instance.getCommunity(form);
143 componentWillUnmount() {
145 this.subscription.unsubscribe();
146 window.isoData.path = undefined;
150 static getDerivedStateFromProps(props: any): CommunityProps {
152 dataType: getDataTypeFromProps(props),
153 sort: getSortTypeFromProps(props),
154 page: getPageFromProps(props),
158 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
159 let pathSplit = req.path.split('/');
160 let promises: Promise<any>[] = [];
162 // It can be /c/main, or /c/1
163 let idOrName = pathSplit[2];
166 if (isNaN(Number(idOrName))) {
169 id = Number(idOrName);
172 let communityForm: GetCommunityForm = id ? { id } : { name: name_ };
173 setAuth(communityForm, req.auth);
174 promises.push(req.client.getCommunity(communityForm));
176 let dataType: DataType = pathSplit[4]
177 ? DataType[pathSplit[4]]
180 let sort: SortType = pathSplit[6]
181 ? SortType[pathSplit[6]]
182 : UserService.Instance.user
183 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
186 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
188 if (dataType == DataType.Post) {
189 let getPostsForm: GetPostsForm = {
193 type_: ListingType.Community,
195 this.setIdOrName(getPostsForm, id, name_);
196 setAuth(getPostsForm, req.auth);
197 promises.push(req.client.getPosts(getPostsForm));
199 let getCommentsForm: GetCommentsForm = {
203 type_: ListingType.Community,
205 this.setIdOrName(getCommentsForm, id, name_);
206 setAuth(getCommentsForm, req.auth);
207 promises.push(req.client.getComments(getCommentsForm));
210 promises.push(req.client.listCategories());
215 static setIdOrName(obj: any, id: number, name_: string) {
217 obj.community_id = id;
219 obj.community_name = name_;
223 componentDidUpdate(_: any, lastState: State) {
225 lastState.dataType !== this.state.dataType ||
226 lastState.sort !== this.state.sort ||
227 lastState.page !== this.state.page
229 this.setState({ postsLoading: true, commentsLoading: true });
234 get documentTitle(): string {
235 return `${this.state.communityRes.community.title} - ${this.state.siteRes.site.name}`;
240 <div class="container">
241 {this.state.communityLoading ? (
243 <svg class="icon icon-spinner spin">
244 <use xlinkHref="#icon-spinner"></use>
249 <div class="col-12 col-md-8">
251 title={this.documentTitle}
252 path={this.context.router.route.match.url}
253 description={this.state.communityRes.community.description}
254 image={this.state.communityRes.community.icon}
256 {this.communityInfo()}
261 <div class="col-12 col-md-4">
263 community={this.state.communityRes.community}
264 moderators={this.state.communityRes.moderators}
265 admins={this.state.siteRes.admins}
266 online={this.state.communityRes.online}
267 enableNsfw={this.state.siteRes.site.enable_nsfw}
268 categories={this.state.categories}
278 return this.state.dataType == DataType.Post ? (
279 this.state.postsLoading ? (
281 <svg class="icon icon-spinner spin">
282 <use xlinkHref="#icon-spinner"></use>
287 posts={this.state.posts}
289 sort={this.state.sort}
290 enableDownvotes={this.state.siteRes.site.enable_downvotes}
291 enableNsfw={this.state.siteRes.site.enable_nsfw}
294 ) : this.state.commentsLoading ? (
296 <svg class="icon icon-spinner spin">
297 <use xlinkHref="#icon-spinner"></use>
302 nodes={commentsToFlatNodes(this.state.comments)}
304 sortType={this.state.sort}
306 enableDownvotes={this.state.siteRes.site.enable_downvotes}
315 banner={this.state.communityRes.community.banner}
316 icon={this.state.communityRes.community.icon}
318 <h5 class="mb-0">{this.state.communityRes.community.title}</h5>
320 community={this.state.communityRes.community}
336 type_={this.state.dataType}
337 onChange={this.handleDataTypeChange}
341 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
344 href={communityRSSUrl(
345 this.state.communityRes.community.actor_id,
352 <svg class="icon text-muted small">
353 <use xlinkHref="#icon-rss">#</use>
363 {this.state.page > 1 && (
365 class="btn btn-secondary mr-1"
366 onClick={linkEvent(this, this.prevPage)}
371 {this.state.posts.length > 0 && (
373 class="btn btn-secondary"
374 onClick={linkEvent(this, this.nextPage)}
383 nextPage(i: Community) {
384 i.updateUrl({ page: i.state.page + 1 });
385 window.scrollTo(0, 0);
388 prevPage(i: Community) {
389 i.updateUrl({ page: i.state.page - 1 });
390 window.scrollTo(0, 0);
393 handleSortChange(val: SortType) {
394 this.updateUrl({ sort: val, page: 1 });
395 window.scrollTo(0, 0);
398 handleDataTypeChange(val: DataType) {
399 this.updateUrl({ dataType: DataType[val], page: 1 });
400 window.scrollTo(0, 0);
403 updateUrl(paramUpdates: UrlParams) {
404 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
405 const sortStr = paramUpdates.sort || this.state.sort;
406 const page = paramUpdates.page || this.state.page;
407 this.props.history.push(
408 `/c/${this.state.communityRes.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
413 if (this.state.dataType == DataType.Post) {
414 let getPostsForm: GetPostsForm = {
415 page: this.state.page,
417 sort: this.state.sort,
418 type_: ListingType.Community,
419 community_id: this.state.communityId,
420 community_name: this.state.communityName,
422 WebSocketService.Instance.getPosts(getPostsForm);
424 let getCommentsForm: GetCommentsForm = {
425 page: this.state.page,
427 sort: this.state.sort,
428 type_: ListingType.Community,
429 community_id: this.state.communityId,
430 community_name: this.state.communityName,
432 WebSocketService.Instance.getComments(getCommentsForm);
436 parseMessage(msg: WebSocketJsonResponse) {
438 let res = wsJsonToRes(msg);
440 toast(i18n.t(msg.error), 'danger');
441 this.context.router.history.push('/');
443 } else if (msg.reconnect) {
444 WebSocketService.Instance.communityJoin({
445 community_id: this.state.communityRes.community.id,
448 } else if (res.op == UserOperation.GetCommunity) {
449 let data = res.data as GetCommunityResponse;
450 this.state.communityRes = data;
451 this.state.communityLoading = false;
452 this.setState(this.state);
453 WebSocketService.Instance.communityJoin({
454 community_id: data.community.id,
457 res.op == UserOperation.EditCommunity ||
458 res.op == UserOperation.DeleteCommunity ||
459 res.op == UserOperation.RemoveCommunity
461 let data = res.data as CommunityResponse;
462 this.state.communityRes.community = data.community;
463 this.setState(this.state);
464 } else if (res.op == UserOperation.FollowCommunity) {
465 let data = res.data as CommunityResponse;
466 this.state.communityRes.community.subscribed = data.community.subscribed;
467 this.state.communityRes.community.number_of_subscribers =
468 data.community.number_of_subscribers;
469 this.setState(this.state);
470 } else if (res.op == UserOperation.GetPosts) {
471 let data = res.data as GetPostsResponse;
472 this.state.posts = data.posts;
473 this.state.postsLoading = false;
474 this.setState(this.state);
477 res.op == UserOperation.EditPost ||
478 res.op == UserOperation.DeletePost ||
479 res.op == UserOperation.RemovePost ||
480 res.op == UserOperation.LockPost ||
481 res.op == UserOperation.StickyPost ||
482 res.op == UserOperation.SavePost
484 let data = res.data as PostResponse;
485 editPostFindRes(data, this.state.posts);
486 this.setState(this.state);
487 } else if (res.op == UserOperation.CreatePost) {
488 let data = res.data as PostResponse;
489 this.state.posts.unshift(data.post);
490 notifyPost(data.post, this.context.router);
491 this.setState(this.state);
492 } else if (res.op == UserOperation.CreatePostLike) {
493 let data = res.data as PostResponse;
494 createPostLikeFindRes(data, this.state.posts);
495 this.setState(this.state);
496 } else if (res.op == UserOperation.AddModToCommunity) {
497 let data = res.data as AddModToCommunityResponse;
498 this.state.communityRes.moderators = data.moderators;
499 this.setState(this.state);
500 } else if (res.op == UserOperation.BanFromCommunity) {
501 let data = res.data as BanFromCommunityResponse;
504 .filter(p => p.creator_id == data.user.id)
505 .forEach(p => (p.banned = data.banned));
507 this.setState(this.state);
508 } else if (res.op == UserOperation.GetComments) {
509 let data = res.data as GetCommentsResponse;
510 this.state.comments = data.comments;
511 this.state.commentsLoading = false;
512 this.setState(this.state);
514 res.op == UserOperation.EditComment ||
515 res.op == UserOperation.DeleteComment ||
516 res.op == UserOperation.RemoveComment
518 let data = res.data as CommentResponse;
519 editCommentRes(data, this.state.comments);
520 this.setState(this.state);
521 } else if (res.op == UserOperation.CreateComment) {
522 let data = res.data as CommentResponse;
524 // Necessary since it might be a user reply
525 if (data.recipient_ids.length == 0) {
526 this.state.comments.unshift(data.comment);
527 this.setState(this.state);
529 } else if (res.op == UserOperation.SaveComment) {
530 let data = res.data as CommentResponse;
531 saveCommentRes(data, this.state.comments);
532 this.setState(this.state);
533 } else if (res.op == UserOperation.CreateCommentLike) {
534 let data = res.data as CommentResponse;
535 createCommentLikeRes(data, this.state.comments);
536 this.setState(this.state);
537 } else if (res.op == UserOperation.ListCategories) {
538 let data = res.data as ListCategoriesResponse;
539 this.state.categories = data.categories;
540 this.setState(this.state);