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

Commit e930d4c

Browse files
committed
DEV: Group PMs by date
1 parent 60ea590 commit e930d4c

File tree

4 files changed

+101
-16
lines changed

4 files changed

+101
-16
lines changed

app/controllers/discourse_ai/ai_bot/conversations_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def index
1010
page = params[:page].to_i
1111
per_page = params[:per_page]&.to_i || 40
1212

13-
bot_user_ids = EntryPoint.all_bot_ids
1413
base_query =
1514
Topic
1615
.private_messages_for_user(current_user)
@@ -26,7 +25,7 @@ def index
2625
pms = base_query.order(last_posted_at: :desc).offset(page * per_page).limit(per_page)
2726

2827
render json: {
29-
conversations: serialize_data(pms, BasicTopicSerializer),
28+
conversations: serialize_data(pms, ListableTopicSerializer),
3029
meta: {
3130
total: total,
3231
page: page,

assets/javascripts/initializers/ai-conversations-sidebar.js

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { tracked } from "@glimmer/tracking";
2+
import { htmlSafe } from "@ember/template";
23
import { TrackedArray } from "@ember-compat/tracked-built-ins";
34
import { ajax } from "discourse/lib/ajax";
45
import { bind } from "discourse/lib/decorators";
6+
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
57
import { withPluginApi } from "discourse/lib/plugin-api";
68
import { i18n } from "discourse-i18n";
79
import AiBotSidebarNewConversation from "../discourse/components/ai-bot-sidebar-new-conversation";
@@ -85,6 +87,9 @@ export default {
8587
@tracked links = new TrackedArray();
8688
@tracked topics = [];
8789
@tracked hasMore = [];
90+
@tracked loadedSevenDayLabel = false;
91+
@tracked loadedThirtyDayLabel = false;
92+
@tracked loadedMonthLabels = new Set();
8893
page = 0;
8994
isFetching = false;
9095
totalTopicsCount = 0;
@@ -127,7 +132,16 @@ export default {
127132
}
128133

129134
addNewPMToSidebar(topic) {
130-
this.links = [new AiConversationLink(topic), ...this.links];
135+
// Reset category labels since we're adding a new topic
136+
this.loadedSevenDayLabel = false;
137+
this.loadedThirtyDayLabel = false;
138+
this.loadedMonthLabels.clear();
139+
140+
// Build links again with the new topic
141+
const allTopics = [topic, ...this.topics];
142+
this.topics = allTopics;
143+
this.buildSidebarLinks();
144+
131145
this.watchForTitleUpdate(topic);
132146
}
133147

@@ -206,10 +220,71 @@ export default {
206220
this.fetchMessages(true);
207221
}
208222

209-
buildSidebarLinks() {
210-
this.links = this.topics.map(
211-
(topic) => new AiConversationLink(topic)
223+
groupByDate(topic) {
224+
const now = new Date();
225+
const lastPostedAt = new Date(topic.last_posted_at);
226+
const daysDiff = Math.round(
227+
(now - lastPostedAt) / (1000 * 60 * 60 * 24)
212228
);
229+
230+
// Last 7 days group
231+
if (daysDiff <= 7) {
232+
if (!this.loadedSevenDayLabel) {
233+
this.loadedSevenDayLabel = true;
234+
return {
235+
text: i18n("discourse_ai.ai_bot.conversations.last_7_days"),
236+
classNames: "date-heading",
237+
name: "date-heading-last-7-days",
238+
};
239+
}
240+
}
241+
// Last 30 days group
242+
else if (daysDiff <= 30) {
243+
if (!this.loadedThirtyDayLabel) {
244+
this.loadedThirtyDayLabel = true;
245+
return {
246+
text: i18n(
247+
"discourse_ai.ai_bot.conversations.last_30_days"
248+
),
249+
classNames: "date-heading",
250+
name: "date-heading-last-30-days",
251+
};
252+
}
253+
}
254+
// Group by month for older conversations
255+
else {
256+
const month = lastPostedAt.getMonth();
257+
const year = lastPostedAt.getFullYear();
258+
const monthKey = `${year}-${month}`;
259+
260+
if (!this.loadedMonthLabels.has(monthKey)) {
261+
this.loadedMonthLabels.add(monthKey);
262+
263+
const formattedDate = autoUpdatingRelativeAge(
264+
new Date(topic.last_posted_at)
265+
);
266+
267+
return {
268+
text: htmlSafe(formattedDate),
269+
classNames: "date-heading",
270+
name: `date-heading-${monthKey}`,
271+
};
272+
}
273+
}
274+
}
275+
276+
buildSidebarLinks() {
277+
// Reset date header tracking
278+
this.loadedSevenDayLabel = false;
279+
this.loadedThirtyDayLabel = false;
280+
this.loadedMonthLabels.clear();
281+
282+
this.links = [...this.topics].flatMap((topic) => {
283+
const dateLabel = this.groupByDate(topic);
284+
return dateLabel
285+
? [dateLabel, new AiConversationLink(topic)]
286+
: [new AiConversationLink(topic)];
287+
});
213288
}
214289

215290
watchForTitleUpdate(topic) {

assets/stylesheets/modules/ai-bot-conversations/common.scss

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ body.has-ai-conversations-sidebar {
1010
margin: 1.8em 1rem 0;
1111
}
1212

13+
.sidebar-toggle-all-sections {
14+
display: none;
15+
}
16+
1317
.sidebar-wrapper {
1418
.ai-conversations-panel {
1519
padding-top: 1em;
@@ -18,19 +22,24 @@ body.has-ai-conversations-sidebar {
1822
// ai related sidebar content
1923
[data-section-name="ai-conversations-history"] {
2024
.sidebar-section-header-wrapper {
21-
pointer-events: none;
22-
font-size: var(--font-down-1);
23-
24-
.sidebar-section-header-caret {
25-
display: none;
26-
}
27-
28-
.sidebar-section-header-text {
29-
letter-spacing: 0.5px;
30-
}
25+
display: none;
3126
}
3227

3328
.sidebar-section-link-wrapper {
29+
.sidebar-section-link.date-heading {
30+
pointer-events: none;
31+
cursor: default;
32+
color: var(--primary-medium);
33+
font-weight: normal;
34+
opacity: 0.8;
35+
font-weight: 700;
36+
margin-top: 1em;
37+
38+
&[data-link-name="date-heading-last-7-days"] {
39+
margin-top: 0;
40+
}
41+
}
42+
3443
.sidebar-section-link {
3544
height: unset;
3645
padding-block: 0.65em;

config/locales/client.en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,8 @@ en:
727727
new: "New Question"
728728
min_input_length_message: "Message must be longer than 10 characters"
729729
messages_sidebar_title: "Conversations"
730+
last_7_days: "Last 7 days"
731+
last_30_days: "Last 30 days"
730732
sentiments:
731733
dashboard:
732734
title: "Sentiment"

0 commit comments

Comments
 (0)