From 4da4b5609f6d76c3ec667348b0d213ca26f1a965 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Tue, 29 Aug 2023 11:58:45 -0700 Subject: [PATCH] FIX: Show warning when trying to generate suggestions without content (#175) --- .../after-composer-title-input/ai-title-suggester.gjs | 10 +++++++++- config/locales/client.en.yml | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/assets/javascripts/discourse/connectors/after-composer-title-input/ai-title-suggester.gjs b/assets/javascripts/discourse/connectors/after-composer-title-input/ai-title-suggester.gjs index 6350270a..5817bcc6 100644 --- a/assets/javascripts/discourse/connectors/after-composer-title-input/ai-title-suggester.gjs +++ b/assets/javascripts/discourse/connectors/after-composer-title-input/ai-title-suggester.gjs @@ -6,6 +6,9 @@ import { ajax } from "discourse/lib/ajax"; import { popupAjaxError } from "discourse/lib/ajax-error"; import didInsert from "@ember/render-modifiers/modifiers/did-insert"; import { bind } from "discourse-common/utils/decorators"; +import { inject as service } from "@ember/service"; +import I18n from "I18n"; + export default class AITitleSuggester extends Component { + @service dialog; @tracked loading = false; @tracked showMenu = false; @tracked generatedTitleSuggestions = []; @@ -50,7 +54,7 @@ export default class AITitleSuggester extends Component { } get composerInput() { - return document.querySelector(".d-editor-input").value || this.args.outletArgs.composer.reply; + return document.querySelector(".d-editor-input")?.value || this.args.outletArgs.composer.reply; } get disableSuggestionButton() { @@ -90,6 +94,10 @@ export default class AITitleSuggester extends Component { @action async suggestTitles() { + if (this.composerInput?.length === 0) { + return this.dialog.alert(I18n.t("discourse_ai.ai_helper.missing_content")); + } + this.loading = true; this.suggestTitleIcon = "spinner"; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index af1a75d8..6077c311 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -17,6 +17,7 @@ en: description: "Choose one of the options below, and the AI will suggest you a new version of the text." selection_hint: "Hint: You can also select a portion of the text before opening the helper to rewrite only that." suggest_titles: "Suggest titles with AI" + missing_content: "Please enter some content to generate suggestions." context_menu: trigger: "AI" undo: "Undo"