Skip to content

A platform for community discussion. Free, open, simple.

FIX: clicking links in discovery search should close quick search (#35660)

Keegan George2a0d46f2ddef
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs
index 871cb6442b9..4e623d34f50 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs
@@ -1,5 +1,6 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
+import { on } from "@ember/modifier";
import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
@@ -11,6 +12,7 @@ import concatClass from "discourse/helpers/concat-class";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bind } from "discourse/lib/decorators";
+import { wantsNewWindow } from "discourse/lib/intercept-click";
import DiscourseURL from "discourse/lib/url";
import Topic from "discourse/models/topic";
import { i18n } from "discourse-i18n";
@@ -143,6 +145,27 @@ export default class AiSearchDiscoveries extends Component {
this.fullDiscoveryToggled = !this.fullDiscoveryToggled;
}
+ @action
+ handleDiscoveryClick(event) {
+ const target = event.target;
+ const link = target.closest("a");
+
+ if (!link) {
+ return;
+ }
+
+ if (wantsNewWindow(event)) {
+ return;
+ }
+
+ event.preventDefault();
+ DiscourseURL.routeTo(link.href);
+
+ if (this.args.closeSearchMenu) {
+ this.args.closeSearchMenu();
+ }
+ }
+
@action
async continueConversation() {
const data = {
@@ -194,6 +217,7 @@ export default class AiSearchDiscoveries extends Component {
{{else if this.discobotDiscoveries.discoveryTimedOut}}
{{i18n "discourse_ai.discobot_discoveries.timed_out"}}
{{else}}
+ {{! template-lint-disable no-invalid-interactive }}
<article
class={{concatClass
"ai-search-discoveries__discovery"
@@ -201,6 +225,7 @@ export default class AiSearchDiscoveries extends Component {
(if this.discobotDiscoveries.isStreaming "streaming")
"streamable-content"
}}
+ {{on "click" this.handleDiscoveryClick}}
>
<CookText
@rawText={{this.discobotDiscoveries.streamedText}}
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-search-discoveries-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-search-discoveries-test.gjs
new file mode 100644
index 00000000000..3e756497a41
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-search-discoveries-test.gjs
@@ -0,0 +1,210 @@
+import Service from "@ember/service";
+import { click, render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import sinon from "sinon";
+import DiscourseURL from "discourse/lib/url";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender from "discourse/tests/helpers/create-pretender";
+import AiSearchDiscoveries from "discourse/plugins/discourse-ai/discourse/components/ai-search-discoveries";
+
+module("Integration | Component | ai-search-discoveries", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.siteSettings.ai_discover_enabled = true;
+ this.currentUser.can_use_ai_discover_persona = true;
+
+ this.closeSearchMenuCalled = false;
+ this.closeSearchMenu = () => {
+ this.closeSearchMenuCalled = true;
+ };
+
+ sinon.stub(DiscourseURL, "routeTo");
+ });
+
+ hooks.afterEach(function () {
+ sinon.restore();
+ });
+
+ test("clicking a link in discovery text closes search menu", async function (assert) {
+ const discovery = {
+ streamedText:
+ "Here is some discovery text with a [link](/t/some-topic/123).",
+ loadingDiscoveries: false,
+ isStreaming: false,
+ discoveryTimedOut: false,
+ };
+
+ this.owner.register(
+ "service:discobot-discoveries",
+ class extends Service {
+ streamedText = discovery.streamedText;
+ loadingDiscoveries = discovery.loadingDiscoveries;
+ isStreaming = discovery.isStreaming;
+ discoveryTimedOut = discovery.discoveryTimedOut;
+
+ triggerDiscovery() {}
+ onDiscoveryUpdate() {}
+ }
+ );
+
+ pretender.get("/discourse-ai/discoveries", () => [
+ 200,
+ { "Content-Type": "application/json" },
+ {},
+ ]);
+
+ await render(
+ <template>
+ <AiSearchDiscoveries
+ @searchTerm="test search"
+ @closeSearchMenu={{this.closeSearchMenu}}
+ />
+ </template>
+ );
+
+ assert
+ .dom(".ai-search-discoveries__discovery")
+ .exists("discovery content is rendered");
+
+ const link = document.querySelector(".ai-search-discoveries__discovery a");
+ assert.strictEqual(link?.tagName, "A", "link exists in discovery text");
+
+ await click(link);
+
+ assert.true(
+ this.closeSearchMenuCalled,
+ "closeSearchMenu was called after clicking link"
+ );
+ assert.true(
+ DiscourseURL.routeTo.calledOnce,
+ "DiscourseURL.routeTo was called"
+ );
+ assert.true(
+ DiscourseURL.routeTo.calledWith(sinon.match("/t/some-topic/123")),
+ "routed to correct URL"
+ );
+ });
+
+ test("clicking a link with ctrl/cmd does not close search menu", async function (assert) {
+ const discovery = {
+ streamedText:
+ "Here is some discovery text with a [link](/t/some-topic/123).",
+ loadingDiscoveries: false,
+ isStreaming: false,
+ discoveryTimedOut: false,
+ };
+
+ this.owner.register(
+ "service:discobot-discoveries",
+ class extends Service {
+ streamedText = discovery.streamedText;
+ loadingDiscoveries = discovery.loadingDiscoveries;
+ isStreaming = discovery.isStreaming;
+ discoveryTimedOut = discovery.discoveryTimedOut;
+
+ triggerDiscovery() {}
+ onDiscoveryUpdate() {}
+ }
+ );
+
+ pretender.get("/discourse-ai/discoveries", () => [
+ 200,
+ { "Content-Type": "application/json" },
+ {},
+ ]);
+
+ await render(
+ <template>
+ <AiSearchDiscoveries
+ @searchTerm="test search"
+ @closeSearchMenu={{this.closeSearchMenu}}
+ />
+ </template>
+ );
+
+ const link = document.querySelector(".ai-search-discoveries__discovery a");
+ assert.strictEqual(link?.tagName, "A", "link exists in discovery text");
+
+ const clickEvent = new MouseEvent("click", {
+ bubbles: true,
+ cancelable: true,
+ ctrlKey: true,
+ });
+ link.dispatchEvent(clickEvent);
+
+ assert.false(
+ this.closeSearchMenuCalled,
+ "closeSearchMenu was not called when ctrl+clicking"
+ );
+ assert.false(
+ DiscourseURL.routeTo.called,
+ "DiscourseURL.routeTo was not called for new window navigation"
+ );
+ });
+
+ test("clicking non-link content does not close search menu", async function (assert) {
+ const discovery = {
+ streamedText:
+ "Here is some discovery text with a [link](/t/some-topic/123) and plain text.",
+ loadingDiscoveries: false,
+ isStreaming: false,
+ discoveryTimedOut: false,
+ };
+
+ this.owner.register(
+ "service:discobot-discoveries",
+ class extends Service {
+ streamedText = discovery.streamedText;
+ loadingDiscoveries = discovery.loadingDiscoveries;
+ isStreaming = discovery.isStreaming;
+ discoveryTimedOut = discovery.discoveryTimedOut;
+
+ triggerDiscovery() {}
+ onDiscoveryUpdate() {}
+ }
+ );
+
+ pretender.get("/discourse-ai/discoveries", () => [
+ 200,
+ { "Content-Type": "application/json" },
+ {},
+ ]);
+
+ await render(
+ <template>
+ <AiSearchDiscoveries
+ @searchTerm="test search"
+ @closeSearchMenu={{this.closeSearchMenu}}
+ />
+ </template>
+ );
+
+ assert
+ .dom(".ai-search-discoveries__discovery")
+ .exists("discovery content is rendered");
+
+ const paragraph = document.querySelector(
+ ".ai-search-discoveries__discovery p"
+ );
+
+ if (paragraph) {
+ await click(paragraph);
+
+ assert.false(
+ this.closeSearchMenuCalled,
+ "closeSearchMenu was not called when clicking non-link content"
+ );
+ assert.false(
+ DiscourseURL.routeTo.called,
+ "DiscourseURL.routeTo was not called"
+ );
+ } else {
+ assert.strictEqual(
+ document.querySelector(".ai-search-discoveries__discovery")?.tagName,
+ "ARTICLE",
+ "article exists"
+ );
+ }
+ });
+});