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">
388 <li className="list-inline-item badge badge-light">
389 {i18n.t('number_online', { count: this.state.siteRes.online })}
391 <li className="list-inline-item badge badge-light">
392 {i18n.t('number_of_users', {
393 count: this.state.siteRes.site.number_of_users,
396 <li className="list-inline-item badge badge-light">
397 {i18n.t('number_of_communities', {
398 count: this.state.siteRes.site.number_of_communities,
401 <li className="list-inline-item badge badge-light">
402 {i18n.t('number_of_posts', {
403 count: this.state.siteRes.site.number_of_posts,
406 <li className="list-inline-item badge badge-light">
407 {i18n.t('number_of_comments', {
408 count: this.state.siteRes.site.number_of_comments,
411 <li className="list-inline-item">
412 <Link className="badge badge-light" to="/modlog">
423 <ul class="list-inline mb-1 text-muted font-weight-bold">
424 <li className="list-inline-item-action">
427 onClick={linkEvent(this, this.handleEditClick)}
428 data-tippy-content={i18n.t('edit')}
430 <svg class="icon icon-inline">
431 <use xlinkHref="#icon-edit"></use>
444 dangerouslySetInnerHTML={mdToHtml(this.state.siteRes.site.description)}
453 {i18n.t('powered_by')}
454 <svg class="icon mx-2">
455 <use xlinkHref="#icon-mouse">#</use>
462 <T i18nKey="landing_0">
464 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
467 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
468 <br class="big"></br>
472 <br class="big"></br>
473 <a href={repoUrl}>#</a>
474 <br class="big"></br>
475 <a href="https://www.rust-lang.org">#</a>
476 <a href="https://actix.rs/">#</a>
477 <a href="https://infernojs.org">#</a>
478 <a href="https://www.typescriptlang.org/">#</a>
479 <br class="big"></br>
480 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
491 <div class="main-content-wrapper">
492 {this.state.loading ? (
494 <svg class="icon icon-spinner spin">
495 <use xlinkHref="#icon-spinner"></use>
510 return this.state.dataType == DataType.Post ? (
512 posts={this.state.posts}
515 sort={this.state.sort}
516 enableDownvotes={this.state.siteRes.site.enable_downvotes}
517 enableNsfw={this.state.siteRes.site.enable_nsfw}
521 nodes={commentsToFlatNodes(this.state.comments)}
524 sortType={this.state.sort}
526 enableDownvotes={this.state.siteRes.site.enable_downvotes}
533 <div className="mb-3">
536 type_={this.state.dataType}
537 onChange={this.handleDataTypeChange}
542 type_={this.state.listingType}
543 onChange={this.handleListingTypeChange}
547 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
549 {this.state.listingType == ListingType.All && (
551 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
556 <svg class="icon text-muted small">
557 <use xlinkHref="#icon-rss">#</use>
561 {UserService.Instance.user &&
562 this.state.listingType == ListingType.Subscribed && (
564 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
565 SortType[this.state.sort]
571 <svg class="icon text-muted small">
572 <use xlinkHref="#icon-rss">#</use>
583 {this.state.page > 1 && (
585 class="btn btn-secondary mr-1"
586 onClick={linkEvent(this, this.prevPage)}
591 {this.state.posts.length > 0 && (
593 class="btn btn-secondary"
594 onClick={linkEvent(this, this.nextPage)}
603 get canAdmin(): boolean {
605 UserService.Instance.user &&
606 this.state.siteRes.admins
608 .includes(UserService.Instance.user.id)
612 handleEditClick(i: Main) {
613 i.state.showEditSite = true;
618 this.state.showEditSite = false;
619 this.setState(this.state);
623 i.updateUrl({ page: i.state.page + 1 });
624 window.scrollTo(0, 0);
628 i.updateUrl({ page: i.state.page - 1 });
629 window.scrollTo(0, 0);
632 handleSortChange(val: SortType) {
633 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
634 window.scrollTo(0, 0);
637 handleListingTypeChange(val: ListingType) {
638 this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
639 window.scrollTo(0, 0);
642 handleDataTypeChange(val: DataType) {
643 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
644 window.scrollTo(0, 0);
648 if (this.state.dataType == DataType.Post) {
649 let getPostsForm: GetPostsForm = {
650 page: this.state.page,
652 sort: SortType[this.state.sort],
653 type_: ListingType[this.state.listingType],
655 WebSocketService.Instance.getPosts(getPostsForm);
657 let getCommentsForm: GetCommentsForm = {
658 page: this.state.page,
660 sort: SortType[this.state.sort],
661 type_: ListingType[this.state.listingType],
663 WebSocketService.Instance.getComments(getCommentsForm);
667 parseMessage(msg: WebSocketJsonResponse) {
669 let res = wsJsonToRes(msg);
671 toast(i18n.t(msg.error), 'danger');
673 } else if (msg.reconnect) {
675 } else if (res.op == UserOperation.GetFollowedCommunities) {
676 let data = res.data as GetFollowedCommunitiesResponse;
677 this.state.subscribedCommunities = data.communities;
678 this.setState(this.state);
679 } else if (res.op == UserOperation.ListCommunities) {
680 let data = res.data as ListCommunitiesResponse;
681 this.state.trendingCommunities = data.communities;
682 this.setState(this.state);
683 } else if (res.op == UserOperation.GetSite) {
684 let data = res.data as GetSiteResponse;
686 // This means it hasn't been set up yet
688 this.context.router.history.push('/setup');
690 this.state.siteRes.admins = data.admins;
691 this.state.siteRes.site = data.site;
692 this.state.siteRes.banned = data.banned;
693 this.state.siteRes.online = data.online;
694 this.setState(this.state);
695 } else if (res.op == UserOperation.EditSite) {
696 let data = res.data as SiteResponse;
697 this.state.siteRes.site = data.site;
698 this.state.showEditSite = false;
699 this.setState(this.state);
700 toast(i18n.t('site_saved'));
701 } else if (res.op == UserOperation.GetPosts) {
702 let data = res.data as GetPostsResponse;
703 this.state.posts = data.posts;
704 this.state.loading = false;
705 this.setState(this.state);
707 } else if (res.op == UserOperation.CreatePost) {
708 let data = res.data as PostResponse;
710 // If you're on subscribed, only push it if you're subscribed.
711 if (this.state.listingType == ListingType.Subscribed) {
713 this.state.subscribedCommunities
714 .map(c => c.community_id)
715 .includes(data.post.community_id)
717 this.state.posts.unshift(data.post);
721 let nsfw = data.post.nsfw || data.post.community_nsfw;
723 // Don't push the post if its nsfw, and don't have that setting on
727 UserService.Instance.user &&
728 UserService.Instance.user.show_nsfw)
730 this.state.posts.unshift(data.post);
733 this.setState(this.state);
734 } else if (res.op == UserOperation.EditPost) {
735 let data = res.data as PostResponse;
736 editPostFindRes(data, this.state.posts);
737 this.setState(this.state);
738 } else if (res.op == UserOperation.CreatePostLike) {
739 let data = res.data as PostResponse;
740 createPostLikeFindRes(data, this.state.posts);
741 this.setState(this.state);
742 } else if (res.op == UserOperation.AddAdmin) {
743 let data = res.data as AddAdminResponse;
744 this.state.siteRes.admins = data.admins;
745 this.setState(this.state);
746 } else if (res.op == UserOperation.BanUser) {
747 let data = res.data as BanUserResponse;
748 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
750 // Remove the banned if its found in the list, and the action is an unban
751 if (found && !data.banned) {
752 this.state.siteRes.banned = this.state.siteRes.banned.filter(
753 i => i.id !== data.user.id
756 this.state.siteRes.banned.push(data.user);
760 .filter(p => p.creator_id == data.user.id)
761 .forEach(p => (p.banned = data.banned));
763 this.setState(this.state);
764 } else if (res.op == UserOperation.GetComments) {
765 let data = res.data as GetCommentsResponse;
766 this.state.comments = data.comments;
767 this.state.loading = false;
768 this.setState(this.state);
770 res.op == UserOperation.EditComment ||
771 res.op == UserOperation.DeleteComment ||
772 res.op == UserOperation.RemoveComment
774 let data = res.data as CommentResponse;
775 editCommentRes(data, this.state.comments);
776 this.setState(this.state);
777 } else if (res.op == UserOperation.CreateComment) {
778 let data = res.data as CommentResponse;
780 // Necessary since it might be a user reply
781 if (data.recipient_ids.length == 0) {
782 // If you're on subscribed, only push it if you're subscribed.
783 if (this.state.listingType == ListingType.Subscribed) {
785 this.state.subscribedCommunities
786 .map(c => c.community_id)
787 .includes(data.comment.community_id)
789 this.state.comments.unshift(data.comment);
792 this.state.comments.unshift(data.comment);
794 this.setState(this.state);
796 } else if (res.op == UserOperation.SaveComment) {
797 let data = res.data as CommentResponse;
798 saveCommentRes(data, this.state.comments);
799 this.setState(this.state);
800 } else if (res.op == UserOperation.CreateCommentLike) {
801 let data = res.data as CommentResponse;
802 createCommentLikeRes(data, this.state.comments);
803 this.setState(this.state);