A platform for community discussion. Free, open, simple.
DEV: Refactor ai-title-suggester (#37365)
Jarek Radosz4dd917c819d0
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjsindex 09f1f9670c5..684696aa8b7 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs@@ -1,6 +1,6 @@import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";-import { fn } from "@ember/helper";+import { array, fn } from "@ember/helper";import { on } from "@ember/modifier";import { action } from "@ember/object";import DButton from "discourse/components/d-button";@@ -21,52 +21,35 @@ import {export default class AiTitleSuggester extends Component {@tracked loading = false;@tracked suggestions = null;- @tracked untriggers = [];@tracked triggerIcon = "discourse-sparkles";- @tracked content = null;+ dMenu;- get showSuggestionButton() {- const composerFields = document.querySelector(".composer-fields");- const editTopicTitleField = document.querySelector(".edit-topic-title");+ get content() {+ return this.args.composer?.reply;+ }- this.content = this.args.composer?.reply;+ get showSuggestionButton() {const showTrigger =this.content?.length > MIN_CHARACTER_COUNT ||this.args.topicState === "edit";- if (composerFields) {- if (showTrigger) {- composerFields.classList.add("showing-ai-suggestions");- } else {- composerFields.classList.remove("showing-ai-suggestions");- }- }-- if (editTopicTitleField) {- if (showTrigger) {- editTopicTitleField.classList.add("showing-ai-suggestions");- } else {- editTopicTitleField.classList.remove("showing-ai-suggestions");- }- }+ document+ .querySelector(".composer-fields")+ ?.classList.toggle("showing-ai-suggestions", showTrigger);+ document+ .querySelector(".edit-topic-title")+ ?.classList.toggle("showing-ai-suggestions", showTrigger);return showTrigger;}get showDropdown() {- if (this.suggestions?.length <= 0) {- this.dMenu.close();- }return !this.loading && this.suggestions?.length > 0;}@actionasync loadSuggestions() {- if (- this.suggestions &&- this.suggestions?.length > 0 &&- !this.dMenu.expanded- ) {+ if (this.suggestions?.length > 0 && !this.dMenu.expanded) {return this.suggestions;}@@ -83,15 +66,12 @@ export default class AiTitleSuggester extends Component {try {const { suggestions } = await ajax("/discourse-ai/ai-helper/suggest_title",- {- method: "POST",- data,- }+ { method: "POST", data });this.suggestions = suggestions;- if (this.suggestions?.length <= 0) {+ if (suggestions?.length === 0) {showSuggestionsError(this, this.loadSuggestions.bind(this));return;}@@ -111,7 +91,7 @@ export default class AiTitleSuggester extends Component {@actionapplySuggestion(suggestion) {- const model = this.args.composer ? this.args.composer : this.args.buffered;+ const model = this.args.composer || this.args.buffered;if (!model) {return;}@@ -150,7 +130,7 @@ export default class AiTitleSuggester extends Component {@contentClass="ai-suggestions-menu"@onRegisterApi={{this.onRegisterApi}}@modalForMobile={{true}}- @untriggers={{this.untriggers}}+ @untriggers={{array}}{{on "click" this.loadSuggestions}}><:content>@@ -159,13 +139,12 @@ export default class AiTitleSuggester extends Component {{{#each this.suggestions as |suggestion index|}}<dropdown.item><DButton+ @translatedLabel={{suggestion}}+ @action={{fn this.applySuggestion suggestion}}data-name={{suggestion}}data-value={{index}}title={{suggestion}}- @action={{fn this.applySuggestion suggestion}}- >- {{suggestion}}- </DButton>+ /></dropdown.item>{{/each}}</DropdownMenu>