2019-06-07 07:17:49 +10:00
|
|
|
export default function suggest (data) {
|
|
|
|
return input => {
|
2019-06-10 03:41:12 +10:00
|
|
|
const firstChar = input[0]
|
2019-06-07 07:17:49 +10:00
|
|
|
if (firstChar === ':' && data.emoji) {
|
2019-06-10 03:41:12 +10:00
|
|
|
return suggestEmoji(data.emoji)(input)
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
if (firstChar === '@' && data.users) {
|
2019-06-10 03:41:12 +10:00
|
|
|
return suggestUsers(data.users)(input)
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function suggestEmoji (emojis) {
|
|
|
|
return input => {
|
2019-06-08 23:23:58 +10:00
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
|
|
|
return emojis
|
|
|
|
.filter(({ displayText }) => displayText.toLowerCase().startsWith(noPrefix))
|
2019-06-10 03:41:12 +10:00
|
|
|
.sort((a, b) => {
|
|
|
|
let aScore = 0
|
|
|
|
let bScore = 0
|
|
|
|
|
|
|
|
// Make custom emojis a priority
|
|
|
|
aScore += Number(!!a.imageUrl) * 10
|
|
|
|
bScore += Number(!!b.imageUrl) * 10
|
|
|
|
|
|
|
|
// Sort alphabetically
|
|
|
|
const alphabetically = a.displayText > b.displayText ? 1 : -1
|
|
|
|
|
|
|
|
return bScore - aScore + alphabetically
|
|
|
|
})
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function suggestUsers (users) {
|
|
|
|
return input => {
|
2019-06-08 23:23:58 +10:00
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
2019-06-07 07:17:49 +10:00
|
|
|
return users.filter(
|
|
|
|
user =>
|
2019-06-08 23:23:58 +10:00
|
|
|
user.screen_name.toLowerCase().startsWith(noPrefix) ||
|
|
|
|
user.name.toLowerCase().startsWith(noPrefix)
|
|
|
|
/* eslint-disable camelcase */
|
2019-06-09 00:15:48 +10:00
|
|
|
).slice(0, 20).sort((a, b) => {
|
|
|
|
let aScore = 0
|
|
|
|
let bScore = 0
|
|
|
|
|
2019-06-10 03:41:12 +10:00
|
|
|
// Matches on screen name (i.e. user@instance) makes a priority
|
2019-06-09 00:15:48 +10:00
|
|
|
aScore += a.screen_name.toLowerCase().startsWith(noPrefix) * 2
|
|
|
|
bScore += b.screen_name.toLowerCase().startsWith(noPrefix) * 2
|
2019-06-10 03:41:12 +10:00
|
|
|
|
|
|
|
// Matches on name takes second priority
|
|
|
|
aScore += a.name.toLowerCase().startsWith(noPrefix)
|
2019-06-09 00:15:48 +10:00
|
|
|
bScore += b.name.toLowerCase().startsWith(noPrefix)
|
|
|
|
|
|
|
|
const diff = bScore * 10 - aScore * 10
|
2019-06-10 03:41:12 +10:00
|
|
|
|
|
|
|
// Then sort alphabetically
|
2019-06-09 00:15:48 +10:00
|
|
|
const nameAlphabetically = a.name > b.name ? 1 : -1
|
|
|
|
const screenNameAlphabetically = a.screen_name > b.screen_name ? 1 : -1
|
|
|
|
|
|
|
|
return diff + nameAlphabetically + screenNameAlphabetically
|
|
|
|
}).map(({ screen_name, name, profile_image_url_original }) => ({
|
2019-06-08 23:23:58 +10:00
|
|
|
displayText: screen_name,
|
|
|
|
detailText: name,
|
|
|
|
imageUrl: profile_image_url_original,
|
2019-06-07 07:17:49 +10:00
|
|
|
replacement: '@' + screen_name
|
|
|
|
}))
|
2019-06-08 23:23:58 +10:00
|
|
|
/* eslint-enable camelcase */
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
}
|