A platform for community discussion. Free, open, simple.
UX: Improve template filtering with usage count and preserving tags (#35881)
Steven Chang0f252b667686
diff --git a/plugins/discourse-templates/assets/javascripts/discourse/components/d-templates/filterable-list.gjs b/plugins/discourse-templates/assets/javascripts/discourse/components/d-templates/filterable-list.gjsindex d7c5cc42731..6dba89d50b2 100644--- a/plugins/discourse-templates/assets/javascripts/discourse/components/d-templates/filterable-list.gjs+++ b/plugins/discourse-templates/assets/javascripts/discourse/components/d-templates/filterable-list.gjs@@ -17,8 +17,11 @@ import { i18n } from "discourse-i18n";import Item from "./item";import TagDrop from "./tag-drop";+const PREV_TEMPLATE_TAG_ID = "template-selected-tag";+export default class DTemplatesFilterableList extends Component {@service siteSettings;+ @service keyValueStore;@tracked loading = true;@tracked listFilter = "";@@ -55,9 +58,11 @@ export default class DTemplatesFilterableList extends Component {return template.tags.includes(this.selectedTag);}).sort((a, b) => {- /* Sort replies by relevance and title. */+ /* Sort replies by relevance, usage, and title. */if (a.score !== b.score) {return a.score > b.score ? -1 : 1; /* descending */+ } else if (a.usages !== b.usages) {+ return a.usages > b.usages ? -1 : 1; /* descending */} else if (a.title !== b.title) {return a.title < b.title ? -1 : 1; /* ascending */}@@ -88,6 +93,17 @@ export default class DTemplatesFilterableList extends Component {return availableTags;}, {}));++ const prevSelectedTag = this.keyValueStore.get(PREV_TEMPLATE_TAG_ID);+ if (+ prevSelectedTag &&+ (prevSelectedTag === NO_TAG_ID ||+ this.availableTags.find((t) => t.id === prevSelectedTag))+ ) {+ this.selectedTag = prevSelectedTag;+ } else {+ this.keyValueStore.remove(PREV_TEMPLATE_TAG_ID);+ }}} catch (e) {this.loading = false;@@ -104,6 +120,11 @@ export default class DTemplatesFilterableList extends Component {@actionchangeSelectedTag(tagId) {this.selectedTag = tagId;+ if (tagId === ALL_TAGS_ID) {+ this.keyValueStore.remove(PREV_TEMPLATE_TAG_ID);+ return;+ }+ this.keyValueStore.set({ key: PREV_TEMPLATE_TAG_ID, value: tagId });}@actiondiff --git a/plugins/discourse-templates/config/locales/client.en.yml b/plugins/discourse-templates/config/locales/client.en.ymlindex c11ba628943..d8113041432 100644--- a/plugins/discourse-templates/config/locales/client.en.yml+++ b/plugins/discourse-templates/config/locales/client.en.yml@@ -12,7 +12,7 @@ en:title: "Templates (within a textarea)"insert_template: "%{shortcut} Insert template"templates:- filter_hint: "Filter by title..."+ filter_hint: "Filter by keyword..."insert_template: "Insert template"close_templates: "Close the templates selector"copy: "Copy template"diff --git a/plugins/discourse-templates/test/javascripts/acceptance/templates-test.js b/plugins/discourse-templates/test/javascripts/acceptance/templates-test.jsindex 18f47719f8d..9e9c21be814 100644--- a/plugins/discourse-templates/test/javascripts/acceptance/templates-test.js+++ b/plugins/discourse-templates/test/javascripts/acceptance/templates-test.js@@ -162,6 +162,62 @@ acceptance("discourse-templates", function (needs) {"navigates to the source");});++ test("Has ordering by relevance, usage, and title", async function (assert) {+ await visit("/");++ await click("#create-topic");+ await selectCategory();+ await click(".toolbar-menu__options-trigger");+ await click(`button[title="${i18n("templates.insert_template")}"]`);++ await fillIn(".templates-filter-bar input.templates-filter", "ipsum");+ const templateItems = document.querySelectorAll(+ ".templates-list .template-item-title-text"+ );+ const titles = Array.from(templateItems).map((el) => el.textContent.trim());++ assert.deepEqual(+ titles,+ [+ "Cupcake Ipsum excerpt",+ "Hipster ipsum excerpt",+ "Liquor ipsum excerpt",+ "Mussum Ipsum excerpt",+ "Lorem ipsum dolor sit amet",+ ],+ "orders templates by relevance, usage, and title"+ );+ });++ test("Remembers selected tag between openings", async function (assert) {+ await visit("/");++ await click("#create-topic");+ await selectCategory();+ await click(".toolbar-menu__options-trigger");+ await click(`button[title="${i18n("templates.insert_template")}"]`);++ const tagDropdown = selectKit(".templates-filter-bar .tag-drop");+ await tagDropdown.expand();++ await tagDropdown.fillInFilter(+ "cupcake",+ ".templates-filter-bar .tag-drop input"+ );+ await tagDropdown.selectRowByIndex(0);+ assert.dom(".templates-list .template-item").exists({ count: 1 });++ await click("#reply-control .toggle-save-and-close");++ await click("#create-topic");+ await selectCategory();+ await click(".toolbar-menu__options-trigger");+ await click(`button[title="${i18n("templates.insert_template")}"]`);+ assert+ .dom(".templates-list .template-item")+ .exists({ count: 1 }, "preserves selected tag across composer sessions");+ });});acceptance(