Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(ai-chat): disable submit on empty input instead of surfacing 'Content missing' (#1697) (#1872)

Jeff84ad60d5411b
diff --git a/app/javascript/controllers/chat_controller.js b/app/javascript/controllers/chat_controller.js
index 7e067309c9..25020f35e9 100644
--- a/app/javascript/controllers/chat_controller.js
+++ b/app/javascript/controllers/chat_controller.js
@@ -1,10 +1,11 @@
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
- static targets = ["messages", "form", "input"];
+ static targets = ["messages", "form", "input", "submit"];
connect() {
this.#configureAutoScroll();
+ this.#updateSubmitState();
}
disconnect() {
@@ -22,10 +23,13 @@ export default class extends Controller {
input.style.height = `${Math.min(input.scrollHeight, lineHeight * maxLines)}px`;
input.style.overflowY =
input.scrollHeight > lineHeight * maxLines ? "auto" : "hidden";
+
+ this.#updateSubmitState();
}
submitSampleQuestion(e) {
this.inputTarget.value = e.target.dataset.chatQuestionParam;
+ this.#updateSubmitState();
setTimeout(() => {
this.formTarget.requestSubmit();
@@ -36,10 +40,21 @@ export default class extends Controller {
handleInputKeyDown(e) {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
- this.formTarget.requestSubmit();
+ if (this.#hasContent()) {
+ this.formTarget.requestSubmit();
+ }
}
}
+ #hasContent() {
+ return this.inputTarget.value.trim().length > 0;
+ }
+
+ #updateSubmitState() {
+ if (!this.hasSubmitTarget) return;
+ this.submitTarget.disabled = !this.#hasContent();
+ }
+
#configureAutoScroll() {
this.messagesObserver = new MutationObserver((_mutations) => {
if (this.hasMessagesTarget) {
diff --git a/app/views/messages/_chat_form.html.erb b/app/views/messages/_chat_form.html.erb
index 1fbc57c820..92d401e250 100644
--- a/app/views/messages/_chat_form.html.erb
+++ b/app/views/messages/_chat_form.html.erb
@@ -23,7 +23,8 @@
<% end %>
</div>
- <%= icon("arrow-up", as_button: true, type: "submit") %>
+ <%= icon("arrow-up", as_button: true, type: "submit",
+ data: { chat_target: "submit" }) %>
</div>
<% end %>