1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
7 GetFollowedCommunitiesResponse,
9 ListCommunitiesResponse,
25 WebSocketJsonResponse,
26 } from 'lemmy-js-client';
27 import { DataType } from '../interfaces';
28 import { WebSocketService, UserService } from '../services';
29 import { PostListings } from './post-listings';
30 import { CommentNodes } from './comment-nodes';
31 import { SortSelect } from './sort-select';
32 import { ListingTypeSelect } from './listing-type-select';
33 import { DataTypeSelect } from './data-type-select';
34 import { SiteForm } from './site-form';
35 import { UserListing } from './user-listing';
36 import { CommunityLink } from './community-link';
37 import { BannerIconHeader } from './banner-icon-header';
44 getListingTypeFromProps,
51 createPostLikeFindRes,
62 import { i18n } from '../i18next';
63 import { T } from 'inferno-i18next';
64 import { HtmlTags } from './html-tags';
67 subscribedCommunities: CommunityUser[];
68 trendingCommunities: Community[];
69 siteRes: GetSiteResponse;
70 showEditSite: boolean;
74 listingType: ListingType;
81 listingType: ListingType;
88 listingType?: ListingType;
94 export class Main extends Component<any, MainState> {
95 private isoData = setIsoData(this.context);
96 private subscription: Subscription;
97 private emptyState: MainState = {
98 subscribedCommunities: [],
99 trendingCommunities: [],
100 siteRes: this.isoData.site,
105 listingType: getListingTypeFromProps(this.props),
106 dataType: getDataTypeFromProps(this.props),
107 sort: getSortTypeFromProps(this.props),
108 page: getPageFromProps(this.props),
111 constructor(props: any, context: any) {
112 super(props, context);
114 this.state = this.emptyState;
115 this.handleEditCancel = this.handleEditCancel.bind(this);
116 this.handleSortChange = this.handleSortChange.bind(this);
117 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
118 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
120 this.parseMessage = this.parseMessage.bind(this);
121 this.subscription = wsSubscribe(this.parseMessage);
123 // Only fetch the data if coming from another route
124 if (this.isoData.path == this.context.router.route.match.url) {
125 if (this.state.dataType == DataType.Post) {
126 this.state.posts = this.isoData.routeData[0].posts;
128 this.state.comments = this.isoData.routeData[0].comments;
130 this.state.trendingCommunities = this.isoData.routeData[1].communities;
131 if (UserService.Instance.user) {
132 this.state.subscribedCommunities = this.isoData.routeData[2].communities;
134 this.state.loading = false;
136 this.fetchTrendingCommunities();
138 if (UserService.Instance.user) {
139 WebSocketService.Instance.getFollowedCommunities();
146 fetchTrendingCommunities() {
147 let listCommunitiesForm: ListCommunitiesForm = {
151 WebSocketService.Instance.listCommunities(listCommunitiesForm);
154 componentDidMount() {
155 WebSocketService.Instance.communityJoin({ community_id: 0 });
158 componentWillUnmount() {
160 this.subscription.unsubscribe();
164 static getDerivedStateFromProps(props: any): MainProps {
166 listingType: getListingTypeFromProps(props),
167 dataType: getDataTypeFromProps(props),
168 sort: getSortTypeFromProps(props),
169 page: getPageFromProps(props),
173 static fetchInitialData(auth: string, path: string): Promise<any>[] {
174 let pathSplit = path.split('/');
175 let dataType: DataType = pathSplit[3]
176 ? DataType[pathSplit[3]]
179 // TODO figure out auth default_listingType, default_sort_type
180 let type_: ListingType = pathSplit[5]
181 ? ListingType[pathSplit[5]]
182 : UserService.Instance.user
183 ? Object.values(ListingType)[
184 UserService.Instance.user.default_listing_type
187 let sort: SortType = pathSplit[7]
188 ? SortType[pathSplit[7]]
189 : UserService.Instance.user
190 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
193 let page = pathSplit[9] ? Number(pathSplit[9]) : 1;
195 let promises: Promise<any>[] = [];
197 if (dataType == DataType.Post) {
198 let getPostsForm: GetPostsForm = {
204 setAuth(getPostsForm, auth);
205 promises.push(lemmyHttp.getPosts(getPostsForm));
207 let getCommentsForm: GetCommentsForm = {
213 setAuth(getCommentsForm, auth);
214 promises.push(lemmyHttp.getComments(getCommentsForm));
217 let trendingCommunitiesForm: ListCommunitiesForm = {
221 promises.push(lemmyHttp.listCommunities(trendingCommunitiesForm));
224 promises.push(lemmyHttp.getFollowedCommunities({ auth }));
230 componentDidUpdate(_: any, lastState: MainState) {
232 lastState.listingType !== this.state.listingType ||
233 lastState.dataType !== this.state.dataType ||
234 lastState.sort !== this.state.sort ||
235 lastState.page !== this.state.page
237 this.setState({ loading: true });
242 get documentTitle(): string {
243 return `${this.state.siteRes.site.name}`;
248 <div class="container">
250 title={this.documentTitle}
251 path={this.context.router.route.match.url}
254 <main role="main" class="col-12 col-md-8">
257 <aside class="col-12 col-md-4">{this.mySidebar()}</aside>
266 {!this.state.loading && (
268 <div class="card bg-transparent border-secondary mb-3">
269 <div class="card-header bg-transparent border-secondary">
272 {this.adminButtons()}
274 <BannerIconHeader banner={this.state.siteRes.site.banner} />
276 <div class="card-body">
277 {this.trendingCommunities()}
278 {this.createCommunityButton()}
280 {this.subscribedCommunities()}
285 <div class="card bg-transparent border-secondary mb-3">
286 <div class="card-body">{this.sidebar()}</div>
289 <div class="card bg-transparent border-secondary">
290 <div class="card-body">{this.landing()}</div>
298 createCommunityButton() {
300 <Link className="btn btn-secondary btn-block" to="/create_community">
301 {i18n.t('create_a_community')}
306 trendingCommunities() {
310 <T i18nKey="trending_communities">
312 <Link className="text-body" to="/communities">
317 <ul class="list-inline">
318 {this.state.trendingCommunities.map(community => (
319 <li class="list-inline-item">
320 <CommunityLink community={community} />
328 subscribedCommunities() {
330 UserService.Instance.user &&
331 this.state.subscribedCommunities.length > 0 && (
334 <T i18nKey="subscribed_to_communities">
336 <Link className="text-body" to="/communities">
341 <ul class="list-inline">
342 {this.state.subscribedCommunities.map(community => (
343 <li class="list-inline-item">
346 name: community.community_name,
347 id: community.community_id,
348 local: community.community_local,
349 actor_id: community.community_actor_id,
350 icon: community.community_icon,
364 {!this.state.showEditSite ? (
368 site={this.state.siteRes.site}
369 onCancel={this.handleEditCancel}
376 updateUrl(paramUpdates: UrlParams) {
377 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
378 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
379 const sortStr = paramUpdates.sort || this.state.sort;
380 const page = paramUpdates.page || this.state.page;
381 this.props.history.push(
382 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
389 {this.state.siteRes.site.description && this.siteDescription()}
397 return <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>;
402 <ul class="mt-1 list-inline small mb-0">
403 <li class="list-inline-item">{i18n.t('admins')}:</li>
404 {this.state.siteRes.admins.map(admin => (
405 <li class="list-inline-item">
409 preferred_username: admin.preferred_username,
410 avatar: admin.avatar,
412 actor_id: admin.actor_id,
424 <ul class="my-2 list-inline">
425 <li className="list-inline-item badge badge-light">
426 {i18n.t('number_online', { count: this.state.siteRes.online })}
428 <li className="list-inline-item badge badge-light">
429 {i18n.t('number_of_users', {
430 count: this.state.siteRes.site.number_of_users,
433 <li className="list-inline-item badge badge-light">
434 {i18n.t('number_of_communities', {
435 count: this.state.siteRes.site.number_of_communities,
438 <li className="list-inline-item badge badge-light">
439 {i18n.t('number_of_posts', {
440 count: this.state.siteRes.site.number_of_posts,
443 <li className="list-inline-item badge badge-light">
444 {i18n.t('number_of_comments', {
445 count: this.state.siteRes.site.number_of_comments,
448 <li className="list-inline-item">
449 <Link className="badge badge-light" to="/modlog">
460 <ul class="list-inline mb-1 text-muted font-weight-bold">
461 <li className="list-inline-item-action">
464 onClick={linkEvent(this, this.handleEditClick)}
465 data-tippy-content={i18n.t('edit')}
467 <svg class="icon icon-inline">
468 <use xlinkHref="#icon-edit"></use>
481 dangerouslySetInnerHTML={mdToHtml(this.state.siteRes.site.description)}
490 {i18n.t('powered_by')}
491 <svg class="icon mx-2">
492 <use xlinkHref="#icon-mouse">#</use>
499 <T i18nKey="landing_0">
501 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
504 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
505 <br class="big"></br>
509 <br class="big"></br>
510 <a href={repoUrl}>#</a>
511 <br class="big"></br>
512 <a href="https://www.rust-lang.org">#</a>
513 <a href="https://actix.rs/">#</a>
514 <a href="https://infernojs.org">#</a>
515 <a href="https://www.typescriptlang.org/">#</a>
516 <br class="big"></br>
517 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
528 <div class="main-content-wrapper">
529 {this.state.loading ? (
531 <svg class="icon icon-spinner spin">
532 <use xlinkHref="#icon-spinner"></use>
547 return this.state.dataType == DataType.Post ? (
549 posts={this.state.posts}
552 sort={this.state.sort}
553 enableDownvotes={this.state.siteRes.site.enable_downvotes}
554 enableNsfw={this.state.siteRes.site.enable_nsfw}
558 nodes={commentsToFlatNodes(this.state.comments)}
561 sortType={this.state.sort}
563 enableDownvotes={this.state.siteRes.site.enable_downvotes}
570 <div className="mb-3">
573 type_={this.state.dataType}
574 onChange={this.handleDataTypeChange}
579 type_={this.state.listingType}
581 this.state.siteRes.federated_instances &&
582 this.state.siteRes.federated_instances.length > 0
584 onChange={this.handleListingTypeChange}
588 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
590 {this.state.listingType == ListingType.All && (
592 href={`/feeds/all.xml?sort=${this.state.sort}`}
597 <svg class="icon text-muted small">
598 <use xlinkHref="#icon-rss">#</use>
602 {UserService.Instance.user &&
603 this.state.listingType == ListingType.Subscribed && (
605 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${this.state.sort}`}
610 <svg class="icon text-muted small">
611 <use xlinkHref="#icon-rss">#</use>
622 {this.state.page > 1 && (
624 class="btn btn-secondary mr-1"
625 onClick={linkEvent(this, this.prevPage)}
630 {this.state.posts.length > 0 && (
632 class="btn btn-secondary"
633 onClick={linkEvent(this, this.nextPage)}
642 get canAdmin(): boolean {
644 UserService.Instance.user &&
645 this.state.siteRes.admins
647 .includes(UserService.Instance.user.id)
651 handleEditClick(i: Main) {
652 i.state.showEditSite = true;
657 this.state.showEditSite = false;
658 this.setState(this.state);
662 i.updateUrl({ page: i.state.page + 1 });
663 window.scrollTo(0, 0);
667 i.updateUrl({ page: i.state.page - 1 });
668 window.scrollTo(0, 0);
671 handleSortChange(val: SortType) {
672 this.updateUrl({ sort: val, page: 1 });
673 window.scrollTo(0, 0);
676 handleListingTypeChange(val: ListingType) {
677 this.updateUrl({ listingType: val, page: 1 });
678 window.scrollTo(0, 0);
681 handleDataTypeChange(val: DataType) {
682 this.updateUrl({ dataType: DataType[val], page: 1 });
683 window.scrollTo(0, 0);
687 if (this.state.dataType == DataType.Post) {
688 let getPostsForm: GetPostsForm = {
689 page: this.state.page,
691 sort: this.state.sort,
692 type_: this.state.listingType,
694 WebSocketService.Instance.getPosts(getPostsForm);
696 let getCommentsForm: GetCommentsForm = {
697 page: this.state.page,
699 sort: this.state.sort,
700 type_: this.state.listingType,
702 WebSocketService.Instance.getComments(getCommentsForm);
706 parseMessage(msg: WebSocketJsonResponse) {
708 let res = wsJsonToRes(msg);
710 toast(i18n.t(msg.error), 'danger');
712 } else if (msg.reconnect) {
714 } else if (res.op == UserOperation.GetFollowedCommunities) {
715 let data = res.data as GetFollowedCommunitiesResponse;
716 this.state.subscribedCommunities = data.communities;
717 this.setState(this.state);
718 } else if (res.op == UserOperation.ListCommunities) {
719 let data = res.data as ListCommunitiesResponse;
720 this.state.trendingCommunities = data.communities;
721 this.setState(this.state);
722 } else if (res.op == UserOperation.GetSite) {
723 let data = res.data as GetSiteResponse;
725 // This means it hasn't been set up yet
727 this.context.router.history.push('/setup');
729 this.state.siteRes.admins = data.admins;
730 this.state.siteRes.site = data.site;
731 this.state.siteRes.banned = data.banned;
732 this.state.siteRes.online = data.online;
733 this.setState(this.state);
734 } else if (res.op == UserOperation.EditSite) {
735 let data = res.data as SiteResponse;
736 this.state.siteRes.site = data.site;
737 this.state.showEditSite = false;
738 this.setState(this.state);
739 toast(i18n.t('site_saved'));
740 } else if (res.op == UserOperation.GetPosts) {
741 let data = res.data as GetPostsResponse;
742 this.state.posts = data.posts;
743 this.state.loading = false;
744 this.setState(this.state);
746 } else if (res.op == UserOperation.CreatePost) {
747 let data = res.data as PostResponse;
749 // If you're on subscribed, only push it if you're subscribed.
750 if (this.state.listingType == ListingType.Subscribed) {
752 this.state.subscribedCommunities
753 .map(c => c.community_id)
754 .includes(data.post.community_id)
756 this.state.posts.unshift(data.post);
757 notifyPost(data.post, this.context.router);
761 let nsfw = data.post.nsfw || data.post.community_nsfw;
763 // Don't push the post if its nsfw, and don't have that setting on
767 UserService.Instance.user &&
768 UserService.Instance.user.show_nsfw)
770 this.state.posts.unshift(data.post);
771 notifyPost(data.post, this.context.router);
774 this.setState(this.state);
775 } else if (res.op == UserOperation.EditPost) {
776 let data = res.data as PostResponse;
777 editPostFindRes(data, this.state.posts);
778 this.setState(this.state);
779 } else if (res.op == UserOperation.CreatePostLike) {
780 let data = res.data as PostResponse;
781 createPostLikeFindRes(data, this.state.posts);
782 this.setState(this.state);
783 } else if (res.op == UserOperation.AddAdmin) {
784 let data = res.data as AddAdminResponse;
785 this.state.siteRes.admins = data.admins;
786 this.setState(this.state);
787 } else if (res.op == UserOperation.BanUser) {
788 let data = res.data as BanUserResponse;
789 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
791 // Remove the banned if its found in the list, and the action is an unban
792 if (found && !data.banned) {
793 this.state.siteRes.banned = this.state.siteRes.banned.filter(
794 i => i.id !== data.user.id
797 this.state.siteRes.banned.push(data.user);
801 .filter(p => p.creator_id == data.user.id)
802 .forEach(p => (p.banned = data.banned));
804 this.setState(this.state);
805 } else if (res.op == UserOperation.GetComments) {
806 let data = res.data as GetCommentsResponse;
807 this.state.comments = data.comments;
808 this.state.loading = false;
809 this.setState(this.state);
811 res.op == UserOperation.EditComment ||
812 res.op == UserOperation.DeleteComment ||
813 res.op == UserOperation.RemoveComment
815 let data = res.data as CommentResponse;
816 editCommentRes(data, this.state.comments);
817 this.setState(this.state);
818 } else if (res.op == UserOperation.CreateComment) {
819 let data = res.data as CommentResponse;
821 // Necessary since it might be a user reply
822 if (data.recipient_ids.length == 0) {
823 // If you're on subscribed, only push it if you're subscribed.
824 if (this.state.listingType == ListingType.Subscribed) {
826 this.state.subscribedCommunities
827 .map(c => c.community_id)
828 .includes(data.comment.community_id)
830 this.state.comments.unshift(data.comment);
833 this.state.comments.unshift(data.comment);
835 this.setState(this.state);
837 } else if (res.op == UserOperation.SaveComment) {
838 let data = res.data as CommentResponse;
839 saveCommentRes(data, this.state.comments);
840 this.setState(this.state);
841 } else if (res.op == UserOperation.CreateCommentLike) {
842 let data = res.data as CommentResponse;
843 createCommentLikeRes(data, this.state.comments);
844 this.setState(this.state);