1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Link } from 'inferno-router';
4 import { Subscription } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
9 GetFollowedCommunitiesResponse,
11 ListCommunitiesResponse,
28 WebSocketJsonResponse,
29 } from '../interfaces';
30 import { WebSocketService, UserService } from '../services';
31 import { PostListings } from './post-listings';
32 import { CommentNodes } from './comment-nodes';
33 import { SortSelect } from './sort-select';
34 import { ListingTypeSelect } from './listing-type-select';
35 import { DataTypeSelect } from './data-type-select';
36 import { SiteForm } from './site-form';
37 import { UserListing } from './user-listing';
38 import { CommunityLink } from './community-link';
39 import { BannerIconHeader } from './banner-icon-header';
46 getListingTypeFromProps,
53 createPostLikeFindRes,
59 import { i18n } from '../i18next';
60 import { T } from 'inferno-i18next';
63 subscribedCommunities: Array<CommunityUser>;
64 trendingCommunities: Array<Community>;
65 siteRes: GetSiteResponse;
66 showEditSite: boolean;
69 comments: Array<Comment>;
70 listingType: ListingType;
77 listingType: ListingType;
90 export class Main extends Component<any, MainState> {
91 private subscription: Subscription;
92 private emptyState: MainState = {
93 subscribedCommunities: [],
94 trendingCommunities: [],
102 number_of_users: null,
103 number_of_posts: null,
104 number_of_comments: null,
105 number_of_communities: null,
106 enable_downvotes: null,
107 open_registration: null,
111 creator_preferred_username: null,
117 federated_instances: null,
123 listingType: getListingTypeFromProps(this.props),
124 dataType: getDataTypeFromProps(this.props),
125 sort: getSortTypeFromProps(this.props),
126 page: getPageFromProps(this.props),
129 constructor(props: any, context: any) {
130 super(props, context);
132 this.state = this.emptyState;
133 this.handleEditCancel = this.handleEditCancel.bind(this);
134 this.handleSortChange = this.handleSortChange.bind(this);
135 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
136 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
138 this.subscription = WebSocketService.Instance.subject
139 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
141 msg => this.parseMessage(msg),
142 err => console.error(err),
143 () => console.log('complete')
146 WebSocketService.Instance.getSite();
148 if (UserService.Instance.user) {
149 WebSocketService.Instance.getFollowedCommunities();
152 let listCommunitiesForm: ListCommunitiesForm = {
153 sort: SortType[SortType.Hot],
157 WebSocketService.Instance.listCommunities(listCommunitiesForm);
162 componentWillUnmount() {
163 this.subscription.unsubscribe();
166 static getDerivedStateFromProps(props: any): MainProps {
168 listingType: getListingTypeFromProps(props),
169 dataType: getDataTypeFromProps(props),
170 sort: getSortTypeFromProps(props),
171 page: getPageFromProps(props),
175 componentDidUpdate(_: any, lastState: MainState) {
177 lastState.listingType !== this.state.listingType ||
178 lastState.dataType !== this.state.dataType ||
179 lastState.sort !== this.state.sort ||
180 lastState.page !== this.state.page
182 this.setState({ loading: true });
187 get documentTitle(): string {
188 if (this.state.siteRes.site.name) {
189 return `${this.state.siteRes.site.name}`;
195 get favIcon(): string {
196 return this.state.siteRes.site.icon
197 ? this.state.siteRes.site.icon
203 <div class="container">
204 <Helmet title={this.documentTitle}>
213 <main role="main" class="col-12 col-md-8">
216 <aside class="col-12 col-md-4">{this.mySidebar()}</aside>
225 {!this.state.loading && (
227 <div class="card bg-transparent border-secondary mb-3">
228 <div class="card-header bg-transparent border-secondary">
231 {this.adminButtons()}
233 <BannerIconHeader banner={this.state.siteRes.site.banner} />
235 <div class="card-body">
236 {this.trendingCommunities()}
237 {this.createCommunityButton()}
239 {this.subscribedCommunities()}
244 <div class="card bg-transparent border-secondary mb-3">
245 <div class="card-body">{this.sidebar()}</div>
248 <div class="card bg-transparent border-secondary">
249 <div class="card-body">{this.landing()}</div>
257 createCommunityButton() {
259 <Link class="btn btn-secondary btn-block" to="/create_community">
260 {i18n.t('create_a_community')}
265 trendingCommunities() {
269 <T i18nKey="trending_communities">
271 <Link class="text-body" to="/communities">
276 <ul class="list-inline">
277 {this.state.trendingCommunities.map(community => (
278 <li class="list-inline-item">
279 <CommunityLink community={community} />
287 subscribedCommunities() {
289 UserService.Instance.user &&
290 this.state.subscribedCommunities.length > 0 && (
293 <T i18nKey="subscribed_to_communities">
295 <Link class="text-body" to="/communities">
300 <ul class="list-inline">
301 {this.state.subscribedCommunities.map(community => (
302 <li class="list-inline-item">
305 name: community.community_name,
306 id: community.community_id,
307 local: community.community_local,
308 actor_id: community.community_actor_id,
309 icon: community.community_icon,
323 {!this.state.showEditSite ? (
327 site={this.state.siteRes.site}
328 onCancel={this.handleEditCancel}
335 updateUrl(paramUpdates: UrlParams) {
336 const listingTypeStr =
337 paramUpdates.listingType ||
338 ListingType[this.state.listingType].toLowerCase();
340 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
342 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
343 const page = paramUpdates.page || this.state.page;
344 this.props.history.push(
345 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
352 {this.state.siteRes.site.description && this.siteDescription()}
360 return <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>;
365 <ul class="mt-1 list-inline small mb-0">
366 <li class="list-inline-item">{i18n.t('admins')}:</li>
367 {this.state.siteRes.admins.map(admin => (
368 <li class="list-inline-item">
372 preferred_username: admin.preferred_username,
373 avatar: admin.avatar,
375 actor_id: admin.actor_id,
387 <ul class="my-2 list-inline">
389 <li className="list-inline-item badge badge-light">
390 {i18n.t('number_online', { count: this.state.siteRes.online })}
393 <li className="list-inline-item badge badge-light">
394 {i18n.t('number_of_users', {
395 count: this.state.siteRes.site.number_of_users,
398 <li className="list-inline-item badge badge-light">
399 {i18n.t('number_of_communities', {
400 count: this.state.siteRes.site.number_of_communities,
403 <li className="list-inline-item badge badge-light">
404 {i18n.t('number_of_posts', {
405 count: this.state.siteRes.site.number_of_posts,
408 <li className="list-inline-item badge badge-light">
409 {i18n.t('number_of_comments', {
410 count: this.state.siteRes.site.number_of_comments,
413 <li className="list-inline-item">
414 <Link className="badge badge-light" to="/modlog">
425 <ul class="list-inline mb-1 text-muted font-weight-bold">
426 <li className="list-inline-item-action">
429 onClick={linkEvent(this, this.handleEditClick)}
430 data-tippy-content={i18n.t('edit')}
432 <svg class="icon icon-inline">
433 <use xlinkHref="#icon-edit"></use>
446 dangerouslySetInnerHTML={mdToHtml(this.state.siteRes.site.description)}
455 {i18n.t('powered_by')}
456 <svg class="icon mx-2">
457 <use xlinkHref="#icon-mouse">#</use>
464 <T i18nKey="landing_0">
466 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
469 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
470 <br class="big"></br>
474 <br class="big"></br>
475 <a href={repoUrl}>#</a>
476 <br class="big"></br>
477 <a href="https://www.rust-lang.org">#</a>
478 <a href="https://actix.rs/">#</a>
479 <a href="https://infernojs.org">#</a>
480 <a href="https://www.typescriptlang.org/">#</a>
481 <br class="big"></br>
482 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
493 <div class="main-content-wrapper">
494 {this.state.loading ? (
496 <svg class="icon icon-spinner spin">
497 <use xlinkHref="#icon-spinner"></use>
512 return this.state.dataType == DataType.Post ? (
514 posts={this.state.posts}
517 sort={this.state.sort}
518 enableDownvotes={this.state.siteRes.site.enable_downvotes}
519 enableNsfw={this.state.siteRes.site.enable_nsfw}
523 nodes={commentsToFlatNodes(this.state.comments)}
526 sortType={this.state.sort}
528 enableDownvotes={this.state.siteRes.site.enable_downvotes}
535 <div className="mb-3">
538 type_={this.state.dataType}
539 onChange={this.handleDataTypeChange}
544 type_={this.state.listingType}
545 onChange={this.handleListingTypeChange}
549 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
551 {this.state.listingType == ListingType.All && (
553 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
558 <svg class="icon text-muted small">
559 <use xlinkHref="#icon-rss">#</use>
563 {UserService.Instance.user &&
564 this.state.listingType == ListingType.Subscribed && (
566 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
567 SortType[this.state.sort]
573 <svg class="icon text-muted small">
574 <use xlinkHref="#icon-rss">#</use>
585 {this.state.page > 1 && (
587 class="btn btn-secondary mr-1"
588 onClick={linkEvent(this, this.prevPage)}
593 {this.state.posts.length > 0 && (
595 class="btn btn-secondary"
596 onClick={linkEvent(this, this.nextPage)}
605 get canAdmin(): boolean {
607 UserService.Instance.user &&
608 this.state.siteRes.admins
610 .includes(UserService.Instance.user.id)
614 handleEditClick(i: Main) {
615 i.state.showEditSite = true;
620 this.state.showEditSite = false;
621 this.setState(this.state);
625 i.updateUrl({ page: i.state.page + 1 });
626 window.scrollTo(0, 0);
630 i.updateUrl({ page: i.state.page - 1 });
631 window.scrollTo(0, 0);
634 handleSortChange(val: SortType) {
635 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
636 window.scrollTo(0, 0);
639 handleListingTypeChange(val: ListingType) {
640 this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
641 window.scrollTo(0, 0);
644 handleDataTypeChange(val: DataType) {
645 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
646 window.scrollTo(0, 0);
650 if (this.state.dataType == DataType.Post) {
651 let getPostsForm: GetPostsForm = {
652 page: this.state.page,
654 sort: SortType[this.state.sort],
655 type_: ListingType[this.state.listingType],
657 WebSocketService.Instance.getPosts(getPostsForm);
659 let getCommentsForm: GetCommentsForm = {
660 page: this.state.page,
662 sort: SortType[this.state.sort],
663 type_: ListingType[this.state.listingType],
665 WebSocketService.Instance.getComments(getCommentsForm);
669 parseMessage(msg: WebSocketJsonResponse) {
671 let res = wsJsonToRes(msg);
673 toast(i18n.t(msg.error), 'danger');
675 } else if (msg.reconnect) {
677 } else if (res.op == UserOperation.GetFollowedCommunities) {
678 let data = res.data as GetFollowedCommunitiesResponse;
679 this.state.subscribedCommunities = data.communities;
680 this.setState(this.state);
681 } else if (res.op == UserOperation.ListCommunities) {
682 let data = res.data as ListCommunitiesResponse;
683 this.state.trendingCommunities = data.communities;
684 this.setState(this.state);
685 } else if (res.op == UserOperation.GetSite) {
686 let data = res.data as GetSiteResponse;
688 // This means it hasn't been set up yet
690 this.context.router.history.push('/setup');
692 this.state.siteRes.admins = data.admins;
693 this.state.siteRes.site = data.site;
694 this.state.siteRes.banned = data.banned;
695 this.state.siteRes.online = data.online;
696 this.setState(this.state);
697 } else if (res.op == UserOperation.EditSite) {
698 let data = res.data as SiteResponse;
699 this.state.siteRes.site = data.site;
700 this.state.showEditSite = false;
701 this.setState(this.state);
702 toast(i18n.t('site_saved'));
703 } else if (res.op == UserOperation.GetPosts) {
704 let data = res.data as GetPostsResponse;
705 this.state.posts = data.posts;
706 this.state.loading = false;
707 this.setState(this.state);
709 } else if (res.op == UserOperation.CreatePost) {
710 let data = res.data as PostResponse;
712 // If you're on subscribed, only push it if you're subscribed.
713 if (this.state.listingType == ListingType.Subscribed) {
715 this.state.subscribedCommunities
716 .map(c => c.community_id)
717 .includes(data.post.community_id)
719 this.state.posts.unshift(data.post);
723 let nsfw = data.post.nsfw || data.post.community_nsfw;
725 // Don't push the post if its nsfw, and don't have that setting on
729 UserService.Instance.user &&
730 UserService.Instance.user.show_nsfw)
732 this.state.posts.unshift(data.post);
735 this.setState(this.state);
736 } else if (res.op == UserOperation.EditPost) {
737 let data = res.data as PostResponse;
738 editPostFindRes(data, this.state.posts);
739 this.setState(this.state);
740 } else if (res.op == UserOperation.CreatePostLike) {
741 let data = res.data as PostResponse;
742 createPostLikeFindRes(data, this.state.posts);
743 this.setState(this.state);
744 } else if (res.op == UserOperation.AddAdmin) {
745 let data = res.data as AddAdminResponse;
746 this.state.siteRes.admins = data.admins;
747 this.setState(this.state);
748 } else if (res.op == UserOperation.BanUser) {
749 let data = res.data as BanUserResponse;
750 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
752 // Remove the banned if its found in the list, and the action is an unban
753 if (found && !data.banned) {
754 this.state.siteRes.banned = this.state.siteRes.banned.filter(
755 i => i.id !== data.user.id
758 this.state.siteRes.banned.push(data.user);
762 .filter(p => p.creator_id == data.user.id)
763 .forEach(p => (p.banned = data.banned));
765 this.setState(this.state);
766 } else if (res.op == UserOperation.GetComments) {
767 let data = res.data as GetCommentsResponse;
768 this.state.comments = data.comments;
769 this.state.loading = false;
770 this.setState(this.state);
772 res.op == UserOperation.EditComment ||
773 res.op == UserOperation.DeleteComment ||
774 res.op == UserOperation.RemoveComment
776 let data = res.data as CommentResponse;
777 editCommentRes(data, this.state.comments);
778 this.setState(this.state);
779 } else if (res.op == UserOperation.CreateComment) {
780 let data = res.data as CommentResponse;
782 // Necessary since it might be a user reply
783 if (data.recipient_ids.length == 0) {
784 // If you're on subscribed, only push it if you're subscribed.
785 if (this.state.listingType == ListingType.Subscribed) {
787 this.state.subscribedCommunities
788 .map(c => c.community_id)
789 .includes(data.comment.community_id)
791 this.state.comments.unshift(data.comment);
794 this.state.comments.unshift(data.comment);
796 this.setState(this.state);
798 } else if (res.op == UserOperation.SaveComment) {
799 let data = res.data as CommentResponse;
800 saveCommentRes(data, this.state.comments);
801 this.setState(this.state);
802 } else if (res.op == UserOperation.CreateCommentLike) {
803 let data = res.data as CommentResponse;
804 createCommentLikeRes(data, this.state.comments);
805 this.setState(this.state);