X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fhome%2Fhome.tsx;h=741dfa57aa7576eb0138bf1d687ab2773ae4a70d;hb=26ff0f7e06bf32024161a60a5503e1a041ab778c;hp=483d8ccb872c7196519e77f46b3dbadd25b8f5aa;hpb=0d35d9a8d28b8f63a1db6e980c9f07ddd8d73db7;p=lemmy-ui.git diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 483d8cc..741dfa5 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -1,5 +1,29 @@ +import { + commentsToFlatNodes, + editComment, + editPost, + editWith, + enableDownvotes, + enableNsfw, + getCommentParentId, + getDataTypeString, + myAuth, + postToCommentSortType, + setIsoData, + showLocal, + updatePersonBlock, +} from "@utils/app"; +import { + getPageFromString, + getQueryParams, + getQueryString, + getRandomFromList, +} from "@utils/helpers"; +import { canCreateCommunity } from "@utils/roles"; +import type { QueryParams } from "@utils/types"; +import { RouteDataResponse } from "@utils/types"; import { NoOptionI18nKeys } from "i18next"; -import { Component, linkEvent, MouseEventHandler } from "inferno"; +import { Component, MouseEventHandler, linkEvent } from "inferno"; import { T } from "inferno-i18next-dess"; import { Link } from "inferno-router"; import { @@ -47,45 +71,22 @@ import { SortType, TransferCommunity, } from "lemmy-js-client"; -import { i18n } from "../../i18next"; +import { fetchLimit, relTags, trendingFetchLimit } from "../../config"; import { CommentViewType, DataType, InitialFetchRequest, } from "../../interfaces"; -import { UserService } from "../../services"; -import { FirstLoadService } from "../../services/FirstLoadService"; -import { HttpService, RequestState } from "../../services/HttpService"; +import { mdToHtml } from "../../markdown"; import { - canCreateCommunity, - commentsToFlatNodes, - editComment, - editPost, - editWith, - enableDownvotes, - enableNsfw, - fetchLimit, - getCommentParentId, - getDataTypeString, - getPageFromString, - getQueryParams, - getQueryString, - getRandomFromList, - mdToHtml, - myAuth, - postToCommentSortType, - QueryParams, - relTags, - restoreScrollPosition, - RouteDataResponse, - saveScrollPosition, - setIsoData, - setupTippy, - showLocal, - toast, - trendingFetchLimit, - updatePersonBlock, -} from "../../utils"; + FirstLoadService, + HomeCacheService, + I18NextService, + UserService, +} from "../../services"; +import { HttpService, RequestState } from "../../services/HttpService"; +import { setupTippy } from "../../tippy"; +import { toast } from "../../toast"; import { CommentNodes } from "../comment/comment-nodes"; import { DataTypeSelect } from "../common/data-type-select"; import { HtmlTags } from "../common/html-tags"; @@ -105,6 +106,7 @@ interface HomeState { showTrendingMobile: boolean; showSidebarMobile: boolean; subscribedCollapsed: boolean; + scrolled: boolean; tagline?: string; siteRes: GetSiteResponse; finished: Map; @@ -112,7 +114,7 @@ interface HomeState { } interface HomeProps { - listingType: ListingType; + listingType?: ListingType; dataType: DataType; sort: SortType; page: number; @@ -161,12 +163,12 @@ function getDataTypeFromQuery(type?: string): DataType { return type ? DataType[type] : DataType.Post; } -function getListingTypeFromQuery(type?: string): ListingType { +function getListingTypeFromQuery(type?: string): ListingType | undefined { const myListingType = UserService.Instance.myUserInfo?.local_user_view?.local_user ?.default_listing_type; - return (type ? (type as ListingType) : myListingType) ?? "Local"; + return type ? (type as ListingType) : myListingType; } function getSortTypeFromQuery(type?: string): SortType { @@ -195,10 +197,10 @@ const MobileButton = ({ onClick: MouseEventHandler; }) => ( ); @@ -210,8 +212,8 @@ const LinkButton = ({ path: string; translationKey: NoOptionI18nKeys; }) => ( - - {i18n.t(translationKey)} + + {I18NextService.i18n.t(translationKey)} ); @@ -221,6 +223,7 @@ export class Home extends Component { postsRes: { state: "empty" }, commentsRes: { state: "empty" }, trendingCommunitiesRes: { state: "empty" }, + scrolled: true, siteRes: this.isoData.site_res, showSubscribedMobile: false, showTrendingMobile: false, @@ -276,11 +279,19 @@ export class Home extends Component { trendingCommunitiesRes, commentsRes, postsRes, - tagline: getRandomFromList(this.state?.siteRes?.taglines ?? []) - ?.content, isIsomorphic: true, }; + + HomeCacheService.postsRes = postsRes; } + + this.state.tagline = getRandomFromList( + this.state?.siteRes?.taglines ?? [] + )?.content; + } + + componentWillUnmount() { + HomeCacheService.activate(); } async componentDidMount() { @@ -296,19 +307,16 @@ export class Home extends Component { setupTippy(); } - componentWillUnmount() { - saveScrollPosition(this.context); - } - static async fetchInitialData({ client, auth, query: { dataType: urlDataType, listingType, page: urlPage, sort: urlSort }, + site, }: InitialFetchRequest>): Promise { const dataType = getDataTypeFromQuery(urlDataType); - - // TODO figure out auth default_listingType, default_sort_type - const type_ = getListingTypeFromQuery(listingType); + const type_ = + getListingTypeFromQuery(listingType) ?? + site.site_view.local_site.default_post_listing_type; const sort = getSortTypeFromQuery(urlSort); const page = urlPage ? Number(urlPage) : 1; @@ -375,14 +383,14 @@ export class Home extends Component { } = this.state; return ( -
+
{site_setup && (
-
+
{tagline && (
{
{this.mobileView}
{this.posts}
-
@@ -448,7 +456,7 @@ export class Home extends Component { )} {showTrendingMobile && (
- {this.trendingCommunities(true)} + {this.trendingCommunities()}
)} {showSubscribedMobile && ( @@ -494,7 +502,7 @@ export class Home extends Component { ); } - trendingCommunities(isMobile = false) { + trendingCommunities() { switch (this.state.trendingCommunitiesRes?.state) { case "loading": return ( @@ -566,13 +574,15 @@ export class Home extends Component { className="btn btn-sm text-muted" onClick={linkEvent(this, this.handleCollapseSubscribe)} aria-label={ - subscribedCollapsed ? i18n.t("expand") : i18n.t("collapse") + subscribedCollapsed + ? I18NextService.i18n.t("expand") + : I18NextService.i18n.t("collapse") } data-tippy-content={ - subscribedCollapsed ? i18n.t("expand") : i18n.t("collapse") + subscribedCollapsed + ? I18NextService.i18n.t("expand") + : I18NextService.i18n.t("collapse") } - data-bs-toggle="collapse" - data-bs-target="#sidebarSubscribedBody" aria-expanded="true" aria-controls="sidebarSubscribedBody" > @@ -583,24 +593,25 @@ export class Home extends Component { )} -
-
-
    - {UserService.Instance.myUserInfo?.follows.map(cfv => ( -
  • - -
  • - ))} -
+ {!subscribedCollapsed && ( +
+
+
    + {UserService.Instance.myUserInfo?.follows.map(cfv => ( +
  • + +
  • + ))} +
+
-
+ )} ); } @@ -625,6 +636,11 @@ export class Home extends Component { search: getQueryString(queryParams), }); + if (!this.state.scrolled) { + this.setState({ scrolled: true }); + setTimeout(() => window.scrollTo(0, 0), 0); + } + await this.fetchData(); } @@ -647,7 +663,9 @@ export class Home extends Component { const siteRes = this.state.siteRes; if (dataType === DataType.Post) { - switch (this.state.postsRes.state) { + switch (this.state.postsRes?.state) { + case "empty": + return
; case "loading": return (
@@ -735,25 +753,33 @@ export class Home extends Component { const { listingType, dataType, sort } = getHomeQueryParams(); return ( -
- +
+
- - +
+
- - +
+
- - {getRss(listingType)} +
+
+ {getRss( + listingType ?? + this.state.siteRes.site_view.local_site.default_post_listing_type + )} +
); } @@ -775,17 +801,30 @@ export class Home extends Component { const { dataType, page, listingType, sort } = getHomeQueryParams(); if (dataType === DataType.Post) { - this.setState({ postsRes: { state: "loading" } }); - this.setState({ - postsRes: await HttpService.client.getPosts({ - page, - limit: fetchLimit, - sort, - saved_only: false, - type_: listingType, - auth, - }), - }); + if (HomeCacheService.active) { + const { postsRes, scrollY } = HomeCacheService; + HomeCacheService.deactivate(); + this.setState({ postsRes }); + window.scrollTo({ + left: 0, + top: scrollY, + behavior: "instant", + }); + } else { + this.setState({ postsRes: { state: "loading" } }); + this.setState({ + postsRes: await HttpService.client.getPosts({ + page, + limit: fetchLimit, + sort, + saved_only: false, + type_: listingType, + auth, + }), + }); + + HomeCacheService.postsRes = this.state.postsRes; + } } else { this.setState({ commentsRes: { state: "loading" } }); this.setState({ @@ -800,7 +839,6 @@ export class Home extends Component { }); } - restoreScrollPosition(this.context); setupTippy(); } @@ -821,23 +859,23 @@ export class Home extends Component { } handlePageChange(page: number) { + this.setState({ scrolled: false }); this.updateUrl({ page }); - window.scrollTo(0, 0); } handleSortChange(val: SortType) { + this.setState({ scrolled: false }); this.updateUrl({ sort: val, page: 1 }); - window.scrollTo(0, 0); } handleListingTypeChange(val: ListingType) { + this.setState({ scrolled: false }); this.updateUrl({ listingType: val, page: 1 }); - window.scrollTo(0, 0); } handleDataTypeChange(val: DataType) { + this.setState({ scrolled: false }); this.updateUrl({ dataType: val, page: 1 }); - window.scrollTo(0, 0); } async handleAddModToCommunity(form: AddModToCommunity) { @@ -934,14 +972,14 @@ export class Home extends Component { async handleCommentReport(form: CreateCommentReport) { const reportRes = await HttpService.client.createCommentReport(form); if (reportRes.state == "success") { - toast(i18n.t("report_created")); + toast(I18NextService.i18n.t("report_created")); } } async handlePostReport(form: CreatePostReport) { const reportRes = await HttpService.client.createPostReport(form); if (reportRes.state == "success") { - toast(i18n.t("report_created")); + toast(I18NextService.i18n.t("report_created")); } } @@ -965,7 +1003,7 @@ export class Home extends Component { async handleTransferCommunity(form: TransferCommunity) { await HttpService.client.transferCommunity(form); - toast(i18n.t("transfer_community")); + toast(I18NextService.i18n.t("transfer_community")); } async handleCommentReplyRead(form: MarkCommentReplyAsRead) { @@ -1032,7 +1070,7 @@ export class Home extends Component { purgeItem(purgeRes: RequestState) { if (purgeRes.state == "success") { - toast(i18n.t("purge_success")); + toast(I18NextService.i18n.t("purge_success")); this.context.router.history.push(`/`); } }