Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 93e6a8d

Browse files
committed
FIX: Search discoveries not working when no regular results
1 parent da2b083 commit 93e6a8d

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

assets/javascripts/discourse/components/ai-search-discoveries.gjs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import DButton from "discourse/components/d-button";
1111
import concatClass from "discourse/helpers/concat-class";
1212
import { ajax } from "discourse/lib/ajax";
1313
import { bind } from "discourse/lib/decorators";
14+
import { withPluginApi } from "discourse/lib/plugin-api";
1415
import { i18n } from "discourse-i18n";
1516
import SmoothStreamer from "../lib/smooth-streamer";
1617
import 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
>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import AiSearchDiscoveriesTooltip from "../../components/ai-search-discoveries-t
88
export default class AiDiscobotDiscoveries extends Component {
99
static shouldRender(args, { siteSettings, currentUser }) {
1010
return (
11-
args.resultType.type === "topic" &&
1211
siteSettings.ai_bot_discover_persona &&
1312
currentUser?.can_use_ai_bot_discover_persona &&
1413
currentUser?.user_option?.ai_search_discoveries
@@ -28,7 +27,10 @@ export default class AiDiscobotDiscoveries extends Component {
2827
<AiSearchDiscoveriesTooltip />
2928
</h3>
3029

31-
<AiSearchDiscoveries @discoveryPreviewLength={{50}} />
30+
<AiSearchDiscoveries
31+
@searchTerm={{@outletArgs.searchTerm}}
32+
@discoveryPreviewLength={{50}}
33+
/>
3234

3335
<h3 class="ai-search-discoveries__regular-results-title">
3436
{{icon "bars-staggered"}}

0 commit comments

Comments
 (0)