X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fhome%2Fhome.tsx;h=741dfa57aa7576eb0138bf1d687ab2773ae4a70d;hb=26ff0f7e06bf32024161a60a5503e1a041ab778c;hp=bad771fccf15cfc0e16105113e2373c4afcfe145;hpb=a2568a41f3da5ca970809a17287df35364d43673;p=lemmy-ui.git diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index bad771f..741dfa5 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -78,7 +78,12 @@ import { InitialFetchRequest, } from "../../interfaces"; import { mdToHtml } from "../../markdown"; -import { FirstLoadService, I18NextService, UserService } from "../../services"; +import { + FirstLoadService, + HomeCacheService, + I18NextService, + UserService, +} from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { setupTippy } from "../../tippy"; import { toast } from "../../toast"; @@ -101,6 +106,7 @@ interface HomeState { showTrendingMobile: boolean; showSidebarMobile: boolean; subscribedCollapsed: boolean; + scrolled: boolean; tagline?: string; siteRes: GetSiteResponse; finished: Map; @@ -108,7 +114,7 @@ interface HomeState { } interface HomeProps { - listingType: ListingType; + listingType?: ListingType; dataType: DataType; sort: SortType; page: number; @@ -157,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 { @@ -217,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, @@ -272,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,11 +311,12 @@ export class Home extends Component { 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; @@ -374,7 +390,7 @@ export class Home extends Component { /> {site_setup && (
-
+
{tagline && (
{
{this.mobileView}
{this.posts}
-
@@ -620,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(); } @@ -643,6 +664,8 @@ export class Home extends Component { if (dataType === DataType.Post) { switch (this.state.postsRes?.state) { + case "empty": + return
; case "loading": return (
@@ -739,7 +762,10 @@ export class Home extends Component {
{
-
{getRss(listingType)}
+
+ {getRss( + listingType ?? + this.state.siteRes.site_view.local_site.default_post_listing_type + )} +
); } @@ -770,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({ @@ -815,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) {