[LS] [AI Context] DuoChat: context items loading state does not handle concurrent requests
When searching for context items within Duo Chat, there is a problem where the loading state can finish, but then more results continue to load in.
The vue app sets contextMenuIsLoading to true when a search is triggered, and then back to false when search results are returned. The search is debounced, but can trigger multiple requests as the user is typing. Currently the first of these requests to return results will set contextMenuIsLoading to false, despite other requests still being in flight. This results in a jarring experience where the search results can flicker/change as you're reading them.
This is not so noticeable when selecting file context items, since they are searched locally and resolve very fast. But it can be very noticeable when searching issue or merge_request context items, which use API calls to return search results.
Expected behaviour
UI stays in loading state until final results are displayed.
Actual behaviour
First set of results is displayed, loading state is removed, further results pop in later.
Suggested fix
- cancel in-flight requests as new searches are run, so only one set of results ever returns
or
- track how many searches we have triggered, and only stop loading when they have all returned