|
|
import { map, sampleSize, times } from 'lodash'; |
|
|
import { Component } from 'react'; |
|
|
import { connect } from 'react-redux'; |
|
|
import { getLocaleSlug } from 'calypso/lib/i18n-utils'; |
|
|
import { suggestions } from 'calypso/reader/search-stream/suggestions'; |
|
|
import { getReaderFollowedTags } from 'calypso/state/reader/tags/selectors'; |
|
|
|
|
|
function createRandomId( randomBytesLength = 9 ) { |
|
|
|
|
|
|
|
|
let randomBytes; |
|
|
|
|
|
if ( window.crypto && window.crypto.getRandomValues ) { |
|
|
randomBytes = new Uint8Array( randomBytesLength ); |
|
|
window.crypto.getRandomValues( randomBytes ); |
|
|
} else { |
|
|
randomBytes = times( randomBytesLength, () => Math.floor( Math.random() * 256 ) ); |
|
|
} |
|
|
|
|
|
return window.btoa( String.fromCharCode.apply( String, randomBytes ) ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function suggestionsFromTags( count, tags ) { |
|
|
if ( tags ) { |
|
|
if ( tags.length <= count ) { |
|
|
return []; |
|
|
} |
|
|
return map( sampleSize( tags, count ), ( tag, i ) => { |
|
|
const text = ( tag.displayName || tag.slug ).replace( /-/g, ' ' ); |
|
|
const ui_algo = 'read:search-suggestions:tags/1'; |
|
|
return suggestionWithRailcar( text, ui_algo, i ); |
|
|
} ); |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function trendingTagsToTags( trendingTags ) { |
|
|
return map( trendingTags, ( tag ) => ( { |
|
|
displayName: tag.tag.display_name, |
|
|
slug: tag.tag.slug, |
|
|
} ) ); |
|
|
} |
|
|
|
|
|
function suggestionsFromPicks( count ) { |
|
|
const lang = getLocaleSlug().split( '-' )[ 0 ]; |
|
|
if ( suggestions[ lang ] ) { |
|
|
return map( sampleSize( suggestions[ lang ], count ), ( tag, i ) => { |
|
|
const ui_algo = 'read:search-suggestions:picks/1'; |
|
|
return suggestionWithRailcar( tag, ui_algo, i ); |
|
|
} ); |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
function suggestionWithRailcar( text, ui_algo, position ) { |
|
|
return { |
|
|
text: text, |
|
|
railcar: { |
|
|
railcar: createRandomId() + '-' + position, |
|
|
ui_algo: ui_algo, |
|
|
ui_position: position, |
|
|
rec_result: text, |
|
|
}, |
|
|
}; |
|
|
} |
|
|
|
|
|
function getSuggestions( count, tags, trendingTags ) { |
|
|
|
|
|
const tagSuggestions = tags |
|
|
? suggestionsFromTags( count, tags ) |
|
|
: suggestionsFromTags( count, trendingTagsToTags( trendingTags ) ); |
|
|
|
|
|
|
|
|
if ( tagSuggestions === null ) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const newSuggestions = tagSuggestions.length ? tagSuggestions : suggestionsFromPicks( count ); |
|
|
return newSuggestions; |
|
|
} |
|
|
|
|
|
const SuggestionsProvider = ( Element, count = 3 ) => |
|
|
class extends Component { |
|
|
|
|
|
|
|
|
memoizedSuggestions = null; |
|
|
getFirstSuggestions = ( state ) => |
|
|
this.memoizedSuggestions |
|
|
? this.memoizedSuggestions |
|
|
: ( this.memoizedSuggestions = getSuggestions( |
|
|
count, |
|
|
getReaderFollowedTags( state ), |
|
|
this.props.trendingTags |
|
|
) ); |
|
|
|
|
|
componentWillUnmount() { |
|
|
|
|
|
this.memoizedSuggestions = null; |
|
|
} |
|
|
|
|
|
EnhancedComponent = connect( ( state ) => ( { |
|
|
suggestions: this.getFirstSuggestions( state ), |
|
|
} ) )( Element ); |
|
|
|
|
|
render() { |
|
|
const EnhancedComponent = this.EnhancedComponent; |
|
|
return <EnhancedComponent { ...this.props } />; |
|
|
} |
|
|
}; |
|
|
|
|
|
export default SuggestionsProvider; |
|
|
|