@@ -11,6 +11,7 @@ import DButton from "discourse/components/d-button";
1111import concatClass from " discourse/helpers/concat-class" ;
1212import { ajax } from " discourse/lib/ajax" ;
1313import { bind } from " discourse/lib/decorators" ;
14+ import { withPluginApi } from " discourse/lib/plugin-api" ;
1415import { i18n } from " discourse-i18n" ;
1516import SmoothStreamer from " ../lib/smooth-streamer" ;
1617import AiBlinkingAnimation from " ./ai-blinking-animation" ;
@@ -24,6 +25,7 @@ export default class AiSearchDiscoveries extends Component {
2425 @service appEvents;
2526
2627 @tracked loadingDiscoveries = false ;
28+ @tracked searching = false ;
2729 @tracked hideDiscoveries = false ;
2830 @tracked fullDiscoveryToggled = false ;
2931 @tracked discoveryPreviewLength = this .args .discoveryPreviewLength || 150 ;
@@ -34,8 +36,6 @@ export default class AiSearchDiscoveries extends Component {
3436 );
3537
3638 discoveryTimeout = null ;
37- typingTimer = null ;
38- streamedTextLength = 0 ;
3939
4040 constructor () {
4141 super (... arguments );
@@ -55,6 +55,26 @@ export default class AiSearchDiscoveries extends Component {
5555 );
5656 }
5757
58+ @bind
59+ detectSearch () {
60+ withPluginApi ((api ) => {
61+ api .addSearchMenuOnKeyDownCallback ((searchMenu , event ) => {
62+ if (! searchMenu || this .searching ) {
63+ return ;
64+ }
65+
66+ if (this .discobotDiscoveries .lastQuery === this .query ) {
67+ return false ;
68+ }
69+
70+ if (event .key === " Enter" && this .query ) {
71+ this .triggerDiscovery ();
72+ }
73+ return true ;
74+ });
75+ });
76+ }
77+
5878 @bind
5979 async _updateDiscovery (update ) {
6080 if (this .query === update .query ) {
@@ -139,11 +159,14 @@ export default class AiSearchDiscoveries extends Component {
139159
140160 try {
141161 this .discobotDiscoveries .lastQuery = this .query ;
162+ this .searching = true ;
142163 await ajax (" /discourse-ai/ai-bot/discover" , {
143164 data: { query: this .query },
144165 });
145166 } catch {
146167 this .hideDiscoveries = true ;
168+ } finally {
169+ this .searching = false ;
147170 }
148171 }
149172
@@ -162,8 +185,9 @@ export default class AiSearchDiscoveries extends Component {
162185 <template >
163186 <div
164187 class =" ai-search-discoveries"
165- {{didInsert this . subscribe @ searchTerm}}
166- {{didUpdate this . subscribe @ searchTerm}}
188+ {{didInsert this . subscribe this . query}}
189+ {{didUpdate this . subscribe this . query}}
190+ {{didUpdate this . detectSearch this . query}}
167191 {{didInsert this . triggerDiscovery this . query}}
168192 {{willDestroy this . unsubscribe}}
169193 >
0 commit comments