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,
27 WebSocketJsonResponse,
28 } from 'lemmy-js-client';
29 import { DataType } 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,
61 import { i18n } from '../i18next';
62 import { T } from 'inferno-i18next';
65 subscribedCommunities: CommunityUser[];
66 trendingCommunities: Community[];
67 siteRes: GetSiteResponse;
68 showEditSite: boolean;
72 listingType: ListingType;
79 listingType: ListingType;
86 listingType?: ListingType;
92 export class Main extends Component<any, MainState> {
93 private subscription: Subscription;
94 private emptyState: MainState = {
95 subscribedCommunities: [],
96 trendingCommunities: [],
104 number_of_users: null,
105 number_of_posts: null,
106 number_of_comments: null,
107 number_of_communities: null,
108 enable_downvotes: null,
109 open_registration: null,
113 creator_preferred_username: null,
119 federated_instances: null,
125 listingType: getListingTypeFromProps(this.props),
126 dataType: getDataTypeFromProps(this.props),
127 sort: getSortTypeFromProps(this.props),
128 page: getPageFromProps(this.props),
131 constructor(props: any, context: any) {
132 super(props, context);
134 this.state = this.emptyState;
135 this.handleEditCancel = this.handleEditCancel.bind(this);
136 this.handleSortChange = this.handleSortChange.bind(this);
137 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
138 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
142 /* this.subscription = WebSocketService.Instance.subject */
143 /* .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) */
145 /* msg => this.parseMessage(msg), */
146 /* err => console.error(err), */
147 /* () => console.log('complete') */
149 /* WebSocketService.Instance.getSite(); */
150 /* if (UserService.Instance.user) { */
151 /* WebSocketService.Instance.getFollowedCommunities(); */
153 /* let listCommunitiesForm: ListCommunitiesForm = { */
154 /* sort: SortType.Hot, */
157 /* WebSocketService.Instance.listCommunities(listCommunitiesForm); */
158 /* this.fetchData(); */
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 }); */
183 /* this.fetchData(); */
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 <h1 className={`text-warning`}>u stink main</h1>
205 <Helmet title={this.documentTitle}>
214 <main role="main" class="col-12 col-md-8">
217 <aside class="col-12 col-md-4">{this.mySidebar()}</aside>
226 {!this.state.loading && (
228 <div class="card bg-transparent border-secondary mb-3">
229 <div class="card-header bg-transparent border-secondary">
232 {this.adminButtons()}
234 <BannerIconHeader banner={this.state.siteRes.site.banner} />
236 <div class="card-body">
237 {this.trendingCommunities()}
238 {this.createCommunityButton()}
240 {this.subscribedCommunities()}
245 <div class="card bg-transparent border-secondary mb-3">
246 <div class="card-body">{this.sidebar()}</div>
249 <div class="card bg-transparent border-secondary">
250 <div class="card-body">{this.landing()}</div>
258 createCommunityButton() {
260 <Link class="btn btn-secondary btn-block" to="/create_community">
261 {i18n.t('create_a_community')}
266 trendingCommunities() {
270 <T i18nKey="trending_communities">
272 <Link class="text-body" to="/communities">
277 <ul class="list-inline">
278 {this.state.trendingCommunities.map(community => (
279 <li class="list-inline-item">
280 <CommunityLink community={community} />
288 subscribedCommunities() {
290 UserService.Instance.user &&
291 this.state.subscribedCommunities.length > 0 && (
294 <T i18nKey="subscribed_to_communities">
296 <Link class="text-body" to="/communities">
301 <ul class="list-inline">
302 {this.state.subscribedCommunities.map(community => (
303 <li class="list-inline-item">
306 name: community.community_name,
307 id: community.community_id,
308 local: community.community_local,
309 actor_id: community.community_actor_id,
310 icon: community.community_icon,
324 {!this.state.showEditSite ? (
328 site={this.state.siteRes.site}
329 onCancel={this.handleEditCancel}
336 updateUrl(paramUpdates: UrlParams) {
337 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
338 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
339 const sortStr = paramUpdates.sort || this.state.sort;
340 const page = paramUpdates.page || this.state.page;
341 this.props.history.push(
342 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
349 {this.state.siteRes.site.description && this.siteDescription()}
357 return <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>;
362 <ul class="mt-1 list-inline small mb-0">
363 <li class="list-inline-item">{i18n.t('admins')}:</li>
364 {this.state.siteRes.admins.map(admin => (
365 <li class="list-inline-item">
369 preferred_username: admin.preferred_username,
370 avatar: admin.avatar,
372 actor_id: admin.actor_id,
384 <ul class="my-2 list-inline">
385 <li className="list-inline-item badge badge-light">
386 {i18n.t('number_online', { count: this.state.siteRes.online })}
388 <li className="list-inline-item badge badge-light">
389 {i18n.t('number_of_users', {
390 count: this.state.siteRes.site.number_of_users,
393 <li className="list-inline-item badge badge-light">
394 {i18n.t('number_of_communities', {
395 count: this.state.siteRes.site.number_of_communities,
398 <li className="list-inline-item badge badge-light">
399 {i18n.t('number_of_posts', {
400 count: this.state.siteRes.site.number_of_posts,
403 <li className="list-inline-item badge badge-light">
404 {i18n.t('number_of_comments', {
405 count: this.state.siteRes.site.number_of_comments,
408 <li className="list-inline-item">
409 <Link className="badge badge-light" to="/modlog">
420 <ul class="list-inline mb-1 text-muted font-weight-bold">
421 <li className="list-inline-item-action">
424 onClick={linkEvent(this, this.handleEditClick)}
425 data-tippy-content={i18n.t('edit')}
427 <svg class="icon icon-inline">
428 <use xlinkHref="#icon-edit"></use>
441 dangerouslySetInnerHTML={mdToHtml(this.state.siteRes.site.description)}
450 {i18n.t('powered_by')}
451 <svg class="icon mx-2">
452 <use xlinkHref="#icon-mouse">#</use>
459 <T i18nKey="landing_0">
461 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
464 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
465 <br class="big"></br>
469 <br class="big"></br>
470 <a href={repoUrl}>#</a>
471 <br class="big"></br>
472 <a href="https://www.rust-lang.org">#</a>
473 <a href="https://actix.rs/">#</a>
474 <a href="https://infernojs.org">#</a>
475 <a href="https://www.typescriptlang.org/">#</a>
476 <br class="big"></br>
477 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
488 <div class="main-content-wrapper">
489 {this.state.loading ? (
491 <svg class="icon icon-spinner spin">
492 <use xlinkHref="#icon-spinner"></use>
507 return this.state.dataType == DataType.Post ? (
509 posts={this.state.posts}
512 sort={this.state.sort}
513 enableDownvotes={this.state.siteRes.site.enable_downvotes}
514 enableNsfw={this.state.siteRes.site.enable_nsfw}
518 nodes={commentsToFlatNodes(this.state.comments)}
521 sortType={this.state.sort}
523 enableDownvotes={this.state.siteRes.site.enable_downvotes}
530 <div className="mb-3">
533 type_={this.state.dataType}
534 onChange={this.handleDataTypeChange}
539 type_={this.state.listingType}
540 onChange={this.handleListingTypeChange}
544 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
546 {this.state.listingType == ListingType.All && (
548 href={`/feeds/all.xml?sort=${this.state.sort}`}
553 <svg class="icon text-muted small">
554 <use xlinkHref="#icon-rss">#</use>
558 {UserService.Instance.user &&
559 this.state.listingType == ListingType.Subscribed && (
561 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${this.state.sort}`}
566 <svg class="icon text-muted small">
567 <use xlinkHref="#icon-rss">#</use>
578 {this.state.page > 1 && (
580 class="btn btn-secondary mr-1"
581 onClick={linkEvent(this, this.prevPage)}
586 {this.state.posts.length > 0 && (
588 class="btn btn-secondary"
589 onClick={linkEvent(this, this.nextPage)}
598 get canAdmin(): boolean {
600 UserService.Instance.user &&
601 this.state.siteRes.admins
603 .includes(UserService.Instance.user.id)
607 handleEditClick(i: Main) {
608 i.state.showEditSite = true;
613 this.state.showEditSite = false;
614 this.setState(this.state);
618 i.updateUrl({ page: i.state.page + 1 });
619 window.scrollTo(0, 0);
623 i.updateUrl({ page: i.state.page - 1 });
624 window.scrollTo(0, 0);
627 handleSortChange(val: SortType) {
628 this.updateUrl({ sort: val, page: 1 });
629 window.scrollTo(0, 0);
632 handleListingTypeChange(val: ListingType) {
633 this.updateUrl({ listingType: val, page: 1 });
634 window.scrollTo(0, 0);
637 handleDataTypeChange(val: DataType) {
638 this.updateUrl({ dataType: DataType[val], page: 1 });
639 window.scrollTo(0, 0);
643 if (this.state.dataType == DataType.Post) {
644 let getPostsForm: GetPostsForm = {
645 page: this.state.page,
647 sort: this.state.sort,
648 type_: this.state.listingType,
650 WebSocketService.Instance.getPosts(getPostsForm);
652 let getCommentsForm: GetCommentsForm = {
653 page: this.state.page,
655 sort: this.state.sort,
656 type_: this.state.listingType,
658 WebSocketService.Instance.getComments(getCommentsForm);
662 parseMessage(msg: WebSocketJsonResponse) {
664 let res = wsJsonToRes(msg);
666 toast(i18n.t(msg.error), 'danger');
668 } else if (msg.reconnect) {
670 } else if (res.op == UserOperation.GetFollowedCommunities) {
671 let data = res.data as GetFollowedCommunitiesResponse;
672 this.state.subscribedCommunities = data.communities;
673 this.setState(this.state);
674 } else if (res.op == UserOperation.ListCommunities) {
675 let data = res.data as ListCommunitiesResponse;
676 this.state.trendingCommunities = data.communities;
677 this.setState(this.state);
678 } else if (res.op == UserOperation.GetSite) {
679 let data = res.data as GetSiteResponse;
681 // This means it hasn't been set up yet
683 this.context.router.history.push('/setup');
685 this.state.siteRes.admins = data.admins;
686 this.state.siteRes.site = data.site;
687 this.state.siteRes.banned = data.banned;
688 this.state.siteRes.online = data.online;
689 this.setState(this.state);
690 } else if (res.op == UserOperation.EditSite) {
691 let data = res.data as SiteResponse;
692 this.state.siteRes.site = data.site;
693 this.state.showEditSite = false;
694 this.setState(this.state);
695 toast(i18n.t('site_saved'));
696 } else if (res.op == UserOperation.GetPosts) {
697 let data = res.data as GetPostsResponse;
698 this.state.posts = data.posts;
699 this.state.loading = false;
700 this.setState(this.state);
702 } else if (res.op == UserOperation.CreatePost) {
703 let data = res.data as PostResponse;
705 // If you're on subscribed, only push it if you're subscribed.
706 if (this.state.listingType == ListingType.Subscribed) {
708 this.state.subscribedCommunities
709 .map(c => c.community_id)
710 .includes(data.post.community_id)
712 this.state.posts.unshift(data.post);
713 notifyPost(data.post, this.context.router);
717 let nsfw = data.post.nsfw || data.post.community_nsfw;
719 // Don't push the post if its nsfw, and don't have that setting on
723 UserService.Instance.user &&
724 UserService.Instance.user.show_nsfw)
726 this.state.posts.unshift(data.post);
727 notifyPost(data.post, this.context.router);
730 this.setState(this.state);
731 } else if (res.op == UserOperation.EditPost) {
732 let data = res.data as PostResponse;
733 editPostFindRes(data, this.state.posts);
734 this.setState(this.state);
735 } else if (res.op == UserOperation.CreatePostLike) {
736 let data = res.data as PostResponse;
737 createPostLikeFindRes(data, this.state.posts);
738 this.setState(this.state);
739 } else if (res.op == UserOperation.AddAdmin) {
740 let data = res.data as AddAdminResponse;
741 this.state.siteRes.admins = data.admins;
742 this.setState(this.state);
743 } else if (res.op == UserOperation.BanUser) {
744 let data = res.data as BanUserResponse;
745 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
747 // Remove the banned if its found in the list, and the action is an unban
748 if (found && !data.banned) {
749 this.state.siteRes.banned = this.state.siteRes.banned.filter(
750 i => i.id !== data.user.id
753 this.state.siteRes.banned.push(data.user);
757 .filter(p => p.creator_id == data.user.id)
758 .forEach(p => (p.banned = data.banned));
760 this.setState(this.state);
761 } else if (res.op == UserOperation.GetComments) {
762 let data = res.data as GetCommentsResponse;
763 this.state.comments = data.comments;
764 this.state.loading = false;
765 this.setState(this.state);
767 res.op == UserOperation.EditComment ||
768 res.op == UserOperation.DeleteComment ||
769 res.op == UserOperation.RemoveComment
771 let data = res.data as CommentResponse;
772 editCommentRes(data, this.state.comments);
773 this.setState(this.state);
774 } else if (res.op == UserOperation.CreateComment) {
775 let data = res.data as CommentResponse;
777 // Necessary since it might be a user reply
778 if (data.recipient_ids.length == 0) {
779 // If you're on subscribed, only push it if you're subscribed.
780 if (this.state.listingType == ListingType.Subscribed) {
782 this.state.subscribedCommunities
783 .map(c => c.community_id)
784 .includes(data.comment.community_id)
786 this.state.comments.unshift(data.comment);
789 this.state.comments.unshift(data.comment);
791 this.setState(this.state);
793 } else if (res.op == UserOperation.SaveComment) {
794 let data = res.data as CommentResponse;
795 saveCommentRes(data, this.state.comments);
796 this.setState(this.state);
797 } else if (res.op == UserOperation.CreateCommentLike) {
798 let data = res.data as CommentResponse;
799 createCommentLikeRes(data, this.state.comments);
800 this.setState(this.state);