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';
45 getListingTypeFromProps,
52 createPostLikeFindRes,
57 import { i18n } from '../i18next';
58 import { T } from 'inferno-i18next';
61 subscribedCommunities: Array<CommunityUser>;
62 trendingCommunities: Array<Community>;
63 siteRes: GetSiteResponse;
64 showEditSite: boolean;
67 comments: Array<Comment>;
68 listingType: ListingType;
75 listingType: ListingType;
88 export class Main extends Component<any, MainState> {
89 private subscription: Subscription;
90 private emptyState: MainState = {
91 subscribedCommunities: [],
92 trendingCommunities: [],
100 number_of_users: null,
101 number_of_posts: null,
102 number_of_comments: null,
103 number_of_communities: null,
104 enable_downvotes: null,
105 open_registration: null,
117 listingType: getListingTypeFromProps(this.props),
118 dataType: getDataTypeFromProps(this.props),
119 sort: getSortTypeFromProps(this.props),
120 page: getPageFromProps(this.props),
123 constructor(props: any, context: any) {
124 super(props, context);
126 this.state = this.emptyState;
127 this.handleEditCancel = this.handleEditCancel.bind(this);
128 this.handleSortChange = this.handleSortChange.bind(this);
129 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
130 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
132 this.subscription = WebSocketService.Instance.subject
133 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
135 msg => this.parseMessage(msg),
136 err => console.error(err),
137 () => console.log('complete')
140 WebSocketService.Instance.getSite();
142 if (UserService.Instance.user) {
143 WebSocketService.Instance.getFollowedCommunities();
146 let listCommunitiesForm: ListCommunitiesForm = {
147 sort: SortType[SortType.Hot],
151 WebSocketService.Instance.listCommunities(listCommunitiesForm);
156 componentWillUnmount() {
157 this.subscription.unsubscribe();
160 static getDerivedStateFromProps(props: any): MainProps {
162 listingType: getListingTypeFromProps(props),
163 dataType: getDataTypeFromProps(props),
164 sort: getSortTypeFromProps(props),
165 page: getPageFromProps(props),
169 componentDidUpdate(_: any, lastState: MainState) {
171 lastState.listingType !== this.state.listingType ||
172 lastState.dataType !== this.state.dataType ||
173 lastState.sort !== this.state.sort ||
174 lastState.page !== this.state.page
176 this.setState({ loading: true });
181 get documentTitle(): string {
182 if (this.state.siteRes.site.name) {
183 return `${this.state.siteRes.site.name}`;
191 <div class="container">
192 <Helmet title={this.documentTitle} />
194 <main role="main" class="col-12 col-md-8">
197 <aside class="col-12 col-md-4">{this.mySidebar()}</aside>
206 {!this.state.loading && (
208 <div class="card bg-transparent border-secondary mb-3">
209 <div class="card-header bg-transparent border-secondary">
211 {this.adminButtons()}
213 <div class="card-body">
214 {this.trendingCommunities()}
215 {this.createCommunityButton()}
217 {this.subscribedCommunities()}
222 <div class="card bg-transparent border-secondary mb-3">
223 <div class="card-body">{this.sidebar()}</div>
226 <div class="card bg-transparent border-secondary">
227 <div class="card-body">{this.landing()}</div>
235 createCommunityButton() {
237 <Link class="btn btn-secondary btn-block" to="/create_community">
238 {i18n.t('create_a_community')}
243 trendingCommunities() {
247 <T i18nKey="trending_communities">
249 <Link class="text-body" to="/communities">
254 <ul class="list-inline">
255 {this.state.trendingCommunities.map(community => (
256 <li class="list-inline-item">
257 <CommunityLink community={community} />
265 subscribedCommunities() {
267 UserService.Instance.user &&
268 this.state.subscribedCommunities.length > 0 && (
271 <T i18nKey="subscribed_to_communities">
273 <Link class="text-body" to="/communities">
278 <ul class="list-inline">
279 {this.state.subscribedCommunities.map(community => (
280 <li class="list-inline-item">
283 name: community.community_name,
284 id: community.community_id,
285 local: community.community_local,
286 actor_id: community.community_actor_id,
300 {!this.state.showEditSite ? (
304 site={this.state.siteRes.site}
305 onCancel={this.handleEditCancel}
312 updateUrl(paramUpdates: UrlParams) {
313 const listingTypeStr =
314 paramUpdates.listingType ||
315 ListingType[this.state.listingType].toLowerCase();
317 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
319 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
320 const page = paramUpdates.page || this.state.page;
321 this.props.history.push(
322 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
329 {this.state.siteRes.site.description && this.siteDescription()}
337 return <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>;
342 <ul class="mt-1 list-inline small mb-0">
343 <li class="list-inline-item">{i18n.t('admins')}:</li>
344 {this.state.siteRes.admins.map(admin => (
345 <li class="list-inline-item">
349 avatar: admin.avatar,
351 actor_id: admin.actor_id,
363 <ul class="my-2 list-inline">
365 <li className="list-inline-item badge badge-light">
366 {i18n.t('number_online', { count: this.state.siteRes.online })}
369 <li className="list-inline-item badge badge-light">
370 {i18n.t('number_of_users', {
371 count: this.state.siteRes.site.number_of_users,
374 <li className="list-inline-item badge badge-light">
375 {i18n.t('number_of_communities', {
376 count: this.state.siteRes.site.number_of_communities,
379 <li className="list-inline-item badge badge-light">
380 {i18n.t('number_of_posts', {
381 count: this.state.siteRes.site.number_of_posts,
384 <li className="list-inline-item badge badge-light">
385 {i18n.t('number_of_comments', {
386 count: this.state.siteRes.site.number_of_comments,
389 <li className="list-inline-item">
390 <Link className="badge badge-light" to="/modlog">
401 <ul class="list-inline mb-1 text-muted font-weight-bold">
402 <li className="list-inline-item-action">
405 onClick={linkEvent(this, this.handleEditClick)}
406 data-tippy-content={i18n.t('edit')}
408 <svg class="icon icon-inline">
409 <use xlinkHref="#icon-edit"></use>
422 dangerouslySetInnerHTML={mdToHtml(this.state.siteRes.site.description)}
431 {i18n.t('powered_by')}
432 <svg class="icon mx-2">
433 <use xlinkHref="#icon-mouse">#</use>
440 <T i18nKey="landing_0">
442 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
445 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
446 <br class="big"></br>
450 <br class="big"></br>
451 <a href={repoUrl}>#</a>
452 <br class="big"></br>
453 <a href="https://www.rust-lang.org">#</a>
454 <a href="https://actix.rs/">#</a>
455 <a href="https://infernojs.org">#</a>
456 <a href="https://www.typescriptlang.org/">#</a>
457 <br class="big"></br>
458 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
469 <div class="main-content-wrapper">
470 {this.state.loading ? (
472 <svg class="icon icon-spinner spin">
473 <use xlinkHref="#icon-spinner"></use>
488 return this.state.dataType == DataType.Post ? (
490 posts={this.state.posts}
493 sort={this.state.sort}
494 enableDownvotes={this.state.siteRes.site.enable_downvotes}
495 enableNsfw={this.state.siteRes.site.enable_nsfw}
499 nodes={commentsToFlatNodes(this.state.comments)}
502 sortType={this.state.sort}
504 enableDownvotes={this.state.siteRes.site.enable_downvotes}
511 <div className="mb-3">
514 type_={this.state.dataType}
515 onChange={this.handleDataTypeChange}
520 type_={this.state.listingType}
521 onChange={this.handleListingTypeChange}
525 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
527 {this.state.listingType == ListingType.All && (
529 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
534 <svg class="icon text-muted small">
535 <use xlinkHref="#icon-rss">#</use>
539 {UserService.Instance.user &&
540 this.state.listingType == ListingType.Subscribed && (
542 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
543 SortType[this.state.sort]
549 <svg class="icon text-muted small">
550 <use xlinkHref="#icon-rss">#</use>
561 {this.state.page > 1 && (
563 class="btn btn-secondary mr-1"
564 onClick={linkEvent(this, this.prevPage)}
569 {this.state.posts.length > 0 && (
571 class="btn btn-secondary"
572 onClick={linkEvent(this, this.nextPage)}
581 get canAdmin(): boolean {
583 UserService.Instance.user &&
584 this.state.siteRes.admins
586 .includes(UserService.Instance.user.id)
590 handleEditClick(i: Main) {
591 i.state.showEditSite = true;
596 this.state.showEditSite = false;
597 this.setState(this.state);
601 i.updateUrl({ page: i.state.page + 1 });
602 window.scrollTo(0, 0);
606 i.updateUrl({ page: i.state.page - 1 });
607 window.scrollTo(0, 0);
610 handleSortChange(val: SortType) {
611 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
612 window.scrollTo(0, 0);
615 handleListingTypeChange(val: ListingType) {
616 this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
617 window.scrollTo(0, 0);
620 handleDataTypeChange(val: DataType) {
621 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
622 window.scrollTo(0, 0);
626 if (this.state.dataType == DataType.Post) {
627 let getPostsForm: GetPostsForm = {
628 page: this.state.page,
630 sort: SortType[this.state.sort],
631 type_: ListingType[this.state.listingType],
633 WebSocketService.Instance.getPosts(getPostsForm);
635 let getCommentsForm: GetCommentsForm = {
636 page: this.state.page,
638 sort: SortType[this.state.sort],
639 type_: ListingType[this.state.listingType],
641 WebSocketService.Instance.getComments(getCommentsForm);
645 parseMessage(msg: WebSocketJsonResponse) {
647 let res = wsJsonToRes(msg);
649 toast(i18n.t(msg.error), 'danger');
651 } else if (msg.reconnect) {
653 } else if (res.op == UserOperation.GetFollowedCommunities) {
654 let data = res.data as GetFollowedCommunitiesResponse;
655 this.state.subscribedCommunities = data.communities;
656 this.setState(this.state);
657 } else if (res.op == UserOperation.ListCommunities) {
658 let data = res.data as ListCommunitiesResponse;
659 this.state.trendingCommunities = data.communities;
660 this.setState(this.state);
661 } else if (res.op == UserOperation.GetSite) {
662 let data = res.data as GetSiteResponse;
664 // This means it hasn't been set up yet
666 this.context.router.history.push('/setup');
668 this.state.siteRes.admins = data.admins;
669 this.state.siteRes.site = data.site;
670 this.state.siteRes.banned = data.banned;
671 this.state.siteRes.online = data.online;
672 this.setState(this.state);
673 } else if (res.op == UserOperation.EditSite) {
674 let data = res.data as SiteResponse;
675 this.state.siteRes.site = data.site;
676 this.state.showEditSite = false;
677 this.setState(this.state);
678 toast(i18n.t('site_saved'));
679 } else if (res.op == UserOperation.GetPosts) {
680 let data = res.data as GetPostsResponse;
681 this.state.posts = data.posts;
682 this.state.loading = false;
683 this.setState(this.state);
685 } else if (res.op == UserOperation.CreatePost) {
686 let data = res.data as PostResponse;
688 // If you're on subscribed, only push it if you're subscribed.
689 if (this.state.listingType == ListingType.Subscribed) {
691 this.state.subscribedCommunities
692 .map(c => c.community_id)
693 .includes(data.post.community_id)
695 this.state.posts.unshift(data.post);
699 let nsfw = data.post.nsfw || data.post.community_nsfw;
701 // Don't push the post if its nsfw, and don't have that setting on
705 UserService.Instance.user &&
706 UserService.Instance.user.show_nsfw)
708 this.state.posts.unshift(data.post);
711 this.setState(this.state);
712 } else if (res.op == UserOperation.EditPost) {
713 let data = res.data as PostResponse;
714 editPostFindRes(data, this.state.posts);
715 this.setState(this.state);
716 } else if (res.op == UserOperation.CreatePostLike) {
717 let data = res.data as PostResponse;
718 createPostLikeFindRes(data, this.state.posts);
719 this.setState(this.state);
720 } else if (res.op == UserOperation.AddAdmin) {
721 let data = res.data as AddAdminResponse;
722 this.state.siteRes.admins = data.admins;
723 this.setState(this.state);
724 } else if (res.op == UserOperation.BanUser) {
725 let data = res.data as BanUserResponse;
726 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
728 // Remove the banned if its found in the list, and the action is an unban
729 if (found && !data.banned) {
730 this.state.siteRes.banned = this.state.siteRes.banned.filter(
731 i => i.id !== data.user.id
734 this.state.siteRes.banned.push(data.user);
738 .filter(p => p.creator_id == data.user.id)
739 .forEach(p => (p.banned = data.banned));
741 this.setState(this.state);
742 } else if (res.op == UserOperation.GetComments) {
743 let data = res.data as GetCommentsResponse;
744 this.state.comments = data.comments;
745 this.state.loading = false;
746 this.setState(this.state);
748 res.op == UserOperation.EditComment ||
749 res.op == UserOperation.DeleteComment ||
750 res.op == UserOperation.RemoveComment
752 let data = res.data as CommentResponse;
753 editCommentRes(data, this.state.comments);
754 this.setState(this.state);
755 } else if (res.op == UserOperation.CreateComment) {
756 let data = res.data as CommentResponse;
758 // Necessary since it might be a user reply
759 if (data.recipient_ids.length == 0) {
760 // If you're on subscribed, only push it if you're subscribed.
761 if (this.state.listingType == ListingType.Subscribed) {
763 this.state.subscribedCommunities
764 .map(c => c.community_id)
765 .includes(data.comment.community_id)
767 this.state.comments.unshift(data.comment);
770 this.state.comments.unshift(data.comment);
772 this.setState(this.state);
774 } else if (res.op == UserOperation.SaveComment) {
775 let data = res.data as CommentResponse;
776 saveCommentRes(data, this.state.comments);
777 this.setState(this.state);
778 } else if (res.op == UserOperation.CreateCommentLike) {
779 let data = res.data as CommentResponse;
780 createCommentLikeRes(data, this.state.comments);
781 this.setState(this.state);