Add user search at

This commit is contained in:
Wyatt Benno 2019-07-18 03:40:02 +00:00 committed by Shpuld Shpludson
parent cc2e35f499
commit 5909baba7c
3 changed files with 22 additions and 5 deletions

View file

@ -1,20 +1,27 @@
import { debounce } from 'lodash'
/** /**
* suggest - generates a suggestor function to be used by emoji-input * suggest - generates a suggestor function to be used by emoji-input
* data: object providing source information for specific types of suggestions: * data: object providing source information for specific types of suggestions:
* data.emoji - optional, an array of all emoji available i.e. * data.emoji - optional, an array of all emoji available i.e.
* (state.instance.emoji + state.instance.customEmoji) * (state.instance.emoji + state.instance.customEmoji)
* data.users - optional, an array of all known users * data.users - optional, an array of all known users
* 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 * 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. * doesn't support user linking you can just provide only emoji.
*/ */
const debounceUserSearch = debounce((data, input) => {
data.updateUsersList(input)
}, 500, {leading: true, trailing: false})
export default data => input => { export default data => input => {
const firstChar = input[0] const firstChar = input[0]
if (firstChar === ':' && data.emoji) { if (firstChar === ':' && data.emoji) {
return suggestEmoji(data.emoji)(input) return suggestEmoji(data.emoji)(input)
} }
if (firstChar === '@' && data.users) { if (firstChar === '@' && data.users) {
return suggestUsers(data.users)(input) return suggestUsers(data)(input)
} }
return [] return []
} }
@ -38,9 +45,11 @@ export const suggestEmoji = emojis => input => {
}) })
} }
export const suggestUsers = users => input => { export const suggestUsers = data => input => {
const noPrefix = input.toLowerCase().substr(1) const noPrefix = input.toLowerCase().substr(1)
return users.filter( const users = data.users
const newUsers = users.filter(
user => user =>
user.screen_name.toLowerCase().startsWith(noPrefix) || user.screen_name.toLowerCase().startsWith(noPrefix) ||
user.name.toLowerCase().startsWith(noPrefix) user.name.toLowerCase().startsWith(noPrefix)
@ -75,5 +84,11 @@ export const suggestUsers = users => input => {
imageUrl: profile_image_url_original, imageUrl: profile_image_url_original,
replacement: '@' + screen_name + ' ' replacement: '@' + screen_name + ' '
})) }))
// BE search users if there are no matches
if (newUsers.length === 0 && data.updateUsersList) {
debounceUserSearch(data, noPrefix)
}
return newUsers
/* eslint-enable camelcase */ /* eslint-enable camelcase */
} }

View file

@ -104,7 +104,8 @@ const PostStatusForm = {
...this.$store.state.instance.emoji, ...this.$store.state.instance.emoji,
...this.$store.state.instance.customEmoji ...this.$store.state.instance.customEmoji
], ],
users: this.$store.state.users.users users: this.$store.state.users.users,
updateUsersList: (input) => this.$store.dispatch('searchUsers', input)
}) })
}, },
emojiSuggestor () { emojiSuggestor () {

View file

@ -91,7 +91,8 @@ const UserSettings = {
...this.$store.state.instance.emoji, ...this.$store.state.instance.emoji,
...this.$store.state.instance.customEmoji ...this.$store.state.instance.customEmoji
], ],
users: this.$store.state.users.users users: this.$store.state.users.users,
updateUsersList: (input) => this.$store.dispatch('searchUsers', input)
}) })
}, },
emojiSuggestor () { emojiSuggestor () {