Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix home page not using site-level listing type #1612 #1778

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 35 additions & 20 deletions src/shared/components/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ type HomeData = RouteDataResponse<{
trendingCommunitiesRes: ListCommunitiesResponse;
}>;

function getRss(listingType: ListingType) {
const { sort } = getHomeQueryParams();
function getRss(listingType: ListingType, site: GetSiteResponse) {
const { sort } = getHomeQueryParams(site);
const auth = myAuth();

let rss: string | undefined = undefined;
Expand Down Expand Up @@ -163,12 +163,19 @@ function getDataTypeFromQuery(type?: string): DataType {
return type ? DataType[type] : DataType.Post;
}

function getListingTypeFromQuery(type?: string): ListingType {
function getListingTypeFromQuery(
type?: string,
site?: GetSiteResponse
): ListingType {
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) ??
site?.site_view.local_site.default_post_listing_type ??
"Local"
);
}

function getSortTypeFromQuery(type?: string): SortType {
Expand All @@ -179,13 +186,16 @@ function getSortTypeFromQuery(type?: string): SortType {
return (type ? (type as SortType) : mySortType) ?? "Active";
}

const getHomeQueryParams = () =>
getQueryParams<HomeProps>({
sort: getSortTypeFromQuery,
listingType: getListingTypeFromQuery,
page: getPageFromString,
dataType: getDataTypeFromQuery,
});
const getHomeQueryParams = (site: GetSiteResponse) =>
getQueryParams<HomeProps>(
{
sort: getSortTypeFromQuery,
listingType: getListingTypeFromQuery,
page: getPageFromString,
dataType: getDataTypeFromQuery,
},
site
);

const MobileButton = ({
textKey,
Expand Down Expand Up @@ -311,11 +321,10 @@ export class Home extends Component<any, HomeState> {
client,
auth,
query: { dataType: urlDataType, listingType, page: urlPage, sort: urlSort },
site,
}: InitialFetchRequest<QueryParams<HomeProps>>): Promise<HomeData> {
const dataType = getDataTypeFromQuery(urlDataType);

// TODO figure out auth default_listingType, default_sort_type
const type_ = getListingTypeFromQuery(listingType);
const type_ = getListingTypeFromQuery(listingType, site);
const sort = getSortTypeFromQuery(urlSort);

const page = urlPage ? Number(urlPage) : 1;
Expand Down Expand Up @@ -621,7 +630,7 @@ export class Home extends Component<any, HomeState> {
listingType: urlListingType,
page: urlPage,
sort: urlSort,
} = getHomeQueryParams();
} = getHomeQueryParams(this.state.siteRes);

const queryParams: QueryParams<HomeProps> = {
dataType: getDataTypeString(dataType ?? urlDataType),
Expand All @@ -644,7 +653,7 @@ export class Home extends Component<any, HomeState> {
}

get posts() {
const { page } = getHomeQueryParams();
const { page } = getHomeQueryParams(this.state.siteRes);

return (
<div className="main-content-wrapper">
Expand All @@ -658,7 +667,7 @@ export class Home extends Component<any, HomeState> {
}

get listings() {
const { dataType } = getHomeQueryParams();
const { dataType } = getHomeQueryParams(this.state.siteRes);
const siteRes = this.state.siteRes;

if (dataType === DataType.Post) {
Expand Down Expand Up @@ -749,7 +758,9 @@ export class Home extends Component<any, HomeState> {
}

get selects() {
const { listingType, dataType, sort } = getHomeQueryParams();
const { listingType, dataType, sort } = getHomeQueryParams(
this.state.siteRes
);

return (
<div className="row align-items-center mb-3 g-3">
Expand All @@ -770,7 +781,9 @@ export class Home extends Component<any, HomeState> {
<div className="col-auto">
<SortSelect sort={sort} onChange={this.handleSortChange} />
</div>
<div className="col-auto ps-0">{getRss(listingType)}</div>
<div className="col-auto ps-0">
{getRss(listingType, this.state.siteRes)}
</div>
</div>
);
}
Expand All @@ -789,7 +802,9 @@ export class Home extends Component<any, HomeState> {

async fetchData() {
const auth = myAuth();
const { dataType, page, listingType, sort } = getHomeQueryParams();
const { dataType, page, listingType, sort } = getHomeQueryParams(
this.state.siteRes
);

if (dataType === DataType.Post) {
if (HomeCacheService.active) {
Expand Down
14 changes: 8 additions & 6 deletions src/shared/utils/helpers/get-query-params.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { isBrowser } from "@utils/browser";
import { GetSiteResponse } from "lemmy-js-client";

export default function getQueryParams<
T extends Record<string, any>
>(processors: {
[K in keyof T]: (param: string) => T[K];
}): T {
export default function getQueryParams<T extends Record<string, any>>(
processors: {
[K in keyof T]: (param: string) => T[K];
},
site?: GetSiteResponse
): T {
if (isBrowser()) {
const searchParams = new URLSearchParams(window.location.search);

return Array.from(Object.entries(processors)).reduce(
(acc, [key, process]) => ({
...acc,
[key]: process(searchParams.get(key)),
[key]: process(searchParams.get(key), site),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would a GetSiteResponse necessarily have properties that should be query params?

Copy link
Contributor Author

@jcgurango jcgurango Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for implementing the fallback for listing type to the site-level setting set in the site admin. If later sort type becomes settable in the admin it'd go for that too. Would it make more sense to do something like return null in these callbacks and do the fallback on the home component?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to do something like return null in these callbacks and do the fallback on the home component?

Yes. You don't want any of these props in the query params since no page uses them:

export interface GetSiteResponse {
  site_view: SiteView;
  admins: Array<PersonView>;
  version: string;
  my_user?: MyUserInfo;
  all_languages: Array<Language>;
  discussion_languages: Array<LanguageId>;
  taglines: Array<Tagline>;
  custom_emojis: Array<CustomEmojiView>;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've updated that.

}),
{} as T
);
Expand Down