A platform for community discussion. Free, open, simple.
FIX: AI usage doesn't refresh custom date ranges correctly (#34816)
Keegan Georgee6e538ee8e1a
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjsindex 0c587dccee5..ad710a38387 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-usage.gjs@@ -331,6 +331,16 @@ export default class AiUsage extends Component {this.startDate = this._startDate;this.endDate = this._endDate;this.fetchData();+ this._startDate = null;+ this._endDate = null;+ }++ get fromDate() {+ return this._startDate || this.startDate;+ }++ get toDate() {+ return this._endDate || this.endDate;}totalSpending(inputSpending, cachedSpending, outputSpending) {@@ -370,8 +380,8 @@ export default class AiUsage extends Component {<div class="ai-usage__custom-date-pickers"><DateTimeInputRange- @from={{this.startDate}}- @to={{this.endDate}}+ @from={{this.fromDate}}+ @to={{this.toDate}}@onChange={{this.onChangeDateRange}}@showFromTime={{false}}@showToTime={{false}}diff --git a/plugins/discourse-ai/spec/system/ai_usage_spec.rb b/plugins/discourse-ai/spec/system/ai_usage_spec.rbnew file mode 100644index 00000000000..ae53f40a072--- /dev/null+++ b/plugins/discourse-ai/spec/system/ai_usage_spec.rb@@ -0,0 +1,38 @@+# frozen_string_literal: true++RSpec.describe "AI Usage Admin Page", type: :system do+ fab!(:admin)++ before do+ enable_current_plugin+ sign_in(admin)+ end++ context "when using custom date range functionality" do+ it "allows selecting custom date range without JavaScript errors" do+ visit "/admin/plugins/discourse-ai/ai-usage"+ expect(page).to have_css(".ai-usage")++ # Click custom date button to show date picker+ find(".ai-usage__period-buttons .btn-default:last-child").click+ expect(page).to have_css(".ai-usage__custom-date-pickers")++ # Set dates+ date_inputs = all(".ai-usage__custom-date-pickers input[type='date']")+ date_inputs[0].set("2025-07-01")+ date_inputs[1].set("2025-07-31")++ # Verify dates are set correctly (preview functionality)+ expect(date_inputs[0].value).to eq("2025-07-01")+ expect(date_inputs[1].value).to eq("2025-07-31")++ # Click refresh - this used to cause visual glitches and date reversion+ find(".ai-usage__custom-date-pickers .btn", text: I18n.t("js.refresh")).click++ # Wait for any potential async operations+ sleep(1)++ expect(page).to have_css(".ai-usage__summary")+ end+ end+enddiff --git a/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rb b/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rbnew file mode 100644index 00000000000..c7f3f6b110e--- /dev/null+++ b/plugins/discourse-ai/spec/system/page_objects/pages/ai_usage.rb@@ -0,0 +1,60 @@+# frozen_string_literal: true++module PageObjects+ module Pages+ class AiUsage < PageObjects::Pages::Base+ def visit+ page.visit "/admin/plugins/discourse-ai/ai-usage"+ end++ def has_usage_content?+ page.has_css?(".ai-usage")+ end++ def click_custom_date_button+ find(".ai-usage__period-buttons .btn-default:last-child").click+ end++ def has_custom_date_picker?+ page.has_css?(".ai-usage__custom-date-pickers")+ end++ def set_start_date(date_string)+ # Find the first date input (from date)+ date_inputs = all(".ai-usage__custom-date-pickers input[type='date']")+ date_inputs[0].set(date_string)+ end++ def set_end_date(date_string)+ # Find the second date input (to date)+ date_inputs = all(".ai-usage__custom-date-pickers input[type='date']")+ date_inputs[1].set(date_string)+ end++ def get_start_date_value+ all(".ai-usage__custom-date-pickers input[type='date']")[0].value+ end++ def get_end_date_value+ all(".ai-usage__custom-date-pickers input[type='date']")[1].value+ end++ def click_refresh+ find(".ai-usage__custom-date-pickers .btn", text: I18n.t("js.refresh")).click+ end++ def has_loading_spinner?+ page.has_css?(".conditional-loading-spinner .spinner")+ end++ def wait_for_data_load+ # Wait for loading spinner to disappear+ expect(page).not_to have_css(".conditional-loading-spinner .spinner", wait: 10)+ end++ def has_summary_data?+ page.has_css?(".ai-usage__summary .d-stat-tiles")+ end+ end+ end+end