convert search options to expandable object

wip

bundle
This commit is contained in:
jessop 2020-01-30 01:30:27 -05:00
parent bfbaa0dbdd
commit 635381a616
5 changed files with 114 additions and 56 deletions

63
dist/bundle.es.js vendored
View file

@ -1071,9 +1071,11 @@ function toQueryString(params) {
return parts.join('&'); return parts.join('&');
} }
const getSearchQueryString = (query, options = {}, includeUserOptions = false, additionalOptions = {}) => { const getSearchQueryString = (query, options = {}) => {
const encodedQuery = encodeURIComponent(query); const encodedQuery = encodeURIComponent(query);
const queryParams = [`s=${encodedQuery}`, `size=${options.size || DEFAULT_SEARCH_SIZE}`, `from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`]; const queryParams = [`s=${encodedQuery}`, `size=${options.size || DEFAULT_SEARCH_SIZE}`, `from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`];
const { isBackgroundSearch } = options;
const includeUserOptions = typeof isBackgroundSearch === 'undefined' ? false : !isBackgroundSearch;
if (includeUserOptions) { if (includeUserOptions) {
const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE]; const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE];
@ -1085,6 +1087,12 @@ const getSearchQueryString = (query, options = {}, includeUserOptions = false, a
} }
} }
const additionalOptions = {};
const { related_to } = options;
const { nsfw } = options;
if (related_to) additionalOptions['related_to'] = related_to;
if (typeof nsfw !== 'undefined') additionalOptions['nsfw'] = nsfw;
if (additionalOptions) { if (additionalOptions) {
Object.keys(additionalOptions).forEach(key => { Object.keys(additionalOptions).forEach(key => {
const option = additionalOptions[key]; const option = additionalOptions[key];
@ -1443,11 +1451,11 @@ const selectSearchSuggestions = reselect.createSelector(selectSearchValue, selec
// Creates a query string based on the state in the search reducer // Creates a query string based on the state in the search reducer
// Can be overrided by passing in custom sizes/from values for other areas pagination // Can be overrided by passing in custom sizes/from values for other areas pagination
const makeSelectQueryWithOptions = (customQuery, customSize, customFrom, isBackgroundSearch = false, // If it's a background search, don't use the users settings
additionalOptions = {}) => reselect.createSelector(selectSearchValue, selectSearchOptions, (query, options) => {
const size = customSize || options[SEARCH_OPTIONS.RESULT_COUNT];
const queryString = getSearchQueryString(customQuery || query, _extends$1({}, options, { size, from: customFrom }), !isBackgroundSearch, additionalOptions);
const makeSelectQueryWithOptions = (customQuery, options) => reselect.createSelector(selectSearchValue, selectSearchOptions, (query, defaultOptions) => {
const searchOptions = _extends$1({}, defaultOptions, options);
const queryString = getSearchQueryString(customQuery || query, searchOptions);
return queryString; return queryString;
}); });
@ -2249,11 +2257,11 @@ const makeSelectRecommendedContentForUri = uri => reselect.createSelector(makeSe
return; return;
} }
const options = { related_to: claim.claim_id }; const options = { related_to: claim.claim_id, isBackgroundSearch: true };
if (!isMature) { if (!isMature) {
options['nsfw'] = false; options['nsfw'] = false;
} }
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), undefined, undefined, options); const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
let searchUris = searchUrisByQuery[searchQuery]; let searchUris = searchUrisByQuery[searchQuery];
if (searchUris) { if (searchUris) {
@ -2326,7 +2334,7 @@ const makeSelectMyStreamUrlsForPage = (page = 1) => reselect.createSelector(sele
const selectMyStreamUrlsCount = reselect.createSelector(selectMyClaimUrisWithoutChannels, channels => channels.length); const selectMyStreamUrlsCount = reselect.createSelector(selectMyClaimUrisWithoutChannels, channels => channels.length);
const makeSelectResolvedRecommendedContentForUri = (uri, size) => reselect.createSelector(makeSelectClaimForUri(uri), selectResolvedSearchResultsByQuery, (claim, resolvedResultsByQuery) => { const makeSelectResolvedRecommendedContentForUri = (uri, size) => reselect.createSelector(makeSelectClaimForUri(uri), selectResolvedSearchResultsByQuery, makeSelectClaimIsNsfw(uri), (claim, resolvedResultsByQuery, isMature) => {
const atVanityURI = !uri.includes('#'); const atVanityURI = !uri.includes('#');
let recommendedContent; let recommendedContent;
@ -2340,9 +2348,12 @@ const makeSelectResolvedRecommendedContentForUri = (uri, size) => reselect.creat
return; return;
} }
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), { size }, undefined, { const options = { related_to: claim.claim_id, isBackgroundSearch: true };
related_to: claim.claim_id if (!isMature) {
}); options['nsfw'] = false;
}
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
let results = resolvedResultsByQuery[searchQuery]; let results = resolvedResultsByQuery[searchQuery];
if (results) { if (results) {
@ -3968,7 +3979,7 @@ function handleFetchResponse(response) {
return response.status === 200 ? Promise.resolve(response.json()) : Promise.reject(new Error(response.statusText)); return response.status === 200 ? Promise.resolve(response.json()) : Promise.reject(new Error(response.statusText));
} }
// var _extends$7 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
const DEBOUNCED_SEARCH_SUGGESTION_MS = 300; const DEBOUNCED_SEARCH_SUGGESTION_MS = 300;
@ -4025,9 +4036,10 @@ const doUpdateSearchQuery = (query, shouldSkipSuggestions) => dispatch => {
} }
}; };
const doSearch = (rawQuery, size, // only pass in if you don't want to use the users setting (ex: related content) const doSearch = (rawQuery, searchOptions) => (dispatch, getState) => {
from, isBackgroundSearch = false, options = {}, resolveResults = true) => (dispatch, getState) => {
const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' '); const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
const resolveResults = searchOptions && searchOptions.resolveResults;
const isBackgroundSearch = searchOptions && searchOptions.isBackgroundSearch || false;
if (!query) { if (!query) {
dispatch({ dispatch({
@ -4037,7 +4049,8 @@ from, isBackgroundSearch = false, options = {}, resolveResults = true) => (dispa
} }
const state = getState(); const state = getState();
let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)(state);
let queryWithOptions = makeSelectQueryWithOptions(query, searchOptions)(state);
// If we have already searched for something, we don't need to do anything // If we have already searched for something, we don't need to do anything
const urisForQuery = makeSelectSearchUris(queryWithOptions)(state); const urisForQuery = makeSelectSearchUris(queryWithOptions)(state);
@ -4108,11 +4121,23 @@ from, isBackgroundSearch = false, options = {}) => (dispatch, getState) => {
return; return;
} }
const optionsWithFrom = _extends$7({
size,
from,
isBackgroundSearch
}, options);
const optionsWithoutFrom = _extends$7({
size,
isBackgroundSearch
}, options);
const state = getState(); const state = getState();
let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)(state);
let queryWithOptions = makeSelectQueryWithOptions(query, optionsWithFrom)(state);
// make from null so that we can maintain a reference to the same query for multiple pages and simply append the found results // make from null so that we can maintain a reference to the same query for multiple pages and simply append the found results
let queryWithoutFrom = makeSelectQueryWithOptions(query, size, null, isBackgroundSearch, options)(state); let queryWithoutFrom = makeSelectQueryWithOptions(query, optionsWithoutFrom)(state);
// If we have already searched for something, we don't need to do anything // If we have already searched for something, we don't need to do anything
// TODO: Tweak this check for multiple page results // TODO: Tweak this check for multiple page results
@ -4162,7 +4187,7 @@ const doBlurSearchInput = () => dispatch => dispatch({
type: SEARCH_BLUR type: SEARCH_BLUR
}); });
const doUpdateSearchOptions = newOptions => (dispatch, getState) => { const doUpdateSearchOptions = (newOptions, additionalOptions) => (dispatch, getState) => {
const state = getState(); const state = getState();
const searchValue = selectSearchValue(state); const searchValue = selectSearchValue(state);
@ -4173,7 +4198,7 @@ const doUpdateSearchOptions = newOptions => (dispatch, getState) => {
if (searchValue) { if (searchValue) {
// After updating, perform a search with the new options // After updating, perform a search with the new options
dispatch(doSearch(searchValue)); dispatch(doSearch(searchValue, additionalOptions));
} }
}; };

View file

@ -17,6 +17,20 @@ const DEBOUNCED_SEARCH_SUGGESTION_MS = 300;
type Dispatch = (action: any) => any; type Dispatch = (action: any) => any;
type GetState = () => { search: SearchState }; type GetState = () => { search: SearchState };
type SearchOptions = {
size?: number,
from?: number,
related_to?: string,
nsfw?: boolean,
isBackgroundSearch?: boolean,
resolveResults?: boolean,
}
type AdditionalOptions = {
isBackgroundSearch: boolean,
nsfw?: boolean,
}
// We can't use env's because they aren't passed into node_modules // We can't use env's because they aren't passed into node_modules
let CONNECTION_STRING = 'https://lighthouse.lbry.com/'; let CONNECTION_STRING = 'https://lighthouse.lbry.com/';
@ -75,17 +89,14 @@ export const doUpdateSearchQuery = (query: string, shouldSkipSuggestions: ?boole
} }
}; };
export const doSearch = ( export const doSearch = (
rawQuery: string, rawQuery: string,
size: ?number, // only pass in if you don't want to use the users setting (ex: related content) searchOptions: SearchOptions,
from: ?number,
isBackgroundSearch: boolean = false,
options: {
related_to?: string,
} = {},
resolveResults: boolean = true
) => (dispatch: Dispatch, getState: GetState) => { ) => (dispatch: Dispatch, getState: GetState) => {
const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' '); const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
const resolveResults = searchOptions && searchOptions.resolveResults;
const isBackgroundSearch = (searchOptions && searchOptions.isBackgroundSearch) || false;
if (!query) { if (!query) {
dispatch({ dispatch({
@ -95,7 +106,8 @@ export const doSearch = (
} }
const state = getState(); const state = getState();
let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)(
let queryWithOptions = makeSelectQueryWithOptions(query, searchOptions)(
state state
); );
@ -167,6 +179,7 @@ export const doResolvedSearch = (
isBackgroundSearch: boolean = false, isBackgroundSearch: boolean = false,
options: { options: {
related_to?: string, related_to?: string,
// nsfw here
} = {} } = {}
) => (dispatch: Dispatch, getState: GetState) => { ) => (dispatch: Dispatch, getState: GetState) => {
const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' '); const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
@ -178,13 +191,27 @@ export const doResolvedSearch = (
return; return;
} }
const optionsWithFrom: SearchOptions = {
size,
from,
isBackgroundSearch,
...options,
}
const optionsWithoutFrom: SearchOptions = {
size,
isBackgroundSearch,
...options,
}
const state = getState(); const state = getState();
let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)(
let queryWithOptions = makeSelectQueryWithOptions(query, optionsWithFrom)(
state state
); );
// make from null so that we can maintain a reference to the same query for multiple pages and simply append the found results // make from null so that we can maintain a reference to the same query for multiple pages and simply append the found results
let queryWithoutFrom = makeSelectQueryWithOptions(query, size, null, isBackgroundSearch, options)( let queryWithoutFrom = makeSelectQueryWithOptions(query, optionsWithoutFrom)(
state state
); );
@ -241,7 +268,7 @@ export const doBlurSearchInput = () => (dispatch: Dispatch) =>
type: ACTIONS.SEARCH_BLUR, type: ACTIONS.SEARCH_BLUR,
}); });
export const doUpdateSearchOptions = (newOptions: SearchOptions) => ( export const doUpdateSearchOptions = (newOptions: SearchOptions, additionalOptions: AdditionalOptions) => (
dispatch: Dispatch, dispatch: Dispatch,
getState: GetState getState: GetState
) => { ) => {
@ -255,6 +282,6 @@ export const doUpdateSearchOptions = (newOptions: SearchOptions) => (
if (searchValue) { if (searchValue) {
// After updating, perform a search with the new options // After updating, perform a search with the new options
dispatch(doSearch(searchValue)); dispatch(doSearch(searchValue, additionalOptions));
} }
}; };

View file

@ -530,16 +530,11 @@ export const makeSelectRecommendedContentForUri = (uri: string) =>
return; return;
} }
const options = { related_to: claim.claim_id }; const options = { related_to: claim.claim_id, isBackgroundSearch: true };
if (!isMature) { if (!isMature) {
options['nsfw'] = false; options['nsfw'] = false;
} }
const searchQuery = getSearchQueryString( const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
title.replace(/\//, ' '),
undefined,
undefined,
options
);
let searchUris = searchUrisByQuery[searchQuery]; let searchUris = searchUrisByQuery[searchQuery];
if (searchUris) { if (searchUris) {
@ -671,7 +666,8 @@ export const makeSelectResolvedRecommendedContentForUri = (uri: string, size: nu
createSelector( createSelector(
makeSelectClaimForUri(uri), makeSelectClaimForUri(uri),
selectResolvedSearchResultsByQuery, selectResolvedSearchResultsByQuery,
(claim, resolvedResultsByQuery) => { makeSelectClaimIsNsfw(uri),
(claim, resolvedResultsByQuery, isMature) => {
const atVanityURI = !uri.includes('#'); const atVanityURI = !uri.includes('#');
let recommendedContent; let recommendedContent;
@ -685,9 +681,12 @@ export const makeSelectResolvedRecommendedContentForUri = (uri: string, size: nu
return; return;
} }
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), { size }, undefined, { const options = { related_to: claim.claim_id, isBackgroundSearch: true }
related_to: claim.claim_id, if (!isMature) {
}); options['nsfw'] = false;
}
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
let results = resolvedResultsByQuery[searchQuery]; let results = resolvedResultsByQuery[searchQuery];
if (results) { if (results) {

View file

@ -165,26 +165,27 @@ export const selectSearchSuggestions: Array<SearchSuggestion> = createSelector(
// Creates a query string based on the state in the search reducer // Creates a query string based on the state in the search reducer
// Can be overrided by passing in custom sizes/from values for other areas pagination // Can be overrided by passing in custom sizes/from values for other areas pagination
type CustomOptions = {
isBackgroundSearch?: boolean,
size?: number,
from?: number,
related_to?: string,
nsfw?: boolean,
}
export const makeSelectQueryWithOptions = ( export const makeSelectQueryWithOptions = (
customQuery: ?string, customQuery: ?string,
customSize: ?number, options: CustomOptions,
customFrom: ?number,
isBackgroundSearch: boolean = false, // If it's a background search, don't use the users settings
additionalOptions: {
related_to?: string,
} = {}
) => ) =>
createSelector( createSelector(
selectSearchValue, selectSearchValue,
selectSearchOptions, selectSearchOptions,
(query, options) => { (query, defaultOptions) => {
const size = customSize || options[SEARCH_OPTIONS.RESULT_COUNT]; const searchOptions = { ...defaultOptions, ...options };
const queryString = getSearchQueryString( const queryString = getSearchQueryString(
customQuery || query, customQuery || query,
{ ...options, size, from: customFrom }, searchOptions,
!isBackgroundSearch,
additionalOptions
); );
return queryString; return queryString;

View file

@ -36,8 +36,6 @@ export function toQueryString(params: { [string]: string | number }) {
export const getSearchQueryString = ( export const getSearchQueryString = (
query: string, query: string,
options: any = {}, options: any = {},
includeUserOptions: boolean = false,
additionalOptions: {} = {}
) => { ) => {
const encodedQuery = encodeURIComponent(query); const encodedQuery = encodeURIComponent(query);
const queryParams = [ const queryParams = [
@ -45,6 +43,8 @@ export const getSearchQueryString = (
`size=${options.size || DEFAULT_SEARCH_SIZE}`, `size=${options.size || DEFAULT_SEARCH_SIZE}`,
`from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`, `from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`,
]; ];
const { isBackgroundSearch } = options;
const includeUserOptions = typeof isBackgroundSearch === 'undefined' ? false : !isBackgroundSearch;
if (includeUserOptions) { if (includeUserOptions) {
const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE]; const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE];
@ -68,6 +68,12 @@ export const getSearchQueryString = (
} }
} }
const additionalOptions = {}
const { related_to } = options;
const { nsfw } = options;
if (related_to) additionalOptions['related_to'] = related_to;
if (typeof nsfw !== 'undefined') additionalOptions['nsfw'] = nsfw;
if (additionalOptions) { if (additionalOptions) {
Object.keys(additionalOptions).forEach(key => { Object.keys(additionalOptions).forEach(key => {
const option = additionalOptions[key]; const option = additionalOptions[key];