home.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. "use client";
  2. import { useState, useRef, useEffect } from "react";
  3. import ReactMarkdown from "react-markdown";
  4. import "katex/dist/katex.min.css";
  5. import RemarkMath from "remark-math";
  6. import RehypeKatex from "rehype-katex";
  7. import EmojiPicker, { Emoji, EmojiClickData } from "emoji-picker-react";
  8. import { IconButton } from "./button";
  9. import styles from "./home.module.css";
  10. import SettingsIcon from "../icons/settings.svg";
  11. import GithubIcon from "../icons/github.svg";
  12. import ChatGptIcon from "../icons/chatgpt.svg";
  13. import SendWhiteIcon from "../icons/send-white.svg";
  14. import BrainIcon from "../icons/brain.svg";
  15. import ExportIcon from "../icons/export.svg";
  16. import BotIcon from "../icons/bot.svg";
  17. import AddIcon from "../icons/add.svg";
  18. import DeleteIcon from "../icons/delete.svg";
  19. import LoadingIcon from "../icons/three-dots.svg";
  20. import { Message, SubmitKey, useChatStore } from "../store";
  21. import { Card, List, ListItem, Popover } from "./ui-lib";
  22. export function Markdown(props: { content: string }) {
  23. return (
  24. <ReactMarkdown remarkPlugins={[RemarkMath]} rehypePlugins={[RehypeKatex]}>
  25. {props.content}
  26. </ReactMarkdown>
  27. );
  28. }
  29. export function Avatar(props: { role: Message["role"] }) {
  30. const config = useChatStore((state) => state.config);
  31. if (props.role === "assistant") {
  32. return <BotIcon className={styles["user-avtar"]} />;
  33. }
  34. return (
  35. <div className={styles["user-avtar"]}>
  36. <Emoji unified={config.avatar} size={18} />
  37. </div>
  38. );
  39. }
  40. export function ChatItem(props: {
  41. onClick?: () => void;
  42. onDelete?: () => void;
  43. title: string;
  44. count: number;
  45. time: string;
  46. selected: boolean;
  47. }) {
  48. return (
  49. <div
  50. className={`${styles["chat-item"]} ${
  51. props.selected && styles["chat-item-selected"]
  52. }`}
  53. onClick={props.onClick}
  54. >
  55. <div className={styles["chat-item-title"]}>{props.title}</div>
  56. <div className={styles["chat-item-info"]}>
  57. <div className={styles["chat-item-count"]}>{props.count} 条对话</div>
  58. <div className={styles["chat-item-date"]}>{props.time}</div>
  59. </div>
  60. <div className={styles["chat-item-delete"]} onClick={props.onDelete}>
  61. <DeleteIcon />
  62. </div>
  63. </div>
  64. );
  65. }
  66. export function ChatList() {
  67. const [sessions, selectedIndex, selectSession, removeSession] = useChatStore(
  68. (state) => [
  69. state.sessions,
  70. state.currentSessionIndex,
  71. state.selectSession,
  72. state.removeSession,
  73. ]
  74. );
  75. return (
  76. <div className={styles["chat-list"]}>
  77. {sessions.map((item, i) => (
  78. <ChatItem
  79. title={item.topic}
  80. time={item.lastUpdate}
  81. count={item.messages.length}
  82. key={i}
  83. selected={i === selectedIndex}
  84. onClick={() => selectSession(i)}
  85. onDelete={() => removeSession(i)}
  86. />
  87. ))}
  88. </div>
  89. );
  90. }
  91. export function Chat() {
  92. type RenderMessage = Message & { preview?: boolean };
  93. const session = useChatStore((state) => state.currentSession());
  94. const [userInput, setUserInput] = useState("");
  95. const [isLoading, setIsLoading] = useState(false);
  96. const onUserInput = useChatStore((state) => state.onUserInput);
  97. const onUserSubmit = () => {
  98. if (userInput.length <= 0) return;
  99. setIsLoading(true);
  100. onUserInput(userInput).then(() => setIsLoading(false));
  101. setUserInput("");
  102. };
  103. const onInputKeyDown = (e: KeyboardEvent) => {
  104. if (e.key === "Enter" && (e.shiftKey || e.ctrlKey || e.metaKey)) {
  105. onUserSubmit();
  106. e.preventDefault();
  107. }
  108. };
  109. const latestMessageRef = useRef<HTMLDivElement>(null);
  110. const messages = (session.messages as RenderMessage[])
  111. .concat(
  112. isLoading
  113. ? [
  114. {
  115. role: "assistant",
  116. content: "……",
  117. date: new Date().toLocaleString(),
  118. preview: true,
  119. },
  120. ]
  121. : []
  122. )
  123. .concat(
  124. userInput.length > 0
  125. ? [
  126. {
  127. role: "user",
  128. content: userInput,
  129. date: new Date().toLocaleString(),
  130. preview: true,
  131. },
  132. ]
  133. : []
  134. );
  135. useEffect(() => {
  136. latestMessageRef.current?.scrollIntoView({
  137. behavior: "smooth",
  138. block: "end",
  139. });
  140. });
  141. return (
  142. <div className={styles.chat} key={session.id}>
  143. <div className={styles["window-header"]}>
  144. <div>
  145. <div className={styles["window-header-title"]}>{session.topic}</div>
  146. <div className={styles["window-header-sub-title"]}>
  147. 与 ChatGPT 的 {session.messages.length} 条对话
  148. </div>
  149. </div>
  150. <div className={styles["chat-actions"]}>
  151. <div className={styles["chat-action-button"]}>
  152. <IconButton icon={<BrainIcon />} bordered />
  153. </div>
  154. <div className={styles["chat-action-button"]}>
  155. <IconButton icon={<ExportIcon />} bordered />
  156. </div>
  157. </div>
  158. </div>
  159. <div className={styles["chat-body"]}>
  160. {messages.map((message, i) => {
  161. const isUser = message.role === "user";
  162. return (
  163. <div
  164. key={i}
  165. className={
  166. isUser ? styles["chat-message-user"] : styles["chat-message"]
  167. }
  168. >
  169. <div className={styles["chat-message-container"]}>
  170. <div className={styles["chat-message-avatar"]}>
  171. <Avatar role={message.role} />
  172. </div>
  173. {(message.preview || message.streaming) && (
  174. <div className={styles["chat-message-status"]}>正在输入…</div>
  175. )}
  176. <div className={styles["chat-message-item"]}>
  177. {(message.preview || message.content.length === 0) &&
  178. !isUser ? (
  179. <LoadingIcon />
  180. ) : (
  181. <div className="markdown-body">
  182. <Markdown content={message.content} />
  183. </div>
  184. )}
  185. </div>
  186. {!isUser && !message.preview && (
  187. <div className={styles["chat-message-actions"]}>
  188. <div className={styles["chat-message-action-date"]}>
  189. {message.date.toLocaleString()}
  190. </div>
  191. </div>
  192. )}
  193. </div>
  194. </div>
  195. );
  196. })}
  197. <span ref={latestMessageRef} style={{ opacity: 0 }}>
  198. -
  199. </span>
  200. </div>
  201. <div className={styles["chat-input-panel"]}>
  202. <div className={styles["chat-input-panel-inner"]}>
  203. <textarea
  204. className={styles["chat-input"]}
  205. placeholder="输入消息,Ctrl + Enter 发送"
  206. rows={3}
  207. onInput={(e) => setUserInput(e.currentTarget.value)}
  208. value={userInput}
  209. onKeyDown={(e) => onInputKeyDown(e as any)}
  210. />
  211. <IconButton
  212. icon={<SendWhiteIcon />}
  213. text={"发送"}
  214. className={styles["chat-input-send"]}
  215. onClick={onUserSubmit}
  216. />
  217. </div>
  218. </div>
  219. </div>
  220. );
  221. }
  222. export function Home() {
  223. const [createNewSession] = useChatStore((state) => [state.newSession]);
  224. // settings
  225. const [openSettings, setOpenSettings] = useState(false);
  226. return (
  227. <div className={styles.container}>
  228. <div className={styles.sidebar}>
  229. <div className={styles["sidebar-header"]}>
  230. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  231. <div className={styles["sidebar-sub-title"]}>
  232. Build your own AI assistant.
  233. </div>
  234. <div className={styles["sidebar-logo"]}>
  235. <ChatGptIcon />
  236. </div>
  237. </div>
  238. <div className={styles["sidebar-body"]}>
  239. <ChatList />
  240. </div>
  241. <div className={styles["sidebar-tail"]}>
  242. <div className={styles["sidebar-actions"]}>
  243. <div className={styles["sidebar-action"]}>
  244. <IconButton
  245. icon={<SettingsIcon />}
  246. onClick={() => setOpenSettings(!openSettings)}
  247. />
  248. </div>
  249. <div className={styles["sidebar-action"]}>
  250. <a href="https://github.com/Yidadaa" target="_blank">
  251. <IconButton icon={<GithubIcon />} />
  252. </a>
  253. </div>
  254. </div>
  255. <div>
  256. <IconButton
  257. icon={<AddIcon />}
  258. text={"新的聊天"}
  259. onClick={createNewSession}
  260. />
  261. </div>
  262. </div>
  263. </div>
  264. <div className={styles["window-content"]}>
  265. {openSettings ? <Settings /> : <Chat key="chat" />}
  266. </div>
  267. </div>
  268. );
  269. }
  270. export function EmojiPickerModal(props: {
  271. show: boolean;
  272. onClose: (_: boolean) => void;
  273. }) {
  274. return <div className=""></div>;
  275. }
  276. export function Settings() {
  277. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  278. const [config, updateConfig] = useChatStore((state) => [
  279. state.config,
  280. state.updateConfig,
  281. ]);
  282. return (
  283. <>
  284. <div className={styles["window-header"]}>
  285. <div>
  286. <div className={styles["window-header-title"]}>设置</div>
  287. <div className={styles["window-header-sub-title"]}>设置选项</div>
  288. </div>
  289. </div>
  290. <div className={styles["settings"]}>
  291. <List>
  292. <ListItem>
  293. <div className={styles["settings-title"]}>头像</div>
  294. <Popover
  295. onClose={() => setShowEmojiPicker(false)}
  296. content={
  297. <EmojiPicker
  298. lazyLoadEmojis
  299. onEmojiClick={(e) => {
  300. updateConfig((config) => (config.avatar = e.unified));
  301. setShowEmojiPicker(false);
  302. }}
  303. />
  304. }
  305. open={showEmojiPicker}
  306. >
  307. <div
  308. className={styles.avatar}
  309. onClick={() => setShowEmojiPicker(true)}
  310. >
  311. <Avatar role="user" />
  312. </div>
  313. </Popover>
  314. </ListItem>
  315. <ListItem>
  316. <div className={styles["settings-title"]}>发送键</div>
  317. <div className="">
  318. <select
  319. value={config.submitKey}
  320. onChange={(e) => {
  321. updateConfig(
  322. (config) =>
  323. (config.submitKey = e.target.value as any as SubmitKey)
  324. );
  325. }}
  326. >
  327. {Object.entries(SubmitKey).map(([k, v]) => (
  328. <option value={k} key={v}>
  329. {v}
  330. </option>
  331. ))}
  332. </select>
  333. </div>
  334. </ListItem>
  335. </List>
  336. <List>
  337. <ListItem>
  338. <div className={styles["settings-title"]}>最大记忆历史消息数</div>
  339. <div className="">{config.historyMessageCount}</div>
  340. </ListItem>
  341. <ListItem>
  342. <div className={styles["settings-title"]}>发送机器人回复消息</div>
  343. <div className="">{config.sendBotMessages ? "是" : "否"}</div>
  344. </ListItem>
  345. </List>
  346. </div>
  347. </>
  348. );
  349. }