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,
30 } from "lemmy-js-client";
31 import { Subscription } from "rxjs";
32 import { i18n } from "../../i18next";
37 } from "../../interfaces";
38 import { UserService, WebSocketService } from "../../services";
43 createPostLikeFindRes,
50 getListingTypeFromProps,
55 postToCommentSortType,
57 restoreScrollPosition,
69 import { CommentNodes } from "../comment/comment-nodes";
70 import { DataTypeSelect } from "../common/data-type-select";
71 import { HtmlTags } from "../common/html-tags";
72 import { Icon, Spinner } from "../common/icon";
73 import { ListingTypeSelect } from "../common/listing-type-select";
74 import { Paginator } from "../common/paginator";
75 import { SortSelect } from "../common/sort-select";
76 import { CommunityLink } from "../community/community-link";
77 import { PostListings } from "../post/post-listings";
78 import { SiteSidebar } from "./site-sidebar";
81 trendingCommunities: CommunityView[];
82 siteRes: GetSiteResponse;
84 comments: CommentView[];
85 listingType: ListingType;
89 showSubscribedMobile: boolean;
90 showTrendingMobile: boolean;
91 showSidebarMobile: boolean;
92 subscribedCollapsed: boolean;
97 listingType: ListingType;
103 interface UrlParams {
104 listingType?: ListingType;
110 export class Home extends Component<any, HomeState> {
111 private isoData = setIsoData(
115 ListCommunitiesResponse
117 private subscription: Subscription;
118 private emptyState: HomeState = {
119 trendingCommunities: [],
120 siteRes: this.isoData.site_res,
121 showSubscribedMobile: false,
122 showTrendingMobile: false,
123 showSidebarMobile: false,
124 subscribedCollapsed: false,
128 listingType: getListingTypeFromProps(
131 this.isoData.site_res.site_view.match({
132 some: type_ => type_.site.default_post_listing_type,
133 none: ListingType.Local,
137 dataType: getDataTypeFromProps(this.props),
138 sort: getSortTypeFromProps(this.props),
139 page: getPageFromProps(this.props),
142 constructor(props: any, context: any) {
143 super(props, context);
145 this.state = this.emptyState;
146 this.handleSortChange = this.handleSortChange.bind(this);
147 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
148 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
149 this.handlePageChange = this.handlePageChange.bind(this);
151 this.parseMessage = this.parseMessage.bind(this);
152 this.subscription = wsSubscribe(this.parseMessage);
154 // Only fetch the data if coming from another route
155 if (this.isoData.path == this.context.router.route.match.url) {
156 let postsRes = Some(this.isoData.routeData[0] as GetPostsResponse);
157 let commentsRes = Some(this.isoData.routeData[1] as GetCommentsResponse);
158 let trendingRes = this.isoData.routeData[2] as ListCommunitiesResponse;
161 some: pvs => (this.state.posts = pvs.posts),
165 some: cvs => (this.state.comments = cvs.comments),
168 this.state.trendingCommunities = trendingRes.communities;
171 WebSocketService.Instance.send(
172 wsClient.communityJoin({ community_id: 0 })
175 this.state.loading = false;
177 this.fetchTrendingCommunities();
182 fetchTrendingCommunities() {
183 let listCommunitiesForm = new ListCommunities({
184 type_: Some(ListingType.Local),
185 sort: Some(SortType.Hot),
186 limit: Some(trendingFetchLimit),
188 auth: auth(false).ok(),
190 WebSocketService.Instance.send(
191 wsClient.listCommunities(listCommunitiesForm)
195 componentDidMount() {
196 // This means it hasn't been set up yet
197 if (this.state.siteRes.site_view.isNone()) {
198 this.context.router.history.push("/setup");
203 componentWillUnmount() {
204 saveScrollPosition(this.context);
205 this.subscription.unsubscribe();
208 static getDerivedStateFromProps(props: any): HomeProps {
210 listingType: getListingTypeFromProps(props, ListingType.Local),
211 dataType: getDataTypeFromProps(props),
212 sort: getSortTypeFromProps(props),
213 page: getPageFromProps(props),
217 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
218 let pathSplit = req.path.split("/");
219 let dataType: DataType = pathSplit[3]
220 ? DataType[pathSplit[3]]
223 // TODO figure out auth default_listingType, default_sort_type
224 let type_: Option<ListingType> = Some(
226 ? ListingType[pathSplit[5]]
227 : UserService.Instance.myUserInfo.match({
229 Object.values(ListingType)[
230 mui.local_user_view.local_user.default_listing_type
232 none: ListingType.Local,
235 let sort: Option<SortType> = Some(
237 ? SortType[pathSplit[7]]
238 : UserService.Instance.myUserInfo.match({
240 Object.values(SortType)[
241 mui.local_user_view.local_user.default_sort_type
243 none: SortType.Active,
247 let page = Some(pathSplit[9] ? Number(pathSplit[9]) : 1);
249 let promises: Promise<any>[] = [];
251 if (dataType == DataType.Post) {
252 let getPostsForm = new GetPosts({
254 community_name: None,
257 limit: Some(fetchLimit),
259 saved_only: Some(false),
263 promises.push(req.client.getPosts(getPostsForm));
264 promises.push(Promise.resolve());
266 let getCommentsForm = new GetComments({
268 community_name: None,
270 limit: Some(fetchLimit),
272 sort: sort.map(postToCommentSortType),
274 saved_only: Some(false),
279 promises.push(Promise.resolve());
280 promises.push(req.client.getComments(getCommentsForm));
283 let trendingCommunitiesForm = new ListCommunities({
284 type_: Some(ListingType.Local),
285 sort: Some(SortType.Hot),
286 limit: Some(trendingFetchLimit),
290 promises.push(req.client.listCommunities(trendingCommunitiesForm));
295 componentDidUpdate(_: any, lastState: HomeState) {
297 lastState.listingType !== this.state.listingType ||
298 lastState.dataType !== this.state.dataType ||
299 lastState.sort !== this.state.sort ||
300 lastState.page !== this.state.page
302 this.setState({ loading: true });
307 get documentTitle(): string {
308 return this.state.siteRes.site_view.match({
310 siteView.site.description.match({
311 some: desc => `${siteView.site.name} - ${desc}`,
312 none: siteView.site.name,
320 <div class="container">
322 title={this.documentTitle}
323 path={this.context.router.route.match.url}
327 {this.state.siteRes.site_view.isSome() && (
329 <main role="main" class="col-12 col-md-8">
330 <div class="d-block d-md-none">{this.mobileView()}</div>
333 <aside class="d-none d-md-block col-md-4">{this.mySidebar()}</aside>
340 get hasFollows(): boolean {
341 return UserService.Instance.myUserInfo.match({
342 some: mui => mui.follows.length > 0,
348 let siteRes = this.state.siteRes;
352 {this.hasFollows && (
354 class="btn btn-secondary d-inline-block mb-2 mr-3"
355 onClick={linkEvent(this, this.handleShowSubscribedMobile)}
357 {i18n.t("subscribed")}{" "}
360 this.state.showSubscribedMobile
364 classes="icon-inline"
369 class="btn btn-secondary d-inline-block mb-2 mr-3"
370 onClick={linkEvent(this, this.handleShowTrendingMobile)}
372 {i18n.t("trending")}{" "}
375 this.state.showTrendingMobile ? `minus-square` : `plus-square`
377 classes="icon-inline"
381 class="btn btn-secondary d-inline-block mb-2 mr-3"
382 onClick={linkEvent(this, this.handleShowSidebarMobile)}
384 {i18n.t("sidebar")}{" "}
387 this.state.showSidebarMobile ? `minus-square` : `plus-square`
389 classes="icon-inline"
392 {this.state.showSidebarMobile &&
393 siteRes.site_view.match({
397 admins={Some(siteRes.admins)}
398 counts={Some(siteView.counts)}
399 online={Some(siteRes.online)}
400 showLocal={showLocal(this.isoData)}
405 {this.state.showTrendingMobile && (
406 <div class="col-12 card border-secondary mb-3">
407 <div class="card-body">{this.trendingCommunities()}</div>
410 {this.state.showSubscribedMobile && (
411 <div class="col-12 card border-secondary mb-3">
412 <div class="card-body">{this.subscribedCommunities()}</div>
421 let siteRes = this.state.siteRes;
424 {!this.state.loading && (
426 <div class="card border-secondary mb-3">
427 <div class="card-body">
428 {this.trendingCommunities()}
429 {this.createCommunityButton()}
430 {this.exploreCommunitiesButton()}
433 {siteRes.site_view.match({
437 admins={Some(siteRes.admins)}
438 counts={Some(siteView.counts)}
439 online={Some(siteRes.online)}
440 showLocal={showLocal(this.isoData)}
445 {this.hasFollows && (
446 <div class="card border-secondary mb-3">
447 <div class="card-body">{this.subscribedCommunities()}</div>
456 createCommunityButton() {
458 <Link className="mt-2 btn btn-secondary btn-block" to="/create_community">
459 {i18n.t("create_a_community")}
464 exploreCommunitiesButton() {
466 <Link className="btn btn-secondary btn-block" to="/communities">
467 {i18n.t("explore_communities")}
472 trendingCommunities() {
476 <T i18nKey="trending_communities">
478 <Link className="text-body" to="/communities">
483 <ul class="list-inline mb-0">
484 {this.state.trendingCommunities.map(cv => (
485 <li class="list-inline-item d-inline-block">
486 <CommunityLink community={cv.community} />
494 subscribedCommunities() {
498 <T class="d-inline" i18nKey="subscribed_to_communities">
500 <Link className="text-body" to="/communities">
505 class="btn btn-sm text-muted"
506 onClick={linkEvent(this, this.handleCollapseSubscribe)}
507 aria-label={i18n.t("collapse")}
508 data-tippy-content={i18n.t("collapse")}
510 {this.state.subscribedCollapsed ? (
511 <Icon icon="plus-square" classes="icon-inline" />
513 <Icon icon="minus-square" classes="icon-inline" />
517 {!this.state.subscribedCollapsed && (
518 <ul class="list-inline mb-0">
519 {UserService.Instance.myUserInfo
523 <li class="list-inline-item d-inline-block">
524 <CommunityLink community={cfv.community} />
533 updateUrl(paramUpdates: UrlParams) {
534 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
535 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
536 const sortStr = paramUpdates.sort || this.state.sort;
537 const page = paramUpdates.page || this.state.page;
538 this.props.history.push(
539 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
545 <div class="main-content-wrapper">
546 {this.state.loading ? (
555 page={this.state.page}
556 onChange={this.handlePageChange}
565 return this.state.dataType == DataType.Post ? (
567 posts={this.state.posts}
570 enableDownvotes={enableDownvotes(this.state.siteRes)}
571 enableNsfw={enableNsfw(this.state.siteRes)}
575 nodes={commentsToFlatNodes(this.state.comments)}
576 viewType={CommentViewType.Flat}
579 maxCommentsShown={None}
583 enableDownvotes={enableDownvotes(this.state.siteRes)}
589 let allRss = `/feeds/all.xml?sort=${this.state.sort}`;
590 let localRss = `/feeds/local.xml?sort=${this.state.sort}`;
591 let frontRss = auth(false)
593 .map(auth => `/feeds/front/${auth}.xml?sort=${this.state.sort}`);
596 <div className="mb-3">
599 type_={this.state.dataType}
600 onChange={this.handleDataTypeChange}
605 type_={this.state.listingType}
606 showLocal={showLocal(this.isoData)}
608 onChange={this.handleListingTypeChange}
612 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
614 {this.state.listingType == ListingType.All && (
616 <a href={allRss} rel={relTags} title="RSS">
617 <Icon icon="rss" classes="text-muted small" />
619 <link rel="alternate" type="application/atom+xml" href={allRss} />
622 {this.state.listingType == ListingType.Local && (
624 <a href={localRss} rel={relTags} title="RSS">
625 <Icon icon="rss" classes="text-muted small" />
627 <link rel="alternate" type="application/atom+xml" href={localRss} />
630 {this.state.listingType == ListingType.Subscribed &&
634 <a href={rss} title="RSS" rel={relTags}>
635 <Icon icon="rss" classes="text-muted small" />
637 <link rel="alternate" type="application/atom+xml" href={rss} />
646 handleShowSubscribedMobile(i: Home) {
647 i.state.showSubscribedMobile = !i.state.showSubscribedMobile;
651 handleShowTrendingMobile(i: Home) {
652 i.state.showTrendingMobile = !i.state.showTrendingMobile;
656 handleShowSidebarMobile(i: Home) {
657 i.state.showSidebarMobile = !i.state.showSidebarMobile;
661 handleCollapseSubscribe(i: Home) {
662 i.state.subscribedCollapsed = !i.state.subscribedCollapsed;
666 handlePageChange(page: number) {
667 this.updateUrl({ page });
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 = new GetPosts({
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),
699 WebSocketService.Instance.send(wsClient.getPosts(getPostsForm));
701 let getCommentsForm = new GetComments({
703 community_name: None,
704 page: Some(this.state.page),
705 limit: Some(fetchLimit),
707 sort: Some(postToCommentSortType(this.state.sort)),
708 saved_only: Some(false),
711 auth: auth(false).ok(),
712 type_: Some(this.state.listingType),
714 WebSocketService.Instance.send(wsClient.getComments(getCommentsForm));
718 parseMessage(msg: any) {
719 let op = wsUserOp(msg);
722 toast(i18n.t(msg.error), "danger");
724 } else if (msg.reconnect) {
725 WebSocketService.Instance.send(
726 wsClient.communityJoin({ community_id: 0 })
729 } else if (op == UserOperation.ListCommunities) {
730 let data = wsJsonToRes<ListCommunitiesResponse>(
732 ListCommunitiesResponse
734 this.state.trendingCommunities = data.communities;
735 this.setState(this.state);
736 } else if (op == UserOperation.EditSite) {
737 let data = wsJsonToRes<SiteResponse>(msg, SiteResponse);
738 this.state.siteRes.site_view = Some(data.site_view);
739 this.setState(this.state);
740 toast(i18n.t("site_saved"));
741 } else if (op == UserOperation.GetPosts) {
742 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
743 this.state.posts = data.posts;
744 this.state.loading = false;
745 this.setState(this.state);
746 WebSocketService.Instance.send(
747 wsClient.communityJoin({ community_id: 0 })
749 restoreScrollPosition(this.context);
751 } else if (op == UserOperation.CreatePost) {
752 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
754 let nsfw = data.post_view.post.nsfw || data.post_view.community.nsfw;
758 UserService.Instance.myUserInfo
759 .map(m => m.local_user_view.local_user.show_nsfw)
762 let showPostNotifs = UserService.Instance.myUserInfo
763 .map(m => m.local_user_view.local_user.show_new_post_notifs)
766 // Only push these if you're on the first page, and you pass the nsfw check
767 if (this.state.page == 1 && nsfwCheck) {
768 // If you're on subscribed, only push it if you're subscribed.
769 if (this.state.listingType == ListingType.Subscribed) {
771 UserService.Instance.myUserInfo
774 .map(c => c.community.id)
775 .includes(data.post_view.community.id)
777 this.state.posts.unshift(data.post_view);
778 if (showPostNotifs) {
779 notifyPost(data.post_view, this.context.router);
782 } else if (this.state.listingType == ListingType.Local) {
783 // If you're on the local view, only push it if its local
784 if (data.post_view.post.local) {
785 this.state.posts.unshift(data.post_view);
786 if (showPostNotifs) {
787 notifyPost(data.post_view, this.context.router);
791 this.state.posts.unshift(data.post_view);
792 if (showPostNotifs) {
793 notifyPost(data.post_view, this.context.router);
796 this.setState(this.state);
799 op == UserOperation.EditPost ||
800 op == UserOperation.DeletePost ||
801 op == UserOperation.RemovePost ||
802 op == UserOperation.LockPost ||
803 op == UserOperation.StickyPost ||
804 op == UserOperation.SavePost
806 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
807 editPostFindRes(data.post_view, this.state.posts);
808 this.setState(this.state);
809 } else if (op == UserOperation.CreatePostLike) {
810 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
811 createPostLikeFindRes(data.post_view, this.state.posts);
812 this.setState(this.state);
813 } else if (op == UserOperation.AddAdmin) {
814 let data = wsJsonToRes<AddAdminResponse>(msg, AddAdminResponse);
815 this.state.siteRes.admins = data.admins;
816 this.setState(this.state);
817 } else if (op == UserOperation.BanPerson) {
818 let data = wsJsonToRes<BanPersonResponse>(msg, BanPersonResponse);
820 .filter(p => p.creator.id == data.person_view.person.id)
821 .forEach(p => (p.creator.banned = data.banned));
823 this.setState(this.state);
824 } else if (op == UserOperation.GetComments) {
825 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
826 this.state.comments = data.comments;
827 this.state.loading = false;
828 this.setState(this.state);
830 op == UserOperation.EditComment ||
831 op == UserOperation.DeleteComment ||
832 op == UserOperation.RemoveComment
834 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
835 editCommentRes(data.comment_view, this.state.comments);
836 this.setState(this.state);
837 } else if (op == UserOperation.CreateComment) {
838 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
840 // Necessary since it might be a user reply
842 // If you're on subscribed, only push it if you're subscribed.
843 if (this.state.listingType == ListingType.Subscribed) {
845 UserService.Instance.myUserInfo
848 .map(c => c.community.id)
849 .includes(data.comment_view.community.id)
851 this.state.comments.unshift(data.comment_view);
854 this.state.comments.unshift(data.comment_view);
856 this.setState(this.state);
858 } else if (op == UserOperation.SaveComment) {
859 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
860 saveCommentRes(data.comment_view, this.state.comments);
861 this.setState(this.state);
862 } else if (op == UserOperation.CreateCommentLike) {
863 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
864 createCommentLikeRes(data.comment_view, this.state.comments);
865 this.setState(this.state);
866 } else if (op == UserOperation.BlockPerson) {
867 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
868 updatePersonBlock(data);
869 } else if (op == UserOperation.CreatePostReport) {
870 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
872 toast(i18n.t("report_created"));
874 } else if (op == UserOperation.CreateCommentReport) {
875 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
877 toast(i18n.t("report_created"));
880 op == UserOperation.PurgePerson ||
881 op == UserOperation.PurgePost ||
882 op == UserOperation.PurgeComment ||
883 op == UserOperation.PurgeCommunity
885 let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
887 toast(i18n.t("purge_success"));
888 this.context.router.history.push(`/`);