From 95124ff52125fc3fdee36dcc5b657f3892473d8e Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Tue, 15 May 2018 23:10:06 -0400 Subject: [PATCH] handle lbry uris in autocomplete results --- dist/bundle.js | 34 ++++++++++++++++++++++++++-------- src/redux/actions/search.js | 31 +++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/dist/bundle.js b/dist/bundle.js index 306a152..50bb75e 100644 --- a/dist/bundle.js +++ b/dist/bundle.js @@ -3840,16 +3840,34 @@ var getSearchSuggestions = exports.getSearchSuggestions = function getSearchSugg } fetch('https://lighthouse.lbry.io/autocomplete?s=' + searchValue).then(_handleFetch2.default).then(function (apiSuggestions) { + // Suggestion could be a channel, uri, or search term var formattedSuggestions = apiSuggestions.slice(0, 6).map(function (suggestion) { - // This will need to be more robust when the api starts returning lbry uris - var isChannel = suggestion.startsWith('@'); - var suggestionObj = { - value: isChannel ? 'lbry://' + suggestion : suggestion, - shorthand: isChannel ? suggestion.slice(1) : '', - type: isChannel ? 'channel' : 'search' - }; + if (suggestion.includes(' ')) { + return { + value: suggestion, + type: SEARCH_TYPES.SEARCH + }; + } - return suggestionObj; + try { + var _uri = (0, _lbryURI.normalizeURI)(suggestion); + + var _parseURI2 = (0, _lbryURI.parseURI)(_uri), + _claimName = _parseURI2.claimName, + _isChannel = _parseURI2.isChannel; + + return { + value: _uri, + shorthand: _isChannel ? _claimName.slice(1) : _claimName, + type: _isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE + }; + } catch (e) { + // search result includes some character that isn't valid in claim names + return { + value: suggestion, + type: SEARCH_TYPES.SEARCH + }; + } }); suggestions = suggestions.concat(formattedSuggestions); diff --git a/src/redux/actions/search.js b/src/redux/actions/search.js index 807a707..fc4d0c0 100644 --- a/src/redux/actions/search.js +++ b/src/redux/actions/search.js @@ -137,16 +137,31 @@ export const getSearchSuggestions = (value: string) => dispatch => { fetch(`https://lighthouse.lbry.io/autocomplete?s=${searchValue}`) .then(handleFetchResponse) .then(apiSuggestions => { + // Suggestion could be a channel, uri, or search term const formattedSuggestions = apiSuggestions.slice(0, 6).map(suggestion => { - // This will need to be more robust when the api starts returning lbry uris - const isChannel = suggestion.startsWith('@'); - const suggestionObj = { - value: isChannel ? `lbry://${suggestion}` : suggestion, - shorthand: isChannel ? suggestion.slice(1) : '', - type: isChannel ? 'channel' : 'search', - }; + if (suggestion.includes(' ')) { + return { + value: suggestion, + type: SEARCH_TYPES.SEARCH, + }; + } - return suggestionObj; + try { + const uri = normalizeURI(suggestion); + const { claimName, isChannel } = parseURI(uri); + + return { + value: uri, + shorthand: isChannel ? claimName.slice(1) : claimName, + type: isChannel ? SEARCH_TYPES.CHANNEL : SEARCH_TYPES.FILE, + }; + } catch (e) { + // search result includes some character that isn't valid in claim names + return { + value: suggestion, + type: SEARCH_TYPES.SEARCH, + }; + } }); suggestions = suggestions.concat(formattedSuggestions);