Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit f64bd1c

Browse files
committed
improve selection logic
1 parent 923ec2f commit f64bd1c

File tree

2 files changed

+137
-39
lines changed

2 files changed

+137
-39
lines changed

assets/javascripts/discourse/components/ai-usage.gjs

Lines changed: 110 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Component from "@glimmer/component";
22
import { tracked } from "@glimmer/tracking";
3-
import { hash } from "@ember/helper";
3+
import { fn, hash } from "@ember/helper";
4+
import { on } from "@ember/modifier";
45
import { action } from "@ember/object";
56
import { service } from "@ember/service";
7+
import { eq } from "truth-helpers";
68
import DatePicker from "discourse/components/date-picker";
79
import { ajax } from "discourse/lib/ajax";
810
import i18n from "discourse-common/helpers/i18n";
@@ -16,7 +18,8 @@ export default class AiUsage extends Component {
1618
@tracked data = this.args.model;
1719
@tracked selectedFeature;
1820
@tracked selectedModel;
19-
@tracked period = "day";
21+
@tracked selectedPeriod = "month";
22+
@tracked isCustomDateActive = false;
2023

2124
@action
2225
async fetchData() {
@@ -26,17 +29,11 @@ export default class AiUsage extends Component {
2629
end_date: moment(this.endDate).format("YYYY-MM-DD"),
2730
feature: this.selectedFeature,
2831
model: this.selectedModel,
29-
period: this.period,
3032
},
3133
});
3234
this.data = response;
3335
}
3436

35-
@action
36-
async onDateChange() {
37-
await this.fetchData();
38-
}
39-
4037
@action
4138
async onFilterChange() {
4239
await this.fetchData();
@@ -108,39 +105,117 @@ export default class AiUsage extends Component {
108105
return this._cachedModels;
109106
}
110107

108+
get periodOptions() {
109+
return [
110+
{ id: "day", name: "Last 24 Hours" },
111+
{ id: "week", name: "Last Week" },
112+
{ id: "month", name: "Last Month" },
113+
];
114+
}
115+
116+
@action
117+
setPeriodDates(period) {
118+
const now = moment();
119+
120+
switch (period) {
121+
case "day":
122+
this.startDate = now.clone().subtract(1, "day").toDate();
123+
this.endDate = now.toDate();
124+
break;
125+
case "week":
126+
this.startDate = now.clone().subtract(7, "days").toDate();
127+
this.endDate = now.toDate();
128+
break;
129+
case "month":
130+
this.startDate = now.clone().subtract(30, "days").toDate();
131+
this.endDate = now.toDate();
132+
break;
133+
}
134+
}
135+
136+
@action
137+
onPeriodSelect(period) {
138+
this.selectedPeriod = period;
139+
this.isCustomDateActive = false;
140+
this.setPeriodDates(period);
141+
this.fetchData();
142+
}
143+
144+
@action
145+
onCustomDateClick() {
146+
this.isCustomDateActive = true;
147+
this.selectedPeriod = null;
148+
}
149+
150+
@action
151+
onDateChange() {
152+
this.isCustomDateActive = true;
153+
this.selectedPeriod = null;
154+
this.fetchData();
155+
}
156+
111157
<template>
112158
<div class="ai-usage">
113159
<div class="ai-usage__filters">
114-
<div class="ai-usage__filters-dates">
115-
<DatePicker
116-
@value={{this.startDate}}
117-
@onChange={{this.onDateChange}}
118-
class="ai-usage__date-picker"
119-
/>
120-
<DatePicker
121-
@value={{this.endDate}}
122-
@onChange={{this.onDateChange}}
123-
class="ai-usage__date-picker"
124-
/>
125160

126-
<div class="ai-usage__filters-row">
127-
<ComboBox
128-
@value={{this.selectedFeature}}
129-
@content={{this.availableFeatures}}
130-
@onChange={{this.onFeatureChanged}}
131-
@options={{hash none="discourse_ai.usage.all_features"}}
132-
class="ai-usage__feature-selector"
133-
/>
134-
135-
<ComboBox
136-
@value={{this.selectedModel}}
137-
@content={{this.availableModels}}
138-
@onChange={{this.onModelChanged}}
139-
@options={{hash none="discourse_ai.usage.all_models"}}
140-
class="ai-usage__model-selector"
141-
/>
161+
<div class="ai-usage__filters-dates">
162+
<div class="ai-usage__period-buttons">
163+
{{#each this.periodOptions as |option|}}
164+
<button
165+
type="button"
166+
class="btn
167+
{{if
168+
(eq this.selectedPeriod option.id)
169+
'btn-primary'
170+
'btn-default'
171+
}}"
172+
{{on "click" (fn this.onPeriodSelect option.id)}}
173+
>
174+
{{option.name}}
175+
</button>
176+
{{/each}}
177+
<button
178+
type="button"
179+
class="btn
180+
{{if this.isCustomDateActive 'btn-primary' 'btn-default'}}"
181+
{{on "click" this.onCustomDateClick}}
182+
>
183+
Custom...
184+
</button>
142185
</div>
143186

187+
{{#if this.isCustomDateActive}}
188+
<div class="ai-usage__custom-date-pickers">
189+
<DatePicker
190+
@value={{this.startDate}}
191+
@onChange={{this.onDateChange}}
192+
class="ai-usage__date-picker"
193+
/>
194+
<DatePicker
195+
@value={{this.endDate}}
196+
@onChange={{this.onDateChange}}
197+
class="ai-usage__date-picker"
198+
/>
199+
</div>
200+
{{/if}}
201+
</div>
202+
203+
<div class="ai-usage__filters-row">
204+
<ComboBox
205+
@value={{this.selectedFeature}}
206+
@content={{this.availableFeatures}}
207+
@onChange={{this.onFeatureChanged}}
208+
@options={{hash none="discourse_ai.usage.all_features"}}
209+
class="ai-usage__feature-selector"
210+
/>
211+
212+
<ComboBox
213+
@value={{this.selectedModel}}
214+
@content={{this.availableModels}}
215+
@onChange={{this.onModelChanged}}
216+
@options={{hash none="discourse_ai.usage.all_models"}}
217+
class="ai-usage__model-selector"
218+
/>
144219
</div>
145220

146221
{{#if this.data}}

assets/stylesheets/modules/llms/common/usage.scss

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
.ai-usage {
22
padding: 1em;
33

4-
&__filters {
5-
margin-bottom: 2em;
6-
}
7-
84
&__filters-dates {
95
display: flex;
6+
flex-direction: column;
107
gap: 1em;
118
margin-bottom: 1em;
129
}
1310

11+
&__period-buttons {
12+
display: flex;
13+
gap: 0.5em;
14+
align-items: center;
15+
16+
.btn {
17+
padding: 0.5em 1em;
18+
19+
&.btn-primary {
20+
background: var(--tertiary);
21+
color: var(--secondary);
22+
}
23+
}
24+
}
25+
26+
&__custom-date-pickers {
27+
display: flex;
28+
gap: 1em;
29+
align-items: center;
30+
margin-top: 0.5em;
31+
}
32+
33+
&__filters {
34+
margin-bottom: 2em;
35+
}
36+
1437
&__filters-period {
1538
display: flex;
1639
align-items: center;

0 commit comments

Comments
 (0)