Skip to content

Commit

Permalink
Experimenting with hook for all search params
Browse files Browse the repository at this point in the history
  • Loading branch information
jaedb committed Oct 9, 2023
1 parent b011292 commit bec0cf9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
16 changes: 8 additions & 8 deletions src/js/components/SearchResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import { Grid } from './Grid';
import { I18n } from '../locale';
import Button from './Button';
import { makeSearchResultsSelector, getSortSelector } from '../util/selectors';
import useSearchQuery from '../util/useSearchQuery';

const SORT_KEY = 'search_results';

const SearchResults = ({
type,
all,
}) => {
const { term, providers = '' } = useParams();
const { term, providers } = useSearchQuery();
const encodedProviders = providers.join(',').replace(/:/g,'');
const [sortField, sortReverse] = useSelector(
(state) => getSortSelector(state, SORT_KEY, 'name'),
);
const providersArray = providers.split(',');
const searchResultsSelector = makeSearchResultsSelector(providersArray, term, type);
const searchResultsSelector = makeSearchResultsSelector(providers, term, type);
const rawResults = useSelector(searchResultsSelector);
console.debug(rawResults, providersArray, term, type)
const encodedTerm = encodeURIComponent(term);
let results = [...rawResults];

Expand All @@ -45,7 +45,7 @@ const SearchResults = ({
<h4>
{!all && (
<span>
<URILink uri={`iris:search:all:all:${encodedTerm}`} uriType="search" unencoded>
<URILink uri={`iris:search:all:${encodedProviders}:${encodedTerm}`} uriType="search" unencoded>
<I18n path="search.title" />
</URILink>
{' '}
Expand All @@ -55,7 +55,7 @@ const SearchResults = ({
</span>
)}
{all && (
<URILink uri={`iris:search:${type}:${providers}:${encodedTerm}`} uriType="search" unencoded>
<URILink uri={`iris:search:${type}:${encodedProviders}:${encodedTerm}`} uriType="search" unencoded>
<I18n path={`search.${type}.title`} />
</URILink>
)}
Expand All @@ -68,7 +68,7 @@ const SearchResults = ({
{type === 'tracks' && (
<TrackList
source={{
uri: `iris:search:${type}:${providers}:${encodedTerm}`,
uri: `iris:search:${type}:${encodedProviders}:${encodedTerm}`,
name: 'Search results',
type: 'search',
}}
Expand All @@ -79,7 +79,7 @@ const SearchResults = ({
{/* <LazyLoadListener enabled={this.props.artists_more && spotify_search_enabled} loadMore={loadMore} /> */}

{resultsCount > results.length && (
<Button uri={`iris:search:${type}:${encodedTerm}`} uriType="search" unencoded>
<Button uri={`iris:search:${type}:${encodedProviders}:${encodedTerm}`} uriType="search" unencoded>
<I18n path={`search.${type}.more`} count={resultsCount} />
</Button>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/URILink.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default memo(({

case 'search':
var exploded = uri.split(':');
to = `/search/${exploded[2]}/${exploded[3]}`;
to = `/search/${exploded[2]}/${exploded[3]}/${exploded[4]}`;
break;

default:
Expand Down
6 changes: 3 additions & 3 deletions src/js/util/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ const makeLibrarySelector = (name, filtered = true) => createSelector(
},
);

const makeSearchResultsSelector = (providers = [], term, type) => createSelector(
const makeSearchResultsSelector = (providers, term, type) => createSelector(
[getSearchResults],
(searchResults) => providers.reduce(
(acc, curr) => {[
(acc, curr) => [
...acc,
...searchResults[getSearchResultKey({ provider: curr, term, type })] || [],
]}, []),
], []),
);

const makeProcessProgressSelector = (keys) => createSelector(
Expand Down
25 changes: 25 additions & 0 deletions src/js/util/useSearchQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useParams } from 'react-router';
import { useSelector } from 'react-redux';

const useSearchQuery = () => {
const {
term = '',
type = 'all',
providers: rawProviders = 'all',
} = useParams();
const allProviders = useSelector((state) => state.mopidy?.uri_schemes || []);
const providers = rawProviders == 'all'
? [...allProviders]
: rawProviders.split(',').filter((str) => allProviders.indexOf(str) > -1);
const providersString = providers.join(',').replace(/:/g,'');

return {
term,
type,
providers,
allProviders,
providersString,
}
};

export default useSearchQuery;
23 changes: 10 additions & 13 deletions src/js/views/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,36 @@ import {
} from '../services/ui/actions';
import { i18n } from '../locale';
import { getSortSelector } from '../util/selectors';
import useSearchQuery from '../util/useSearchQuery';

const SORT_KEY = 'search_results';

const Search = () => {
const {
term,
providers: providersString = 'all',
type = 'all',
} = useParams();
const dispatch = useDispatch();
const navigate = useNavigate();
const lastQuery = useSelector((state) => state.core?.search_results?.query);
const {
term,
type,
providers,
allProviders,
providersString,
} = useSearchQuery();
const [sortField, sortReverse] = useSelector(
(state) => getSortSelector(state, SORT_KEY, 'name'),
);
const allProviders = useSelector((state) => state.mopidy?.uri_schemes || []);
const providers = providersString == 'all'
? [...allProviders]
: providersString.split(',').filter((str) => allProviders.indexOf(str) > -1);

useEffect(() => {
dispatch(setWindowTitle('Search'));
$(document).find('.search-form input').focus();
}, []);

useEffect(() => {
console.debug(providers, lastQuery?.providers)
if (term && type && (term !== lastQuery?.term || providers.length !== lastQuery?.providers?.length)) {
if (term) {
console.debug('STARTING SEARCH', { term, type, providers })
dispatch(setWindowTitle(i18n('search.title_window', { term: decodeURIComponent(term) })));
dispatch(startSearch({ term, type, providers }));
}
}, [term, type, providersString])
}, [])

const onSubmit = (term) => {
updateSearchQuery(term, providers);
Expand Down

0 comments on commit bec0cf9

Please sign in to comment.