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();
199 static getDerivedStateFromProps(props: any): HomeProps {
201 listingType: getListingTypeFromProps(props, ListingType.Local),
202 dataType: getDataTypeFromProps(props),
203 sort: getSortTypeFromProps(props),
204 page: getPageFromProps(props),
208 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
209 let pathSplit = req.path.split("/");
210 let dataType: DataType = pathSplit[3]
211 ? DataType[pathSplit[3]]
214 // TODO figure out auth default_listingType, default_sort_type
215 let type_: Option<ListingType> = Some(
217 ? ListingType[pathSplit[5]]
218 : UserService.Instance.myUserInfo.match({
220 Object.values(ListingType)[
221 mui.local_user_view.local_user.default_listing_type
223 none: ListingType.Local,
226 let sort: Option<SortType> = Some(
228 ? SortType[pathSplit[7]]
229 : UserService.Instance.myUserInfo.match({
231 Object.values(SortType)[
232 mui.local_user_view.local_user.default_sort_type
234 none: SortType.Active,
238 let page = Some(pathSplit[9] ? Number(pathSplit[9]) : 1);
240 let promises: Promise<any>[] = [];
242 if (dataType == DataType.Post) {
243 let getPostsForm = new GetPosts({
245 community_name: None,
248 limit: Some(fetchLimit),
250 saved_only: Some(false),
254 promises.push(req.client.getPosts(getPostsForm));
255 promises.push(Promise.resolve());
257 let getCommentsForm = new GetComments({
259 community_name: None,
261 limit: Some(fetchLimit),
264 saved_only: Some(false),
267 promises.push(Promise.resolve());
268 promises.push(req.client.getComments(getCommentsForm));
271 let trendingCommunitiesForm = new ListCommunities({
272 type_: Some(ListingType.Local),
273 sort: Some(SortType.Hot),
274 limit: Some(trendingFetchLimit),
278 promises.push(req.client.listCommunities(trendingCommunitiesForm));
283 componentDidUpdate(_: any, lastState: HomeState) {
285 lastState.listingType !== this.state.listingType ||
286 lastState.dataType !== this.state.dataType ||
287 lastState.sort !== this.state.sort ||
288 lastState.page !== this.state.page
290 this.setState({ loading: true });
295 get documentTitle(): string {
296 return this.state.siteRes.site_view.match({
298 siteView.site.description.match({
299 some: desc => `${siteView.site.name} - ${desc}`,
300 none: siteView.site.name,
308 <div class="container">
310 title={this.documentTitle}
311 path={this.context.router.route.match.url}
315 {this.state.siteRes.site_view.isSome() && (
317 <main role="main" class="col-12 col-md-8">
318 <div class="d-block d-md-none">{this.mobileView()}</div>
321 <aside class="d-none d-md-block col-md-4">{this.mySidebar()}</aside>
328 get hasFollows(): boolean {
329 return UserService.Instance.myUserInfo.match({
330 some: mui => mui.follows.length > 0,
336 let siteRes = this.state.siteRes;
340 {this.hasFollows && (
342 class="btn btn-secondary d-inline-block mb-2 mr-3"
343 onClick={linkEvent(this, this.handleShowSubscribedMobile)}
345 {i18n.t("subscribed")}{" "}
348 this.state.showSubscribedMobile
352 classes="icon-inline"
357 class="btn btn-secondary d-inline-block mb-2 mr-3"
358 onClick={linkEvent(this, this.handleShowTrendingMobile)}
360 {i18n.t("trending")}{" "}
363 this.state.showTrendingMobile ? `minus-square` : `plus-square`
365 classes="icon-inline"
369 class="btn btn-secondary d-inline-block mb-2 mr-3"
370 onClick={linkEvent(this, this.handleShowSidebarMobile)}
372 {i18n.t("sidebar")}{" "}
375 this.state.showSidebarMobile ? `minus-square` : `plus-square`
377 classes="icon-inline"
380 {this.state.showSidebarMobile &&
381 siteRes.site_view.match({
385 admins={Some(siteRes.admins)}
386 counts={Some(siteView.counts)}
387 online={Some(siteRes.online)}
388 showLocal={showLocal(this.isoData)}
393 {this.state.showTrendingMobile && (
394 <div class="col-12 card border-secondary mb-3">
395 <div class="card-body">{this.trendingCommunities()}</div>
398 {this.state.showSubscribedMobile && (
399 <div class="col-12 card border-secondary mb-3">
400 <div class="card-body">{this.subscribedCommunities()}</div>
409 let siteRes = this.state.siteRes;
412 {!this.state.loading && (
414 <div class="card border-secondary mb-3">
415 <div class="card-body">
416 {this.trendingCommunities()}
417 {this.createCommunityButton()}
418 {this.exploreCommunitiesButton()}
421 {siteRes.site_view.match({
425 admins={Some(siteRes.admins)}
426 counts={Some(siteView.counts)}
427 online={Some(siteRes.online)}
428 showLocal={showLocal(this.isoData)}
433 {this.hasFollows && (
434 <div class="card border-secondary mb-3">
435 <div class="card-body">{this.subscribedCommunities()}</div>
444 createCommunityButton() {
446 <Link className="mt-2 btn btn-secondary btn-block" to="/create_community">
447 {i18n.t("create_a_community")}
452 exploreCommunitiesButton() {
454 <Link className="btn btn-secondary btn-block" to="/communities">
455 {i18n.t("explore_communities")}
460 trendingCommunities() {
464 <T i18nKey="trending_communities">
466 <Link className="text-body" to="/communities">
471 <ul class="list-inline mb-0">
472 {this.state.trendingCommunities.map(cv => (
473 <li class="list-inline-item d-inline-block">
474 <CommunityLink community={cv.community} />
482 subscribedCommunities() {
486 <T class="d-inline" i18nKey="subscribed_to_communities">
488 <Link className="text-body" to="/communities">
493 class="btn btn-sm text-muted"
494 onClick={linkEvent(this, this.handleCollapseSubscribe)}
495 aria-label={i18n.t("collapse")}
496 data-tippy-content={i18n.t("collapse")}
498 {this.state.subscribedCollapsed ? (
499 <Icon icon="plus-square" classes="icon-inline" />
501 <Icon icon="minus-square" classes="icon-inline" />
505 {!this.state.subscribedCollapsed && (
506 <ul class="list-inline mb-0">
507 {UserService.Instance.myUserInfo
511 <li class="list-inline-item d-inline-block">
512 <CommunityLink community={cfv.community} />
521 updateUrl(paramUpdates: UrlParams) {
522 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
523 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
524 const sortStr = paramUpdates.sort || this.state.sort;
525 const page = paramUpdates.page || this.state.page;
526 this.props.history.push(
527 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
533 <div class="main-content-wrapper">
534 {this.state.loading ? (
543 page={this.state.page}
544 onChange={this.handlePageChange}
553 return this.state.dataType == DataType.Post ? (
555 posts={this.state.posts}
558 enableDownvotes={enableDownvotes(this.state.siteRes)}
559 enableNsfw={enableNsfw(this.state.siteRes)}
563 nodes={commentsToFlatNodes(this.state.comments)}
566 maxCommentsShown={None}
570 enableDownvotes={enableDownvotes(this.state.siteRes)}
576 let allRss = `/feeds/all.xml?sort=${this.state.sort}`;
577 let localRss = `/feeds/local.xml?sort=${this.state.sort}`;
578 let frontRss = auth(false)
580 .map(auth => `/feeds/front/${auth}.xml?sort=${this.state.sort}`);
583 <div className="mb-3">
586 type_={this.state.dataType}
587 onChange={this.handleDataTypeChange}
592 type_={this.state.listingType}
593 showLocal={showLocal(this.isoData)}
595 onChange={this.handleListingTypeChange}
599 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
601 {this.state.listingType == ListingType.All && (
603 <a href={allRss} rel={relTags} title="RSS">
604 <Icon icon="rss" classes="text-muted small" />
606 <link rel="alternate" type="application/atom+xml" href={allRss} />
609 {this.state.listingType == ListingType.Local && (
611 <a href={localRss} rel={relTags} title="RSS">
612 <Icon icon="rss" classes="text-muted small" />
614 <link rel="alternate" type="application/atom+xml" href={localRss} />
617 {this.state.listingType == ListingType.Subscribed &&
621 <a href={rss} title="RSS" rel={relTags}>
622 <Icon icon="rss" classes="text-muted small" />
624 <link rel="alternate" type="application/atom+xml" href={rss} />
633 handleShowSubscribedMobile(i: Home) {
634 i.state.showSubscribedMobile = !i.state.showSubscribedMobile;
638 handleShowTrendingMobile(i: Home) {
639 i.state.showTrendingMobile = !i.state.showTrendingMobile;
643 handleShowSidebarMobile(i: Home) {
644 i.state.showSidebarMobile = !i.state.showSidebarMobile;
648 handleCollapseSubscribe(i: Home) {
649 i.state.subscribedCollapsed = !i.state.subscribedCollapsed;
653 handlePageChange(page: number) {
654 this.updateUrl({ page });
655 window.scrollTo(0, 0);
658 handleSortChange(val: SortType) {
659 this.updateUrl({ sort: val, page: 1 });
660 window.scrollTo(0, 0);
663 handleListingTypeChange(val: ListingType) {
664 this.updateUrl({ listingType: val, page: 1 });
665 window.scrollTo(0, 0);
668 handleDataTypeChange(val: DataType) {
669 this.updateUrl({ dataType: DataType[val], page: 1 });
670 window.scrollTo(0, 0);
674 if (this.state.dataType == DataType.Post) {
675 let getPostsForm = new GetPosts({
677 community_name: None,
678 page: Some(this.state.page),
679 limit: Some(fetchLimit),
680 sort: Some(this.state.sort),
681 saved_only: Some(false),
682 auth: auth(false).ok(),
683 type_: Some(this.state.listingType),
686 WebSocketService.Instance.send(wsClient.getPosts(getPostsForm));
688 let getCommentsForm = new GetComments({
690 community_name: None,
691 page: Some(this.state.page),
692 limit: Some(fetchLimit),
693 sort: Some(this.state.sort),
694 saved_only: Some(false),
695 auth: auth(false).ok(),
696 type_: Some(this.state.listingType),
698 WebSocketService.Instance.send(wsClient.getComments(getCommentsForm));
702 parseMessage(msg: any) {
703 let op = wsUserOp(msg);
706 toast(i18n.t(msg.error), "danger");
708 } else if (msg.reconnect) {
709 WebSocketService.Instance.send(
710 wsClient.communityJoin({ community_id: 0 })
713 } else if (op == UserOperation.ListCommunities) {
714 let data = wsJsonToRes<ListCommunitiesResponse>(
716 ListCommunitiesResponse
718 this.state.trendingCommunities = data.communities;
719 this.setState(this.state);
720 } else if (op == UserOperation.EditSite) {
721 let data = wsJsonToRes<SiteResponse>(msg, SiteResponse);
722 this.state.siteRes.site_view = Some(data.site_view);
723 this.setState(this.state);
724 toast(i18n.t("site_saved"));
725 } else if (op == UserOperation.GetPosts) {
726 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
727 this.state.posts = data.posts;
728 this.state.loading = false;
729 this.setState(this.state);
730 WebSocketService.Instance.send(
731 wsClient.communityJoin({ community_id: 0 })
733 restoreScrollPosition(this.context);
735 } else if (op == UserOperation.CreatePost) {
736 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
738 let nsfw = data.post_view.post.nsfw || data.post_view.community.nsfw;
742 UserService.Instance.myUserInfo
743 .map(m => m.local_user_view.local_user.show_nsfw)
746 let showPostNotifs = UserService.Instance.myUserInfo
747 .map(m => m.local_user_view.local_user.show_new_post_notifs)
750 // Only push these if you're on the first page, and you pass the nsfw check
751 if (this.state.page == 1 && nsfwCheck) {
752 // If you're on subscribed, only push it if you're subscribed.
753 if (this.state.listingType == ListingType.Subscribed) {
755 UserService.Instance.myUserInfo
758 .map(c => c.community.id)
759 .includes(data.post_view.community.id)
761 this.state.posts.unshift(data.post_view);
762 if (showPostNotifs) {
763 notifyPost(data.post_view, this.context.router);
766 } else if (this.state.listingType == ListingType.Local) {
767 // If you're on the local view, only push it if its local
768 if (data.post_view.post.local) {
769 this.state.posts.unshift(data.post_view);
770 if (showPostNotifs) {
771 notifyPost(data.post_view, this.context.router);
775 this.state.posts.unshift(data.post_view);
776 if (showPostNotifs) {
777 notifyPost(data.post_view, this.context.router);
780 this.setState(this.state);
783 op == UserOperation.EditPost ||
784 op == UserOperation.DeletePost ||
785 op == UserOperation.RemovePost ||
786 op == UserOperation.LockPost ||
787 op == UserOperation.StickyPost ||
788 op == UserOperation.SavePost
790 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
791 editPostFindRes(data.post_view, this.state.posts);
792 this.setState(this.state);
793 } else if (op == UserOperation.CreatePostLike) {
794 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
795 createPostLikeFindRes(data.post_view, this.state.posts);
796 this.setState(this.state);
797 } else if (op == UserOperation.AddAdmin) {
798 let data = wsJsonToRes<AddAdminResponse>(msg, AddAdminResponse);
799 this.state.siteRes.admins = data.admins;
800 this.setState(this.state);
801 } else if (op == UserOperation.BanPerson) {
802 let data = wsJsonToRes<BanPersonResponse>(msg, BanPersonResponse);
804 .filter(p => p.creator.id == data.person_view.person.id)
805 .forEach(p => (p.creator.banned = data.banned));
807 this.setState(this.state);
808 } else if (op == UserOperation.GetComments) {
809 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
810 this.state.comments = data.comments;
811 this.state.loading = false;
812 this.setState(this.state);
814 op == UserOperation.EditComment ||
815 op == UserOperation.DeleteComment ||
816 op == UserOperation.RemoveComment
818 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
819 editCommentRes(data.comment_view, this.state.comments);
820 this.setState(this.state);
821 } else if (op == UserOperation.CreateComment) {
822 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
824 // Necessary since it might be a user reply
826 // If you're on subscribed, only push it if you're subscribed.
827 if (this.state.listingType == ListingType.Subscribed) {
829 UserService.Instance.myUserInfo
832 .map(c => c.community.id)
833 .includes(data.comment_view.community.id)
835 this.state.comments.unshift(data.comment_view);
838 this.state.comments.unshift(data.comment_view);
840 this.setState(this.state);
842 } else if (op == UserOperation.SaveComment) {
843 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
844 saveCommentRes(data.comment_view, this.state.comments);
845 this.setState(this.state);
846 } else if (op == UserOperation.CreateCommentLike) {
847 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
848 createCommentLikeRes(data.comment_view, this.state.comments);
849 this.setState(this.state);
850 } else if (op == UserOperation.BlockPerson) {
851 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
852 updatePersonBlock(data);
853 } else if (op == UserOperation.CreatePostReport) {
854 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
856 toast(i18n.t("report_created"));
858 } else if (op == UserOperation.CreateCommentReport) {
859 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
861 toast(i18n.t("report_created"));