A platform for community discussion. Free, open, simple.
DEV: replace hashtag autocomplete templates with components (#35829)
Kelv54723610355e
diff --git a/frontend/discourse/app/components/hashtag-autocomplete-results.gjs b/frontend/discourse/app/components/hashtag-autocomplete-results.gjsnew file mode 100644index 00000000000..9fdfb276879--- /dev/null+++ b/frontend/discourse/app/components/hashtag-autocomplete-results.gjs@@ -0,0 +1,89 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { fn } from "@ember/helper";+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";+import { eq } from "truth-helpers";+import categoryLink from "discourse/helpers/category-link";+import icon from "discourse/helpers/d-icon";+import scrollIntoView from "discourse/modifiers/scroll-into-view";++/**+ * Component for rendering hashtag autocomplete results for the DAutocomplete modifier.+ *+ * This component handles rendering of categories and tags in the autocomplete+ * dropdown, and is designed to be used with DAutocomplete's `component` API.+ *+ * @component HashtagAutocompleteResults+ * @param {Array} results - Array of autocomplete results (categories and tags)+ * @param {number} selectedIndex - Currently selected index in the results list+ * @param {Function} onSelect - Callback function triggered when a result is selected+ * @param {Function} onRender - Optional callback function triggered after component renders+ */+export default class HashtagAutocompleteResults extends Component {+ static TRIGGER_KEY = "#";++ @tracked isInitialRender = true;++ @action+ handleResultClick(result, index, event) {+ event.preventDefault();+ event.stopPropagation();+ this.args.onSelect(result, index, event);+ }++ @action+ handleInsert() {+ this.args.onRender?.(this.args.results);+ }++ @action+ handleUpdate() {+ this.isInitialRender = false;+ this.args.onRender?.(this.args.results);+ }++ @action+ shouldScroll(index) {+ return index === this.args.selectedIndex && !this.isInitialRender;+ }++ @action+ getResultLabel(result) {+ if (result.model) {+ return categoryLink(result.model, {+ allowUncategorized: true,+ link: false,+ });+ }++ return `${result.name} x ${result.count}`;+ }++ <template>+ <div+ class="autocomplete ac-category-or-tag"+ {{didInsert this.handleInsert}}+ {{didUpdate this.handleUpdate @selectedIndex}}+ >+ <ul>+ {{#each @results as |result index|}}+ <li {{scrollIntoView (this.shouldScroll index)}}>+ <a+ href+ class={{if (eq index @selectedIndex) "selected"}}+ {{on "click" (fn this.handleResultClick result index)}}+ >+ {{#unless result.model}}+ {{icon "tag"}}+ {{/unless}}+ <span class="text-content">{{this.getResultLabel result}}</span>+ </a>+ </li>+ {{/each}}+ </ul>+ </div>+ </template>+}diff --git a/frontend/discourse/app/components/rich-hashtag-autocomplete-results.gjs b/frontend/discourse/app/components/rich-hashtag-autocomplete-results.gjsnew file mode 100644index 00000000000..759c7978c62--- /dev/null+++ b/frontend/discourse/app/components/rich-hashtag-autocomplete-results.gjs@@ -0,0 +1,89 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { fn } from "@ember/helper";+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";+import { htmlSafe } from "@ember/template";+import { eq } from "truth-helpers";+import scrollIntoView from "discourse/modifiers/scroll-into-view";++/**+ * Component for rendering rich hashtag autocomplete results for the DAutocomplete modifier.+ *+ * This component handles rendering of hashtags with rich metadata (icons, colors, descriptions)+ * for categories, tags, and other contextual types in composer contexts.+ * Designed to be used with DAutocomplete's `component` API.+ *+ * @component RichHashtagAutocompleteResults+ * @param {Array} results - Array of rich hashtag results with type, icon, text, secondary_text, description+ * @param {number} selectedIndex - Currently selected index in the results list+ * @param {Function} onSelect - Callback function triggered when a result is selected+ * @param {Function} onRender - Optional callback function triggered after component renders+ */+export default class RichHashtagAutocompleteResults extends Component {+ static TRIGGER_KEY = "#";++ @tracked isInitialRender = true;++ @action+ handleResultClick(result, index, event) {+ event.preventDefault();+ event.stopPropagation();+ this.args.onSelect(result, index, event);+ }++ @action+ handleInsert() {+ this.args.onRender?.(this.args.results);+ }++ @action+ handleUpdate() {+ this.isInitialRender = false;+ this.args.onRender?.(this.args.results);+ }++ @action+ shouldScroll(index) {+ return index === this.args.selectedIndex && !this.isInitialRender;+ }++ <template>+ <div+ class="autocomplete hashtag-autocomplete"+ {{didInsert this.handleInsert}}+ {{didUpdate this.handleUpdate @selectedIndex}}+ >+ <div class="hashtag-autocomplete__fadeout">+ <ul>+ {{#each @results as |result index|}}+ <li+ class="hashtag-autocomplete__option"+ {{scrollIntoView (this.shouldScroll index)}}+ >+ <a+ class="hashtag-autocomplete__link+ {{if (eq index @selectedIndex) 'selected'}}"+ title={{result.description}}+ href+ {{on "click" (fn this.handleResultClick result index)}}+ >+ {{htmlSafe result.iconHtml}}+ <span class="hashtag-autocomplete__text">+ {{result.text}}+ {{#if result.secondary_text}}+ <span+ class="hashtag-autocomplete__meta-text"+ >({{result.secondary_text}})</span>+ {{/if}}+ </span>+ </a>+ </li>+ {{/each}}+ </ul>+ </div>+ </div>+ </template>+}diff --git a/frontend/discourse/app/lib/autocomplete/category-tag.js b/frontend/discourse/app/lib/autocomplete/category-tag.jsdeleted file mode 100644index 1fd61a1b035..00000000000--- a/frontend/discourse/app/lib/autocomplete/category-tag.js+++ /dev/null@@ -1,31 +0,0 @@-import categoryLink from "discourse/helpers/category-link";-import { iconHTML } from "discourse/lib/icon-library";-import { escapeExpression } from "discourse/lib/utilities";--export function renderCategory(option) {- const link = categoryLink(option.model, {- allowUncategorized: true,- link: false,- });-- return `<li><a href><span class='text-content'>${link}</span></a></li>`;-}--export function renderTag(option) {- const text = `${escapeExpression(option.name)} x ${escapeExpression(option.count)}`;- return `<li><a href>${iconHTML("tag")}<span class='text-content'>${text}</span></a></li>`;-}--export function renderOption(option) {- return option.model ? renderCategory(option) : renderTag(option);-}--export default function categoryTagAutocomplete({ options }) {- return `- <div class='autocomplete ac-category-or-tag'>- <ul>- ${options.map(renderOption).join("")}- </ul>- </div>- `;-}diff --git a/frontend/discourse/app/lib/hashtag-autocomplete.js b/frontend/discourse/app/lib/hashtag-autocomplete.jsindex 602fb6e8f70..c2faac5406c 100644--- a/frontend/discourse/app/lib/hashtag-autocomplete.js+++ b/frontend/discourse/app/lib/hashtag-autocomplete.js@@ -1,5 +1,6 @@import { cancel } from "@ember/runloop";import { htmlSafe } from "@ember/template";+import RichHashtagAutocompleteResults from "discourse/components/rich-hashtag-autocomplete-results";import { ajax } from "discourse/lib/ajax";import discourseDebounce from "discourse/lib/debounce";import { INPUT_DELAY, isTesting } from "discourse/lib/environment";@@ -51,8 +52,8 @@ export function hashtagAutocompleteOptions(autocompleteOptions) {return {- template: renderHashtagAutocomplete,- key: "#",+ component: RichHashtagAutocompleteResults,+ key: RichHashtagAutocompleteResults.TRIGGER_KEY,scrollElementSelector: ".hashtag-autocomplete__fadeout",autoSelectFirstSuggestion: true,transformComplete: (obj) => obj.ref,@@ -151,36 +152,6 @@ function _searchRequest(term, contextualHashtagConfiguration, resultFunc) {return currentSearch;}-function renderOption(option) {- const metaText = option.secondary_text- ? `<span class="hashtag-autocomplete__meta-text">(${escapeExpression(option.secondary_text)})</span>`- : "";-- return `- <li class="hashtag-autocomplete__option">- <a class="hashtag-autocomplete__link" title="${escapeExpression(option.description)}" href>- ${option.iconHtml}- <span class="hashtag-autocomplete__text">- ${option.text}- ${metaText}- </span>- </a>- </li>- `;-}--export default function renderHashtagAutocomplete({ options }) {- return `- <div class="autocomplete hashtag-autocomplete">- <div class="hashtag-autocomplete__fadeout">- <ul>- ${options.map(renderOption).join("")}- </ul>- </div>- </div>- `;-}-export function getHashtagAutocompleteCache() {return searchCache;}diff --git a/frontend/discourse/app/lib/search.js b/frontend/discourse/app/lib/search.jsindex 0e5e79eb0d8..9a32e083dc6 100644--- a/frontend/discourse/app/lib/search.js+++ b/frontend/discourse/app/lib/search.js@@ -1,6 +1,7 @@import EmberObject from "@ember/object";import { isEmpty } from "@ember/utils";import { Promise } from "rsvp";+import HashtagAutocompleteResults from "discourse/components/hashtag-autocomplete-results";import UserAutocompleteResults from "discourse/components/user-autocomplete-results";import { ajax } from "discourse/lib/ajax";import { search as searchCategoryTag } from "discourse/lib/category-tag-search";@@ -17,7 +18,6 @@ import Topic from "discourse/models/topic";import User from "discourse/models/user";import DAutocompleteModifier from "discourse/modifiers/d-autocomplete";import { i18n } from "discourse-i18n";-import categoryTagAutocomplete from "./autocomplete/category-tag";const translateResultsCallbacks = [];const MAX_RECENT_SEARCHES = 5; // should match backend constant with the same name@@ -234,8 +234,8 @@ export function applySearchAutocomplete(inputElement, siteSettings, owner) {inputElement,autocompleteHandler,{- template: categoryTagAutocomplete,- key: "#",+ component: HashtagAutocompleteResults,+ key: HashtagAutocompleteResults.TRIGGER_KEY,autoSelectFirstSuggestion: false,transformComplete: (obj) => obj.text,dataSource: (term) => searchCategoryTag(term, siteSettings),