diff --git a/assets/javascripts/discourse/components/suggestion-menus/ai-category-suggester.gjs b/assets/javascripts/discourse/components/suggestion-menus/ai-category-suggester.gjs index b957d129..9847255a 100644 --- a/assets/javascripts/discourse/components/suggestion-menus/ai-category-suggester.gjs +++ b/assets/javascripts/discourse/components/suggestion-menus/ai-category-suggester.gjs @@ -11,7 +11,10 @@ import { ajax } from "discourse/lib/ajax"; import { popupAjaxError } from "discourse/lib/ajax-error"; import { i18n } from "discourse-i18n"; import DMenu from "float-kit/components/d-menu"; -import { MIN_CHARACTER_COUNT } from "../../lib/ai-helper-suggestions"; +import { + MIN_CHARACTER_COUNT, + showSuggestionsError, +} from "../../lib/ai-helper-suggestions"; export default class AiCategorySuggester extends Component { @service siteSettings; @@ -40,9 +43,20 @@ export default class AiCategorySuggester extends Component { return this.siteSettings.ai_embeddings_enabled && showTrigger; } + get showDropdown() { + if (this.suggestions?.length <= 0) { + this.dMenu.close(); + } + return !this.loading && this.suggestions?.length > 0; + } + @action async loadSuggestions() { - if (this.suggestions && !this.dMenu.expanded) { + if ( + this.suggestions && + this.suggestions?.length > 0 && + !this.dMenu.expanded + ) { return this.suggestions; } @@ -65,7 +79,13 @@ export default class AiCategorySuggester extends Component { data, } ); + this.suggestions = assistant; + + if (this.suggestions?.length <= 0) { + showSuggestionsError(this, this.loadSuggestions.bind(this)); + return; + } } catch (error) { popupAjaxError(error); } finally { @@ -100,7 +120,13 @@ export default class AiCategorySuggester extends Component { @action onClose() { - this.triggerIcon = "discourse-sparkles"; + if (this.suggestions?.length > 0) { + // If all suggestions have been used, + // re-triggering when no suggestions present + // will cause computation issues with + // setting the icon, so we prevent it + this.triggerIcon = "discourse-sparkles"; + } }