A platform for community discussion. Free, open, simple.
DEV: Modernize SearchResultEntry component (#44615)
Jarek Radosz28529a6d63f9
diff --git a/frontend/discourse/app/components/search-result-entry.gjs b/frontend/discourse/app/components/search-result-entry.gjsindex 516d02dbe60..20ab559015c 100644--- a/frontend/discourse/app/components/search-result-entry.gjs+++ b/frontend/discourse/app/components/search-result-entry.gjs@@ -1,15 +1,9 @@-/* eslint-disable ember/no-classic-components, ember/require-tagless-components */-import Component from "@ember/component";+import Component from "@glimmer/component";import { fn } from "@ember/helper";import { on } from "@ember/modifier";import { action } from "@ember/object";+import { service } from "@ember/service";import { trustHTML } from "@ember/template";-import {- attributeBindings,- classNameBindings,- classNames,- tagName,-} from "@ember-decorators/component";import HighlightSearch from "discourse/components/highlight-search";import PluginOutlet from "discourse/components/plugin-outlet";import TopicStatus from "discourse/components/topic-status";@@ -17,22 +11,26 @@ import TrackSelected from "discourse/components/track-selected";import lazyHash from "discourse/helpers/lazy-hash";import { wantsNewWindow } from "discourse/lib/intercept-click";import { logSearchLinkClick } from "discourse/lib/search";+import { applyValueTransformer } from "discourse/lib/transformer";import dAvatar from "discourse/ui-kit/helpers/d-avatar";import dCategoryLink from "discourse/ui-kit/helpers/d-category-link";+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";import dDiscourseTags from "discourse/ui-kit/helpers/d-discourse-tags";import dFormatDate from "discourse/ui-kit/helpers/d-format-date";import dIcon from "discourse/ui-kit/helpers/d-icon";-@tagName("div")-@classNames("fps-result")-@classNameBindings("bulkSelectEnabled")-@attributeBindings("role")export default class SearchResultEntry extends Component {- role = "listitem";+ @service siteSettings;get shouldShowPrivateMessageIcon() {// Only show PM icon if this is a PM AND we're not in a PM-only search- return this.post.topic.isPrivateMessage && !this.isPMOnly;+ return this.args.post.topic.isPrivateMessage && !this.args.isPMOnly;+ }++ get additionalClasses() {+ return applyValueTransformer("search-result-entry-class", [], {+ post: this.args.post,+ });}@action@@ -42,9 +40,9 @@ export default class SearchResultEntry extends Component {return;}- if (this.searchLogId && topicId) {+ if (this.args.searchLogId && topicId) {logSearchLinkClick({- searchLogId: this.searchLogId,+ searchLogId: this.args.searchLogId,searchResultId: topicId,searchResultType: "topic",});@@ -52,118 +50,131 @@ export default class SearchResultEntry extends Component {}<template>- <PluginOutlet- @name="search-results-topic-avatar-wrapper"- @outletArgs={{lazyHash post=this.post}}+ <div+ role="listitem"+ ...attributes+ class={{dConcatClass+ "fps-result"+ (if @bulkSelectEnabled "bulk-select-enabled")+ this.additionalClasses+ }}>- <div class="author">- <a data-user-card={{this.post.username}} href={{this.post.userPath}}>- {{dAvatar this.post imageSize="large"}}- </a>- </div>+ <PluginOutlet+ @name="search-results-topic-avatar-wrapper"+ @outletArgs={{lazyHash post=@post}}+ >+ <div class="author">+ <a data-user-card={{@post.username}} href={{@post.userPath}}>+ {{dAvatar @post imageSize="large"}}+ </a>+ </div>- </PluginOutlet>-- <div class="fps-topic" data-topic-id={{this.post.topic.id}}>- <div class="topic">-- {{#if this.bulkSelectEnabled}}- <TrackSelected- class="bulk-select"- @selectedId={{this.post}}- @selectedList={{this.selected}}- />- {{/if}}-- <a- aria-level="2"- class="search-link{{if this.post.topic.visited ' visited'}}"- href={{this.post.url}}- role="heading"- {{on "click" (fn this.logClick this.post.topic_id)}}- >- <TopicStatus- @disableActions={{true}}- @showPrivateMessageIcon={{this.shouldShowPrivateMessageIcon}}- @topic={{this.post.topic}}- />-- <span class="topic-title">- {{#if this.post.useTopicTitleHeadline}}- {{trustHTML this.post.topicTitleHeadline}}- {{else}}- <HighlightSearch @highlight={{this.highlightQuery}}>- {{trustHTML this.post.topic.fancyTitle}}- </HighlightSearch>- {{/if}}- </span>- <PluginOutlet- @name="search-results-topic-title-suffix"- @outletArgs={{lazyHash topic=this.post.topic}}- />- </a>-- <div class="search-category">- {{#if this.post.topic.category.parentCategory}}- {{dCategoryLink this.post.topic.category.parentCategory}}- {{/if}}- {{dCategoryLink this.post.topic.category hideParent=true}}- {{#if this.post.topic}}- {{dDiscourseTags this.post.topic}}+ </PluginOutlet>++ <div class="fps-topic" data-topic-id={{@post.topic.id}}>+ <div class="topic">++ {{#if @bulkSelectEnabled}}+ <TrackSelected+ class="bulk-select"+ @selectedId={{@post}}+ @selectedList={{@selected}}+ />{{/if}}- <span>++ <a+ aria-level="2"+ class="search-link{{if @post.topic.visited ' visited'}}"+ href={{@post.url}}+ role="heading"+ {{on "click" (fn this.logClick @post.topic_id)}}+ >+ <TopicStatus+ @disableActions={{true}}+ @showPrivateMessageIcon={{this.shouldShowPrivateMessageIcon}}+ @topic={{@post.topic}}+ />++ <span class="topic-title">+ {{#if @post.useTopicTitleHeadline}}+ {{trustHTML @post.topicTitleHeadline}}+ {{else}}+ <HighlightSearch @highlight={{@highlightQuery}}>+ {{trustHTML @post.topic.fancyTitle}}+ </HighlightSearch>+ {{/if}}+ </span><PluginOutlet- @connectorTagName="div"- @name="full-page-search-category"- @outletArgs={{lazyHash post=this.post}}+ @name="search-results-topic-title-suffix"+ @outletArgs={{lazyHash [email protected]}}/>- </span>- </div>- </div>+ </a>- <PluginOutlet- @name="search-result-entry-blurb-wrapper"- @outletArgs={{lazyHash- post=this.post- logClick=this.logClick- highlightQuery=this.highlightQuery- }}- >- <div class="blurb container">- <span class="date">- {{dFormatDate this.post.created_at format="tiny"}}- {{#if this.post.blurb}}- <span class="separator">-</span>+ <div class="search-category">+ {{#if @post.topic.category.parentCategory}}+ {{dCategoryLink @post.topic.category.parentCategory}}{{/if}}- </span>-- {{#if this.post.blurb}}- {{#if this.siteSettings.use_pg_headlines_for_excerpt}}- {{trustHTML this.post.blurb}}- {{else}}- <HighlightSearch @highlight={{this.highlightQuery}}>- {{trustHTML this.post.blurb}}- </HighlightSearch>+ {{dCategoryLink @post.topic.category hideParent=true}}+ {{#if @post.topic}}+ {{dDiscourseTags @post.topic}}{{/if}}- {{/if}}+ <span>+ <PluginOutlet+ @connectorTagName="div"+ @name="full-page-search-category"+ @outletArgs={{lazyHash post=@post}}+ />+ </span>+ </div></div>- </PluginOutlet>- <PluginOutlet- @name="search-result-entry-stats-wrapper"- @outletArgs={{lazyHash post=this.post}}- >- {{#if this.showLikeCount}}- {{#if this.post.like_count}}- <span class="like-count">- <span class="value">{{this.post.like_count}}</span>- {{dIcon "heart"}}+ <PluginOutlet+ @name="search-result-entry-blurb-wrapper"+ @outletArgs={{lazyHash+ post=@post+ logClick=this.logClick+ highlightQuery=@highlightQuery+ }}+ >+ <div class="blurb container">+ <span class="date">+ {{dFormatDate @post.created_at format="tiny"}}+ {{#if @post.blurb}}+ <span class="separator">-</span>+ {{/if}}</span>++ {{#if @post.blurb}}+ {{#if this.siteSettings.use_pg_headlines_for_excerpt}}+ {{trustHTML @post.blurb}}+ {{else}}+ <HighlightSearch @highlight={{@highlightQuery}}>+ {{trustHTML @post.blurb}}+ </HighlightSearch>+ {{/if}}+ {{/if}}+ </div>+ </PluginOutlet>++ <PluginOutlet+ @name="search-result-entry-stats-wrapper"+ @outletArgs={{lazyHash post=@post}}+ >+ {{#if @showLikeCount}}+ {{#if @post.like_count}}+ <span class="like-count">+ <span class="value">{{@post.like_count}}</span>+ {{dIcon "heart"}}+ </span>+ {{/if}}{{/if}}- {{/if}}- </PluginOutlet>- </div>+ </PluginOutlet>+ </div>- <PluginOutlet @name="after-search-result-entry" />+ <PluginOutlet+ @name="after-search-result-entry"+ @outletArgs={{lazyHash post=@post}}+ />+ </div></template>}diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex 54f8079a76f..dd5a2b63a31 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -149,6 +149,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["search-menu-recent-searches","search-menu-search-context-enabled","search-menu-search-shortcuts-enabled",+ "search-result-entry-class","sidebar-anonymous-default-categories","sidebar-panel","site-setting-allows-none",diff --git a/frontend/discourse/tests/integration/components/search-result-entry-test.gjs b/frontend/discourse/tests/integration/components/search-result-entry-test.gjsnew file mode 100644index 00000000000..63799b94ad5--- /dev/null+++ b/frontend/discourse/tests/integration/components/search-result-entry-test.gjs@@ -0,0 +1,74 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import SearchResultEntry from "discourse/components/search-result-entry";+import { withPluginApi } from "discourse/lib/plugin-api";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++function buildPost(owner, attrs = {}) {+ const store = owner.lookup("service:store");+ const topic = store.createRecord("topic", { id: 1, title: "A topic" });+ return store.createRecord("post", {+ id: 2,+ topic,+ topic_id: 1,+ username: "eviltrout",+ created_at: "2026-01-01T00:00:00Z",+ ...attrs,+ });+}++module("Integration | Component | SearchResultEntry", function (hooks) {+ setupRenderingTest(hooks);++ test("renders a list item for the result", async function (assert) {+ const post = buildPost(this.owner);++ await render(+ <template>+ <SearchResultEntry @bulkSelectEnabled={{true}} @post={{post}} />+ </template>+ );++ assert+ .dom(".fps-result")+ .hasAttribute("role", "listitem")+ .hasClass("bulk-select-enabled", "marks bulk selection");+ });++ test("passes the post to the after-search-result-entry outlet", async function (assert) {+ withPluginApi((api) => {+ api.renderInOutlet(+ "after-search-result-entry",+ <template>+ <span class="probe">{{@post.id}}</span>+ </template>+ );+ });+ const post = buildPost(this.owner);++ await render(<template><SearchResultEntry @post={{post}} /></template>);++ assert.dom(".fps-result .probe").hasText("2");+ });++ test("search-result-entry-class value transformer", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer(+ "search-result-entry-class",+ ({ value, context }) => {+ if (context.post.flagged) {+ value.push("flagged-result");+ }+ return value;+ }+ );+ });+ const post = buildPost(this.owner, { flagged: true });++ await render(<template><SearchResultEntry @post={{post}} /></template>);++ assert+ .dom(".fps-result")+ .hasClass("flagged-result", "adds the transformed classes");+ });+});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/after-search-result-entry/search-result-decoration.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/after-search-result-entry/search-result-decoration.gjsindex 76e530f2026..a8e3ecbd85a 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/after-search-result-entry/search-result-decoration.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/after-search-result-entry/search-result-decoration.gjs@@ -2,12 +2,14 @@ import dIcon from "discourse/ui-kit/helpers/d-icon";import { i18n } from "discourse-i18n";const SearchResultDecoration = <template>- <div- class="ai-result__icon"- title={{i18n "discourse_ai.embeddings.ai_generated_result"}}- >- {{dIcon "discourse-sparkles"}}- </div>+ {{#if @post.generatedByAi}}+ <div+ class="ai-result__icon"+ title={{i18n "discourse_ai.embeddings.ai_generated_result"}}+ >+ {{dIcon "discourse-sparkles"}}+ </div>+ {{/if}}</template>;export default SearchResultDecoration;diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-semantic-search.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-semantic-search.jsindex cfc556766c0..f04d8fc11a3 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-semantic-search.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-semantic-search.js@@ -1,9 +1,13 @@import { apiInitializer } from "discourse/lib/api";export default apiInitializer((api) => {- api.modifyClass("component:search-result-entry", {- pluginId: "discourse-ai",-- classNameBindings: ["bulkSelectEnabled", "post.generatedByAi:ai-result"],- });+ api.registerValueTransformer(+ "search-result-entry-class",+ ({ value, context }) => {+ if (context.post.generatedByAi) {+ value.push("ai-result");+ }+ return value;+ }+ );});diff --git a/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scss b/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scssindex c2262c1e0e1..ff3c3e2d820 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scss@@ -62,12 +62,6 @@ body.ai-semantic-search-loading .no-results-container {}.search-results {- .fps-result {- .ai-result__icon {- display: none;- }- }-.ai-result {border-radius: var(--d-border-radius);diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-semantic-search-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-semantic-search-test.jsnew file mode 100644index 00000000000..36efce8acf2--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-semantic-search-test.js@@ -0,0 +1,27 @@+import { visit } from "@ember/test-helpers";+import { test } from "qunit";+import { applyValueTransformer } from "discourse/lib/transformer";+import { acceptance } from "discourse/tests/helpers/qunit-helpers";++acceptance("AI semantic search result classes", function (needs) {+ needs.settings({ discourse_ai_enabled: true });++ test("marks AI-generated search results", async function (assert) {+ await visit("/");++ assert.deepEqual(+ applyValueTransformer("search-result-entry-class", [], {+ post: { generatedByAi: true },+ }),+ ["ai-result"],+ "adds the class for AI results"+ );+ assert.deepEqual(+ applyValueTransformer("search-result-entry-class", [], {+ post: {},+ }),+ [],+ "leaves other results alone"+ );+ });+});diff --git a/plugins/discourse-ai/test/javascripts/integration/components/search-result-decoration-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/search-result-decoration-test.gjsnew file mode 100644index 00000000000..ed398eb5f86--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/integration/components/search-result-decoration-test.gjs@@ -0,0 +1,39 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import SearchResultEntry from "discourse/components/search-result-entry";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++function buildPost(owner, attrs = {}) {+ const store = owner.lookup("service:store");+ const topic = store.createRecord("topic", { id: 1, title: "A topic" });+ return store.createRecord("post", {+ id: 2,+ topic,+ topic_id: 1,+ created_at: "2026-01-01T00:00:00Z",+ ...attrs,+ });+}++module(+ "Integration | Component | AI search result decoration",+ function (hooks) {+ setupRenderingTest(hooks);++ test("marks AI-generated results", async function (assert) {+ const post = buildPost(this.owner, { generatedByAi: true });++ await render(<template><SearchResultEntry @post={{post}} /></template>);++ assert.dom(".fps-result .ai-result__icon").exists("shows the AI icon");+ });++ test("leaves other results alone", async function (assert) {+ const post = buildPost(this.owner);++ await render(<template><SearchResultEntry @post={{post}} /></template>);++ assert.dom(".ai-result__icon").doesNotExist();+ });+ }+);