-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroute-chat.tsx
83 lines (77 loc) · 3.03 KB
/
route-chat.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { useParams } from "react-router-dom";
import { useQueryGetWorkspaceMessages } from "@/hooks/use-query-get-workspace-messages";
import { parsingPromptText, sanitizeQuestionPrompt } from "@/lib/utils";
import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
import {
ChatBubble,
ChatBubbleAvatar,
ChatBubbleMessage,
} from "@/components/ui/chat/chat-bubble";
import { Markdown } from "@/components/Markdown";
import { Breadcrumb, Breadcrumbs, Card, CardBody } from "@stacklok/ui-kit";
import { BreadcrumbHome } from "@/components/BreadcrumbHome";
import { useQueryGetWorkspaceAlertTable } from "@/features/alerts/hooks/use-query-get-workspace-alerts-table";
import { AlertDetail } from "@/components/AlertDetail";
export function RouteChat() {
const { id } = useParams();
const { data = [] } = useQueryGetWorkspaceAlertTable();
const { data: prompts } = useQueryGetWorkspaceMessages();
const chat = prompts?.find((prompt) => prompt.chat_id === id);
const title =
chat === undefined ||
chat.question_answers?.[0]?.question?.message === undefined
? `Prompt ${id}`
: parsingPromptText(
sanitizeQuestionPrompt({
question: chat.question_answers?.[0].question.message,
answer: chat.question_answers?.[0]?.answer?.message ?? "",
}),
chat.conversation_timestamp,
);
// we have an issue on BE, we received duplicated alerts
const alertDetail = data.filter((alert) =>
alert.conversation.question_answers.some(
(item) => item.question.message_id === id,
),
)[0];
return (
<>
<Breadcrumbs>
<BreadcrumbHome />
<Breadcrumb className="w-96 block truncate">{title}</Breadcrumb>
</Breadcrumbs>
<div className="w-[calc(100vw-18rem)]">
{alertDetail && (
<Card className="w-full mb-2">
<CardBody className="w-full h-fit overflow-auto max-h-[500px]">
<AlertDetail alert={alertDetail} />
</CardBody>
</Card>
)}
<ChatMessageList>
{(chat?.question_answers ?? []).map(({ question, answer }, index) => (
<div key={index} className="flex flex-col size-full gap-6">
<ChatBubble variant="sent">
<ChatBubbleAvatar data-testid="avatar-user" fallback="User" />
<ChatBubbleMessage variant="sent">
<Markdown>
{sanitizeQuestionPrompt({
question: question?.message ?? "",
answer: answer?.message ?? "",
})}
</Markdown>
</ChatBubbleMessage>
</ChatBubble>
<ChatBubble variant="received">
<ChatBubbleAvatar data-testid="avatar-ai" fallback="AI" />
<ChatBubbleMessage variant="received">
<Markdown>{answer?.message ?? ""}</Markdown>
</ChatBubbleMessage>
</ChatBubble>
</div>
))}
</ChatMessageList>
</div>
</>
);
}