1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
3 import { T } from "inferno-i18next-dess";
4 import { Link } from "inferno-router";
19 ListCommunitiesResponse,
29 } from "lemmy-js-client";
30 import { Subscription } from "rxjs";
31 import { i18n } from "../../i18next";
32 import { DataType, InitialFetchRequest } from "../../interfaces";
33 import { UserService, WebSocketService } from "../../services";
38 createPostLikeFindRes,
45 getListingTypeFromProps,
51 restoreScrollPosition,
63 import { CommentNodes } from "../comment/comment-nodes";
64 import { DataTypeSelect } from "../common/data-type-select";
65 import { HtmlTags } from "../common/html-tags";
66 import { Icon, Spinner } from "../common/icon";
67 import { ListingTypeSelect } from "../common/listing-type-select";
68 import { Paginator } from "../common/paginator";
69 import { SortSelect } from "../common/sort-select";
70 import { CommunityLink } from "../community/community-link";
71 import { PostListings } from "../post/post-listings";
72 import { SiteSidebar } from "./site-sidebar";
75 trendingCommunities: CommunityView[];
76 siteRes: GetSiteResponse;
78 comments: CommentView[];
79 listingType: ListingType;
83 showSubscribedMobile: boolean;
84 showTrendingMobile: boolean;
85 showSidebarMobile: boolean;
86 subscribedCollapsed: boolean;
91 listingType: ListingType;
98 listingType?: ListingType;
104 export class Home extends Component<any, HomeState> {
105 private isoData = setIsoData(
109 ListCommunitiesResponse
111 private subscription: Subscription;
112 private emptyState: HomeState = {
113 trendingCommunities: [],
114 siteRes: this.isoData.site_res,
115 showSubscribedMobile: false,
116 showTrendingMobile: false,
117 showSidebarMobile: false,
118 subscribedCollapsed: false,
122 listingType: getListingTypeFromProps(
125 this.isoData.site_res.site_view.unwrap().site.default_post_listing_type
128 dataType: getDataTypeFromProps(this.props),
129 sort: getSortTypeFromProps(this.props),
130 page: getPageFromProps(this.props),
133 constructor(props: any, context: any) {
134 super(props, context);
136 this.state = this.emptyState;
137 this.handleSortChange = this.handleSortChange.bind(this);
138 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
139 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
140 this.handlePageChange = this.handlePageChange.bind(this);
142 this.parseMessage = this.parseMessage.bind(this);
143 this.subscription = wsSubscribe(this.parseMessage);
145 // Only fetch the data if coming from another route
146 if (this.isoData.path == this.context.router.route.match.url) {
147 let postsRes = Some(this.isoData.routeData[0] as GetPostsResponse);
148 let commentsRes = Some(this.isoData.routeData[1] as GetCommentsResponse);
149 let trendingRes = this.isoData.routeData[2] as ListCommunitiesResponse;
152 some: pvs => (this.state.posts = pvs.posts),
156 some: cvs => (this.state.comments = cvs.comments),
159 this.state.trendingCommunities = trendingRes.communities;
162 WebSocketService.Instance.send(
163 wsClient.communityJoin({ community_id: 0 })
166 this.state.loading = false;
168 this.fetchTrendingCommunities();
173 fetchTrendingCommunities() {
174 let listCommunitiesForm = new ListCommunities({
175 type_: Some(ListingType.Local),
176 sort: Some(SortType.Hot),
177 limit: Some(trendingFetchLimit),
179 auth: auth(false).ok(),
181 WebSocketService.Instance.send(
182 wsClient.listCommunities(listCommunitiesForm)
186 componentDidMount() {
187 // This means it hasn't been set up yet
188 if (!this.state.siteRes.site_view) {
189 this.context.router.history.push("/setup");
194 componentWillUnmount() {
195 saveScrollPosition(this.context);
196 this.subscription.unsubscribe();
197 window.isoData.path = undefined;
200 static getDerivedStateFromProps(props: any): HomeProps {
202 listingType: getListingTypeFromProps(props, ListingType.Local),
203 dataType: getDataTypeFromProps(props),
204 sort: getSortTypeFromProps(props),
205 page: getPageFromProps(props),
209 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
210 let pathSplit = req.path.split("/");
211 let dataType: DataType = pathSplit[3]
212 ? DataType[pathSplit[3]]
215 // TODO figure out auth default_listingType, default_sort_type
216 let type_: Option<ListingType> = Some(
218 ? ListingType[pathSplit[5]]
219 : UserService.Instance.myUserInfo.match({
221 Object.values(ListingType)[
222 mui.local_user_view.local_user.default_listing_type
224 none: ListingType.Local,
227 let sort: Option<SortType> = Some(
229 ? SortType[pathSplit[7]]
230 : UserService.Instance.myUserInfo.match({
232 Object.values(SortType)[
233 mui.local_user_view.local_user.default_sort_type
235 none: SortType.Active,
239 let page = Some(pathSplit[9] ? Number(pathSplit[9]) : 1);
241 let promises: Promise<any>[] = [];
243 if (dataType == DataType.Post) {
244 let getPostsForm = new GetPosts({
246 community_name: None,
249 limit: Some(fetchLimit),
251 saved_only: Some(false),
255 promises.push(req.client.getPosts(getPostsForm));
256 promises.push(Promise.resolve());
258 let getCommentsForm = new GetComments({
260 community_name: None,
262 limit: Some(fetchLimit),
265 saved_only: Some(false),
268 promises.push(Promise.resolve());
269 promises.push(req.client.getComments(getCommentsForm));
272 let trendingCommunitiesForm = new ListCommunities({
273 type_: Some(ListingType.Local),
274 sort: Some(SortType.Hot),
275 limit: Some(trendingFetchLimit),
279 promises.push(req.client.listCommunities(trendingCommunitiesForm));
284 componentDidUpdate(_: any, lastState: HomeState) {
286 lastState.listingType !== this.state.listingType ||
287 lastState.dataType !== this.state.dataType ||
288 lastState.sort !== this.state.sort ||
289 lastState.page !== this.state.page
291 this.setState({ loading: true });
296 get documentTitle(): string {
297 return this.state.siteRes.site_view.match({
299 siteView.site.description.match({
300 some: desc => `${siteView.site.name} - ${desc}`,
301 none: siteView.site.name,
309 <div class="container">
311 title={this.documentTitle}
312 path={this.context.router.route.match.url}
316 {this.state.siteRes.site_view.isSome() && (
318 <main role="main" class="col-12 col-md-8">
319 <div class="d-block d-md-none">{this.mobileView()}</div>
322 <aside class="d-none d-md-block col-md-4">{this.mySidebar()}</aside>
329 get hasFollows(): boolean {
330 return UserService.Instance.myUserInfo.match({
331 some: mui => mui.follows.length > 0,
337 let siteRes = this.state.siteRes;
341 {this.hasFollows && (
343 class="btn btn-secondary d-inline-block mb-2 mr-3"
344 onClick={linkEvent(this, this.handleShowSubscribedMobile)}
346 {i18n.t("subscribed")}{" "}
349 this.state.showSubscribedMobile
353 classes="icon-inline"
358 class="btn btn-secondary d-inline-block mb-2 mr-3"
359 onClick={linkEvent(this, this.handleShowTrendingMobile)}
361 {i18n.t("trending")}{" "}
364 this.state.showTrendingMobile ? `minus-square` : `plus-square`
366 classes="icon-inline"
370 class="btn btn-secondary d-inline-block mb-2 mr-3"
371 onClick={linkEvent(this, this.handleShowSidebarMobile)}
373 {i18n.t("sidebar")}{" "}
376 this.state.showSidebarMobile ? `minus-square` : `plus-square`
378 classes="icon-inline"
381 {this.state.showSidebarMobile &&
382 siteRes.site_view.match({
386 admins={Some(siteRes.admins)}
387 counts={Some(siteView.counts)}
388 online={Some(siteRes.online)}
389 showLocal={showLocal(this.isoData)}
394 {this.state.showTrendingMobile && (
395 <div class="col-12 card border-secondary mb-3">
396 <div class="card-body">{this.trendingCommunities()}</div>
399 {this.state.showSubscribedMobile && (
400 <div class="col-12 card border-secondary mb-3">
401 <div class="card-body">{this.subscribedCommunities()}</div>
410 let siteRes = this.state.siteRes;
413 {!this.state.loading && (
415 <div class="card border-secondary mb-3">
416 <div class="card-body">
417 {this.trendingCommunities()}
418 {this.createCommunityButton()}
419 {this.exploreCommunitiesButton()}
422 {siteRes.site_view.match({
426 admins={Some(siteRes.admins)}
427 counts={Some(siteView.counts)}
428 online={Some(siteRes.online)}
429 showLocal={showLocal(this.isoData)}
434 {this.hasFollows && (
435 <div class="card border-secondary mb-3">
436 <div class="card-body">{this.subscribedCommunities()}</div>
445 createCommunityButton() {
447 <Link className="mt-2 btn btn-secondary btn-block" to="/create_community">
448 {i18n.t("create_a_community")}
453 exploreCommunitiesButton() {
455 <Link className="btn btn-secondary btn-block" to="/communities">
456 {i18n.t("explore_communities")}
461 trendingCommunities() {
465 <T i18nKey="trending_communities">
467 <Link className="text-body" to="/communities">
472 <ul class="list-inline mb-0">
473 {this.state.trendingCommunities.map(cv => (
474 <li class="list-inline-item d-inline-block">
475 <CommunityLink community={cv.community} />
483 subscribedCommunities() {
487 <T class="d-inline" i18nKey="subscribed_to_communities">
489 <Link className="text-body" to="/communities">
494 class="btn btn-sm text-muted"
495 onClick={linkEvent(this, this.handleCollapseSubscribe)}
496 aria-label={i18n.t("collapse")}
497 data-tippy-content={i18n.t("collapse")}
499 {this.state.subscribedCollapsed ? (
500 <Icon icon="plus-square" classes="icon-inline" />
502 <Icon icon="minus-square" classes="icon-inline" />
506 {!this.state.subscribedCollapsed && (
507 <ul class="list-inline mb-0">
508 {UserService.Instance.myUserInfo
512 <li class="list-inline-item d-inline-block">
513 <CommunityLink community={cfv.community} />
522 updateUrl(paramUpdates: UrlParams) {
523 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
524 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
525 const sortStr = paramUpdates.sort || this.state.sort;
526 const page = paramUpdates.page || this.state.page;
527 this.props.history.push(
528 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
534 <div class="main-content-wrapper">
535 {this.state.loading ? (
544 page={this.state.page}
545 onChange={this.handlePageChange}
554 return this.state.dataType == DataType.Post ? (
556 posts={this.state.posts}
559 enableDownvotes={enableDownvotes(this.state.siteRes)}
560 enableNsfw={enableNsfw(this.state.siteRes)}
564 nodes={commentsToFlatNodes(this.state.comments)}
567 maxCommentsShown={None}
571 enableDownvotes={enableDownvotes(this.state.siteRes)}
577 let allRss = `/feeds/all.xml?sort=${this.state.sort}`;
578 let localRss = `/feeds/local.xml?sort=${this.state.sort}`;
579 let frontRss = auth(false)
581 .map(auth => `/feeds/front/${auth}.xml?sort=${this.state.sort}`);
584 <div className="mb-3">
587 type_={this.state.dataType}
588 onChange={this.handleDataTypeChange}
593 type_={this.state.listingType}
594 showLocal={showLocal(this.isoData)}
596 onChange={this.handleListingTypeChange}
600 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
602 {this.state.listingType == ListingType.All && (
604 <a href={allRss} rel={relTags} title="RSS">
605 <Icon icon="rss" classes="text-muted small" />
607 <link rel="alternate" type="application/atom+xml" href={allRss} />
610 {this.state.listingType == ListingType.Local && (
612 <a href={localRss} rel={relTags} title="RSS">
613 <Icon icon="rss" classes="text-muted small" />
615 <link rel="alternate" type="application/atom+xml" href={localRss} />
618 {this.state.listingType == ListingType.Subscribed &&
622 <a href={rss} title="RSS" rel={relTags}>
623 <Icon icon="rss" classes="text-muted small" />
625 <link rel="alternate" type="application/atom+xml" href={rss} />
634 handleShowSubscribedMobile(i: Home) {
635 i.state.showSubscribedMobile = !i.state.showSubscribedMobile;
639 handleShowTrendingMobile(i: Home) {
640 i.state.showTrendingMobile = !i.state.showTrendingMobile;
644 handleShowSidebarMobile(i: Home) {
645 i.state.showSidebarMobile = !i.state.showSidebarMobile;
649 handleCollapseSubscribe(i: Home) {
650 i.state.subscribedCollapsed = !i.state.subscribedCollapsed;
654 handlePageChange(page: number) {
655 this.updateUrl({ page });
656 window.scrollTo(0, 0);
659 handleSortChange(val: SortType) {
660 this.updateUrl({ sort: val, page: 1 });
661 window.scrollTo(0, 0);
664 handleListingTypeChange(val: ListingType) {
665 this.updateUrl({ listingType: val, page: 1 });
666 window.scrollTo(0, 0);
669 handleDataTypeChange(val: DataType) {
670 this.updateUrl({ dataType: DataType[val], page: 1 });
671 window.scrollTo(0, 0);
675 if (this.state.dataType == DataType.Post) {
676 let getPostsForm = new GetPosts({
678 community_name: None,
679 page: Some(this.state.page),
680 limit: Some(fetchLimit),
681 sort: Some(this.state.sort),
682 saved_only: Some(false),
683 auth: auth(false).ok(),
684 type_: Some(this.state.listingType),
687 WebSocketService.Instance.send(wsClient.getPosts(getPostsForm));
689 let getCommentsForm = new GetComments({
691 community_name: None,
692 page: Some(this.state.page),
693 limit: Some(fetchLimit),
694 sort: Some(this.state.sort),
695 saved_only: Some(false),
696 auth: auth(false).ok(),
697 type_: Some(this.state.listingType),
699 WebSocketService.Instance.send(wsClient.getComments(getCommentsForm));
703 parseMessage(msg: any) {
704 let op = wsUserOp(msg);
707 toast(i18n.t(msg.error), "danger");
709 } else if (msg.reconnect) {
710 WebSocketService.Instance.send(
711 wsClient.communityJoin({ community_id: 0 })
714 } else if (op == UserOperation.ListCommunities) {
715 let data = wsJsonToRes<ListCommunitiesResponse>(
717 ListCommunitiesResponse
719 this.state.trendingCommunities = data.communities;
720 this.setState(this.state);
721 } else if (op == UserOperation.EditSite) {
722 let data = wsJsonToRes<SiteResponse>(msg, SiteResponse);
723 this.state.siteRes.site_view = Some(data.site_view);
724 this.setState(this.state);
725 toast(i18n.t("site_saved"));
726 } else if (op == UserOperation.GetPosts) {
727 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
728 this.state.posts = data.posts;
729 this.state.loading = false;
730 this.setState(this.state);
731 WebSocketService.Instance.send(
732 wsClient.communityJoin({ community_id: 0 })
734 restoreScrollPosition(this.context);
736 } else if (op == UserOperation.CreatePost) {
737 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
739 let nsfw = data.post_view.post.nsfw || data.post_view.community.nsfw;
743 UserService.Instance.myUserInfo
744 .map(m => m.local_user_view.local_user.show_nsfw)
747 let showPostNotifs = UserService.Instance.myUserInfo
748 .map(m => m.local_user_view.local_user.show_new_post_notifs)
751 // Only push these if you're on the first page, and you pass the nsfw check
752 if (this.state.page == 1 && nsfwCheck) {
753 // If you're on subscribed, only push it if you're subscribed.
754 if (this.state.listingType == ListingType.Subscribed) {
756 UserService.Instance.myUserInfo
759 .map(c => c.community.id)
760 .includes(data.post_view.community.id)
762 this.state.posts.unshift(data.post_view);
763 if (showPostNotifs) {
764 notifyPost(data.post_view, this.context.router);
767 } else if (this.state.listingType == ListingType.Local) {
768 // If you're on the local view, only push it if its local
769 if (data.post_view.post.local) {
770 this.state.posts.unshift(data.post_view);
771 if (showPostNotifs) {
772 notifyPost(data.post_view, this.context.router);
776 this.state.posts.unshift(data.post_view);
777 if (showPostNotifs) {
778 notifyPost(data.post_view, this.context.router);
781 this.setState(this.state);
784 op == UserOperation.EditPost ||
785 op == UserOperation.DeletePost ||
786 op == UserOperation.RemovePost ||
787 op == UserOperation.LockPost ||
788 op == UserOperation.StickyPost ||
789 op == UserOperation.SavePost
791 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
792 editPostFindRes(data.post_view, this.state.posts);
793 this.setState(this.state);
794 } else if (op == UserOperation.CreatePostLike) {
795 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
796 createPostLikeFindRes(data.post_view, this.state.posts);
797 this.setState(this.state);
798 } else if (op == UserOperation.AddAdmin) {
799 let data = wsJsonToRes<AddAdminResponse>(msg, AddAdminResponse);
800 this.state.siteRes.admins = data.admins;
801 this.setState(this.state);
802 } else if (op == UserOperation.BanPerson) {
803 let data = wsJsonToRes<BanPersonResponse>(msg, BanPersonResponse);
805 .filter(p => p.creator.id == data.person_view.person.id)
806 .forEach(p => (p.creator.banned = data.banned));
808 this.setState(this.state);
809 } else if (op == UserOperation.GetComments) {
810 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
811 this.state.comments = data.comments;
812 this.state.loading = false;
813 this.setState(this.state);
815 op == UserOperation.EditComment ||
816 op == UserOperation.DeleteComment ||
817 op == UserOperation.RemoveComment
819 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
820 editCommentRes(data.comment_view, this.state.comments);
821 this.setState(this.state);
822 } else if (op == UserOperation.CreateComment) {
823 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
825 // Necessary since it might be a user reply
827 // If you're on subscribed, only push it if you're subscribed.
828 if (this.state.listingType == ListingType.Subscribed) {
830 UserService.Instance.myUserInfo
833 .map(c => c.community.id)
834 .includes(data.comment_view.community.id)
836 this.state.comments.unshift(data.comment_view);
839 this.state.comments.unshift(data.comment_view);
841 this.setState(this.state);
843 } else if (op == UserOperation.SaveComment) {
844 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
845 saveCommentRes(data.comment_view, this.state.comments);
846 this.setState(this.state);
847 } else if (op == UserOperation.CreateCommentLike) {
848 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
849 createCommentLikeRes(data.comment_view, this.state.comments);
850 this.setState(this.state);
851 } else if (op == UserOperation.BlockPerson) {
852 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
853 updatePersonBlock(data);
854 } else if (op == UserOperation.CreatePostReport) {
855 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
857 toast(i18n.t("report_created"));
859 } else if (op == UserOperation.CreateCommentReport) {
860 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
862 toast(i18n.t("report_created"));