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.my_sidebar()}</aside>
206 {!this.state.loading && (
208 <div class="card bg-transparent border-secondary mb-3">
209 <div class="card-body">
210 {this.trendingCommunities()}
211 {UserService.Instance.user &&
212 this.state.subscribedCommunities.length > 0 && (
215 <T i18nKey="subscribed_to_communities">
217 <Link class="text-body" to="/communities">
222 <ul class="list-inline">
223 {this.state.subscribedCommunities.map(community => (
224 <li class="list-inline-item">
227 name: community.community_name,
228 id: community.community_id,
229 local: community.community_local,
230 actor_id: community.community_actor_id,
239 class="btn btn-secondary btn-block"
240 to="/create_community"
242 {i18n.t('create_a_community')}
254 trendingCommunities() {
258 <T i18nKey="trending_communities">
260 <Link class="text-body" to="/communities">
265 <ul class="list-inline">
266 {this.state.trendingCommunities.map(community => (
267 <li class="list-inline-item">
268 <CommunityLink community={community} />
279 {!this.state.showEditSite ? (
283 site={this.state.siteRes.site}
284 onCancel={this.handleEditCancel}
291 updateUrl(paramUpdates: UrlParams) {
292 const listingTypeStr =
293 paramUpdates.listingType ||
294 ListingType[this.state.listingType].toLowerCase();
296 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
298 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
299 const page = paramUpdates.page || this.state.page;
300 this.props.history.push(
301 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
308 <div class="card bg-transparent border-secondary mb-3">
309 <div class="card-body">
310 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
312 <ul class="list-inline mb-1 text-muted font-weight-bold">
313 <li className="list-inline-item-action">
316 onClick={linkEvent(this, this.handleEditClick)}
317 data-tippy-content={i18n.t('edit')}
319 <svg class="icon icon-inline">
320 <use xlinkHref="#icon-edit"></use>
326 <ul class="my-2 list-inline">
328 <li className="list-inline-item badge badge-light">
329 {i18n.t('number_online', { count: this.state.siteRes.online })}
332 <li className="list-inline-item badge badge-light">
333 {i18n.t('number_of_users', {
334 count: this.state.siteRes.site.number_of_users,
337 <li className="list-inline-item badge badge-light">
338 {i18n.t('number_of_communities', {
339 count: this.state.siteRes.site.number_of_communities,
342 <li className="list-inline-item badge badge-light">
343 {i18n.t('number_of_posts', {
344 count: this.state.siteRes.site.number_of_posts,
347 <li className="list-inline-item badge badge-light">
348 {i18n.t('number_of_comments', {
349 count: this.state.siteRes.site.number_of_comments,
352 <li className="list-inline-item">
353 <Link className="badge badge-light" to="/modlog">
358 <ul class="mt-1 list-inline small mb-0">
359 <li class="list-inline-item">{i18n.t('admins')}:</li>
360 {this.state.siteRes.admins.map(admin => (
361 <li class="list-inline-item">
365 avatar: admin.avatar,
367 actor_id: admin.actor_id,
376 {this.state.siteRes.site.description && (
377 <div class="card bg-transparent border-secondary mb-3">
378 <div class="card-body">
381 dangerouslySetInnerHTML={mdToHtml(
382 this.state.siteRes.site.description
394 <div class="card bg-transparent border-secondary">
395 <div class="card-body">
397 {i18n.t('powered_by')}
398 <svg class="icon mx-2">
399 <use xlinkHref="#icon-mouse">#</use>
406 <T i18nKey="landing_0">
408 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
411 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
412 <br class="big"></br>
416 <br class="big"></br>
417 <a href={repoUrl}>#</a>
418 <br class="big"></br>
419 <a href="https://www.rust-lang.org">#</a>
420 <a href="https://actix.rs/">#</a>
421 <a href="https://infernojs.org">#</a>
422 <a href="https://www.typescriptlang.org/">#</a>
423 <br class="big"></br>
424 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
436 <div class="main-content-wrapper">
438 {this.state.loading ? (
440 <svg class="icon icon-spinner spin">
441 <use xlinkHref="#icon-spinner"></use>
455 return this.state.dataType == DataType.Post ? (
457 posts={this.state.posts}
460 sort={this.state.sort}
461 enableDownvotes={this.state.siteRes.site.enable_downvotes}
462 enableNsfw={this.state.siteRes.site.enable_nsfw}
466 nodes={commentsToFlatNodes(this.state.comments)}
469 sortType={this.state.sort}
471 enableDownvotes={this.state.siteRes.site.enable_downvotes}
478 <div className="mb-3">
481 type_={this.state.dataType}
482 onChange={this.handleDataTypeChange}
487 type_={this.state.listingType}
488 onChange={this.handleListingTypeChange}
492 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
494 {this.state.listingType == ListingType.All && (
496 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
501 <svg class="icon text-muted small">
502 <use xlinkHref="#icon-rss">#</use>
506 {UserService.Instance.user &&
507 this.state.listingType == ListingType.Subscribed && (
509 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
510 SortType[this.state.sort]
516 <svg class="icon text-muted small">
517 <use xlinkHref="#icon-rss">#</use>
528 {this.state.page > 1 && (
530 class="btn btn-secondary mr-1"
531 onClick={linkEvent(this, this.prevPage)}
536 {this.state.posts.length > 0 && (
538 class="btn btn-secondary"
539 onClick={linkEvent(this, this.nextPage)}
548 get canAdmin(): boolean {
550 UserService.Instance.user &&
551 this.state.siteRes.admins
553 .includes(UserService.Instance.user.id)
557 handleEditClick(i: Main) {
558 i.state.showEditSite = true;
563 this.state.showEditSite = false;
564 this.setState(this.state);
568 i.updateUrl({ page: i.state.page + 1 });
569 window.scrollTo(0, 0);
573 i.updateUrl({ page: i.state.page - 1 });
574 window.scrollTo(0, 0);
577 handleSortChange(val: SortType) {
578 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
579 window.scrollTo(0, 0);
582 handleListingTypeChange(val: ListingType) {
583 this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
584 window.scrollTo(0, 0);
587 handleDataTypeChange(val: DataType) {
588 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
589 window.scrollTo(0, 0);
593 if (this.state.dataType == DataType.Post) {
594 let getPostsForm: GetPostsForm = {
595 page: this.state.page,
597 sort: SortType[this.state.sort],
598 type_: ListingType[this.state.listingType],
600 WebSocketService.Instance.getPosts(getPostsForm);
602 let getCommentsForm: GetCommentsForm = {
603 page: this.state.page,
605 sort: SortType[this.state.sort],
606 type_: ListingType[this.state.listingType],
608 WebSocketService.Instance.getComments(getCommentsForm);
612 parseMessage(msg: WebSocketJsonResponse) {
614 let res = wsJsonToRes(msg);
616 toast(i18n.t(msg.error), 'danger');
618 } else if (msg.reconnect) {
620 } else if (res.op == UserOperation.GetFollowedCommunities) {
621 let data = res.data as GetFollowedCommunitiesResponse;
622 this.state.subscribedCommunities = data.communities;
623 this.setState(this.state);
624 } else if (res.op == UserOperation.ListCommunities) {
625 let data = res.data as ListCommunitiesResponse;
626 this.state.trendingCommunities = data.communities;
627 this.setState(this.state);
628 } else if (res.op == UserOperation.GetSite) {
629 let data = res.data as GetSiteResponse;
631 // This means it hasn't been set up yet
633 this.context.router.history.push('/setup');
635 this.state.siteRes.admins = data.admins;
636 this.state.siteRes.site = data.site;
637 this.state.siteRes.banned = data.banned;
638 this.state.siteRes.online = data.online;
639 this.setState(this.state);
640 } else if (res.op == UserOperation.EditSite) {
641 let data = res.data as SiteResponse;
642 this.state.siteRes.site = data.site;
643 this.state.showEditSite = false;
644 this.setState(this.state);
645 toast(i18n.t('site_saved'));
646 } else if (res.op == UserOperation.GetPosts) {
647 let data = res.data as GetPostsResponse;
648 this.state.posts = data.posts;
649 this.state.loading = false;
650 this.setState(this.state);
652 } else if (res.op == UserOperation.CreatePost) {
653 let data = res.data as PostResponse;
655 // If you're on subscribed, only push it if you're subscribed.
656 if (this.state.listingType == ListingType.Subscribed) {
658 this.state.subscribedCommunities
659 .map(c => c.community_id)
660 .includes(data.post.community_id)
662 this.state.posts.unshift(data.post);
666 let nsfw = data.post.nsfw || data.post.community_nsfw;
668 // Don't push the post if its nsfw, and don't have that setting on
672 UserService.Instance.user &&
673 UserService.Instance.user.show_nsfw)
675 this.state.posts.unshift(data.post);
678 this.setState(this.state);
679 } else if (res.op == UserOperation.EditPost) {
680 let data = res.data as PostResponse;
681 editPostFindRes(data, this.state.posts);
682 this.setState(this.state);
683 } else if (res.op == UserOperation.CreatePostLike) {
684 let data = res.data as PostResponse;
685 createPostLikeFindRes(data, this.state.posts);
686 this.setState(this.state);
687 } else if (res.op == UserOperation.AddAdmin) {
688 let data = res.data as AddAdminResponse;
689 this.state.siteRes.admins = data.admins;
690 this.setState(this.state);
691 } else if (res.op == UserOperation.BanUser) {
692 let data = res.data as BanUserResponse;
693 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
695 // Remove the banned if its found in the list, and the action is an unban
696 if (found && !data.banned) {
697 this.state.siteRes.banned = this.state.siteRes.banned.filter(
698 i => i.id !== data.user.id
701 this.state.siteRes.banned.push(data.user);
705 .filter(p => p.creator_id == data.user.id)
706 .forEach(p => (p.banned = data.banned));
708 this.setState(this.state);
709 } else if (res.op == UserOperation.GetComments) {
710 let data = res.data as GetCommentsResponse;
711 this.state.comments = data.comments;
712 this.state.loading = false;
713 this.setState(this.state);
715 res.op == UserOperation.EditComment ||
716 res.op == UserOperation.DeleteComment ||
717 res.op == UserOperation.RemoveComment
719 let data = res.data as CommentResponse;
720 editCommentRes(data, this.state.comments);
721 this.setState(this.state);
722 } else if (res.op == UserOperation.CreateComment) {
723 let data = res.data as CommentResponse;
725 // Necessary since it might be a user reply
726 if (data.recipient_ids.length == 0) {
727 // If you're on subscribed, only push it if you're subscribed.
728 if (this.state.listingType == ListingType.Subscribed) {
730 this.state.subscribedCommunities
731 .map(c => c.community_id)
732 .includes(data.comment.community_id)
734 this.state.comments.unshift(data.comment);
737 this.state.comments.unshift(data.comment);
739 this.setState(this.state);
741 } else if (res.op == UserOperation.SaveComment) {
742 let data = res.data as CommentResponse;
743 saveCommentRes(data, this.state.comments);
744 this.setState(this.state);
745 } else if (res.op == UserOperation.CreateCommentLike) {
746 let data = res.data as CommentResponse;
747 createCommentLikeRes(data, this.state.comments);
748 this.setState(this.state);