1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
8 GetFollowedCommunitiesResponse,
10 ListCommunitiesResponse,
27 WebSocketJsonResponse,
28 } from '../interfaces';
29 import { WebSocketService, UserService } from '../services';
30 import { PostListings } from './post-listings';
31 import { CommentNodes } from './comment-nodes';
32 import { SortSelect } from './sort-select';
33 import { ListingTypeSelect } from './listing-type-select';
34 import { DataTypeSelect } from './data-type-select';
35 import { SiteForm } from './site-form';
36 import { UserListing } from './user-listing';
37 import { CommunityLink } from './community-link';
44 getListingTypeFromProps,
51 createPostLikeFindRes,
56 import { i18n } from '../i18next';
57 import { T } from 'inferno-i18next';
60 subscribedCommunities: Array<CommunityUser>;
61 trendingCommunities: Array<Community>;
62 siteRes: GetSiteResponse;
63 showEditSite: boolean;
66 comments: Array<Comment>;
67 listingType: ListingType;
74 listingType: ListingType;
87 export class Main extends Component<any, MainState> {
88 private subscription: Subscription;
89 private emptyState: MainState = {
90 subscribedCommunities: [],
91 trendingCommunities: [],
99 number_of_users: null,
100 number_of_posts: null,
101 number_of_comments: null,
102 number_of_communities: null,
103 enable_downvotes: null,
104 open_registration: null,
116 listingType: getListingTypeFromProps(this.props),
117 dataType: getDataTypeFromProps(this.props),
118 sort: getSortTypeFromProps(this.props),
119 page: getPageFromProps(this.props),
122 constructor(props: any, context: any) {
123 super(props, context);
125 this.state = this.emptyState;
126 this.handleEditCancel = this.handleEditCancel.bind(this);
127 this.handleSortChange = this.handleSortChange.bind(this);
128 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
129 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
131 this.subscription = WebSocketService.Instance.subject
132 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
134 msg => this.parseMessage(msg),
135 err => console.error(err),
136 () => console.log('complete')
139 WebSocketService.Instance.getSite();
141 if (UserService.Instance.user) {
142 WebSocketService.Instance.getFollowedCommunities();
145 let listCommunitiesForm: ListCommunitiesForm = {
146 sort: SortType[SortType.Hot],
150 WebSocketService.Instance.listCommunities(listCommunitiesForm);
155 componentWillUnmount() {
156 this.subscription.unsubscribe();
159 static getDerivedStateFromProps(props: any): MainProps {
161 listingType: getListingTypeFromProps(props),
162 dataType: getDataTypeFromProps(props),
163 sort: getSortTypeFromProps(props),
164 page: getPageFromProps(props),
168 componentDidUpdate(_: any, lastState: MainState) {
170 lastState.listingType !== this.state.listingType ||
171 lastState.dataType !== this.state.dataType ||
172 lastState.sort !== this.state.sort ||
173 lastState.page !== this.state.page
175 this.setState({ loading: true });
182 <div class="container">
184 <main role="main" class="col-12 col-md-8">
187 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
196 {!this.state.loading && (
198 <div class="card border-secondary mb-3">
199 <div class="card-body">
200 {this.trendingCommunities()}
201 {UserService.Instance.user &&
202 this.state.subscribedCommunities.length > 0 && (
205 <T i18nKey="subscribed_to_communities">
207 <Link class="text-body" to="/communities">
212 <ul class="list-inline">
213 {this.state.subscribedCommunities.map(community => (
214 <li class="list-inline-item">
217 name: community.community_name,
218 id: community.community_id,
219 local: community.community_local,
220 actor_id: community.community_actor_id,
229 class="btn btn-sm btn-secondary btn-block"
230 to="/create_community"
232 {i18n.t('create_a_community')}
244 trendingCommunities() {
248 <T i18nKey="trending_communities">
250 <Link class="text-body" to="/communities">
255 <ul class="list-inline">
256 {this.state.trendingCommunities.map(community => (
257 <li class="list-inline-item">
258 <CommunityLink community={community} />
269 {!this.state.showEditSite ? (
273 site={this.state.siteRes.site}
274 onCancel={this.handleEditCancel}
281 updateUrl(paramUpdates: UrlParams) {
282 const listingTypeStr =
283 paramUpdates.listingType ||
284 ListingType[this.state.listingType].toLowerCase();
286 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
288 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
289 const page = paramUpdates.page || this.state.page;
290 this.props.history.push(
291 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
298 <div class="card border-secondary mb-3">
299 <div class="card-body">
300 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
302 <ul class="list-inline mb-1 text-muted font-weight-bold">
303 <li className="list-inline-item-action">
306 onClick={linkEvent(this, this.handleEditClick)}
307 data-tippy-content={i18n.t('edit')}
309 <svg class="icon icon-inline">
310 <use xlinkHref="#icon-edit"></use>
316 <ul class="my-2 list-inline">
318 <li className="list-inline-item badge badge-secondary">
319 {i18n.t('number_online', { count: this.state.siteRes.online })}
322 <li className="list-inline-item badge badge-secondary">
323 {i18n.t('number_of_users', {
324 count: this.state.siteRes.site.number_of_users,
327 <li className="list-inline-item badge badge-secondary">
328 {i18n.t('number_of_communities', {
329 count: this.state.siteRes.site.number_of_communities,
332 <li className="list-inline-item badge badge-secondary">
333 {i18n.t('number_of_posts', {
334 count: this.state.siteRes.site.number_of_posts,
337 <li className="list-inline-item badge badge-secondary">
338 {i18n.t('number_of_comments', {
339 count: this.state.siteRes.site.number_of_comments,
342 <li className="list-inline-item">
343 <Link className="badge badge-secondary" to="/modlog">
348 <ul class="mt-1 list-inline small mb-0">
349 <li class="list-inline-item">{i18n.t('admins')}:</li>
350 {this.state.siteRes.admins.map(admin => (
351 <li class="list-inline-item">
355 avatar: admin.avatar,
357 actor_id: admin.actor_id,
366 {this.state.siteRes.site.description && (
367 <div class="card border-secondary mb-3">
368 <div class="card-body">
371 dangerouslySetInnerHTML={mdToHtml(
372 this.state.siteRes.site.description
384 <div class="card border-secondary">
385 <div class="card-body">
387 {i18n.t('powered_by')}
388 <svg class="icon mx-2">
389 <use xlinkHref="#icon-mouse">#</use>
396 <T i18nKey="landing_0">
398 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
401 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
402 <br class="big"></br>
406 <br class="big"></br>
407 <a href={repoUrl}>#</a>
408 <br class="big"></br>
409 <a href="https://www.rust-lang.org">#</a>
410 <a href="https://actix.rs/">#</a>
411 <a href="https://infernojs.org">#</a>
412 <a href="https://www.typescriptlang.org/">#</a>
413 <br class="big"></br>
414 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
426 <div class="main-content-wrapper">
428 {this.state.loading ? (
430 <svg class="icon icon-spinner spin">
431 <use xlinkHref="#icon-spinner"></use>
445 return this.state.dataType == DataType.Post ? (
447 posts={this.state.posts}
450 sort={this.state.sort}
451 enableDownvotes={this.state.siteRes.site.enable_downvotes}
452 enableNsfw={this.state.siteRes.site.enable_nsfw}
456 nodes={commentsToFlatNodes(this.state.comments)}
459 sortType={this.state.sort}
461 enableDownvotes={this.state.siteRes.site.enable_downvotes}
468 <div className="mb-3">
471 type_={this.state.dataType}
472 onChange={this.handleDataTypeChange}
477 type_={this.state.listingType}
478 onChange={this.handleListingTypeChange}
482 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
484 {this.state.listingType == ListingType.All && (
486 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
491 <svg class="icon text-muted small">
492 <use xlinkHref="#icon-rss">#</use>
496 {UserService.Instance.user &&
497 this.state.listingType == ListingType.Subscribed && (
499 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
500 SortType[this.state.sort]
506 <svg class="icon text-muted small">
507 <use xlinkHref="#icon-rss">#</use>
518 {this.state.page > 1 && (
520 class="btn btn-sm btn-secondary mr-1"
521 onClick={linkEvent(this, this.prevPage)}
526 {this.state.posts.length > 0 && (
528 class="btn btn-sm btn-secondary"
529 onClick={linkEvent(this, this.nextPage)}
538 get canAdmin(): boolean {
540 UserService.Instance.user &&
541 this.state.siteRes.admins
543 .includes(UserService.Instance.user.id)
547 handleEditClick(i: Main) {
548 i.state.showEditSite = true;
553 this.state.showEditSite = false;
554 this.setState(this.state);
558 i.updateUrl({ page: i.state.page + 1 });
559 window.scrollTo(0, 0);
563 i.updateUrl({ page: i.state.page - 1 });
564 window.scrollTo(0, 0);
567 handleSortChange(val: SortType) {
568 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
569 window.scrollTo(0, 0);
572 handleListingTypeChange(val: ListingType) {
573 this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
574 window.scrollTo(0, 0);
577 handleDataTypeChange(val: DataType) {
578 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
579 window.scrollTo(0, 0);
583 if (this.state.dataType == DataType.Post) {
584 let getPostsForm: GetPostsForm = {
585 page: this.state.page,
587 sort: SortType[this.state.sort],
588 type_: ListingType[this.state.listingType],
590 WebSocketService.Instance.getPosts(getPostsForm);
592 let getCommentsForm: GetCommentsForm = {
593 page: this.state.page,
595 sort: SortType[this.state.sort],
596 type_: ListingType[this.state.listingType],
598 WebSocketService.Instance.getComments(getCommentsForm);
602 parseMessage(msg: WebSocketJsonResponse) {
604 let res = wsJsonToRes(msg);
606 toast(i18n.t(msg.error), 'danger');
608 } else if (msg.reconnect) {
610 } else if (res.op == UserOperation.GetFollowedCommunities) {
611 let data = res.data as GetFollowedCommunitiesResponse;
612 this.state.subscribedCommunities = data.communities;
613 this.setState(this.state);
614 } else if (res.op == UserOperation.ListCommunities) {
615 let data = res.data as ListCommunitiesResponse;
616 this.state.trendingCommunities = data.communities;
617 this.setState(this.state);
618 } else if (res.op == UserOperation.GetSite) {
619 let data = res.data as GetSiteResponse;
621 // This means it hasn't been set up yet
623 this.context.router.history.push('/setup');
625 this.state.siteRes.admins = data.admins;
626 this.state.siteRes.site = data.site;
627 this.state.siteRes.banned = data.banned;
628 this.state.siteRes.online = data.online;
629 this.setState(this.state);
630 document.title = `${this.state.siteRes.site.name}`;
631 } else if (res.op == UserOperation.EditSite) {
632 let data = res.data as SiteResponse;
633 this.state.siteRes.site = data.site;
634 this.state.showEditSite = false;
635 this.setState(this.state);
636 toast(i18n.t('site_saved'));
637 } else if (res.op == UserOperation.GetPosts) {
638 let data = res.data as GetPostsResponse;
639 this.state.posts = data.posts;
640 this.state.loading = false;
641 this.setState(this.state);
643 } else if (res.op == UserOperation.CreatePost) {
644 let data = res.data as PostResponse;
646 // If you're on subscribed, only push it if you're subscribed.
647 if (this.state.listingType == ListingType.Subscribed) {
649 this.state.subscribedCommunities
650 .map(c => c.community_id)
651 .includes(data.post.community_id)
653 this.state.posts.unshift(data.post);
657 let nsfw = data.post.nsfw || data.post.community_nsfw;
659 // Don't push the post if its nsfw, and don't have that setting on
663 UserService.Instance.user &&
664 UserService.Instance.user.show_nsfw)
666 this.state.posts.unshift(data.post);
669 this.setState(this.state);
670 } else if (res.op == UserOperation.EditPost) {
671 let data = res.data as PostResponse;
672 editPostFindRes(data, this.state.posts);
673 this.setState(this.state);
674 } else if (res.op == UserOperation.CreatePostLike) {
675 let data = res.data as PostResponse;
676 createPostLikeFindRes(data, this.state.posts);
677 this.setState(this.state);
678 } else if (res.op == UserOperation.AddAdmin) {
679 let data = res.data as AddAdminResponse;
680 this.state.siteRes.admins = data.admins;
681 this.setState(this.state);
682 } else if (res.op == UserOperation.BanUser) {
683 let data = res.data as BanUserResponse;
684 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
686 // Remove the banned if its found in the list, and the action is an unban
687 if (found && !data.banned) {
688 this.state.siteRes.banned = this.state.siteRes.banned.filter(
689 i => i.id !== data.user.id
692 this.state.siteRes.banned.push(data.user);
696 .filter(p => p.creator_id == data.user.id)
697 .forEach(p => (p.banned = data.banned));
699 this.setState(this.state);
700 } else if (res.op == UserOperation.GetComments) {
701 let data = res.data as GetCommentsResponse;
702 this.state.comments = data.comments;
703 this.state.loading = false;
704 this.setState(this.state);
705 } else if (res.op == UserOperation.EditComment) {
706 let data = res.data as CommentResponse;
707 editCommentRes(data, this.state.comments);
708 this.setState(this.state);
709 } else if (res.op == UserOperation.CreateComment) {
710 let data = res.data as CommentResponse;
712 // Necessary since it might be a user reply
713 if (data.recipient_ids.length == 0) {
714 // If you're on subscribed, only push it if you're subscribed.
715 if (this.state.listingType == ListingType.Subscribed) {
717 this.state.subscribedCommunities
718 .map(c => c.community_id)
719 .includes(data.comment.community_id)
721 this.state.comments.unshift(data.comment);
724 this.state.comments.unshift(data.comment);
726 this.setState(this.state);
728 } else if (res.op == UserOperation.SaveComment) {
729 let data = res.data as CommentResponse;
730 saveCommentRes(data, this.state.comments);
731 this.setState(this.state);
732 } else if (res.op == UserOperation.CreateCommentLike) {
733 let data = res.data as CommentResponse;
734 createCommentLikeRes(data, this.state.comments);
735 this.setState(this.state);