yandere_fe/src/components/emoji_input/suggestor.js

131 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
* 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
*
* 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
2020-11-17 07:46:26 -08:00
export default data => {
const emojiCurry = suggestEmoji(data.emoji)
2020-11-18 08:43:24 -08:00
const usersCurry = data.store && suggestUsers(data.store)
2020-11-17 07:46:26 -08:00
return input => {
const firstChar = input[0]
if (firstChar === ':' && data.emoji) {
return emojiCurry(input)
}
2020-11-18 08:43:24 -08:00
if (firstChar === '@' && usersCurry) {
2020-11-17 07:46:26 -08:00
return usersCurry(input)
}
return []
2019-06-18 12:13:03 -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
.filter(({ displayText }) => displayText.toLowerCase().match(noPrefix))
2019-06-18 11:30:35 -07:00
.sort((a, b) => {
let aScore = 0
let bScore = 0
// 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
aScore += a.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
bScore += b.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
// 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-18 11:30:35 -07:00
return bScore - aScore + alphabetically
})
}
2020-11-18 08:43:24 -08:00
export const suggestUsers = ({ dispatch, state }) => {
2020-11-19 02:35:21 -08:00
// Keep some persistent values in closure, most importantly for the
// custom debounce to work. Lodash debounce does not return a promise.
2020-11-17 07:46:26 -08:00
let suggestions = []
let previousQuery = ''
let timeout = null
2020-11-18 08:43:24 -08:00
let cancelUserSearch = null
2020-11-17 07:46:26 -08:00
2020-11-18 08:43:24 -08:00
const userSearch = (query) => dispatch('searchUsers', { query })
2020-11-17 07:46:26 -08:00
const debounceUserSearch = (query) => {
2020-11-18 08:43:24 -08:00
cancelUserSearch && cancelUserSearch()
2020-11-17 07:46:26 -08:00
return new Promise((resolve, reject) => {
timeout = setTimeout(() => {
userSearch(query).then(resolve).catch(reject)
}, 300)
2020-11-18 08:43:24 -08:00
cancelUserSearch = () => {
clearTimeout(timeout)
resolve([])
}
2020-11-17 07:46:26 -08:00
})
}
return async input => {
const noPrefix = input.toLowerCase().substr(1)
if (previousQuery === noPrefix) return suggestions
suggestions = []
previousQuery = noPrefix
2020-11-18 08:43:24 -08:00
// Fetch more and wait, don't fetch if there's the 2nd @ because
// the backend user search can't deal with it.
// Reference semantics make it so that we get the updated data after
// the await.
if (!noPrefix.includes('@')) {
await debounceUserSearch(noPrefix)
}
2020-11-17 07:46:26 -08:00
2020-11-18 08:43:24 -08:00
const newSuggestions = state.users.users.filter(
2020-11-17 07:46:26 -08:00
user =>
user.screen_name.toLowerCase().startsWith(noPrefix) ||
user.name.toLowerCase().startsWith(noPrefix)
).slice(0, 20).sort((a, b) => {
let aScore = 0
let bScore = 0
// 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
// Matches on name takes second priority
aScore += a.name.toLowerCase().startsWith(noPrefix) ? 1 : 0
bScore += b.name.toLowerCase().startsWith(noPrefix) ? 1 : 0
const diff = (bScore - aScore) * 10
// Then sort alphabetically
const nameAlphabetically = a.name > b.name ? 1 : -1
const screenNameAlphabetically = a.screen_name > b.screen_name ? 1 : -1
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 + ' '
}))
2020-11-18 08:43:24 -08:00
/* eslint-enable camelcase */
2020-11-17 07:46:26 -08:00
2020-11-18 08:43:24 -08:00
suggestions = newSuggestions || []
2020-11-17 07:46:26 -08:00
return suggestions
2019-07-17 20:40:02 -07:00
}
}