forked from sqlchat/sqlchat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageView.tsx
More file actions
164 lines (158 loc) · 7.17 KB
/
Copy pathMessageView.tsx
File metadata and controls
164 lines (158 loc) · 7.17 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import dayjs from "dayjs";
import { ReactElement } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "react-hot-toast";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { useConversationStore, useConnectionStore, useMessageStore, useUserStore, useSettingStore } from "@/store";
import { Message } from "@/types";
import Dropdown, { DropdownItem } from "../kit/Dropdown";
import Icon from "../Icon";
import { CodeBlock } from "../CodeBlock";
import EngineIcon from "../EngineIcon";
import ThreeDotsLoader from "./ThreeDotsLoader";
interface Props {
message: Message;
}
const MessageView = (props: Props) => {
const message = props.message;
const { t } = useTranslation();
const settingStore = useSettingStore();
const userStore = useUserStore();
const conversationStore = useConversationStore();
const connectionStore = useConnectionStore();
const messageStore = useMessageStore();
const isCurrentUser = message.creatorId === userStore.currentUser.id;
const connection = connectionStore.getConnectionById(conversationStore.getConversationById(message.conversationId)?.connectionId || "");
const copyMessage = () => {
navigator.clipboard.writeText(message.content);
toast.success("Copied to clipboard");
};
const deleteMessage = (message: Message) => {
messageStore.clearMessage((item) => item.id !== message.id);
};
return (
<div
className={`w-full max-w-full flex flex-row justify-start items-start my-4 group ${
isCurrentUser ? "justify-end sm:pl-24" : "sm:pr-24"
}`}
>
{isCurrentUser ? (
<>
<div className="invisible group-hover:visible">
<Dropdown
tigger={
<button className="w-6 h-6 mr-1 mt-2 shrink-0 flex justify-center items-center text-gray-400 hover:text-gray-500">
<Icon.IoMdMore className="w-5 h-auto" />
</button>
}
>
<div className="p-1 flex flex-col justify-start items-start bg-white dark:bg-zinc-900 rounded-lg">
<DropdownItem
className="w-full p-1 px-2 flex flex-row justify-start items-center rounded-lg cursor-pointer hover:bg-gray-100 dark:hover:bg-zinc-800"
onClick={copyMessage}
>
<Icon.BiClipboard className="w-4 h-auto mr-2 opacity-70" />
{t("common.copy")}
</DropdownItem>
<DropdownItem
className="w-full p-1 px-2 flex flex-row justify-start items-center rounded-lg cursor-pointer hover:bg-gray-100 dark:hover:bg-zinc-800"
onClick={() => deleteMessage(message)}
>
<Icon.BiTrash className="w-4 h-auto mr-2 opacity-70" />
{t("common.delete")}
</DropdownItem>
</div>
</Dropdown>
</div>
<div className="w-auto max-w-[calc(100%-2rem)] flex flex-col justify-start items-start">
<div className="w-full bg-indigo-600 text-white dark:text-gray-200 px-4 py-2 rounded-lg whitespace-pre-wrap break-all">
{message.content}
</div>
</div>
<div className="w-10 h-10 p-1 border dark:border-zinc-700 rounded-full flex justify-center items-center ml-2 shrink-0">
<Icon.AiOutlineUser className="w-6 h-6" />
</div>
</>
) : (
<>
<div className="flex justify-center items-center mr-2 shrink-0">
{connection ? (
<EngineIcon className="w-10 h-auto p-1 border dark:border-zinc-700 rounded-full" engine={connection.engineType} />
) : (
<img className="w-10 h-auto p-1" src="/chat-logo-bot.webp" alt="" />
)}
</div>
{message.status === "LOADING" && message.content === "" ? (
<div className="mt-0.5 w-12 bg-gray-100 dark:bg-zinc-700 px-4 py-2 rounded-lg">
<ThreeDotsLoader />
</div>
) : (
<>
<div className="w-auto max-w-[calc(100%-2rem)] flex flex-col justify-start items-start">
<ReactMarkdown
className={`w-auto max-w-full bg-gray-100 dark:bg-zinc-700 px-4 py-2 rounded-lg prose prose-neutral dark:prose-invert ${
message.status === "FAILED" && "border border-red-400 bg-red-100 text-red-500"
}`}
remarkPlugins={[remarkGfm]}
components={{
pre({ node, className, children, ...props }) {
const child = children[0] as ReactElement;
const match = /language-(\w+)/.exec(child.props.className || "");
const language = match ? match[1] : "SQL";
return (
<pre className={`${className || ""} w-full p-0 my-1`} {...props}>
<CodeBlock
key={Math.random()}
language={language || "SQL"}
value={String(child.props.children).replace(/\n$/, "")}
{...props}
/>
</pre>
);
},
code({ children }) {
return <code className="px-0">{children}</code>;
},
}}
>
{message.content}
</ReactMarkdown>
<span className="self-end text-sm text-gray-400 pt-1 pr-1">
{dayjs(message.createdAt).locale(settingStore.setting.locale).format("lll")}
</span>
</div>
<div className="invisible group-hover:visible">
<Dropdown
tigger={
<button className="w-6 h-6 ml-1 mt-2 shrink-0 flex justify-center items-center text-gray-400 hover:text-gray-500">
<Icon.IoMdMore className="w-5 h-auto" />
</button>
}
>
<div className="p-1 flex flex-col justify-start items-start bg-white dark:bg-zinc-900 rounded-lg">
<DropdownItem
className="w-full p-1 px-2 flex flex-row justify-start items-center rounded-lg cursor-pointer hover:bg-gray-100 dark:hover:bg-zinc-800"
onClick={copyMessage}
>
<Icon.BiClipboard className="w-4 h-auto mr-2 opacity-70" />
{t("common.copy")}
</DropdownItem>
<DropdownItem
className="w-full p-1 px-2 flex flex-row justify-start items-center rounded-lg cursor-pointer hover:bg-gray-100 dark:hover:bg-zinc-800"
onClick={() => deleteMessage(message)}
>
<Icon.BiTrash className="w-4 h-auto mr-2 opacity-70" />
{t("common.delete")}
</DropdownItem>
</div>
</Dropdown>
</div>
</>
)}
</>
)}
</div>
);
};
export default MessageView;