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,
122 listingType: getListingTypeFromProps(this.props),
123 dataType: getDataTypeFromProps(this.props),
124 sort: getSortTypeFromProps(this.props),
125 page: getPageFromProps(this.props),
128 constructor(props: any, context: any) {
129 super(props, context);
131 this.state = this.emptyState;
132 this.handleEditCancel = this.handleEditCancel.bind(this);
133 this.handleSortChange = this.handleSortChange.bind(this);
134 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
135 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
137 this.subscription = WebSocketService.Instance.subject
138 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
140 msg => this.parseMessage(msg),
141 err => console.error(err),
142 () => console.log('complete')
145 WebSocketService.Instance.getSite();
147 if (UserService.Instance.user) {
148 WebSocketService.Instance.getFollowedCommunities();
151 let listCommunitiesForm: ListCommunitiesForm = {
152 sort: SortType[SortType.Hot],
156 WebSocketService.Instance.listCommunities(listCommunitiesForm);
161 componentWillUnmount() {
162 this.subscription.unsubscribe();
165 static getDerivedStateFromProps(props: any): MainProps {
167 listingType: getListingTypeFromProps(props),
168 dataType: getDataTypeFromProps(props),
169 sort: getSortTypeFromProps(props),
170 page: getPageFromProps(props),
174 componentDidUpdate(_: any, lastState: MainState) {
176 lastState.listingType !== this.state.listingType ||
177 lastState.dataType !== this.state.dataType ||
178 lastState.sort !== this.state.sort ||
179 lastState.page !== this.state.page
181 this.setState({ loading: true });
186 get documentTitle(): string {
187 if (this.state.siteRes.site.name) {
188 return `${this.state.siteRes.site.name}`;
194 get favIcon(): string {
195 return this.state.siteRes.site.icon
196 ? this.state.siteRes.site.icon
202 <div class="container">
203 <Helmet title={this.documentTitle}>
212 <main role="main" class="col-12 col-md-8">
215 <aside class="col-12 col-md-4">{this.mySidebar()}</aside>
224 {!this.state.loading && (
226 <div class="card bg-transparent border-secondary mb-3">
227 <div class="card-header bg-transparent border-secondary">
230 {this.adminButtons()}
232 <BannerIconHeader banner={this.state.siteRes.site.banner} />
234 <div class="card-body">
235 {this.trendingCommunities()}
236 {this.createCommunityButton()}
238 {this.subscribedCommunities()}
243 <div class="card bg-transparent border-secondary mb-3">
244 <div class="card-body">{this.sidebar()}</div>
247 <div class="card bg-transparent border-secondary">
248 <div class="card-body">{this.landing()}</div>
256 createCommunityButton() {
258 <Link class="btn btn-secondary btn-block" to="/create_community">
259 {i18n.t('create_a_community')}
264 trendingCommunities() {
268 <T i18nKey="trending_communities">
270 <Link class="text-body" to="/communities">
275 <ul class="list-inline">
276 {this.state.trendingCommunities.map(community => (
277 <li class="list-inline-item">
278 <CommunityLink community={community} />
286 subscribedCommunities() {
288 UserService.Instance.user &&
289 this.state.subscribedCommunities.length > 0 && (
292 <T i18nKey="subscribed_to_communities">
294 <Link class="text-body" to="/communities">
299 <ul class="list-inline">
300 {this.state.subscribedCommunities.map(community => (
301 <li class="list-inline-item">
304 name: community.community_name,
305 id: community.community_id,
306 local: community.community_local,
307 actor_id: community.community_actor_id,
308 icon: community.community_icon,
322 {!this.state.showEditSite ? (
326 site={this.state.siteRes.site}
327 onCancel={this.handleEditCancel}
334 updateUrl(paramUpdates: UrlParams) {
335 const listingTypeStr =
336 paramUpdates.listingType ||
337 ListingType[this.state.listingType].toLowerCase();
339 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
341 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
342 const page = paramUpdates.page || this.state.page;
343 this.props.history.push(
344 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
351 {this.state.siteRes.site.description && this.siteDescription()}
359 return <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>;
364 <ul class="mt-1 list-inline small mb-0">
365 <li class="list-inline-item">{i18n.t('admins')}:</li>
366 {this.state.siteRes.admins.map(admin => (
367 <li class="list-inline-item">
371 preferred_username: admin.preferred_username,
372 avatar: admin.avatar,
374 actor_id: admin.actor_id,
386 <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 })}
392 <li className="list-inline-item badge badge-light">
393 {i18n.t('number_of_users', {
394 count: this.state.siteRes.site.number_of_users,
397 <li className="list-inline-item badge badge-light">
398 {i18n.t('number_of_communities', {
399 count: this.state.siteRes.site.number_of_communities,
402 <li className="list-inline-item badge badge-light">
403 {i18n.t('number_of_posts', {
404 count: this.state.siteRes.site.number_of_posts,
407 <li className="list-inline-item badge badge-light">
408 {i18n.t('number_of_comments', {
409 count: this.state.siteRes.site.number_of_comments,
412 <li className="list-inline-item">
413 <Link className="badge badge-light" to="/modlog">
424 <ul class="list-inline mb-1 text-muted font-weight-bold">
425 <li className="list-inline-item-action">
428 onClick={linkEvent(this, this.handleEditClick)}
429 data-tippy-content={i18n.t('edit')}
431 <svg class="icon icon-inline">
432 <use xlinkHref="#icon-edit"></use>
445 dangerouslySetInnerHTML={mdToHtml(this.state.siteRes.site.description)}
454 {i18n.t('powered_by')}
455 <svg class="icon mx-2">
456 <use xlinkHref="#icon-mouse">#</use>
463 <T i18nKey="landing_0">
465 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
468 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
469 <br class="big"></br>
473 <br class="big"></br>
474 <a href={repoUrl}>#</a>
475 <br class="big"></br>
476 <a href="https://www.rust-lang.org">#</a>
477 <a href="https://actix.rs/">#</a>
478 <a href="https://infernojs.org">#</a>
479 <a href="https://www.typescriptlang.org/">#</a>
480 <br class="big"></br>
481 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
492 <div class="main-content-wrapper">
493 {this.state.loading ? (
495 <svg class="icon icon-spinner spin">
496 <use xlinkHref="#icon-spinner"></use>
511 return this.state.dataType == DataType.Post ? (
513 posts={this.state.posts}
516 sort={this.state.sort}
517 enableDownvotes={this.state.siteRes.site.enable_downvotes}
518 enableNsfw={this.state.siteRes.site.enable_nsfw}
522 nodes={commentsToFlatNodes(this.state.comments)}
525 sortType={this.state.sort}
527 enableDownvotes={this.state.siteRes.site.enable_downvotes}
534 <div className="mb-3">
537 type_={this.state.dataType}
538 onChange={this.handleDataTypeChange}
543 type_={this.state.listingType}
544 onChange={this.handleListingTypeChange}
548 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
550 {this.state.listingType == ListingType.All && (
552 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
557 <svg class="icon text-muted small">
558 <use xlinkHref="#icon-rss">#</use>
562 {UserService.Instance.user &&
563 this.state.listingType == ListingType.Subscribed && (
565 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
566 SortType[this.state.sort]
572 <svg class="icon text-muted small">
573 <use xlinkHref="#icon-rss">#</use>
584 {this.state.page > 1 && (
586 class="btn btn-secondary mr-1"
587 onClick={linkEvent(this, this.prevPage)}
592 {this.state.posts.length > 0 && (
594 class="btn btn-secondary"
595 onClick={linkEvent(this, this.nextPage)}
604 get canAdmin(): boolean {
606 UserService.Instance.user &&
607 this.state.siteRes.admins
609 .includes(UserService.Instance.user.id)
613 handleEditClick(i: Main) {
614 i.state.showEditSite = true;
619 this.state.showEditSite = false;
620 this.setState(this.state);
624 i.updateUrl({ page: i.state.page + 1 });
625 window.scrollTo(0, 0);
629 i.updateUrl({ page: i.state.page - 1 });
630 window.scrollTo(0, 0);
633 handleSortChange(val: SortType) {
634 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
635 window.scrollTo(0, 0);
638 handleListingTypeChange(val: ListingType) {
639 this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
640 window.scrollTo(0, 0);
643 handleDataTypeChange(val: DataType) {
644 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
645 window.scrollTo(0, 0);
649 if (this.state.dataType == DataType.Post) {
650 let getPostsForm: GetPostsForm = {
651 page: this.state.page,
653 sort: SortType[this.state.sort],
654 type_: ListingType[this.state.listingType],
656 WebSocketService.Instance.getPosts(getPostsForm);
658 let getCommentsForm: GetCommentsForm = {
659 page: this.state.page,
661 sort: SortType[this.state.sort],
662 type_: ListingType[this.state.listingType],
664 WebSocketService.Instance.getComments(getCommentsForm);
668 parseMessage(msg: WebSocketJsonResponse) {
670 let res = wsJsonToRes(msg);
672 toast(i18n.t(msg.error), 'danger');
674 } else if (msg.reconnect) {
676 } else if (res.op == UserOperation.GetFollowedCommunities) {
677 let data = res.data as GetFollowedCommunitiesResponse;
678 this.state.subscribedCommunities = data.communities;
679 this.setState(this.state);
680 } else if (res.op == UserOperation.ListCommunities) {
681 let data = res.data as ListCommunitiesResponse;
682 this.state.trendingCommunities = data.communities;
683 this.setState(this.state);
684 } else if (res.op == UserOperation.GetSite) {
685 let data = res.data as GetSiteResponse;
687 // This means it hasn't been set up yet
689 this.context.router.history.push('/setup');
691 this.state.siteRes.admins = data.admins;
692 this.state.siteRes.site = data.site;
693 this.state.siteRes.banned = data.banned;
694 this.state.siteRes.online = data.online;
695 this.setState(this.state);
696 } else if (res.op == UserOperation.EditSite) {
697 let data = res.data as SiteResponse;
698 this.state.siteRes.site = data.site;
699 this.state.showEditSite = false;
700 this.setState(this.state);
701 toast(i18n.t('site_saved'));
702 } else if (res.op == UserOperation.GetPosts) {
703 let data = res.data as GetPostsResponse;
704 this.state.posts = data.posts;
705 this.state.loading = false;
706 this.setState(this.state);
708 } else if (res.op == UserOperation.CreatePost) {
709 let data = res.data as PostResponse;
711 // If you're on subscribed, only push it if you're subscribed.
712 if (this.state.listingType == ListingType.Subscribed) {
714 this.state.subscribedCommunities
715 .map(c => c.community_id)
716 .includes(data.post.community_id)
718 this.state.posts.unshift(data.post);
722 let nsfw = data.post.nsfw || data.post.community_nsfw;
724 // Don't push the post if its nsfw, and don't have that setting on
728 UserService.Instance.user &&
729 UserService.Instance.user.show_nsfw)
731 this.state.posts.unshift(data.post);
734 this.setState(this.state);
735 } else if (res.op == UserOperation.EditPost) {
736 let data = res.data as PostResponse;
737 editPostFindRes(data, this.state.posts);
738 this.setState(this.state);
739 } else if (res.op == UserOperation.CreatePostLike) {
740 let data = res.data as PostResponse;
741 createPostLikeFindRes(data, this.state.posts);
742 this.setState(this.state);
743 } else if (res.op == UserOperation.AddAdmin) {
744 let data = res.data as AddAdminResponse;
745 this.state.siteRes.admins = data.admins;
746 this.setState(this.state);
747 } else if (res.op == UserOperation.BanUser) {
748 let data = res.data as BanUserResponse;
749 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
751 // Remove the banned if its found in the list, and the action is an unban
752 if (found && !data.banned) {
753 this.state.siteRes.banned = this.state.siteRes.banned.filter(
754 i => i.id !== data.user.id
757 this.state.siteRes.banned.push(data.user);
761 .filter(p => p.creator_id == data.user.id)
762 .forEach(p => (p.banned = data.banned));
764 this.setState(this.state);
765 } else if (res.op == UserOperation.GetComments) {
766 let data = res.data as GetCommentsResponse;
767 this.state.comments = data.comments;
768 this.state.loading = false;
769 this.setState(this.state);
771 res.op == UserOperation.EditComment ||
772 res.op == UserOperation.DeleteComment ||
773 res.op == UserOperation.RemoveComment
775 let data = res.data as CommentResponse;
776 editCommentRes(data, this.state.comments);
777 this.setState(this.state);
778 } else if (res.op == UserOperation.CreateComment) {
779 let data = res.data as CommentResponse;
781 // Necessary since it might be a user reply
782 if (data.recipient_ids.length == 0) {
783 // If you're on subscribed, only push it if you're subscribed.
784 if (this.state.listingType == ListingType.Subscribed) {
786 this.state.subscribedCommunities
787 .map(c => c.community_id)
788 .includes(data.comment.community_id)
790 this.state.comments.unshift(data.comment);
793 this.state.comments.unshift(data.comment);
795 this.setState(this.state);
797 } else if (res.op == UserOperation.SaveComment) {
798 let data = res.data as CommentResponse;
799 saveCommentRes(data, this.state.comments);
800 this.setState(this.state);
801 } else if (res.op == UserOperation.CreateCommentLike) {
802 let data = res.data as CommentResponse;
803 createCommentLikeRes(data, this.state.comments);
804 this.setState(this.state);