From a3fb5cdc2116facc1ce71712af0aaf89009f1d68 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Thu, 27 Feb 2025 10:18:42 -0800 Subject: [PATCH] FIX: Subsequent results in full page search should re-trigger discovery (#1156) This update fixes an issue where subsequent results in full page search were not trigger Discobot discoveries to search for the new result. --- .../components/ai-search-discoveries.gjs | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/assets/javascripts/discourse/components/ai-search-discoveries.gjs b/assets/javascripts/discourse/components/ai-search-discoveries.gjs index fc9e16c9..c429c5ef 100644 --- a/assets/javascripts/discourse/components/ai-search-discoveries.gjs +++ b/assets/javascripts/discourse/components/ai-search-discoveries.gjs @@ -21,6 +21,7 @@ export default class AiSearchDiscoveries extends Component { @service search; @service messageBus; @service discobotDiscoveries; + @service appEvents; @tracked loadingDiscoveries = false; @tracked hideDiscoveries = false; @@ -34,6 +35,24 @@ export default class AiSearchDiscoveries extends Component { typingTimer = null; streamedTextLength = 0; + constructor() { + super(...arguments); + this.appEvents.on( + "full-page-search:trigger-search", + this, + this.triggerDiscovery + ); + } + + willDestroy() { + super.willDestroy(...arguments); + this.appEvents.off( + "full-page-search:trigger-search", + this, + this.triggerDiscovery + ); + } + typeCharacter() { if (this.streamedTextLength < this.discobotDiscoveries.discovery.length) { this.streamedText += this.discobotDiscoveries.discovery.charAt( @@ -48,11 +67,20 @@ export default class AiSearchDiscoveries extends Component { } onTextUpdate() { + this.cancelTypingTimer(); + this.typeCharacter(); + } + + cancelTypingTimer() { if (this.typingTimer) { cancel(this.typingTimer); } + } - this.typeCharacter(); + resetStreaming() { + this.cancelTypingTimer(); + this.streamedText = ""; + this.streamedTextLength = 0; } @bind @@ -77,10 +105,7 @@ export default class AiSearchDiscoveries extends Component { this.isStreaming = false; // Clear pending animations - if (this.typingTimer) { - cancel(this.typingTimer); - this.typingTimer = null; - } + this.cancelTypingTimer(); } else if (newText.length > this.discobotDiscoveries.discovery.length) { this.discobotDiscoveries.discovery = newText; this.isStreaming = true; @@ -148,6 +173,7 @@ export default class AiSearchDiscoveries extends Component { this.hideDiscoveries = false; return; } else { + this.resetStreaming(); this.discobotDiscoveries.resetDiscovery(); }