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";
44 createPostLikeFindRes,
51 getListingTypeFromProps,
58 postToCommentSortType,
60 restoreScrollPosition,
72 import { CommentNodes } from "../comment/comment-nodes";
73 import { DataTypeSelect } from "../common/data-type-select";
74 import { HtmlTags } from "../common/html-tags";
75 import { Icon, Spinner } from "../common/icon";
76 import { ListingTypeSelect } from "../common/listing-type-select";
77 import { Paginator } from "../common/paginator";
78 import { SortSelect } from "../common/sort-select";
79 import { CommunityLink } from "../community/community-link";
80 import { PostListings } from "../post/post-listings";
81 import { SiteSidebar } from "./site-sidebar";
84 trendingCommunities: CommunityView[];
85 siteRes: GetSiteResponse;
87 comments: CommentView[];
88 listingType: ListingType;
92 showSubscribedMobile: boolean;
93 showTrendingMobile: boolean;
94 showSidebarMobile: boolean;
95 subscribedCollapsed: boolean;
100 listingType: ListingType;
106 interface UrlParams {
107 listingType?: ListingType;
113 export class Home extends Component<any, HomeState> {
114 private isoData = setIsoData(
118 ListCommunitiesResponse
120 private subscription: Subscription;
121 private emptyState: HomeState = {
122 trendingCommunities: [],
123 siteRes: this.isoData.site_res,
124 showSubscribedMobile: false,
125 showTrendingMobile: false,
126 showSidebarMobile: false,
127 subscribedCollapsed: false,
131 listingType: getListingTypeFromProps(
134 this.isoData.site_res.site_view.match({
135 some: type_ => type_.site.default_post_listing_type,
136 none: ListingType.Local,
140 dataType: getDataTypeFromProps(this.props),
141 sort: getSortTypeFromProps(this.props),
142 page: getPageFromProps(this.props),
145 constructor(props: any, context: any) {
146 super(props, context);
148 this.state = this.emptyState;
149 this.handleSortChange = this.handleSortChange.bind(this);
150 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
151 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
152 this.handlePageChange = this.handlePageChange.bind(this);
154 this.parseMessage = this.parseMessage.bind(this);
155 this.subscription = wsSubscribe(this.parseMessage);
157 // Only fetch the data if coming from another route
158 if (this.isoData.path == this.context.router.route.match.url) {
159 let postsRes = Some(this.isoData.routeData[0] as GetPostsResponse);
160 let commentsRes = Some(this.isoData.routeData[1] as GetCommentsResponse);
161 let trendingRes = this.isoData.routeData[2] as ListCommunitiesResponse;
163 if (postsRes.isSome()) {
164 this.state = { ...this.state, posts: postsRes.unwrap().posts };
167 if (commentsRes.isSome()) {
168 this.state = { ...this.state, comments: commentsRes.unwrap().comments };
172 WebSocketService.Instance.send(
173 wsClient.communityJoin({ community_id: 0 })
178 trendingCommunities: trendingRes.communities,
182 this.fetchTrendingCommunities();
187 fetchTrendingCommunities() {
188 let listCommunitiesForm = new ListCommunities({
189 type_: Some(ListingType.Local),
190 sort: Some(SortType.Hot),
191 limit: Some(trendingFetchLimit),
193 auth: auth(false).ok(),
195 WebSocketService.Instance.send(
196 wsClient.listCommunities(listCommunitiesForm)
200 componentDidMount() {
201 // This means it hasn't been set up yet
202 if (this.state.siteRes.site_view.isNone()) {
203 this.context.router.history.push("/setup");
208 componentWillUnmount() {
209 saveScrollPosition(this.context);
210 this.subscription.unsubscribe();
213 static getDerivedStateFromProps(props: any): HomeProps {
215 listingType: getListingTypeFromProps(props, ListingType.Local),
216 dataType: getDataTypeFromProps(props),
217 sort: getSortTypeFromProps(props),
218 page: getPageFromProps(props),
222 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
223 let pathSplit = req.path.split("/");
224 let dataType: DataType = pathSplit[3]
225 ? DataType[pathSplit[3]]
228 // TODO figure out auth default_listingType, default_sort_type
229 let type_: Option<ListingType> = Some(
231 ? ListingType[pathSplit[5]]
232 : UserService.Instance.myUserInfo.match({
234 Object.values(ListingType)[
235 mui.local_user_view.local_user.default_listing_type
237 none: ListingType.Local,
240 let sort: Option<SortType> = Some(
242 ? SortType[pathSplit[7]]
243 : UserService.Instance.myUserInfo.match({
245 Object.values(SortType)[
246 mui.local_user_view.local_user.default_sort_type
248 none: SortType.Active,
252 let page = Some(pathSplit[9] ? Number(pathSplit[9]) : 1);
254 let promises: Promise<any>[] = [];
256 if (dataType == DataType.Post) {
257 let getPostsForm = new GetPosts({
259 community_name: None,
262 limit: Some(fetchLimit),
264 saved_only: Some(false),
268 promises.push(req.client.getPosts(getPostsForm));
269 promises.push(Promise.resolve());
271 let getCommentsForm = new GetComments({
273 community_name: None,
275 limit: Some(fetchLimit),
277 sort: sort.map(postToCommentSortType),
279 saved_only: Some(false),
284 promises.push(Promise.resolve());
285 promises.push(req.client.getComments(getCommentsForm));
288 let trendingCommunitiesForm = new ListCommunities({
289 type_: Some(ListingType.Local),
290 sort: Some(SortType.Hot),
291 limit: Some(trendingFetchLimit),
295 promises.push(req.client.listCommunities(trendingCommunitiesForm));
300 componentDidUpdate(_: any, lastState: HomeState) {
302 lastState.listingType !== this.state.listingType ||
303 lastState.dataType !== this.state.dataType ||
304 lastState.sort !== this.state.sort ||
305 lastState.page !== this.state.page
307 this.setState({ loading: true });
312 get documentTitle(): string {
313 return this.state.siteRes.site_view.match({
315 siteView.site.description.match({
316 some: desc => `${siteView.site.name} - ${desc}`,
317 none: siteView.site.name,
325 <div className="container">
327 title={this.documentTitle}
328 path={this.context.router.route.match.url}
332 {this.state.siteRes.site_view.isSome() && (
333 <div className="row">
334 <main role="main" className="col-12 col-md-8">
335 <div className="d-block d-md-none">{this.mobileView()}</div>
338 <aside className="d-none d-md-block col-md-4">
347 get hasFollows(): boolean {
348 return UserService.Instance.myUserInfo.match({
349 some: mui => mui.follows.length > 0,
355 let siteRes = this.state.siteRes;
357 <div className="row">
358 <div className="col-12">
359 {this.hasFollows && (
361 className="btn btn-secondary d-inline-block mb-2 mr-3"
362 onClick={linkEvent(this, this.handleShowSubscribedMobile)}
364 {i18n.t("subscribed")}{" "}
367 this.state.showSubscribedMobile
371 classes="icon-inline"
376 className="btn btn-secondary d-inline-block mb-2 mr-3"
377 onClick={linkEvent(this, this.handleShowTrendingMobile)}
379 {i18n.t("trending")}{" "}
382 this.state.showTrendingMobile ? `minus-square` : `plus-square`
384 classes="icon-inline"
388 className="btn btn-secondary d-inline-block mb-2 mr-3"
389 onClick={linkEvent(this, this.handleShowSidebarMobile)}
391 {i18n.t("sidebar")}{" "}
394 this.state.showSidebarMobile ? `minus-square` : `plus-square`
396 classes="icon-inline"
399 {this.state.showSidebarMobile &&
400 siteRes.site_view.match({
404 admins={Some(siteRes.admins)}
405 counts={Some(siteView.counts)}
406 online={Some(siteRes.online)}
407 showLocal={showLocal(this.isoData)}
412 {this.state.showTrendingMobile && (
413 <div className="col-12 card border-secondary mb-3">
414 <div className="card-body">{this.trendingCommunities()}</div>
417 {this.state.showSubscribedMobile && (
418 <div className="col-12 card border-secondary mb-3">
419 <div className="card-body">{this.subscribedCommunities()}</div>
428 let siteRes = this.state.siteRes;
431 {!this.state.loading && (
433 <div className="card border-secondary mb-3">
434 <div className="card-body">
435 {this.trendingCommunities()}
436 {canCreateCommunity(this.state.siteRes) &&
437 this.createCommunityButton()}
438 {this.exploreCommunitiesButton()}
441 {siteRes.site_view.match({
445 admins={Some(siteRes.admins)}
446 counts={Some(siteView.counts)}
447 online={Some(siteRes.online)}
448 showLocal={showLocal(this.isoData)}
453 {this.hasFollows && (
454 <div className="card border-secondary mb-3">
455 <div className="card-body">{this.subscribedCommunities()}</div>
464 createCommunityButton() {
466 <Link className="mt-2 btn btn-secondary btn-block" to="/create_community">
467 {i18n.t("create_a_community")}
472 exploreCommunitiesButton() {
474 <Link className="btn btn-secondary btn-block" to="/communities">
475 {i18n.t("explore_communities")}
480 trendingCommunities() {
484 <T i18nKey="trending_communities">
486 <Link className="text-body" to="/communities">
491 <ul className="list-inline mb-0">
492 {this.state.trendingCommunities.map(cv => (
494 key={cv.community.id}
495 className="list-inline-item d-inline-block"
497 <CommunityLink community={cv.community} />
505 subscribedCommunities() {
509 <T class="d-inline" i18nKey="subscribed_to_communities">
511 <Link className="text-body" to="/communities">
516 className="btn btn-sm text-muted"
517 onClick={linkEvent(this, this.handleCollapseSubscribe)}
518 aria-label={i18n.t("collapse")}
519 data-tippy-content={i18n.t("collapse")}
521 {this.state.subscribedCollapsed ? (
522 <Icon icon="plus-square" classes="icon-inline" />
524 <Icon icon="minus-square" classes="icon-inline" />
528 {!this.state.subscribedCollapsed && (
529 <ul className="list-inline mb-0">
530 {UserService.Instance.myUserInfo
535 key={cfv.community.id}
536 className="list-inline-item d-inline-block"
538 <CommunityLink community={cfv.community} />
547 updateUrl(paramUpdates: UrlParams) {
548 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
549 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
550 const sortStr = paramUpdates.sort || this.state.sort;
551 const page = paramUpdates.page || this.state.page;
552 this.props.history.push(
553 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
559 <div className="main-content-wrapper">
560 {this.state.loading ? (
569 page={this.state.page}
570 onChange={this.handlePageChange}
579 return this.state.dataType == DataType.Post ? (
581 posts={this.state.posts}
584 enableDownvotes={enableDownvotes(this.state.siteRes)}
585 enableNsfw={enableNsfw(this.state.siteRes)}
586 allLanguages={this.state.siteRes.all_languages}
590 nodes={commentsToFlatNodes(this.state.comments)}
591 viewType={CommentViewType.Flat}
594 maxCommentsShown={None}
598 enableDownvotes={enableDownvotes(this.state.siteRes)}
599 allLanguages={this.state.siteRes.all_languages}
605 let allRss = `/feeds/all.xml?sort=${this.state.sort}`;
606 let localRss = `/feeds/local.xml?sort=${this.state.sort}`;
607 let frontRss = auth(false)
609 .map(auth => `/feeds/front/${auth}.xml?sort=${this.state.sort}`);
612 <div className="mb-3">
613 <span className="mr-3">
615 type_={this.state.dataType}
616 onChange={this.handleDataTypeChange}
619 <span className="mr-3">
621 type_={this.state.listingType}
622 showLocal={showLocal(this.isoData)}
624 onChange={this.handleListingTypeChange}
627 <span className="mr-2">
628 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
630 {this.state.listingType == ListingType.All && (
632 <a href={allRss} rel={relTags} title="RSS">
633 <Icon icon="rss" classes="text-muted small" />
635 <link rel="alternate" type="application/atom+xml" href={allRss} />
638 {this.state.listingType == ListingType.Local && (
640 <a href={localRss} rel={relTags} title="RSS">
641 <Icon icon="rss" classes="text-muted small" />
643 <link rel="alternate" type="application/atom+xml" href={localRss} />
646 {this.state.listingType == ListingType.Subscribed &&
650 <a href={rss} title="RSS" rel={relTags}>
651 <Icon icon="rss" classes="text-muted small" />
653 <link rel="alternate" type="application/atom+xml" href={rss} />
662 handleShowSubscribedMobile(i: Home) {
663 i.setState({ showSubscribedMobile: !i.state.showSubscribedMobile });
666 handleShowTrendingMobile(i: Home) {
667 i.setState({ showTrendingMobile: !i.state.showTrendingMobile });
670 handleShowSidebarMobile(i: Home) {
671 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
674 handleCollapseSubscribe(i: Home) {
675 i.setState({ subscribedCollapsed: !i.state.subscribedCollapsed });
678 handlePageChange(page: number) {
679 this.updateUrl({ page });
680 window.scrollTo(0, 0);
683 handleSortChange(val: SortType) {
684 this.updateUrl({ sort: val, page: 1 });
685 window.scrollTo(0, 0);
688 handleListingTypeChange(val: ListingType) {
689 this.updateUrl({ listingType: val, page: 1 });
690 window.scrollTo(0, 0);
693 handleDataTypeChange(val: DataType) {
694 this.updateUrl({ dataType: DataType[val], page: 1 });
695 window.scrollTo(0, 0);
699 if (this.state.dataType == DataType.Post) {
700 let getPostsForm = new GetPosts({
702 community_name: None,
703 page: Some(this.state.page),
704 limit: Some(fetchLimit),
705 sort: Some(this.state.sort),
706 saved_only: Some(false),
707 auth: auth(false).ok(),
708 type_: Some(this.state.listingType),
711 WebSocketService.Instance.send(wsClient.getPosts(getPostsForm));
713 let getCommentsForm = new GetComments({
715 community_name: None,
716 page: Some(this.state.page),
717 limit: Some(fetchLimit),
719 sort: Some(postToCommentSortType(this.state.sort)),
720 saved_only: Some(false),
723 auth: auth(false).ok(),
724 type_: Some(this.state.listingType),
726 WebSocketService.Instance.send(wsClient.getComments(getCommentsForm));
730 parseMessage(msg: any) {
731 let op = wsUserOp(msg);
734 toast(i18n.t(msg.error), "danger");
736 } else if (msg.reconnect) {
737 WebSocketService.Instance.send(
738 wsClient.communityJoin({ community_id: 0 })
741 } else if (op == UserOperation.ListCommunities) {
742 let data = wsJsonToRes<ListCommunitiesResponse>(
744 ListCommunitiesResponse
746 this.setState({ trendingCommunities: data.communities });
747 } else if (op == UserOperation.EditSite) {
748 let data = wsJsonToRes<SiteResponse>(msg, SiteResponse);
749 this.setState(s => ((s.siteRes.site_view = Some(data.site_view)), s));
750 toast(i18n.t("site_saved"));
751 } else if (op == UserOperation.GetPosts) {
752 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
753 this.setState({ posts: data.posts, loading: false });
754 WebSocketService.Instance.send(
755 wsClient.communityJoin({ community_id: 0 })
757 restoreScrollPosition(this.context);
759 } else if (op == UserOperation.CreatePost) {
760 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
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, you pass the nsfw check, and it isn't blocked
768 this.state.page == 1 &&
769 nsfwCheck(data.post_view) &&
770 !isPostBlocked(data.post_view)
772 // If you're on subscribed, only push it if you're subscribed.
773 if (this.state.listingType == ListingType.Subscribed) {
775 UserService.Instance.myUserInfo
778 .map(c => c.community.id)
779 .includes(data.post_view.community.id)
781 this.state.posts.unshift(data.post_view);
782 if (showPostNotifs) {
783 notifyPost(data.post_view, this.context.router);
786 } else if (this.state.listingType == ListingType.Local) {
787 // If you're on the local view, only push it if its local
788 if (data.post_view.post.local) {
789 this.state.posts.unshift(data.post_view);
790 if (showPostNotifs) {
791 notifyPost(data.post_view, this.context.router);
795 this.state.posts.unshift(data.post_view);
796 if (showPostNotifs) {
797 notifyPost(data.post_view, this.context.router);
800 this.setState(this.state);
803 op == UserOperation.EditPost ||
804 op == UserOperation.DeletePost ||
805 op == UserOperation.RemovePost ||
806 op == UserOperation.LockPost ||
807 op == UserOperation.StickyPost ||
808 op == UserOperation.SavePost
810 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
811 editPostFindRes(data.post_view, this.state.posts);
812 this.setState(this.state);
813 } else if (op == UserOperation.CreatePostLike) {
814 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
815 createPostLikeFindRes(data.post_view, this.state.posts);
816 this.setState(this.state);
817 } else if (op == UserOperation.AddAdmin) {
818 let data = wsJsonToRes<AddAdminResponse>(msg, AddAdminResponse);
819 this.setState(s => ((s.siteRes.admins = data.admins), s));
820 } else if (op == UserOperation.BanPerson) {
821 let data = wsJsonToRes<BanPersonResponse>(msg, BanPersonResponse);
823 .filter(p => p.creator.id == data.person_view.person.id)
824 .forEach(p => (p.creator.banned = data.banned));
826 this.setState(this.state);
827 } else if (op == UserOperation.GetComments) {
828 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
829 this.setState({ comments: data.comments, loading: false });
831 op == UserOperation.EditComment ||
832 op == UserOperation.DeleteComment ||
833 op == UserOperation.RemoveComment
835 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
836 editCommentRes(data.comment_view, this.state.comments);
837 this.setState(this.state);
838 } else if (op == UserOperation.CreateComment) {
839 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
841 // Necessary since it might be a user reply
843 // If you're on subscribed, only push it if you're subscribed.
844 if (this.state.listingType == ListingType.Subscribed) {
846 UserService.Instance.myUserInfo
849 .map(c => c.community.id)
850 .includes(data.comment_view.community.id)
852 this.state.comments.unshift(data.comment_view);
855 this.state.comments.unshift(data.comment_view);
857 this.setState(this.state);
859 } else if (op == UserOperation.SaveComment) {
860 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
861 saveCommentRes(data.comment_view, this.state.comments);
862 this.setState(this.state);
863 } else if (op == UserOperation.CreateCommentLike) {
864 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
865 createCommentLikeRes(data.comment_view, this.state.comments);
866 this.setState(this.state);
867 } else if (op == UserOperation.BlockPerson) {
868 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
869 updatePersonBlock(data);
870 } else if (op == UserOperation.CreatePostReport) {
871 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
873 toast(i18n.t("report_created"));
875 } else if (op == UserOperation.CreateCommentReport) {
876 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
878 toast(i18n.t("report_created"));
881 op == UserOperation.PurgePerson ||
882 op == UserOperation.PurgePost ||
883 op == UserOperation.PurgeComment ||
884 op == UserOperation.PurgeCommunity
886 let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
888 toast(i18n.t("purge_success"));
889 this.context.router.history.push(`/`);