Persist trending selectors between tags.
---The issue: When switching between tags, the selector defaults back to Trending even though you had another option already selected. ---Changes: - 'orderParamUser' will store the last user state persistently. The persistent state is also made unique for each page (i.e. Your Tags and All Content will be unique). - If the parent component passes in a specific order, that will be respected and will also become the new persisted value. One example is "Your Following", where it always starts at 'New'. - Handled navigation history correctly The test case: - Enter "Your Tags" (assume start at 'Trending') - Click 'New' - Click 'Top' - Back - Back (it should return to 'Trending') As the top page history does not have any "?order=" value, we ended up with a no-op for the last Back. 'orderParamEntry' is added to handle this.
This commit is contained in:
parent
19eefdf1da
commit
9d6af38a21
1 changed files with 23 additions and 1 deletions
|
@ -2,6 +2,7 @@
|
|||
import type { Node } from 'react';
|
||||
import classnames from 'classnames';
|
||||
import React, { Fragment, useEffect, useState } from 'react';
|
||||
import usePersistedState from 'effects/use-persisted-state';
|
||||
import { withRouter } from 'react-router';
|
||||
import * as CS from 'constants/claim_search';
|
||||
import { createNormalizedClaimSearchKey, MATURE_TAGS } from 'lbry-redux';
|
||||
|
@ -101,13 +102,14 @@ function ClaimListDiscover(props: Props) {
|
|||
const [page, setPage] = useState(1);
|
||||
const [forceRefresh, setForceRefresh] = useState();
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
const [orderParamEntry, setOrderParamEntry] = useState(CS.ORDER_BY_TRENDING);
|
||||
const [orderParamUser, setOrderParamUser] = usePersistedState(`orderUser-${location.pathname}`, CS.ORDER_BY_TRENDING);
|
||||
const followed = (followedTags && followedTags.map(t => t.name)) || [];
|
||||
const urlParams = new URLSearchParams(search);
|
||||
const tagsParam = // can be 'x,y,z' or 'x' or ['x','y'] or CS.CONSTANT
|
||||
(tags && getParamFromTags(tags)) ||
|
||||
(urlParams.get(CS.TAGS_KEY) !== null && urlParams.get(CS.TAGS_KEY)) ||
|
||||
(defaultTags && getParamFromTags(defaultTags));
|
||||
const orderParam = orderBy || urlParams.get(CS.ORDER_BY_KEY) || defaultOrderBy || CS.ORDER_BY_TRENDING;
|
||||
const freshnessParam = freshness || urlParams.get(CS.FRESH_KEY) || defaultFreshness;
|
||||
const contentTypeParam = urlParams.get(CS.CONTENT_KEY);
|
||||
const claimTypeParam =
|
||||
|
@ -133,6 +135,26 @@ function ClaimListDiscover(props: Props) {
|
|||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
let orderParam = orderBy || urlParams.get(CS.ORDER_BY_KEY) || defaultOrderBy;
|
||||
if (!orderParam) {
|
||||
if (history.action === 'POP') {
|
||||
// Reaching here means user have popped back to the page's entry point (e.g. '/$/tags' without any '?order=').
|
||||
orderParam = orderParamEntry;
|
||||
} else {
|
||||
// This is the direct entry into the page, so we load the user's previous value.
|
||||
orderParam = orderParamUser;
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setOrderParamUser(orderParam);
|
||||
}, [orderParam]);
|
||||
|
||||
useEffect(() => {
|
||||
// One-time update to stash the finalized 'orderParam' at entry.
|
||||
setOrderParamEntry(orderParam);
|
||||
}, []);
|
||||
|
||||
let options: {
|
||||
page_size: number,
|
||||
page: number,
|
||||
|
|
Loading…
Add table
Reference in a new issue