Skip to content

Commit

Permalink
perf: only return translations, transliteration, citations if setting…
Browse files Browse the repository at this point in the history
…s are enabled
  • Loading branch information
Harjot1Singh committed May 15, 2020
1 parent 031a072 commit 2ebc5b5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 47 deletions.
78 changes: 47 additions & 31 deletions app/frontend/src/Controller/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ const highlightMatches = gurmukhi => ( value, input, mode ) => {
* Displays results.
*/
const Search = ( { updateFocus, register, focused } ) => {
const { local: {
sources,
search: { showResultCitations, resultTransliterationLanguage, resultTranslationLanguage },
} = {} } = useContext( SettingsContext )

// Set the initial search query from URL
const history = useHistory()
const { search } = useLocation()
Expand Down Expand Up @@ -121,8 +126,13 @@ const Search = ( { updateFocus, register, focused } ) => {
// Search if enough letters
const doSearch = searchValue.length >= MIN_SEARCH_CHARS

if ( doSearch ) controller.search( searchValue, searchType )
else setResults( [] )
if ( doSearch ) {
controller.search( searchValue, searchType, {
translations: !!resultTranslationLanguage,
transliterations: !!resultTransliterationLanguage,
citations: !!showResultCitations,
} )
} else setResults( [] )

inputValue.current = searchValue
setAnchor( anchor )
Expand All @@ -132,14 +142,16 @@ const Search = ( { updateFocus, register, focused } ) => {
...getUrlState( search ),
query: value,
} )}` } )
}, [ history, search ] )
}, [
history,
search,
resultTranslationLanguage,
resultTransliterationLanguage,
showResultCitations,
] )

const writers = useContext( WritersContext )
const recommendedSources = useContext( RecommendedSourcesContext )
const { local: {
sources,
search: { showResultCitations, resultTransliterationLanguage, resultTranslationLanguage },
} = {} } = useContext( SettingsContext )

/**
* Renders a single result, highlighting the match.
Expand All @@ -165,18 +177,14 @@ const Search = ( { updateFocus, register, focused } ) => {
translations,
transliterations,
} ) => {
const { section, writerId } = shabad
const { nameEnglish: raag } = section
const { pageNameEnglish: pageName } = recommendedSources[ sourceId ]
const { nameEnglish: writerName } = writers[ writerId ]

const transliteration = resultTransliterationLanguage && getTransliteration(
const transliteration = resultTransliterationLanguage && transliterations && getTransliteration(
{ transliterations },
resultTransliterationLanguage,
)
const translation = resultTranslationLanguage && getTranslation( {

const translation = resultTranslationLanguage && translations && getTranslation( {
line: { translations },
shabad,
shabad: { sourceId },
recommendedSources,
sources,
languageId: resultTranslationLanguage,
Expand All @@ -197,6 +205,13 @@ const Search = ( { updateFocus, register, focused } ) => {
// Send the shabad id and line id to the server on click
const onClick = () => controller.shabad( { shabadId, lineId } )

// Helper render functions for citation
const showCitation = showResultCitations && shabad && shabad.section
const getEnglish = ( { nameEnglish } ) => nameEnglish
const getWriterName = () => getEnglish( writers[ shabad.writerId ] )
const getSection = () => getEnglish( shabad.section )
const getPageName = () => recommendedSources[ shabad.sourceId ].pageNameEnglish

return (
<ListItem className={classNames( { focused } )} key={lineId} onClick={onClick} ref={ref}>
<div className="result">
Expand Down Expand Up @@ -224,22 +239,17 @@ const Search = ( { updateFocus, register, focused } ) => {

</span>

{showResultCitations
&& (
<span className="citation">
<span className="author">
{`(${writerName},`}
{showCitation && (
<span className="citation">
(
{[
getWriterName(),
getSection(),
SOURCE_ABBREVIATIONS[ sourceId ],
`${getPageName()} ${sourcePage}`,
].reduce( ( prev, curr ) => [ prev, ', ', curr ] )}
)
</span>
<span className="section">
{`"${raag}",`}
</span>
<span className="section">
{`${SOURCE_ABBREVIATIONS[ sourceId ]},`}
</span>
<span className="page">
{`${pageName} ${sourcePage})`}
</span>
</span>
)}

</div>
Expand Down Expand Up @@ -279,7 +289,13 @@ const Search = ( { updateFocus, register, focused } ) => {

useEffect( () => {
if ( inputValue.current ) onChange( { target: { value: `${anchor || ''}${inputValue.current}` } } )
}, [ onChange, anchor ] )
}, [
onChange,
anchor,
resultTransliterationLanguage,
resultTranslationLanguage,
showResultCitations,
] )

useEffect( () => { highlightSearch() }, [] )

Expand Down
3 changes: 2 additions & 1 deletion app/frontend/src/lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ class Controller extends EventEmitter {
* Convenience method for searching.
* @param query The first letters to search with.
* @param type The type of search (first-letter/full-word).
* @param options Additional options to pass.
*/
search = ( query, type ) => this.sendJSON( `search:${type}`, toAscii( query ) )
search = ( query, type, options = {} ) => this.sendJSON( `search:${type}`, { ...options, query: toAscii( query ) } )

/**
* Convenience method for setting the line.
Expand Down
33 changes: 19 additions & 14 deletions app/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,38 @@ import { Lines, Shabads, Banis, Sources, Languages, Writers } from '@shabados/da

import { MAX_RESULTS } from './consts'

/**
* Decorates a query function with the ability to accept modifier options.
* @param {*} queryFn A query function that returns a knex instance.
*/
const withSearchOptions = queryFn => ( query, options = {} ) => [
[ options.transliterations, model => model.withTransliterations() ],
[ options.translations, model => model.eager( 'shabad' ).withTranslations() ],
[ options.citations, model => model.eager( 'shabad.section' ) ],
].reduce(
( model, [ option, modifier ] ) => ( option ? modifier( model ) : model ),
queryFn( query ).limit( MAX_RESULTS ),
)

/**
* Queries the database for all lines with the first letters of each word.
* @param {string} letters The letters to search for.
* @async
* @returns {Array} A list of lines with the provided first letters of each word.
*/
export const firstLetterSearch = letters => Lines
.query()
.eager( 'shabad.section' )
.withTranslations()
.withTransliterations()
.limit( MAX_RESULTS )
.firstLetters( letters )
export const firstLetterSearch = withSearchOptions(
letters => Lines.query().firstLetters( letters ),
)

/**
* Queries the database for all lines, containing the full word.
* @param {string} words The words to search for.
* @async
* @returns {Array} A list of lines containing the full word.
*/
export const fullWordSearch = words => Lines
.query()
.eager( 'shabad.section' )
.withTranslations()
.withTransliterations()
.limit( MAX_RESULTS )
.fullWord( words )
export const fullWordSearch = withSearchOptions(
words => Lines.query().fullWord( words ),
)

/**
* Gets the Shabad of given `shabadId`, along with all the lines.
Expand Down
2 changes: 1 addition & 1 deletion app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function main() {

// Register searches on the socket instance
searches.forEach(
( [ name, searchFn ] ) => socket.on( `search:${name}`, async ( client, query ) => client.sendJSON( 'results', await searchFn( query ) ) ),
( [ name, searchFn ] ) => socket.on( `search:${name}`, async ( client, { query, ...options } ) => client.sendJSON( 'results', await searchFn( query, options ) ) ),
)

// Register all action handlers on the socket instance
Expand Down

0 comments on commit 2ebc5b5

Please sign in to comment.