2020-06-18 02:29:13 -07:00
|
|
|
import debounce from 'lodash/debounce'
|
2019-06-11 11:18:09 -07:00
|
|
|
/**
|
|
|
|
* suggest - generates a suggestor function to be used by emoji-input
|
|
|
|
* data: object providing source information for specific types of suggestions:
|
|
|
|
* data.emoji - optional, an array of all emoji available i.e.
|
|
|
|
* (state.instance.emoji + state.instance.customEmoji)
|
|
|
|
* data.users - optional, an array of all known users
|
2019-07-17 20:40:02 -07:00
|
|
|
* updateUsersList - optional, a function to search and append to users
|
2019-06-11 11:18:09 -07:00
|
|
|
*
|
|
|
|
* Depending on data present one or both (or none) can be present, so if field
|
|
|
|
* doesn't support user linking you can just provide only emoji.
|
|
|
|
*/
|
2019-07-17 20:40:02 -07:00
|
|
|
|
|
|
|
const debounceUserSearch = debounce((data, input) => {
|
|
|
|
data.updateUsersList(input)
|
2020-06-18 02:29:13 -07:00
|
|
|
}, 500)
|
2019-07-17 20:40:02 -07:00
|
|
|
|
2019-06-18 12:13:03 -07:00
|
|
|
export default data => input => {
|
|
|
|
const firstChar = input[0]
|
|
|
|
if (firstChar === ':' && data.emoji) {
|
|
|
|
return suggestEmoji(data.emoji)(input)
|
2019-06-06 14:17:49 -07:00
|
|
|
}
|
2019-06-18 12:13:03 -07:00
|
|
|
if (firstChar === '@' && data.users) {
|
2019-07-17 20:40:02 -07:00
|
|
|
return suggestUsers(data)(input)
|
2019-06-18 12:13:03 -07:00
|
|
|
}
|
|
|
|
return []
|
2019-06-06 14:17:49 -07:00
|
|
|
}
|
|
|
|
|
2019-06-18 12:13:03 -07:00
|
|
|
export const suggestEmoji = emojis => input => {
|
2019-06-18 11:30:35 -07:00
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
|
|
|
return emojis
|
2020-02-09 14:25:24 -08:00
|
|
|
.filter(({ displayText }) => displayText.toLowerCase().match(noPrefix))
|
2019-06-18 11:30:35 -07:00
|
|
|
.sort((a, b) => {
|
|
|
|
let aScore = 0
|
|
|
|
let bScore = 0
|
2019-06-09 10:41:12 -07:00
|
|
|
|
2020-02-10 06:32:07 -08:00
|
|
|
// An exact match always wins
|
|
|
|
aScore += a.displayText.toLowerCase() === noPrefix ? 200 : 0
|
|
|
|
bScore += b.displayText.toLowerCase() === noPrefix ? 200 : 0
|
|
|
|
|
|
|
|
// Prioritize custom emoji a lot
|
|
|
|
aScore += a.imageUrl ? 100 : 0
|
|
|
|
bScore += b.imageUrl ? 100 : 0
|
|
|
|
|
|
|
|
// Prioritize prefix matches somewhat
|
2020-02-09 14:25:24 -08:00
|
|
|
aScore += a.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
|
|
|
|
bScore += b.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
|
2019-06-09 10:41:12 -07:00
|
|
|
|
2020-02-09 14:25:24 -08:00
|
|
|
// Sort by length
|
|
|
|
aScore -= a.displayText.length
|
|
|
|
bScore -= b.displayText.length
|
|
|
|
|
|
|
|
// Break ties alphabetically
|
|
|
|
const alphabetically = a.displayText > b.displayText ? 0.5 : -0.5
|
2019-06-09 10:41:12 -07:00
|
|
|
|
2019-06-18 11:30:35 -07:00
|
|
|
return bScore - aScore + alphabetically
|
|
|
|
})
|
2019-06-06 14:17:49 -07:00
|
|
|
}
|
|
|
|
|
2019-07-17 20:40:02 -07:00
|
|
|
export const suggestUsers = data => input => {
|
2019-06-18 11:30:35 -07:00
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
2019-07-17 20:40:02 -07:00
|
|
|
const users = data.users
|
|
|
|
|
|
|
|
const newUsers = users.filter(
|
2019-06-18 11:30:35 -07:00
|
|
|
user =>
|
|
|
|
user.screen_name.toLowerCase().startsWith(noPrefix) ||
|
|
|
|
user.name.toLowerCase().startsWith(noPrefix)
|
2019-06-11 11:18:09 -07:00
|
|
|
|
2019-06-18 11:30:35 -07:00
|
|
|
/* taking only 20 results so that sorting is a bit cheaper, we display
|
|
|
|
* only 5 anyway. could be inaccurate, but we ideally we should query
|
|
|
|
* backend anyway
|
|
|
|
*/
|
|
|
|
).slice(0, 20).sort((a, b) => {
|
|
|
|
let aScore = 0
|
|
|
|
let bScore = 0
|
2019-06-08 07:15:48 -07:00
|
|
|
|
2019-06-18 11:30:35 -07:00
|
|
|
// Matches on screen name (i.e. user@instance) makes a priority
|
|
|
|
aScore += a.screen_name.toLowerCase().startsWith(noPrefix) ? 2 : 0
|
|
|
|
bScore += b.screen_name.toLowerCase().startsWith(noPrefix) ? 2 : 0
|
2019-06-09 10:41:12 -07:00
|
|
|
|
2019-06-18 11:30:35 -07:00
|
|
|
// Matches on name takes second priority
|
|
|
|
aScore += a.name.toLowerCase().startsWith(noPrefix) ? 1 : 0
|
|
|
|
bScore += b.name.toLowerCase().startsWith(noPrefix) ? 1 : 0
|
2019-06-08 07:15:48 -07:00
|
|
|
|
2019-06-18 11:30:35 -07:00
|
|
|
const diff = (bScore - aScore) * 10
|
2019-06-09 10:41:12 -07:00
|
|
|
|
2019-06-18 11:30:35 -07:00
|
|
|
// Then sort alphabetically
|
|
|
|
const nameAlphabetically = a.name > b.name ? 1 : -1
|
|
|
|
const screenNameAlphabetically = a.screen_name > b.screen_name ? 1 : -1
|
2019-06-08 07:15:48 -07:00
|
|
|
|
2019-06-18 11:30:35 -07:00
|
|
|
return diff + nameAlphabetically + screenNameAlphabetically
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
}).map(({ screen_name, name, profile_image_url_original }) => ({
|
|
|
|
displayText: screen_name,
|
|
|
|
detailText: name,
|
|
|
|
imageUrl: profile_image_url_original,
|
|
|
|
replacement: '@' + screen_name + ' '
|
|
|
|
}))
|
2019-07-17 20:40:02 -07:00
|
|
|
|
2020-06-18 02:29:13 -07:00
|
|
|
// BE search users to get more comprehensive results
|
|
|
|
if (data.updateUsersList) {
|
2019-07-17 20:40:02 -07:00
|
|
|
debounceUserSearch(data, noPrefix)
|
|
|
|
}
|
|
|
|
return newUsers
|
2019-06-18 11:30:35 -07:00
|
|
|
/* eslint-enable camelcase */
|
2019-06-06 14:17:49 -07:00
|
|
|
}
|