From 057da6d0b3dc94972bdfe72528dab6b1335d6357 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Wed, 9 Oct 2019 16:37:51 +0200 Subject: [PATCH] Handle error case when autocompleting channels (#3917) --- .../suggestion/channel_mention_provider.jsx | 99 ++++++++++--------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/components/suggestion/channel_mention_provider.jsx b/components/suggestion/channel_mention_provider.jsx index c8c498677878..b29d06788f5f 100644 --- a/components/suggestion/channel_mention_provider.jsx +++ b/components/suggestion/channel_mention_provider.jsx @@ -155,65 +155,70 @@ export default class ChannelMentionProvider extends Provider { matchedPretext: captured[1], }); - this.autocompleteChannels( - prefix, - (channels) => { - const myMembers = getMyChannelMemberships(store.getState()); - if (this.shouldCancelDispatch(prefix)) { - return; - } + const handleChannels = (channels, withError) => { + const myMembers = getMyChannelMemberships(store.getState()); + if (this.shouldCancelDispatch(prefix)) { + return; + } - if (channels.length === 0) { - this.lastPrefixWithNoResults = prefix; - } + if (channels.length === 0 && !withError) { + this.lastPrefixWithNoResults = prefix; + } - // Wrap channels in an outer object to avoid overwriting the 'type' property. - const wrappedMoreChannels = []; - const moreChannels = []; - channels.forEach((item) => { - if (item.delete_at > 0 && !myMembers[item.id]) { - return; - } - if (getChannel(store.getState(), item.id)) { - if (!wrappedChannelIds[item.id]) { - wrappedChannelIds[item.id] = true; - wrappedChannels.push({ - type: Constants.MENTION_CHANNELS, - channel: item, - }); - } - return; + // Wrap channels in an outer object to avoid overwriting the 'type' property. + const wrappedMoreChannels = []; + const moreChannels = []; + channels.forEach((item) => { + if (item.delete_at > 0 && !myMembers[item.id]) { + return; + } + if (getChannel(store.getState(), item.id)) { + if (!wrappedChannelIds[item.id]) { + wrappedChannelIds[item.id] = true; + wrappedChannels.push({ + type: Constants.MENTION_CHANNELS, + channel: item, + }); } + return; + } - wrappedMoreChannels.push({ - type: Constants.MENTION_MORE_CHANNELS, - channel: item, - }); - - moreChannels.push(item); + wrappedMoreChannels.push({ + type: Constants.MENTION_MORE_CHANNELS, + channel: item, }); - wrappedChannels = wrappedChannels.sort((a, b) => { - // - // MM-12677 When this is migrated this needs to be fixed to pull the user's locale - // - return sortChannelsByTypeAndDisplayName('en', a.channel, b.channel); - }); - const wrapped = wrappedChannels.concat(wrappedMoreChannels); - const mentions = wrapped.map((item) => '~' + item.channel.name); + moreChannels.push(item); + }); + wrappedChannels = wrappedChannels.sort((a, b) => { + // + // MM-12677 When this is migrated this needs to be fixed to pull the user's locale + // + return sortChannelsByTypeAndDisplayName('en', a.channel, b.channel); + }); + const wrapped = wrappedChannels.concat(wrappedMoreChannels); + const mentions = wrapped.map((item) => '~' + item.channel.name); + + if (moreChannels.length > 0) { store.dispatch({ type: ChannelTypes.RECEIVED_CHANNELS, data: moreChannels, }); - - resultCallback({ - matchedPretext: captured[1], - terms: mentions, - items: wrapped, - component: ChannelMentionSuggestion, - }); } + + resultCallback({ + matchedPretext: captured[1], + terms: mentions, + items: wrapped, + component: ChannelMentionSuggestion, + }); + }; + + this.autocompleteChannels( + prefix, + (channels) => handleChannels(channels, false), + () => handleChannels([], true), ); return true;