Skip to content

Commit 5f55549

Browse files
drew-harrisskeptrunedev
authored andcommitted
fix: timeline date ordering
1 parent bc14a50 commit 5f55549

2 files changed

Lines changed: 21 additions & 19 deletions

File tree

clients/trieve-shopify-extension/app/routes/app._dashboard.chatview.$topic.tsx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "app/components/chat/MessageEventTimeline";
2727
import { EventsForTopicResponse } from "trieve-ts-sdk";
2828
import { format } from "date-fns";
29+
import { parseCustomDateString } from "app/utils/formatting";
2930

3031
export const links: LinksFunction = () => {
3132
return [{ rel: "stylesheet", href: styles }];
@@ -59,19 +60,8 @@ export default function ChatRoute() {
5960
new Date(b.created_at).getUTCSeconds() -
6061
new Date(a.created_at).getUTCSeconds(),
6162
);
62-
if (sortedMessages.length > 0) {
63-
events.push({
64-
date: new Date(sortedMessages.at(0)!.created_at),
65-
type: "New Chat",
66-
additional: format(
67-
new Date(sortedMessages.at(0)!.created_at),
68-
"h:m aa, LLL d y",
69-
),
70-
icon: <PlusIcon width={20} fill="#000" height={20} />,
71-
});
72-
}
7363

74-
messages.forEach((message) => {
64+
sortedMessages.forEach((message) => {
7565
if (message.role === "user") {
7666
events.push({
7767
date: new Date(message.created_at),
@@ -86,23 +76,35 @@ export default function ChatRoute() {
8676
if (event.event_name === "View") return;
8777
if (event.event_name === "Click") {
8878
events.push({
89-
date: new Date(event.created_at),
79+
date: new Date(parseCustomDateString(event.updated_at, false)),
9080
type: "Click on Product",
9181
icon: <CursorIcon width={20} fill="#000" height={20} />,
9282
});
9383
}
9484
if (event.event_name === "site-add_to_cart") {
9585
events.push({
96-
date: new Date(event.created_at),
86+
date: new Date(parseCustomDateString(event.updated_at, false)),
9787
type: "Add To Cart",
9888
icon: <CartIcon width={20} fill="#000" height={20} />,
9989
});
10090
}
10191
});
10292

103-
const result = events.sort(
104-
(a, b) => b.date.getUTCSeconds() - a.date.getUTCSeconds(),
105-
);
93+
const result = events.sort((a, b) => a.date.getTime() - b.date.getTime());
94+
95+
if (sortedMessages.length > 0) {
96+
result.unshift({
97+
date: new Date(sortedMessages.at(0)!.created_at),
98+
type: "New Chat",
99+
additional: format(
100+
new Date(
101+
parseCustomDateString(sortedMessages.at(0)!.created_at, true),
102+
),
103+
"h:m aa, LLL d y",
104+
),
105+
icon: <PlusIcon width={20} fill="#000" height={20} />,
106+
});
107+
}
106108
return result;
107109
};
108110

clients/trieve-shopify-extension/app/utils/formatting.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const formatDateForApi = (date: Date) => {
2626
.replace(",", "");
2727
};
2828

29-
export const parseCustomDateString = (dateString: string) => {
29+
export const parseCustomDateString = (dateString: string, isUtc = true) => {
3030
const [datePart, timePart] = dateString.includes(" ")
3131
? dateString.split(" ")
3232
: dateString.split("T");
@@ -42,7 +42,7 @@ export const parseCustomDateString = (dateString: string) => {
4242
minute = minute.padStart(2, "0");
4343
wholeSec = wholeSec.padStart(2, "0");
4444

45-
const isoString = `${year}-${month}-${day}T${hour}:${minute}:${wholeSec}Z`;
45+
const isoString = `${year}-${month}-${day}T${hour}:${minute}:${wholeSec}${isUtc ? "Z" : ""}`;
4646
return new Date(isoString);
4747
};
4848

0 commit comments

Comments
 (0)