Refactor search.js

Calling 'normalizeURI' then 'parseURI' needlessly runs the 'parseURI' function twice. This is a better way of doing it.
This commit is contained in:
ioan.cole 2020-08-30 19:27:21 +08:00 committed by Sean Yesmunt
parent 3f598f76d1
commit 254a2f5138

View file

@ -1,7 +1,7 @@
// @flow // @flow
import { SEARCH_TYPES } from 'constants/search'; import { SEARCH_TYPES } from 'constants/search';
import { getSearchQueryString } from 'util/query-params'; import { getSearchQueryString } from 'util/query-params';
import { normalizeURI, parseURI, makeSelectClaimForUri, makeSelectClaimIsNsfw, buildURI } from 'lbry-redux'; import { parseURI, makeSelectClaimForUri, makeSelectClaimIsNsfw, buildURI } from 'lbry-redux';
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
type State = { search: SearchState }; type State = { search: SearchState };
@ -54,11 +54,9 @@ export const selectSearchSuggestions: Array<SearchSuggestion> = createSelector(
// They are probably typing/pasting in a lbry uri // They are probably typing/pasting in a lbry uri
let type: string; let type: string;
try { try {
const uri = normalizeURI(query); let { isChannel } = parseURI(query);
let { isChannel } = parseURI(uri);
type = isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE; type = isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE;
} catch (e) { } catch (e) {
console.log('Query not recognized: ' + query);
type = SEARCH_TYPES.SEARCH; type = SEARCH_TYPES.SEARCH;
} }
@ -71,26 +69,19 @@ export const selectSearchSuggestions: Array<SearchSuggestion> = createSelector(
} }
let searchSuggestions = []; let searchSuggestions = [];
searchSuggestions.push({
value: query,
type: SEARCH_TYPES.SEARCH,
});
try { try {
const uri = normalizeURI(query); const uriObj = parseURI(query);
const { channelName, streamName, isChannel } = parseURI(uri);
searchSuggestions.push(
{
value: query,
type: SEARCH_TYPES.SEARCH,
},
{
value: uri,
shorthand: isChannel ? channelName : streamName,
type: isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE,
}
);
} catch (e) {
searchSuggestions.push({ searchSuggestions.push({
value: query, value: buildURI(uriObj),
type: SEARCH_TYPES.SEARCH, shorthand: uriObj.isChannel ? uriObj.channelName : uriObj.streamName,
type: uriObj.isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE,
}); });
} } catch (e) {}
searchSuggestions.push({ searchSuggestions.push({
value: query, value: query,
@ -105,13 +96,11 @@ export const selectSearchSuggestions: Array<SearchSuggestion> = createSelector(
.map(suggestion => { .map(suggestion => {
// determine if it's a channel // determine if it's a channel
try { try {
const uri = normalizeURI(suggestion); const uriObj = parseURI(suggestion);
const { channelName, streamName, isChannel } = parseURI(uri);
return { return {
value: uri, value: buildURI(uriObj),
shorthand: isChannel ? channelName : streamName, shorthand: uriObj.isChannel ? uriObj.channelName : uriObj.streamName,
type: isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE, type: uriObj.isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE,
}; };
} catch (e) { } catch (e) {
// search result includes some character that isn't valid in claim names // search result includes some character that isn't valid in claim names