Skip to content

Commit dfc7742

Browse files
committed
cleanup: use messages array to get the last query id
1 parent 6d5f12b commit dfc7742

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

clients/search-component/src/TrieveModal/Chat/FollowupQueries.tsx

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useModalState } from "../../utils/hooks/modal-context";
77

88
export const FollowupQueries = () => {
99
const { props, trieveSDK, fingerprint } = useModalState();
10-
const { isDoneReading, askQuestion } = useChatState();
10+
const { isDoneReading, askQuestion, messages } = useChatState();
1111
const { suggestedQuestions, isLoadingSuggestedQueries } =
1212
useFollowupQuestions();
1313
const [parent] = useAutoAnimate();
@@ -17,40 +17,36 @@ export const FollowupQueries = () => {
1717
}
1818

1919
const handleFollowupQuery = async (q: string) => {
20-
const lastMessage = JSON.parse(
21-
window.localStorage.getItem("lastMessage") ?? "{}",
22-
);
23-
24-
const requestId =
25-
Object.keys(lastMessage)[0] || "00000000-0000-0000-0000-000000000000";
26-
27-
await trieveSDK.sendAnalyticsEvent({
28-
event_name: `site-followup_query`,
29-
event_type: "click",
30-
user_id: fingerprint,
31-
location: window.location.href,
32-
metadata: {
33-
followup_query: q,
34-
component_props: props,
35-
},
36-
request: {
37-
request_id: requestId,
38-
request_type: "rag",
39-
},
40-
});
20+
const requestId = messages[messages.length - 1].queryId;
4121

22+
if (requestId) {
23+
await trieveSDK.sendAnalyticsEvent({
24+
event_name: `site-followup_query`,
25+
event_type: "click",
26+
user_id: fingerprint,
27+
location: window.location.href,
28+
metadata: {
29+
followup_query: q,
30+
component_props: props,
31+
},
32+
request: {
33+
request_id: requestId,
34+
request_type: "rag",
35+
},
36+
});
37+
};
4238
askQuestion(q);
43-
};
39+
40+
}
4441

4542
return (
4643
<div ref={parent} className="followup-questions">
4744
{suggestedQuestions?.map((q) => (
4845
<button
4946
onClick={() => handleFollowupQuery(q)}
5047
key={q}
51-
className={`followup-question ${
52-
isLoadingSuggestedQueries ? "loading" : ""
53-
}`}
48+
className={`followup-question ${isLoadingSuggestedQueries ? "loading" : ""
49+
}`}
5450
>
5551
<SparklesIcon className="followup-icon" />
5652
{q}

clients/search-component/src/TrieveModal/Chat/SuggestedQuestions.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,33 @@ export const SuggestedQuestions = ({
1616
const { suggestedQuestions, isLoadingSuggestedQueries, getQuestions } =
1717
useSuggestedQuestions();
1818

19-
const { props } = useModalState();
19+
const { props, trieveSDK, fingerprint } = useModalState();
2020
const [parent] = useAutoAnimate({ duration: 100 });
2121

2222
if (messages.length) {
2323
return null;
2424
}
2525

2626
const handleSuggestedQuestion = async (q: string) => {
27+
const requestId = messages[messages.length - 1].queryId;
28+
29+
if (requestId) {
30+
await trieveSDK.sendAnalyticsEvent({
31+
event_name: `site-followup_query`,
32+
event_type: "click",
33+
user_id: fingerprint,
34+
location: window.location.href,
35+
metadata: {
36+
followup_query: q,
37+
component_props: props,
38+
},
39+
request: {
40+
request_id: requestId,
41+
request_type: "rag",
42+
},
43+
});
44+
};
45+
2746
setCurrentQuestion(q);
2847
askQuestion(q);
2948
if (onMessageSend) {
@@ -65,9 +84,8 @@ export const SuggestedQuestions = ({
6584
handleSuggestedQuestion(q);
6685
}}
6786
key={q}
68-
className={`suggested-question tv-flex tv-gap-1 tv-items-center${
69-
isLoadingSuggestedQueries ? " loading" : ""
70-
}`}
87+
className={`suggested-question tv-flex tv-gap-1 tv-items-center${isLoadingSuggestedQueries ? " loading" : ""
88+
}`}
7189
>
7290
<SparklesIcon width={15} height={15} />
7391
{q}

0 commit comments

Comments
 (0)