From 7457feced86f68dc4dccf9786f020ad9748c4ff0 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Tue, 29 Aug 2023 09:45:53 -0700 Subject: [PATCH] FEATURE: Show suggested title prompt in new location (#171) --- .../ai-title-suggester.gjs | 108 ++++++++++++++++++ .../after-d-editor/ai-helper-context-menu.hbs | 14 --- .../after-d-editor/ai-helper-context-menu.js | 43 +++---- .../modules/ai-helper/common/ai-helper.scss | 33 ++++++ config/locales/client.en.yml | 1 + .../ai_helper/ai_composer_helper_spec.rb | 54 ++++++--- .../components/ai_helper_context_menu.rb | 9 -- .../components/ai_title_suggester.rb | 26 +++++ 8 files changed, 221 insertions(+), 67 deletions(-) create mode 100644 assets/javascripts/discourse/connectors/after-composer-title-input/ai-title-suggester.gjs create mode 100644 spec/system/page_objects/components/ai_title_suggester.rb 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 new file mode 100644 index 00000000..6350270a --- /dev/null +++ b/assets/javascripts/discourse/connectors/after-composer-title-input/ai-title-suggester.gjs @@ -0,0 +1,108 @@ +import Component from '@glimmer/component'; +import DButton from "discourse/components/d-button"; +import { tracked } from "@glimmer/tracking"; +import { action } from "@ember/object"; +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"; + +export default class AITitleSuggester extends Component { + + + @tracked loading = false; + @tracked showMenu = false; + @tracked generatedTitleSuggestions = []; + @tracked suggestTitleIcon = "discourse-sparkles"; + mode = { + id: -2, + name: "generate_titles", + translated_name: "Suggest topic titles", + prompt_type: "list" + }; + + willDestroy() { + super.willDestroy(...arguments); + document.removeEventListener("click", this.onClickOutside); + } + + get composerInput() { + return document.querySelector(".d-editor-input").value || this.args.outletArgs.composer.reply; + } + + get disableSuggestionButton() { + return this.loading; + } + + closeMenu() { + this.suggestTitleIcon = "discourse-sparkles"; + this.showMenu = false; + } + + @bind + onClickOutside(event) { + const menu = document.querySelector(".ai-title-suggestions-menu"); + + if (event.target === menu) { + return; + } + + return this.closeMenu(); + } + + @action + handleClickOutside() { + document.addEventListener("click", this.onClickOutside); + } + + @action + updateTopicTitle(title) { + const composer = this.args.outletArgs?.composer; + + if (composer) { + composer.set("title", title); + this.closeMenu(); + } + } + + @action + async suggestTitles() { + this.loading = true; + this.suggestTitleIcon = "spinner"; + + return ajax("/discourse-ai/ai-helper/suggest", { + method: "POST", + data: { mode: this.mode.id, text: this.composerInput }, + }).then((data) => { + this.generatedTitleSuggestions = data.suggestions; + }).catch(popupAjaxError).finally(() => { + this.loading = false; + this.suggestTitleIcon = "sync-alt"; + this.showMenu = true; + }); + + } +} \ No newline at end of file diff --git a/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.hbs b/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.hbs index edb11df2..d4e3e557 100644 --- a/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.hbs +++ b/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.hbs @@ -27,20 +27,6 @@ {{/each}} - {{else if (eq this.menuState this.CONTEXT_MENU_STATES.suggestions)}} - - {{else if (eq this.menuState this.CONTEXT_MENU_STATES.loading)}}