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, InitialFetchRequest } 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';
43 getListingTypeFromProps,
50 createPostLikeFindRes,
60 import { i18n } from '../i18next';
61 import { T } from 'inferno-i18next';
62 import { HtmlTags } from './html-tags';
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 isoData = setIsoData(this.context);
94 private subscription: Subscription;
95 private emptyState: MainState = {
96 subscribedCommunities: [],
97 trendingCommunities: [],
98 siteRes: this.isoData.site,
103 listingType: getListingTypeFromProps(this.props),
104 dataType: getDataTypeFromProps(this.props),
105 sort: getSortTypeFromProps(this.props),
106 page: getPageFromProps(this.props),
109 constructor(props: any, context: any) {
110 super(props, context);
112 this.state = this.emptyState;
113 this.handleEditCancel = this.handleEditCancel.bind(this);
114 this.handleSortChange = this.handleSortChange.bind(this);
115 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
116 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
118 this.parseMessage = this.parseMessage.bind(this);
119 this.subscription = wsSubscribe(this.parseMessage);
121 // Only fetch the data if coming from another route
122 if (this.isoData.path == this.context.router.route.match.url) {
123 if (this.state.dataType == DataType.Post) {
124 this.state.posts = this.isoData.routeData[0].posts;
126 this.state.comments = this.isoData.routeData[0].comments;
128 this.state.trendingCommunities = this.isoData.routeData[1].communities;
129 if (UserService.Instance.user) {
130 this.state.subscribedCommunities = this.isoData.routeData[2].communities;
132 this.state.loading = false;
134 this.fetchTrendingCommunities();
136 if (UserService.Instance.user) {
137 WebSocketService.Instance.getFollowedCommunities();
144 fetchTrendingCommunities() {
145 let listCommunitiesForm: ListCommunitiesForm = {
149 WebSocketService.Instance.listCommunities(listCommunitiesForm);
152 componentDidMount() {
153 // This means it hasn't been set up yet
154 if (!this.state.siteRes.site) {
155 this.context.router.history.push('/setup');
158 WebSocketService.Instance.communityJoin({ community_id: 0 });
161 componentWillUnmount() {
163 this.subscription.unsubscribe();
164 window.isoData.path = undefined;
168 static getDerivedStateFromProps(props: any): MainProps {
170 listingType: getListingTypeFromProps(props),
171 dataType: getDataTypeFromProps(props),
172 sort: getSortTypeFromProps(props),
173 page: getPageFromProps(props),
177 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
178 let pathSplit = req.path.split('/');
179 let dataType: DataType = pathSplit[3]
180 ? DataType[pathSplit[3]]
183 // TODO figure out auth default_listingType, default_sort_type
184 let type_: ListingType = pathSplit[5]
185 ? ListingType[pathSplit[5]]
186 : UserService.Instance.user
187 ? Object.values(ListingType)[
188 UserService.Instance.user.default_listing_type
191 let sort: SortType = pathSplit[7]
192 ? SortType[pathSplit[7]]
193 : UserService.Instance.user
194 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
197 let page = pathSplit[9] ? Number(pathSplit[9]) : 1;
199 let promises: Promise<any>[] = [];
201 if (dataType == DataType.Post) {
202 let getPostsForm: GetPostsForm = {
208 setAuth(getPostsForm, req.auth);
209 promises.push(req.client.getPosts(getPostsForm));
211 let getCommentsForm: GetCommentsForm = {
217 setAuth(getCommentsForm, req.auth);
218 promises.push(req.client.getComments(getCommentsForm));
221 let trendingCommunitiesForm: ListCommunitiesForm = {
225 promises.push(req.client.listCommunities(trendingCommunitiesForm));
228 promises.push(req.client.getFollowedCommunities({ auth: req.auth }));
234 componentDidUpdate(_: any, lastState: MainState) {
236 lastState.listingType !== this.state.listingType ||
237 lastState.dataType !== this.state.dataType ||
238 lastState.sort !== this.state.sort ||
239 lastState.page !== this.state.page
241 this.setState({ loading: true });
246 get documentTitle(): string {
248 this.state.siteRes.site ? this.state.siteRes.site.name : 'Lemmy'
254 <div class="container">
256 title={this.documentTitle}
257 path={this.context.router.route.match.url}
259 {this.state.siteRes.site && (
261 <main role="main" class="col-12 col-md-8">
264 <aside class="col-12 col-md-4">{this.mySidebar()}</aside>
274 {!this.state.loading && (
276 <div class="card border-secondary mb-3">
277 <div class="card-body">
278 {this.trendingCommunities()}
279 {this.createCommunityButton()}
283 {UserService.Instance.user &&
284 this.state.subscribedCommunities.length > 0 && (
285 <div class="card border-secondary mb-3">
286 <div class="card-body">{this.subscribedCommunities()}</div>
290 <div class="card border-secondary mb-3">
291 <div class="card-body">{this.sidebar()}</div>
299 createCommunityButton() {
301 <Link className="btn btn-secondary btn-block" to="/create_community">
302 {i18n.t('create_a_community')}
307 trendingCommunities() {
311 <T i18nKey="trending_communities">
313 <Link className="text-body" to="/communities">
318 <ul class="list-inline">
319 {this.state.trendingCommunities.map(community => (
320 <li class="list-inline-item d-inline">
321 <CommunityLink community={community} />
329 subscribedCommunities() {
333 <T i18nKey="subscribed_to_communities">
335 <Link className="text-body" to="/communities">
340 <ul class="list-inline mb-0">
341 {this.state.subscribedCommunities.map(community => (
342 <li class="list-inline-item d-inline">
345 name: community.community_name,
346 id: community.community_id,
347 local: community.community_local,
348 actor_id: community.community_actor_id,
349 icon: community.community_icon,
362 {!this.state.showEditSite ? (
366 {this.adminButtons()}
368 <BannerIconHeader banner={this.state.siteRes.site.banner} />
373 site={this.state.siteRes.site}
374 onCancel={this.handleEditCancel}
381 updateUrl(paramUpdates: UrlParams) {
382 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
383 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
384 const sortStr = paramUpdates.sort || this.state.sort;
385 const page = paramUpdates.page || this.state.page;
386 this.props.history.push(
387 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
394 {this.state.siteRes.site.description && this.siteDescription()}
402 return <h5 class="mb-0">{`${this.documentTitle}`}</h5>;
407 <ul class="mt-1 list-inline small mb-0">
408 <li class="list-inline-item">{i18n.t('admins')}:</li>
409 {this.state.siteRes.admins.map(admin => (
410 <li class="list-inline-item">
414 preferred_username: admin.preferred_username,
415 avatar: admin.avatar,
417 actor_id: admin.actor_id,
429 <ul class="my-2 list-inline">
430 <li className="list-inline-item badge badge-secondary">
431 {i18n.t('number_online', { count: this.state.siteRes.online })}
433 <li className="list-inline-item badge badge-secondary">
434 {i18n.t('number_of_users', {
435 count: this.state.siteRes.site.number_of_users,
438 <li className="list-inline-item badge badge-secondary">
439 {i18n.t('number_of_communities', {
440 count: this.state.siteRes.site.number_of_communities,
443 <li className="list-inline-item badge badge-secondary">
444 {i18n.t('number_of_posts', {
445 count: this.state.siteRes.site.number_of_posts,
448 <li className="list-inline-item badge badge-secondary">
449 {i18n.t('number_of_comments', {
450 count: this.state.siteRes.site.number_of_comments,
453 <li className="list-inline-item">
454 <Link className="badge badge-secondary" to="/modlog">
465 <ul class="list-inline mb-1 text-muted font-weight-bold">
466 <li className="list-inline-item-action">
469 onClick={linkEvent(this, this.handleEditClick)}
470 data-tippy-content={i18n.t('edit')}
472 <svg class="icon icon-inline">
473 <use xlinkHref="#icon-edit"></use>
486 dangerouslySetInnerHTML={mdToHtml(this.state.siteRes.site.description)}
493 <div class="main-content-wrapper">
494 {this.state.loading ? (
496 <svg class="icon icon-spinner spin">
497 <use xlinkHref="#icon-spinner"></use>
512 return this.state.dataType == DataType.Post ? (
514 posts={this.state.posts}
517 sort={this.state.sort}
518 enableDownvotes={this.state.siteRes.site.enable_downvotes}
519 enableNsfw={this.state.siteRes.site.enable_nsfw}
523 nodes={commentsToFlatNodes(this.state.comments)}
526 sortType={this.state.sort}
528 enableDownvotes={this.state.siteRes.site.enable_downvotes}
535 <div className="mb-3">
538 type_={this.state.dataType}
539 onChange={this.handleDataTypeChange}
544 type_={this.state.listingType}
545 showLocal={this.showLocal}
546 onChange={this.handleListingTypeChange}
550 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
552 {this.state.listingType == ListingType.All && (
554 href={`/feeds/all.xml?sort=${this.state.sort}`}
559 <svg class="icon text-muted small">
560 <use xlinkHref="#icon-rss">#</use>
564 {this.state.listingType == ListingType.Local && (
566 href={`/feeds/local.xml?sort=${this.state.sort}`}
571 <svg class="icon text-muted small">
572 <use xlinkHref="#icon-rss">#</use>
576 {UserService.Instance.user &&
577 this.state.listingType == ListingType.Subscribed && (
579 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${this.state.sort}`}
584 <svg class="icon text-muted small">
585 <use xlinkHref="#icon-rss">#</use>
596 {this.state.page > 1 && (
598 class="btn btn-secondary mr-1"
599 onClick={linkEvent(this, this.prevPage)}
604 {this.state.posts.length > 0 && (
606 class="btn btn-secondary"
607 onClick={linkEvent(this, this.nextPage)}
616 get showLocal(): boolean {
618 this.isoData.site.federated_instances !== null &&
619 this.isoData.site.federated_instances.length > 0
623 get canAdmin(): boolean {
625 UserService.Instance.user &&
626 this.state.siteRes.admins
628 .includes(UserService.Instance.user.id)
632 handleEditClick(i: Main) {
633 i.state.showEditSite = true;
638 this.state.showEditSite = false;
639 this.setState(this.state);
643 i.updateUrl({ page: i.state.page + 1 });
644 window.scrollTo(0, 0);
648 i.updateUrl({ page: i.state.page - 1 });
649 window.scrollTo(0, 0);
652 handleSortChange(val: SortType) {
653 this.updateUrl({ sort: val, page: 1 });
654 window.scrollTo(0, 0);
657 handleListingTypeChange(val: ListingType) {
658 this.updateUrl({ listingType: val, page: 1 });
659 window.scrollTo(0, 0);
662 handleDataTypeChange(val: DataType) {
663 this.updateUrl({ dataType: DataType[val], page: 1 });
664 window.scrollTo(0, 0);
668 if (this.state.dataType == DataType.Post) {
669 let getPostsForm: GetPostsForm = {
670 page: this.state.page,
672 sort: this.state.sort,
673 type_: this.state.listingType,
675 WebSocketService.Instance.getPosts(getPostsForm);
677 let getCommentsForm: GetCommentsForm = {
678 page: this.state.page,
680 sort: this.state.sort,
681 type_: this.state.listingType,
683 WebSocketService.Instance.getComments(getCommentsForm);
687 parseMessage(msg: WebSocketJsonResponse) {
689 let res = wsJsonToRes(msg);
691 toast(i18n.t(msg.error), 'danger');
693 } else if (msg.reconnect) {
694 WebSocketService.Instance.communityJoin({ community_id: 0 });
696 } else if (res.op == UserOperation.GetFollowedCommunities) {
697 let data = res.data as GetFollowedCommunitiesResponse;
698 this.state.subscribedCommunities = data.communities;
699 this.setState(this.state);
700 } else if (res.op == UserOperation.ListCommunities) {
701 let data = res.data as ListCommunitiesResponse;
702 this.state.trendingCommunities = data.communities;
703 this.setState(this.state);
704 } else if (res.op == UserOperation.EditSite) {
705 let data = res.data as SiteResponse;
706 this.state.siteRes.site = data.site;
707 this.state.showEditSite = false;
708 this.setState(this.state);
709 toast(i18n.t('site_saved'));
710 } else if (res.op == UserOperation.GetPosts) {
711 let data = res.data as GetPostsResponse;
712 this.state.posts = data.posts;
713 this.state.loading = false;
714 this.setState(this.state);
716 } else if (res.op == UserOperation.CreatePost) {
717 let data = res.data as PostResponse;
719 // If you're on subscribed, only push it if you're subscribed.
720 if (this.state.listingType == ListingType.Subscribed) {
722 this.state.subscribedCommunities
723 .map(c => c.community_id)
724 .includes(data.post.community_id)
726 this.state.posts.unshift(data.post);
727 notifyPost(data.post, this.context.router);
731 let nsfw = data.post.nsfw || data.post.community_nsfw;
733 // Don't push the post if its nsfw, and don't have that setting on
737 UserService.Instance.user &&
738 UserService.Instance.user.show_nsfw)
740 this.state.posts.unshift(data.post);
741 notifyPost(data.post, this.context.router);
744 this.setState(this.state);
746 res.op == UserOperation.EditPost ||
747 res.op == UserOperation.DeletePost ||
748 res.op == UserOperation.RemovePost ||
749 res.op == UserOperation.LockPost ||
750 res.op == UserOperation.StickyPost ||
751 res.op == UserOperation.SavePost
753 let data = res.data as PostResponse;
754 editPostFindRes(data, this.state.posts);
755 this.setState(this.state);
756 } else if (res.op == UserOperation.CreatePostLike) {
757 let data = res.data as PostResponse;
758 createPostLikeFindRes(data, this.state.posts);
759 this.setState(this.state);
760 } else if (res.op == UserOperation.AddAdmin) {
761 let data = res.data as AddAdminResponse;
762 this.state.siteRes.admins = data.admins;
763 this.setState(this.state);
764 } else if (res.op == UserOperation.BanUser) {
765 let data = res.data as BanUserResponse;
766 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
768 // Remove the banned if its found in the list, and the action is an unban
769 if (found && !data.banned) {
770 this.state.siteRes.banned = this.state.siteRes.banned.filter(
771 i => i.id !== data.user.id
774 this.state.siteRes.banned.push(data.user);
778 .filter(p => p.creator_id == data.user.id)
779 .forEach(p => (p.banned = data.banned));
781 this.setState(this.state);
782 } else if (res.op == UserOperation.GetComments) {
783 let data = res.data as GetCommentsResponse;
784 this.state.comments = data.comments;
785 this.state.loading = false;
786 this.setState(this.state);
788 res.op == UserOperation.EditComment ||
789 res.op == UserOperation.DeleteComment ||
790 res.op == UserOperation.RemoveComment
792 let data = res.data as CommentResponse;
793 editCommentRes(data, this.state.comments);
794 this.setState(this.state);
795 } else if (res.op == UserOperation.CreateComment) {
796 let data = res.data as CommentResponse;
798 // Necessary since it might be a user reply
799 if (data.recipient_ids.length == 0) {
800 // If you're on subscribed, only push it if you're subscribed.
801 if (this.state.listingType == ListingType.Subscribed) {
803 this.state.subscribedCommunities
804 .map(c => c.community_id)
805 .includes(data.comment.community_id)
807 this.state.comments.unshift(data.comment);
810 this.state.comments.unshift(data.comment);
812 this.setState(this.state);
814 } else if (res.op == UserOperation.SaveComment) {
815 let data = res.data as CommentResponse;
816 saveCommentRes(data, this.state.comments);
817 this.setState(this.state);
818 } else if (res.op == UserOperation.CreateCommentLike) {
819 let data = res.data as CommentResponse;
820 createCommentLikeRes(data, this.state.comments);
821 this.setState(this.state);