diff --git a/assets/javascripts/discourse/components/ai-bot-conversations.gjs b/assets/javascripts/discourse/components/ai-bot-conversations.gjs index 1d859a4c..462a8452 100644 --- a/assets/javascripts/discourse/components/ai-bot-conversations.gjs +++ b/assets/javascripts/discourse/components/ai-bot-conversations.gjs @@ -260,10 +260,11 @@ export default class AiBotConversations extends Component { @action async prepareAndSubmitToBot() { - // Pass uploads to the service before submitting - this.aiBotConversationsHiddenSubmit.uploads = this.uploads; try { - await this.aiBotConversationsHiddenSubmit.submitToBot(); + await this.aiBotConversationsHiddenSubmit.submitToBot({ + uploads: this.uploads, + inProgressUploadsCount: this.inProgressUploads.length, + }); this.uploads = new TrackedArray(); } catch (error) { popupAjaxError(error); diff --git a/assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js b/assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js index 1749ba98..bc7bc02e 100644 --- a/assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js +++ b/assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js @@ -18,7 +18,6 @@ export default class AiBotConversationsHiddenSubmit extends Service { personaId; targetUsername; - uploads = []; inputValue = ""; @@ -32,7 +31,7 @@ export default class AiBotConversationsHiddenSubmit extends Service { } @action - async submitToBot() { + async submitToBot(uploadData) { if ( this.inputValue.length < this.siteSettings.min_personal_message_post_length @@ -48,7 +47,7 @@ export default class AiBotConversationsHiddenSubmit extends Service { } // Don't submit if there are still uploads in progress - if (document.querySelector(".ai-bot-upload--in-progress")) { + if (uploadData.inProgressUploadsCount > 0) { return this.dialog.alert({ message: i18n("discourse_ai.ai_bot.conversations.uploads_in_progress"), }); @@ -61,10 +60,10 @@ export default class AiBotConversationsHiddenSubmit extends Service { let rawContent = this.inputValue; // Append upload markdown if we have uploads - if (this.uploads && this.uploads.length > 0) { + if (uploadData.uploads && uploadData.uploads.length > 0) { rawContent += "\n\n"; - this.uploads.forEach((upload) => { + uploadData.uploads.forEach((upload) => { const uploadMarkdown = getUploadMarkdown(upload); rawContent += uploadMarkdown + "\n"; }); @@ -83,7 +82,6 @@ export default class AiBotConversationsHiddenSubmit extends Service { }); // Reset uploads after successful submission - this.uploads = []; this.inputValue = ""; this.appEvents.trigger("discourse-ai:bot-pm-created", { diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index bd774cd1..c11c165b 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -872,6 +872,7 @@ en: last_7_days: "Last 7 days" last_30_days: "Last 30 days" upload_files: "Upload files" + uploads_in_progress: "Cannot submit while uploads are in progress" sentiments: dashboard: title: "Sentiment" diff --git a/spec/system/ai_bot/homepage_spec.rb b/spec/system/ai_bot/homepage_spec.rb index 13393894..203dd104 100644 --- a/spec/system/ai_bot/homepage_spec.rb +++ b/spec/system/ai_bot/homepage_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true RSpec.describe "AI Bot - Homepage", type: :system do + let(:cdp) { PageObjects::CDP.new } let(:topic_page) { PageObjects::Pages::Topic.new } let(:composer) { PageObjects::Components::Composer.new } let(:ai_pm_homepage) { PageObjects::Components::AiPmHomepage.new } @@ -164,6 +165,28 @@ RSpec.describe "AI Bot - Homepage", type: :system do expect(page).to have_no_css(".ai-bot-upload") end + it "shows an error when trying to submit while uploads are in progress" do + ai_pm_homepage.visit + expect(ai_pm_homepage).to have_homepage + + file_path_1 = file_from_fixtures("logo.png", "images").path + file_path_2 = file_from_fixtures("logo.jpg", "images").path + + ai_pm_homepage.input.fill_in(with: "Some message to send to AI with uploads") + + cdp.with_slow_upload do + attach_file([file_path_1, file_path_2]) do + find(".ai-bot-upload-btn", visible: true).click + end + expect(page).to have_css(".ai-bot-upload--in-progress", count: 2) + + ai_pm_homepage.submit + expect(page).to have_content( + I18n.t("js.discourse_ai.ai_bot.conversations.uploads_in_progress"), + ) + end + end + it "allows removing an upload before submission" do ai_pm_homepage.visit expect(ai_pm_homepage).to have_homepage