1 import { Component, linkEvent } from "inferno";
2 import { T } from "inferno-i18next-dess";
3 import { Link } from "inferno-router";
18 ListCommunitiesResponse,
26 } from "lemmy-js-client";
27 import { Subscription } from "rxjs";
28 import { i18n } from "../../i18next";
29 import { DataType, InitialFetchRequest } from "../../interfaces";
30 import { UserService, WebSocketService } from "../../services";
35 createPostLikeFindRes,
40 getListingTypeFromProps,
46 restoreScrollPosition,
60 import { CommentNodes } from "../comment/comment-nodes";
61 import { BannerIconHeader } from "../common/banner-icon-header";
62 import { DataTypeSelect } from "../common/data-type-select";
63 import { HtmlTags } from "../common/html-tags";
64 import { Icon, Spinner } from "../common/icon";
65 import { ListingTypeSelect } from "../common/listing-type-select";
66 import { Paginator } from "../common/paginator";
67 import { SortSelect } from "../common/sort-select";
68 import { CommunityLink } from "../community/community-link";
69 import { PersonListing } from "../person/person-listing";
70 import { PostListings } from "../post/post-listings";
71 import { SiteForm } from "./site-form";
74 trendingCommunities: CommunityView[];
75 siteRes: GetSiteResponse;
76 showEditSite: boolean;
77 showSubscribedMobile: boolean;
78 showTrendingMobile: boolean;
79 showSidebarMobile: boolean;
82 comments: CommentView[];
83 listingType: ListingType;
90 listingType: ListingType;
97 listingType?: ListingType;
103 export class Home extends Component<any, HomeState> {
104 private isoData = setIsoData(this.context);
105 private subscription: Subscription;
106 private emptyState: HomeState = {
107 trendingCommunities: [],
108 siteRes: this.isoData.site_res,
110 showSubscribedMobile: false,
111 showTrendingMobile: false,
112 showSidebarMobile: false,
116 listingType: getListingTypeFromProps(this.props),
117 dataType: getDataTypeFromProps(this.props),
118 sort: getSortTypeFromProps(this.props),
119 page: getPageFromProps(this.props),
122 constructor(props: any, context: any) {
123 super(props, context);
125 this.state = this.emptyState;
126 this.handleEditCancel = this.handleEditCancel.bind(this);
127 this.handleSortChange = this.handleSortChange.bind(this);
128 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
129 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
130 this.handlePageChange = this.handlePageChange.bind(this);
132 this.parseMessage = this.parseMessage.bind(this);
133 this.subscription = wsSubscribe(this.parseMessage);
135 // Only fetch the data if coming from another route
136 if (this.isoData.path == this.context.router.route.match.url) {
137 if (this.state.dataType == DataType.Post) {
138 this.state.posts = this.isoData.routeData[0].posts;
140 this.state.comments = this.isoData.routeData[0].comments;
142 this.state.trendingCommunities = this.isoData.routeData[1].communities;
143 this.state.loading = false;
145 this.fetchTrendingCommunities();
150 fetchTrendingCommunities() {
151 let listCommunitiesForm: ListCommunities = {
152 type_: ListingType.Local,
155 auth: authField(false),
157 WebSocketService.Instance.send(
158 wsClient.listCommunities(listCommunitiesForm)
162 componentDidMount() {
163 // This means it hasn't been set up yet
164 if (!this.state.siteRes.site_view) {
165 this.context.router.history.push("/setup");
168 WebSocketService.Instance.send(wsClient.communityJoin({ community_id: 0 }));
172 componentWillUnmount() {
173 saveScrollPosition(this.context);
174 this.subscription.unsubscribe();
175 window.isoData.path = undefined;
178 static getDerivedStateFromProps(props: any): HomeProps {
180 listingType: getListingTypeFromProps(props),
181 dataType: getDataTypeFromProps(props),
182 sort: getSortTypeFromProps(props),
183 page: getPageFromProps(props),
187 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
188 let pathSplit = req.path.split("/");
189 let dataType: DataType = pathSplit[3]
190 ? DataType[pathSplit[3]]
193 // TODO figure out auth default_listingType, default_sort_type
194 let type_: ListingType = pathSplit[5]
195 ? ListingType[pathSplit[5]]
196 : UserService.Instance.myUserInfo
197 ? Object.values(ListingType)[
198 UserService.Instance.myUserInfo.local_user_view.local_user
199 .default_listing_type
202 let sort: SortType = pathSplit[7]
203 ? SortType[pathSplit[7]]
204 : UserService.Instance.myUserInfo
205 ? Object.values(SortType)[
206 UserService.Instance.myUserInfo.local_user_view.local_user
211 let page = pathSplit[9] ? Number(pathSplit[9]) : 1;
213 let promises: Promise<any>[] = [];
215 if (dataType == DataType.Post) {
216 let getPostsForm: GetPosts = {
223 setOptionalAuth(getPostsForm, req.auth);
224 promises.push(req.client.getPosts(getPostsForm));
226 let getCommentsForm: GetComments = {
233 setOptionalAuth(getCommentsForm, req.auth);
234 promises.push(req.client.getComments(getCommentsForm));
237 let trendingCommunitiesForm: ListCommunities = {
238 type_: ListingType.Local,
242 promises.push(req.client.listCommunities(trendingCommunitiesForm));
247 componentDidUpdate(_: any, lastState: HomeState) {
249 lastState.listingType !== this.state.listingType ||
250 lastState.dataType !== this.state.dataType ||
251 lastState.sort !== this.state.sort ||
252 lastState.page !== this.state.page
254 this.setState({ loading: true });
259 get documentTitle(): string {
261 this.state.siteRes.site_view
262 ? this.state.siteRes.site_view.site.description
263 ? `${this.state.siteRes.site_view.site.name} - ${this.state.siteRes.site_view.site.description}`
264 : this.state.siteRes.site_view.site.name
271 <div class="container">
273 title={this.documentTitle}
274 path={this.context.router.route.match.url}
276 {this.state.siteRes.site_view?.site && (
278 <main role="main" class="col-12 col-md-8">
279 <div class="d-block d-md-none">{this.mobileView()}</div>
282 <aside class="d-none d-md-block col-md-4">{this.mySidebar()}</aside>
293 {UserService.Instance.myUserInfo &&
294 UserService.Instance.myUserInfo.follows.length > 0 && (
296 class="btn btn-secondary d-inline-block mb-2 mr-3"
297 onClick={linkEvent(this, this.handleShowSubscribedMobile)}
299 {i18n.t("subscribed")}{" "}
302 this.state.showSubscribedMobile
306 classes="icon-inline"
311 class="btn btn-secondary d-inline-block mb-2 mr-3"
312 onClick={linkEvent(this, this.handleShowTrendingMobile)}
314 {i18n.t("trending")}{" "}
317 this.state.showTrendingMobile ? `minus-square` : `plus-square`
319 classes="icon-inline"
323 class="btn btn-secondary d-inline-block mb-2 mr-3"
324 onClick={linkEvent(this, this.handleShowSidebarMobile)}
326 {i18n.t("sidebar")}{" "}
329 this.state.showSidebarMobile ? `minus-square` : `plus-square`
331 classes="icon-inline"
334 {this.state.showSubscribedMobile && (
335 <div class="col-12 card border-secondary mb-3">
336 <div class="card-body">{this.subscribedCommunities()}</div>
339 {this.state.showTrendingMobile && (
340 <div class="col-12 card border-secondary mb-3">
341 <div class="card-body">{this.trendingCommunities()}</div>
344 {this.state.showSidebarMobile && (
345 <div class="col-12 card border-secondary mb-3">
346 <div class="card-body">{this.sidebar()}</div>
357 {!this.state.loading && (
359 <div class="card border-secondary mb-3">
360 <div class="card-body">
361 {this.trendingCommunities()}
362 {this.createCommunityButton()}
363 {this.exploreCommunitiesButton()}
367 {UserService.Instance.myUserInfo &&
368 UserService.Instance.myUserInfo.follows.length > 0 && (
369 <div class="card border-secondary mb-3">
370 <div class="card-body">{this.subscribedCommunities()}</div>
374 <div class="card border-secondary mb-3">
375 <div class="card-body">{this.sidebar()}</div>
383 createCommunityButton() {
385 <Link className="mt-2 btn btn-secondary btn-block" to="/create_community">
386 {i18n.t("create_a_community")}
391 exploreCommunitiesButton() {
393 <Link className="btn btn-secondary btn-block" to="/communities">
394 {i18n.t("explore_communities")}
399 trendingCommunities() {
403 <T i18nKey="trending_communities">
405 <Link className="text-body" to="/communities">
410 <ul class="list-inline mb-0">
411 {this.state.trendingCommunities.map(cv => (
412 <li class="list-inline-item d-inline-block">
413 <CommunityLink community={cv.community} />
421 subscribedCommunities() {
425 <T i18nKey="subscribed_to_communities">
427 <Link className="text-body" to="/communities">
432 <ul class="list-inline mb-0">
433 {UserService.Instance.myUserInfo.follows.map(cfv => (
434 <li class="list-inline-item d-inline-block">
435 <CommunityLink community={cfv.community} />
444 let site = this.state.siteRes.site_view.site;
447 {!this.state.showEditSite ? (
451 {this.adminButtons()}
453 <BannerIconHeader banner={site.banner} />
457 <SiteForm site={site} onCancel={this.handleEditCancel} />
463 updateUrl(paramUpdates: UrlParams) {
464 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
465 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
466 const sortStr = paramUpdates.sort || this.state.sort;
467 const page = paramUpdates.page || this.state.page;
468 this.props.history.push(
469 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
474 let site = this.state.siteRes.site_view.site;
477 {site.description && <h6>{site.description}</h6>}
478 {site.sidebar && this.siteSidebar()}
486 let site = this.state.siteRes.site_view.site;
487 return site.name && <h5 class="mb-0">{site.name}</h5>;
492 <ul class="mt-1 list-inline small mb-0">
493 <li class="list-inline-item">{i18n.t("admins")}:</li>
494 {this.state.siteRes.admins.map(av => (
495 <li class="list-inline-item">
496 <PersonListing person={av.person} />
504 let counts = this.state.siteRes.site_view.counts;
506 <ul class="my-2 list-inline">
507 <li className="list-inline-item badge badge-secondary">
508 {i18n.t("number_online", {
509 count: this.state.siteRes.online,
510 formattedCount: numToSI(this.state.siteRes.online),
514 className="list-inline-item badge badge-secondary pointer"
515 data-tippy-content={i18n.t("active_users_in_the_last_day", {
516 count: counts.users_active_day,
517 formattedCount: numToSI(counts.users_active_day),
520 {i18n.t("number_of_users", {
521 count: counts.users_active_day,
522 formattedCount: numToSI(counts.users_active_day),
527 className="list-inline-item badge badge-secondary pointer"
528 data-tippy-content={i18n.t("active_users_in_the_last_week", {
529 count: counts.users_active_week,
530 formattedCount: counts.users_active_week,
533 {i18n.t("number_of_users", {
534 count: counts.users_active_week,
535 formattedCount: numToSI(counts.users_active_week),
540 className="list-inline-item badge badge-secondary pointer"
541 data-tippy-content={i18n.t("active_users_in_the_last_month", {
542 count: counts.users_active_month,
543 formattedCount: counts.users_active_month,
546 {i18n.t("number_of_users", {
547 count: counts.users_active_month,
548 formattedCount: numToSI(counts.users_active_month),
553 className="list-inline-item badge badge-secondary pointer"
554 data-tippy-content={i18n.t("active_users_in_the_last_six_months", {
555 count: counts.users_active_half_year,
556 formattedCount: counts.users_active_half_year,
559 {i18n.t("number_of_users", {
560 count: counts.users_active_half_year,
561 formattedCount: numToSI(counts.users_active_half_year),
563 / {i18n.t("number_of_months", { count: 6, formattedCount: 6 })}
565 <li className="list-inline-item badge badge-secondary">
566 {i18n.t("number_of_users", {
568 formattedCount: numToSI(counts.users),
571 <li className="list-inline-item badge badge-secondary">
572 {i18n.t("number_of_communities", {
573 count: counts.communities,
574 formattedCount: numToSI(counts.communities),
577 <li className="list-inline-item badge badge-secondary">
578 {i18n.t("number_of_posts", {
580 formattedCount: numToSI(counts.posts),
583 <li className="list-inline-item badge badge-secondary">
584 {i18n.t("number_of_comments", {
585 count: counts.comments,
586 formattedCount: numToSI(counts.comments),
589 <li className="list-inline-item">
590 <Link className="badge badge-secondary" to="/modlog">
601 <ul class="list-inline mb-1 text-muted font-weight-bold">
602 <li className="list-inline-item-action">
604 class="btn btn-link d-inline-block text-muted"
605 onClick={linkEvent(this, this.handleEditClick)}
606 aria-label={i18n.t("edit")}
607 data-tippy-content={i18n.t("edit")}
609 <Icon icon="edit" classes="icon-inline" />
621 dangerouslySetInnerHTML={mdToHtml(
622 this.state.siteRes.site_view.site.sidebar
630 <div class="main-content-wrapper">
631 {this.state.loading ? (
640 page={this.state.page}
641 onChange={this.handlePageChange}
650 let site = this.state.siteRes.site_view.site;
651 return this.state.dataType == DataType.Post ? (
653 posts={this.state.posts}
656 enableDownvotes={site.enable_downvotes}
657 enableNsfw={site.enable_nsfw}
661 nodes={commentsToFlatNodes(this.state.comments)}
665 enableDownvotes={site.enable_downvotes}
672 <div className="mb-3">
675 type_={this.state.dataType}
676 onChange={this.handleDataTypeChange}
681 type_={this.state.listingType}
682 showLocal={showLocal(this.isoData)}
683 onChange={this.handleListingTypeChange}
687 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
689 {this.state.listingType == ListingType.All && (
691 href={`/feeds/all.xml?sort=${this.state.sort}`}
695 <Icon icon="rss" classes="text-muted small" />
698 {this.state.listingType == ListingType.Local && (
700 href={`/feeds/local.xml?sort=${this.state.sort}`}
704 <Icon icon="rss" classes="text-muted small" />
707 {UserService.Instance.myUserInfo &&
708 this.state.listingType == ListingType.Subscribed && (
710 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${this.state.sort}`}
714 <Icon icon="rss" classes="text-muted small" />
721 get canAdmin(): boolean {
723 UserService.Instance.myUserInfo &&
724 this.state.siteRes.admins
725 .map(a => a.person.id)
726 .includes(UserService.Instance.myUserInfo.local_user_view.person.id)
730 handleEditClick(i: Home) {
731 i.state.showEditSite = true;
736 this.state.showEditSite = false;
737 this.setState(this.state);
740 handleShowSubscribedMobile(i: Home) {
741 i.state.showSubscribedMobile = !i.state.showSubscribedMobile;
745 handleShowTrendingMobile(i: Home) {
746 i.state.showTrendingMobile = !i.state.showTrendingMobile;
750 handleShowSidebarMobile(i: Home) {
751 i.state.showSidebarMobile = !i.state.showSidebarMobile;
755 handlePageChange(page: number) {
756 this.updateUrl({ page });
757 window.scrollTo(0, 0);
760 handleSortChange(val: SortType) {
761 this.updateUrl({ sort: val, page: 1 });
762 window.scrollTo(0, 0);
765 handleListingTypeChange(val: ListingType) {
766 this.updateUrl({ listingType: val, page: 1 });
767 window.scrollTo(0, 0);
770 handleDataTypeChange(val: DataType) {
771 this.updateUrl({ dataType: DataType[val], page: 1 });
772 window.scrollTo(0, 0);
776 if (this.state.dataType == DataType.Post) {
777 let getPostsForm: GetPosts = {
778 page: this.state.page,
780 sort: this.state.sort,
781 type_: this.state.listingType,
783 auth: authField(false),
785 WebSocketService.Instance.send(wsClient.getPosts(getPostsForm));
787 let getCommentsForm: GetComments = {
788 page: this.state.page,
790 sort: this.state.sort,
791 type_: this.state.listingType,
793 auth: authField(false),
795 WebSocketService.Instance.send(wsClient.getComments(getCommentsForm));
799 parseMessage(msg: any) {
800 let op = wsUserOp(msg);
803 toast(i18n.t(msg.error), "danger");
805 } else if (msg.reconnect) {
806 WebSocketService.Instance.send(
807 wsClient.communityJoin({ community_id: 0 })
810 } else if (op == UserOperation.ListCommunities) {
811 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
812 this.state.trendingCommunities = data.communities;
813 this.setState(this.state);
814 } else if (op == UserOperation.EditSite) {
815 let data = wsJsonToRes<SiteResponse>(msg).data;
816 this.state.siteRes.site_view = data.site_view;
817 this.state.showEditSite = false;
818 this.setState(this.state);
819 toast(i18n.t("site_saved"));
820 } else if (op == UserOperation.GetPosts) {
821 let data = wsJsonToRes<GetPostsResponse>(msg).data;
822 this.state.posts = data.posts;
823 this.state.loading = false;
824 this.setState(this.state);
825 restoreScrollPosition(this.context);
827 } else if (op == UserOperation.CreatePost) {
828 let data = wsJsonToRes<PostResponse>(msg).data;
831 let nsfw = data.post_view.post.nsfw || data.post_view.community.nsfw;
835 UserService.Instance.myUserInfo &&
836 UserService.Instance.myUserInfo.local_user_view.local_user.show_nsfw);
838 // Only push these if you're on the first page, and you pass the nsfw check
839 if (this.state.page == 1 && nsfwCheck) {
840 // If you're on subscribed, only push it if you're subscribed.
841 if (this.state.listingType == ListingType.Subscribed) {
843 UserService.Instance.myUserInfo.follows
844 .map(c => c.community.id)
845 .includes(data.post_view.community.id)
847 this.state.posts.unshift(data.post_view);
849 UserService.Instance.myUserInfo?.local_user_view.local_user
850 .show_new_post_notifs
852 notifyPost(data.post_view, this.context.router);
855 } else if (this.state.listingType == ListingType.Local) {
856 // If you're on the local view, only push it if its local
857 if (data.post_view.post.local) {
858 this.state.posts.unshift(data.post_view);
860 UserService.Instance.myUserInfo?.local_user_view.local_user
861 .show_new_post_notifs
863 notifyPost(data.post_view, this.context.router);
867 this.state.posts.unshift(data.post_view);
869 UserService.Instance.myUserInfo?.local_user_view.local_user
870 .show_new_post_notifs
872 notifyPost(data.post_view, this.context.router);
875 this.setState(this.state);
878 op == UserOperation.EditPost ||
879 op == UserOperation.DeletePost ||
880 op == UserOperation.RemovePost ||
881 op == UserOperation.LockPost ||
882 op == UserOperation.StickyPost ||
883 op == UserOperation.SavePost
885 let data = wsJsonToRes<PostResponse>(msg).data;
886 editPostFindRes(data.post_view, this.state.posts);
887 this.setState(this.state);
888 } else if (op == UserOperation.CreatePostLike) {
889 let data = wsJsonToRes<PostResponse>(msg).data;
890 createPostLikeFindRes(data.post_view, this.state.posts);
891 this.setState(this.state);
892 } else if (op == UserOperation.AddAdmin) {
893 let data = wsJsonToRes<AddAdminResponse>(msg).data;
894 this.state.siteRes.admins = data.admins;
895 this.setState(this.state);
896 } else if (op == UserOperation.BanPerson) {
897 let data = wsJsonToRes<BanPersonResponse>(msg).data;
898 let found = this.state.siteRes.banned.find(
899 p => (p.person.id = data.person_view.person.id)
902 // Remove the banned if its found in the list, and the action is an unban
903 if (found && !data.banned) {
904 this.state.siteRes.banned = this.state.siteRes.banned.filter(
905 i => i.person.id !== data.person_view.person.id
908 this.state.siteRes.banned.push(data.person_view);
912 .filter(p => p.creator.id == data.person_view.person.id)
913 .forEach(p => (p.creator.banned = data.banned));
915 this.setState(this.state);
916 } else if (op == UserOperation.GetComments) {
917 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
918 this.state.comments = data.comments;
919 this.state.loading = false;
920 this.setState(this.state);
922 op == UserOperation.EditComment ||
923 op == UserOperation.DeleteComment ||
924 op == UserOperation.RemoveComment
926 let data = wsJsonToRes<CommentResponse>(msg).data;
927 editCommentRes(data.comment_view, this.state.comments);
928 this.setState(this.state);
929 } else if (op == UserOperation.CreateComment) {
930 let data = wsJsonToRes<CommentResponse>(msg).data;
932 // Necessary since it might be a user reply
934 // If you're on subscribed, only push it if you're subscribed.
935 if (this.state.listingType == ListingType.Subscribed) {
937 UserService.Instance.myUserInfo.follows
938 .map(c => c.community.id)
939 .includes(data.comment_view.community.id)
941 this.state.comments.unshift(data.comment_view);
944 this.state.comments.unshift(data.comment_view);
946 this.setState(this.state);
948 } else if (op == UserOperation.SaveComment) {
949 let data = wsJsonToRes<CommentResponse>(msg).data;
950 saveCommentRes(data.comment_view, this.state.comments);
951 this.setState(this.state);
952 } else if (op == UserOperation.CreateCommentLike) {
953 let data = wsJsonToRes<CommentResponse>(msg).data;
954 createCommentLikeRes(data.comment_view, this.state.comments);
955 this.setState(this.state);
956 } else if (op == UserOperation.BlockPerson) {
957 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
958 updatePersonBlock(data);
959 } else if (op == UserOperation.CreatePostReport) {
960 let data = wsJsonToRes<PostReportResponse>(msg).data;
962 toast(i18n.t("report_created"));
964 } else if (op == UserOperation.CreateCommentReport) {
965 let data = wsJsonToRes<CommentReportResponse>(msg).data;
967 toast(i18n.t("report_created"));