The personal finance app for everyone (by everyone)
Ruby
Let the chat box grow to three lines before it scrolls (#4809)
Yong Cong Sin1c8b8f1fa301
diff --git a/app/javascript/controllers/chat_controller.js b/app/javascript/controllers/chat_controller.jsindex 2f1d577840..62d7bd6315 100644--- a/app/javascript/controllers/chat_controller.js+++ b/app/javascript/controllers/chat_controller.js@@ -34,13 +34,19 @@ export default class extends Controller {autoResize() {const input = this.inputTarget;- const lineHeight = 20; // text-sm line-height (14px * 1.429 ≈ 20px)- const maxLines = 3; // 3 lines = 60px total+ const style = getComputedStyle(input);+ const lineHeight = Number.parseFloat(style.lineHeight) || 20; // text-sm: 20px+ const maxLines = 3;+ // scrollHeight includes the vertical padding, so the cap must too, or the+ // box stops growing short of three lines and scrolls early.+ const maxHeight =+ lineHeight * maxLines ++ Number.parseFloat(style.paddingTop) ++ Number.parseFloat(style.paddingBottom);input.style.height = "auto";- input.style.height = `${Math.min(input.scrollHeight, lineHeight * maxLines)}px`;- input.style.overflowY =- input.scrollHeight > lineHeight * maxLines ? "auto" : "hidden";+ input.style.height = `${Math.min(input.scrollHeight, maxHeight)}px`;+ input.style.overflowY = input.scrollHeight > maxHeight ? "auto" : "hidden";this.#updateSubmitState();}diff --git a/test/system/chats_test.rb b/test/system/chats_test.rbindex ddab4c14b8..f689012b7f 100644--- a/test/system/chats_test.rb+++ b/test/system/chats_test.rb@@ -45,6 +45,78 @@ class ChatsTest < ApplicationSystemTestCaseendend+ test "the message box shows three lines before it scrolls" do+ with_env_overrides OPENAI_ACCESS_TOKEN: "test-token" do+ chat = @user.chats.first+ @user.update!(ai_enabled: true, last_viewed_chat: chat)++ visit root_url++ within "#chat-container" do+ input = find("#chat-form textarea")+ input.send_keys("One")+ one_line = input.evaluate_script("this.clientHeight")+ input.send_keys([ :shift, :enter ], "Two", [ :shift, :enter ], "Three")+ assert_equal "hidden", input.evaluate_script("getComputedStyle(this).overflowY")+ assert_equal input.evaluate_script("this.scrollHeight"), input.evaluate_script("this.clientHeight")++ three_lines = input.evaluate_script("this.clientHeight")+ assert_operator three_lines, :>, one_line, "the box grows with its lines rather than starting at the cap"+ input.send_keys([ :shift, :enter ], "Four")+ assert_equal "auto", input.evaluate_script("getComputedStyle(this).overflowY")+ assert_equal three_lines, input.evaluate_script("this.clientHeight")+ assert_operator input.evaluate_script("this.scrollHeight"), :>, input.evaluate_script("this.clientHeight")+ end+ end+ end++ test "the message box sizes its three lines from its own line height" do+ with_env_overrides OPENAI_ACCESS_TOKEN: "test-token" do+ chat = @user.chats.first+ @user.update!(ai_enabled: true, last_viewed_chat: chat)++ visit root_url++ within "#chat-container" do+ input = find("#chat-form textarea")+ input.execute_script("this.style.lineHeight = '30px'")+ input.send_keys("One")+ one_line = input.evaluate_script("this.clientHeight")+ input.send_keys([ :shift, :enter ], "Two", [ :shift, :enter ], "Three")+ assert_equal "hidden", input.evaluate_script("getComputedStyle(this).overflowY")+ assert_equal input.evaluate_script("this.scrollHeight"), input.evaluate_script("this.clientHeight")++ three_lines = input.evaluate_script("this.clientHeight")+ assert_operator three_lines, :>, one_line+ input.send_keys([ :shift, :enter ], "Four")+ assert_equal three_lines, input.evaluate_script("this.clientHeight")+ assert_operator input.evaluate_script("this.scrollHeight"), :>, input.evaluate_script("this.clientHeight")+ end+ end+ end++ test "the message box counts its own padding toward the three-line height" do+ with_env_overrides OPENAI_ACCESS_TOKEN: "test-token" do+ chat = @user.chats.first+ @user.update!(ai_enabled: true, last_viewed_chat: chat)++ visit root_url++ within "#chat-container" do+ input = find("#chat-form textarea")+ input.execute_script("this.style.paddingTop = '20px'; this.style.paddingBottom = '20px'")+ input.send_keys("One", [ :shift, :enter ], "Two", [ :shift, :enter ], "Three")+ assert_equal "hidden", input.evaluate_script("getComputedStyle(this).overflowY")+ assert_equal input.evaluate_script("this.scrollHeight"), input.evaluate_script("this.clientHeight")++ three_lines = input.evaluate_script("this.clientHeight")+ input.send_keys([ :shift, :enter ], "Four")+ assert_equal three_lines, input.evaluate_script("this.clientHeight")+ assert_operator input.evaluate_script("this.scrollHeight"), :>, input.evaluate_script("this.clientHeight")+ end+ end+ end+test "sidebar shows last viewed chat" dowith_env_overrides OPENAI_ACCESS_TOKEN: "test-token" dochat = @user.chats.first