handle lbry uris in autocomplete results

This commit is contained in:
Sean Yesmunt 2018-05-15 23:10:06 -04:00
parent 86530690e9
commit 95124ff521
2 changed files with 49 additions and 16 deletions

34
dist/bundle.js vendored
View file

@ -3840,16 +3840,34 @@ var getSearchSuggestions = exports.getSearchSuggestions = function getSearchSugg
} }
fetch('https://lighthouse.lbry.io/autocomplete?s=' + searchValue).then(_handleFetch2.default).then(function (apiSuggestions) { 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) { var formattedSuggestions = apiSuggestions.slice(0, 6).map(function (suggestion) {
// This will need to be more robust when the api starts returning lbry uris if (suggestion.includes(' ')) {
var isChannel = suggestion.startsWith('@'); return {
var suggestionObj = { value: suggestion,
value: isChannel ? 'lbry://' + suggestion : suggestion, type: SEARCH_TYPES.SEARCH
shorthand: isChannel ? suggestion.slice(1) : '', };
type: isChannel ? 'channel' : '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); suggestions = suggestions.concat(formattedSuggestions);

View file

@ -137,16 +137,31 @@ export const getSearchSuggestions = (value: string) => dispatch => {
fetch(`https://lighthouse.lbry.io/autocomplete?s=${searchValue}`) fetch(`https://lighthouse.lbry.io/autocomplete?s=${searchValue}`)
.then(handleFetchResponse) .then(handleFetchResponse)
.then(apiSuggestions => { .then(apiSuggestions => {
// Suggestion could be a channel, uri, or search term
const formattedSuggestions = apiSuggestions.slice(0, 6).map(suggestion => { const formattedSuggestions = apiSuggestions.slice(0, 6).map(suggestion => {
// This will need to be more robust when the api starts returning lbry uris if (suggestion.includes(' ')) {
const isChannel = suggestion.startsWith('@'); return {
const suggestionObj = { value: suggestion,
value: isChannel ? `lbry://${suggestion}` : suggestion, type: SEARCH_TYPES.SEARCH,
shorthand: isChannel ? suggestion.slice(1) : '', };
type: isChannel ? 'channel' : '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); suggestions = suggestions.concat(formattedSuggestions);