Skip to content

Commit

Permalink
Prevent outdated results in bot search modal (keybase#22760)
Browse files Browse the repository at this point in the history
* feat: first pass bot search results as map

* chore: add comment to botsearchresults change

* fix: add undefined to map type and remove check
  • Loading branch information
cori-hudson authored Feb 26, 2020
1 parent ef91214 commit a83240f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 8 deletions.
7 changes: 5 additions & 2 deletions shared/actions/bots-gen.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shared/actions/bots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const searchFeaturedAndUsers = async (action: BotsGen.SearchFeaturedAndUsersPayl
return
}
return BotsGen.createSetSearchFeaturedAndUsersResults({
query,
results: {
bots: botRes?.bots ?? [],
users: (userRes ?? []).reduce<Array<string>>((l, r) => {
Expand Down
1 change: 1 addition & 0 deletions shared/actions/json/bots.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"setSearchFeaturedAndUsersResults": {
"_description": "Set results of a search",
"query": "string",
"results?": "Types.BotSearchResults"
}
}
Expand Down
13 changes: 9 additions & 4 deletions shared/chat/conversation/bot/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const SearchBotPopup = (props: Props) => {
if (query.length > 0) {
dispatch(BotsGen.createSearchFeaturedAndUsers({query}))
} else {
dispatch(BotsGen.createSetSearchFeaturedAndUsersResults({results: undefined}))
dispatch(BotsGen.createSetSearchFeaturedAndUsersResults({query, results: undefined}))
}
}, 200)
const onSelect = (username: string) => {
Expand All @@ -66,12 +66,14 @@ const SearchBotPopup = (props: Props) => {
}
// lifecycle
React.useEffect(() => {
dispatch(BotsGen.createSetSearchFeaturedAndUsersResults({results: undefined}))
dispatch(BotsGen.createSetSearchFeaturedAndUsersResults({query: '', results: undefined}))
dispatch(BotsGen.createGetFeaturedBots({}))
}, [dispatch])

const botData: Array<RPCTypes.FeaturedBot | string> =
lastQuery.length > 0 ? results?.bots.slice() ?? [] : Constants.getFeaturedSorted(featuredBotsMap)
lastQuery.length > 0
? results?.get(lastQuery)?.bots.slice() ?? []
: Constants.getFeaturedSorted(featuredBotsMap)
if (!botData.length && !waiting) {
botData.push(resultEmptyPlaceholder)
}
Expand All @@ -93,7 +95,10 @@ const SearchBotPopup = (props: Props) => {
}
const userData = !lastQuery.length
? [userEmptyPlaceholder]
: results?.users.filter(u => !featuredBotsMap.get(u)).slice(0, 3) ?? []
: results
.get(lastQuery)
?.users.filter(u => !featuredBotsMap.get(u))
.slice(0, 3) ?? []
if (!userData.length && !waiting) {
userData.push(resultEmptyPlaceholder)
}
Expand Down
1 change: 1 addition & 0 deletions shared/constants/chat2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const makeState = (): Types.State => ({
blockButtonsMap: new Map(),
botCommandsUpdateStatusMap: new Map(),
botPublicCommands: new Map(),
botSearchResults: new Map(),
botSettings: new Map(),
botTeamRoleInConvMap: new Map(),
channelSearchText: '',
Expand Down
2 changes: 1 addition & 1 deletion shared/constants/types/chat2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export type State = {
RPCChatTypes.UIBotCommandsUpdateStatusTyp
>
readonly botPublicCommands: Map<string, BotPublicCommands>
readonly botSearchResults?: BotSearchResults
readonly botSearchResults: Map<string, BotSearchResults | undefined> // Keyed so that we never show results that don't match the user's input (e.g. outdated results)
readonly botSettings: Map<Common.ConversationIDKey, Map<string, RPCTypes.TeamBotSettings>>
readonly botTeamRoleInConvMap: Map<Common.ConversationIDKey, Map<string, Team.TeamRoleType | null>>
readonly channelSearchText: string
Expand Down
2 changes: 1 addition & 1 deletion shared/reducers/chat2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const botActions: Container.ActionHandler<Actions, Types.State> = {
draftState.featuredBotsLoaded = loaded
},
[BotsGen.setSearchFeaturedAndUsersResults]: (draftState, action) => {
draftState.botSearchResults = action.payload.results
draftState.botSearchResults.set(action.payload.query, action.payload.results)
},
}

Expand Down

0 comments on commit a83240f

Please sign in to comment.