2019-06-06 21:17:49 +00:00
|
|
|
export default function suggest (data) {
|
|
|
|
return input => {
|
|
|
|
const trimmed = input.trim()
|
|
|
|
const firstChar = trimmed[0]
|
|
|
|
console.log(`'${trimmed}'`, firstChar, firstChar === ':')
|
|
|
|
if (firstChar === ':' && data.emoji) {
|
|
|
|
return suggestEmoji(data.emoji)(trimmed)
|
|
|
|
}
|
|
|
|
if (firstChar === '@' && data.users) {
|
|
|
|
return suggestUsers(data.users)(trimmed)
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function suggestEmoji (emojis) {
|
|
|
|
return input => {
|
2019-06-08 13:23:58 +00:00
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
|
|
|
return emojis
|
|
|
|
.filter(({ displayText }) => displayText.toLowerCase().startsWith(noPrefix))
|
2019-06-06 21:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function suggestUsers (users) {
|
|
|
|
return input => {
|
2019-06-08 13:23:58 +00:00
|
|
|
const noPrefix = input.toLowerCase().substr(1)
|
2019-06-06 21:17:49 +00:00
|
|
|
return users.filter(
|
|
|
|
user =>
|
2019-06-08 13:23:58 +00:00
|
|
|
user.screen_name.toLowerCase().startsWith(noPrefix) ||
|
|
|
|
user.name.toLowerCase().startsWith(noPrefix)
|
|
|
|
/* eslint-disable camelcase */
|
2019-06-06 21:17:49 +00:00
|
|
|
).map(({ screen_name, name, profile_image_url_original }) => ({
|
2019-06-08 13:23:58 +00:00
|
|
|
displayText: screen_name,
|
|
|
|
detailText: name,
|
|
|
|
imageUrl: profile_image_url_original,
|
2019-06-06 21:17:49 +00:00
|
|
|
replacement: '@' + screen_name
|
|
|
|
}))
|
2019-06-08 13:23:58 +00:00
|
|
|
/* eslint-enable camelcase */
|
2019-06-06 21:17:49 +00:00
|
|
|
}
|
|
|
|
}
|