1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
8 GetFollowedCommunitiesResponse,
10 ListCommunitiesResponse,
27 WebSocketJsonResponse,
28 } from '../interfaces';
29 import { WebSocketService, UserService } from '../services';
30 import { PostListings } from './post-listings';
31 import { CommentNodes } from './comment-nodes';
32 import { SortSelect } from './sort-select';
33 import { ListingTypeSelect } from './listing-type-select';
34 import { DataTypeSelect } from './data-type-select';
35 import { SiteForm } from './site-form';
36 import { UserListing } from './user-listing';
37 import { CommunityLink } from './community-link';
44 getListingTypeFromProps,
51 createPostLikeFindRes,
56 import { i18n } from '../i18next';
57 import { T } from 'inferno-i18next';
60 subscribedCommunities: Array<CommunityUser>;
61 trendingCommunities: Array<Community>;
62 siteRes: GetSiteResponse;
63 showEditSite: boolean;
66 comments: Array<Comment>;
67 listingType: ListingType;
73 export class Main extends Component<any, MainState> {
74 private subscription: Subscription;
75 private emptyState: MainState = {
76 subscribedCommunities: [],
77 trendingCommunities: [],
85 number_of_users: null,
86 number_of_posts: null,
87 number_of_comments: null,
88 number_of_communities: null,
89 enable_downvotes: null,
90 open_registration: null,
101 listingType: getListingTypeFromProps(this.props),
102 dataType: getDataTypeFromProps(this.props),
103 sort: getSortTypeFromProps(this.props),
104 page: getPageFromProps(this.props),
107 constructor(props: any, context: any) {
108 super(props, context);
110 this.state = this.emptyState;
111 this.handleEditCancel = this.handleEditCancel.bind(this);
112 this.handleSortChange = this.handleSortChange.bind(this);
113 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
114 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
116 this.subscription = WebSocketService.Instance.subject
117 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
119 msg => this.parseMessage(msg),
120 err => console.error(err),
121 () => console.log('complete')
124 WebSocketService.Instance.getSite();
126 if (UserService.Instance.user) {
127 WebSocketService.Instance.getFollowedCommunities();
130 let listCommunitiesForm: ListCommunitiesForm = {
131 sort: SortType[SortType.Hot],
135 WebSocketService.Instance.listCommunities(listCommunitiesForm);
140 componentWillUnmount() {
141 this.subscription.unsubscribe();
144 static getDerivedStateFromProps(props) {
146 listingType: getListingTypeFromProps(props),
147 dataType: getDataTypeFromProps(props),
148 sort: getSortTypeFromProps(props),
149 page: getPageFromProps(props),
153 componentDidUpdate(_, lastState) {
155 lastState.listingType !== this.state.listingType ||
156 lastState.dataType !== this.state.dataType ||
157 lastState.sort !== this.state.sort ||
158 lastState.page !== this.state.page
160 this.setState({ loading: true });
167 <div class="container">
169 <main role="main" class="col-12 col-md-8">
172 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
181 {!this.state.loading && (
183 <div class="card border-secondary mb-3">
184 <div class="card-body">
185 {this.trendingCommunities()}
186 {UserService.Instance.user &&
187 this.state.subscribedCommunities.length > 0 && (
190 <T i18nKey="subscribed_to_communities">
192 <Link class="text-body" to="/communities">
197 <ul class="list-inline">
198 {this.state.subscribedCommunities.map(community => (
199 <li class="list-inline-item">
202 name: community.community_name,
203 id: community.community_id,
204 local: community.community_local,
205 actor_id: community.community_actor_id,
214 class="btn btn-sm btn-secondary btn-block"
215 to="/create_community"
217 {i18n.t('create_a_community')}
229 trendingCommunities() {
233 <T i18nKey="trending_communities">
235 <Link class="text-body" to="/communities">
240 <ul class="list-inline">
241 {this.state.trendingCommunities.map(community => (
242 <li class="list-inline-item">
243 <CommunityLink community={community} />
254 {!this.state.showEditSite ? (
258 site={this.state.siteRes.site}
259 onCancel={this.handleEditCancel}
266 updateUrl(paramUpdates: {
267 listing_type?: string;
272 const listingTypeStr =
273 paramUpdates.listing_type ||
274 ListingType[this.state.listingType].toLowerCase();
276 paramUpdates.data_type || DataType[this.state.dataType].toLowerCase();
278 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
279 const page = paramUpdates.page || this.state.page;
280 this.props.history.push(
281 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
288 <div class="card border-secondary mb-3">
289 <div class="card-body">
290 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
292 <ul class="list-inline mb-1 text-muted font-weight-bold">
293 <li className="list-inline-item-action">
296 onClick={linkEvent(this, this.handleEditClick)}
297 data-tippy-content={i18n.t('edit')}
299 <svg class="icon icon-inline">
300 <use xlinkHref="#icon-edit"></use>
306 <ul class="my-2 list-inline">
308 <li className="list-inline-item badge badge-secondary">
309 {i18n.t('number_online', { count: this.state.siteRes.online })}
312 <li className="list-inline-item badge badge-secondary">
313 {i18n.t('number_of_users', {
314 count: this.state.siteRes.site.number_of_users,
317 <li className="list-inline-item badge badge-secondary">
318 {i18n.t('number_of_communities', {
319 count: this.state.siteRes.site.number_of_communities,
322 <li className="list-inline-item badge badge-secondary">
323 {i18n.t('number_of_posts', {
324 count: this.state.siteRes.site.number_of_posts,
327 <li className="list-inline-item badge badge-secondary">
328 {i18n.t('number_of_comments', {
329 count: this.state.siteRes.site.number_of_comments,
332 <li className="list-inline-item">
333 <Link className="badge badge-secondary" to="/modlog">
338 <ul class="mt-1 list-inline small mb-0">
339 <li class="list-inline-item">{i18n.t('admins')}:</li>
340 {this.state.siteRes.admins.map(admin => (
341 <li class="list-inline-item">
345 avatar: admin.avatar,
347 actor_id: admin.actor_id,
356 {this.state.siteRes.site.description && (
357 <div class="card border-secondary mb-3">
358 <div class="card-body">
361 dangerouslySetInnerHTML={mdToHtml(
362 this.state.siteRes.site.description
374 <div class="card border-secondary">
375 <div class="card-body">
377 {i18n.t('powered_by')}
378 <svg class="icon mx-2">
379 <use xlinkHref="#icon-mouse">#</use>
386 <T i18nKey="landing_0">
388 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
391 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
392 <br class="big"></br>
396 <br class="big"></br>
397 <a href={repoUrl}>#</a>
398 <br class="big"></br>
399 <a href="https://www.rust-lang.org">#</a>
400 <a href="https://actix.rs/">#</a>
401 <a href="https://infernojs.org">#</a>
402 <a href="https://www.typescriptlang.org/">#</a>
403 <br class="big"></br>
404 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
416 <div class="main-content-wrapper">
418 {this.state.loading ? (
420 <svg class="icon icon-spinner spin">
421 <use xlinkHref="#icon-spinner"></use>
435 return this.state.dataType == DataType.Post ? (
437 posts={this.state.posts}
440 sort={this.state.sort}
441 enableDownvotes={this.state.siteRes.site.enable_downvotes}
442 enableNsfw={this.state.siteRes.site.enable_nsfw}
446 nodes={commentsToFlatNodes(this.state.comments)}
449 sortType={this.state.sort}
451 enableDownvotes={this.state.siteRes.site.enable_downvotes}
458 <div className="mb-3">
461 type_={this.state.dataType}
462 onChange={this.handleDataTypeChange}
467 type_={this.state.listingType}
468 onChange={this.handleListingTypeChange}
472 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
474 {this.state.listingType == ListingType.All && (
476 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
481 <svg class="icon text-muted small">
482 <use xlinkHref="#icon-rss">#</use>
486 {UserService.Instance.user &&
487 this.state.listingType == ListingType.Subscribed && (
489 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
490 SortType[this.state.sort]
496 <svg class="icon text-muted small">
497 <use xlinkHref="#icon-rss">#</use>
508 {this.state.page > 1 && (
510 class="btn btn-sm btn-secondary mr-1"
511 onClick={linkEvent(this, this.prevPage)}
516 {this.state.posts.length > 0 && (
518 class="btn btn-sm btn-secondary"
519 onClick={linkEvent(this, this.nextPage)}
528 get canAdmin(): boolean {
530 UserService.Instance.user &&
531 this.state.siteRes.admins
533 .includes(UserService.Instance.user.id)
537 handleEditClick(i: Main) {
538 i.state.showEditSite = true;
543 this.state.showEditSite = false;
544 this.setState(this.state);
548 i.updateUrl({ page: this.state.page + 1 });
549 window.scrollTo(0, 0);
553 i.updateUrl({ page: this.state.page - 1 });
554 window.scrollTo(0, 0);
557 handleSortChange(val: SortType) {
558 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
559 window.scrollTo(0, 0);
562 handleListingTypeChange(val: ListingType) {
563 this.updateUrl({ listing_type: ListingType[val].toLowerCase(), page: 1 });
564 window.scrollTo(0, 0);
567 handleDataTypeChange(val: DataType) {
568 this.updateUrl({ data_type: DataType[val].toLowerCase(), page: 1 });
569 window.scrollTo(0, 0);
573 if (this.state.dataType == DataType.Post) {
574 let getPostsForm: GetPostsForm = {
575 page: this.state.page,
577 sort: SortType[this.state.sort],
578 type_: ListingType[this.state.listingType],
580 WebSocketService.Instance.getPosts(getPostsForm);
582 let getCommentsForm: GetCommentsForm = {
583 page: this.state.page,
585 sort: SortType[this.state.sort],
586 type_: ListingType[this.state.listingType],
588 WebSocketService.Instance.getComments(getCommentsForm);
592 parseMessage(msg: WebSocketJsonResponse) {
594 let res = wsJsonToRes(msg);
596 toast(i18n.t(msg.error), 'danger');
598 } else if (msg.reconnect) {
600 } else if (res.op == UserOperation.GetFollowedCommunities) {
601 let data = res.data as GetFollowedCommunitiesResponse;
602 this.state.subscribedCommunities = data.communities;
603 this.setState(this.state);
604 } else if (res.op == UserOperation.ListCommunities) {
605 let data = res.data as ListCommunitiesResponse;
606 this.state.trendingCommunities = data.communities;
607 this.setState(this.state);
608 } else if (res.op == UserOperation.GetSite) {
609 let data = res.data as GetSiteResponse;
611 // This means it hasn't been set up yet
613 this.context.router.history.push('/setup');
615 this.state.siteRes.admins = data.admins;
616 this.state.siteRes.site = data.site;
617 this.state.siteRes.banned = data.banned;
618 this.state.siteRes.online = data.online;
619 this.setState(this.state);
620 document.title = `${this.state.siteRes.site.name}`;
621 } else if (res.op == UserOperation.EditSite) {
622 let data = res.data as SiteResponse;
623 this.state.siteRes.site = data.site;
624 this.state.showEditSite = false;
625 this.setState(this.state);
626 toast(i18n.t('site_saved'));
627 } else if (res.op == UserOperation.GetPosts) {
628 let data = res.data as GetPostsResponse;
629 this.state.posts = data.posts;
630 this.state.loading = false;
631 this.setState(this.state);
633 } else if (res.op == UserOperation.CreatePost) {
634 let data = res.data as PostResponse;
636 // If you're on subscribed, only push it if you're subscribed.
637 if (this.state.listingType == ListingType.Subscribed) {
639 this.state.subscribedCommunities
640 .map(c => c.community_id)
641 .includes(data.post.community_id)
643 this.state.posts.unshift(data.post);
647 let nsfw = data.post.nsfw || data.post.community_nsfw;
649 // Don't push the post if its nsfw, and don't have that setting on
653 UserService.Instance.user &&
654 UserService.Instance.user.show_nsfw)
656 this.state.posts.unshift(data.post);
659 this.setState(this.state);
660 } else if (res.op == UserOperation.EditPost) {
661 let data = res.data as PostResponse;
662 editPostFindRes(data, this.state.posts);
663 this.setState(this.state);
664 } else if (res.op == UserOperation.CreatePostLike) {
665 let data = res.data as PostResponse;
666 createPostLikeFindRes(data, this.state.posts);
667 this.setState(this.state);
668 } else if (res.op == UserOperation.AddAdmin) {
669 let data = res.data as AddAdminResponse;
670 this.state.siteRes.admins = data.admins;
671 this.setState(this.state);
672 } else if (res.op == UserOperation.BanUser) {
673 let data = res.data as BanUserResponse;
674 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
676 // Remove the banned if its found in the list, and the action is an unban
677 if (found && !data.banned) {
678 this.state.siteRes.banned = this.state.siteRes.banned.filter(
679 i => i.id !== data.user.id
682 this.state.siteRes.banned.push(data.user);
686 .filter(p => p.creator_id == data.user.id)
687 .forEach(p => (p.banned = data.banned));
689 this.setState(this.state);
690 } else if (res.op == UserOperation.GetComments) {
691 let data = res.data as GetCommentsResponse;
692 this.state.comments = data.comments;
693 this.state.loading = false;
694 this.setState(this.state);
695 } else if (res.op == UserOperation.EditComment) {
696 let data = res.data as CommentResponse;
697 editCommentRes(data, this.state.comments);
698 this.setState(this.state);
699 } else if (res.op == UserOperation.CreateComment) {
700 let data = res.data as CommentResponse;
702 // Necessary since it might be a user reply
703 if (data.recipient_ids.length == 0) {
704 // If you're on subscribed, only push it if you're subscribed.
705 if (this.state.listingType == ListingType.Subscribed) {
707 this.state.subscribedCommunities
708 .map(c => c.community_id)
709 .includes(data.comment.community_id)
711 this.state.comments.unshift(data.comment);
714 this.state.comments.unshift(data.comment);
716 this.setState(this.state);
718 } else if (res.op == UserOperation.SaveComment) {
719 let data = res.data as CommentResponse;
720 saveCommentRes(data, this.state.comments);
721 this.setState(this.state);
722 } else if (res.op == UserOperation.CreateCommentLike) {
723 let data = res.data as CommentResponse;
724 createCommentLikeRes(data, this.state.comments);
725 this.setState(this.state);