home.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 { IconButton } from "./button";
  8. import styles from "./home.module.css";
  9. import SettingsIcon from "../icons/settings.svg";
  10. import GithubIcon from "../icons/github.svg";
  11. import ChatGptIcon from "../icons/chatgpt.svg";
  12. import SendWhiteIcon from "../icons/send-white.svg";
  13. import BrainIcon from "../icons/brain.svg";
  14. import ExportIcon from "../icons/export.svg";
  15. import BotIcon from "../icons/bot.svg";
  16. import AddIcon from "../icons/add.svg";
  17. import DeleteIcon from "../icons/delete.svg";
  18. import LoadingIcon from "../icons/three-dots.svg";
  19. import { Message, useChatStore } from "../store";
  20. export function Markdown(props: { content: string }) {
  21. return (
  22. <ReactMarkdown remarkPlugins={[RemarkMath]} rehypePlugins={[RehypeKatex]}>
  23. {props.content}
  24. </ReactMarkdown>
  25. );
  26. }
  27. export function Avatar(props: { role: Message["role"] }) {
  28. if (props.role === "assistant") {
  29. return <BotIcon className={styles["user-avtar"]} />;
  30. }
  31. return <div className={styles["user-avtar"]}>🤣</div>;
  32. }
  33. export function ChatItem(props: {
  34. onClick?: () => void;
  35. onDelete?: () => void;
  36. title: string;
  37. count: number;
  38. time: string;
  39. selected: boolean;
  40. }) {
  41. return (
  42. <div
  43. className={`${styles["chat-item"]} ${
  44. props.selected && styles["chat-item-selected"]
  45. }`}
  46. onClick={props.onClick}
  47. >
  48. <div className={styles["chat-item-title"]}>{props.title}</div>
  49. <div className={styles["chat-item-info"]}>
  50. <div className={styles["chat-item-count"]}>{props.count} 条对话</div>
  51. <div className={styles["chat-item-date"]}>{props.time}</div>
  52. </div>
  53. <div className={styles["chat-item-delete"]} onClick={props.onDelete}>
  54. <DeleteIcon />
  55. </div>
  56. </div>
  57. );
  58. }
  59. export function ChatList() {
  60. const [sessions, selectedIndex, selectSession, removeSession] = useChatStore(
  61. (state) => [
  62. state.sessions,
  63. state.currentSessionIndex,
  64. state.selectSession,
  65. state.removeSession,
  66. ]
  67. );
  68. return (
  69. <div className={styles["chat-list"]}>
  70. {sessions.map((item, i) => (
  71. <ChatItem
  72. title={item.topic}
  73. time={item.lastUpdate}
  74. count={item.messages.length}
  75. key={i}
  76. selected={i === selectedIndex}
  77. onClick={() => selectSession(i)}
  78. onDelete={() => removeSession(i)}
  79. />
  80. ))}
  81. </div>
  82. );
  83. }
  84. export function Chat() {
  85. type RenderMessage = Message & { preview?: boolean };
  86. const session = useChatStore((state) => state.currentSession());
  87. const [userInput, setUserInput] = useState("");
  88. const [isLoading, setIsLoading] = useState(false);
  89. const onUserInput = useChatStore((state) => state.onUserInput);
  90. const onUserSubmit = () => {
  91. if (userInput.length <= 0) return;
  92. setIsLoading(true);
  93. onUserInput(userInput).then(() => setIsLoading(false));
  94. setUserInput("");
  95. };
  96. const onInputKeyDown = (e: KeyboardEvent) => {
  97. if (e.key === "Enter" && (e.shiftKey || e.ctrlKey || e.metaKey)) {
  98. onUserSubmit();
  99. e.preventDefault();
  100. }
  101. };
  102. const latestMessageRef = useRef<HTMLDivElement>(null);
  103. const messages = (session.messages as RenderMessage[])
  104. .concat(
  105. isLoading
  106. ? [
  107. {
  108. role: "assistant",
  109. content: "……",
  110. date: new Date().toLocaleString(),
  111. preview: true,
  112. },
  113. ]
  114. : []
  115. )
  116. .concat(
  117. userInput.length > 0
  118. ? [
  119. {
  120. role: "user",
  121. content: userInput,
  122. date: new Date().toLocaleString(),
  123. preview: true,
  124. },
  125. ]
  126. : []
  127. );
  128. useEffect(() => {
  129. latestMessageRef.current?.scrollIntoView({
  130. behavior: "smooth",
  131. block: "end",
  132. });
  133. });
  134. return (
  135. <div className={styles.chat} key={session.topic}>
  136. <div className={styles["chat-header"]}>
  137. <div>
  138. <div className={styles["chat-header-title"]}>{session.topic}</div>
  139. <div className={styles["chat-header-sub-title"]}>
  140. 与 ChatGPT 的 {session.messages.length} 条对话
  141. </div>
  142. </div>
  143. <div className={styles["chat-actions"]}>
  144. <div className={styles["chat-action-button"]}>
  145. <IconButton icon={<BrainIcon />} bordered />
  146. </div>
  147. <div className={styles["chat-action-button"]}>
  148. <IconButton icon={<ExportIcon />} bordered />
  149. </div>
  150. </div>
  151. </div>
  152. <div className={styles["chat-body"]}>
  153. {messages.map((message, i) => {
  154. const isUser = message.role === "user";
  155. return (
  156. <div
  157. key={i}
  158. className={
  159. isUser ? styles["chat-message-user"] : styles["chat-message"]
  160. }
  161. >
  162. <div className={styles["chat-message-container"]}>
  163. <div className={styles["chat-message-avatar"]}>
  164. <Avatar role={message.role} />
  165. </div>
  166. {message.preview && (
  167. <div className={styles["chat-message-status"]}>正在输入…</div>
  168. )}
  169. <div className={styles["chat-message-item"]}>
  170. {(message.preview || message.content.length === 0) &&
  171. !isUser ? (
  172. <LoadingIcon />
  173. ) : (
  174. <div className="markdown-body">
  175. <Markdown content={message.content} />
  176. </div>
  177. )}
  178. </div>
  179. {!isUser && !message.preview && (
  180. <div className={styles["chat-message-actions"]}>
  181. <div className={styles["chat-message-action-date"]}>
  182. {message.date.toLocaleString()}
  183. </div>
  184. </div>
  185. )}
  186. </div>
  187. </div>
  188. );
  189. })}
  190. <span ref={latestMessageRef} style={{ opacity: 0 }}>
  191. -
  192. </span>
  193. </div>
  194. <div className={styles["chat-input-panel"]}>
  195. <div className={styles["chat-input-panel-inner"]}>
  196. <textarea
  197. className={styles["chat-input"]}
  198. placeholder="输入消息,Ctrl + Enter 发送"
  199. rows={3}
  200. onInput={(e) => setUserInput(e.currentTarget.value)}
  201. value={userInput}
  202. onKeyDown={(e) => onInputKeyDown(e as any)}
  203. />
  204. <IconButton
  205. icon={<SendWhiteIcon />}
  206. text={"发送"}
  207. className={styles["chat-input-send"]}
  208. onClick={onUserSubmit}
  209. />
  210. </div>
  211. </div>
  212. </div>
  213. );
  214. }
  215. export function Home() {
  216. const [createNewSession] = useChatStore((state) => [state.newSession]);
  217. return (
  218. <div className={styles.container}>
  219. <div className={styles.sidebar}>
  220. <div className={styles["sidebar-header"]}>
  221. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  222. <div className={styles["sidebar-sub-title"]}>
  223. Build your own AI assistant.
  224. </div>
  225. <div className={styles["sidebar-logo"]}>
  226. <ChatGptIcon />
  227. </div>
  228. </div>
  229. <div className={styles["sidebar-body"]}>
  230. <ChatList />
  231. </div>
  232. <div className={styles["sidebar-tail"]}>
  233. <div className={styles["sidebar-actions"]}>
  234. <div className={styles["sidebar-action"]}>
  235. <IconButton icon={<SettingsIcon />} />
  236. </div>
  237. <div className={styles["sidebar-action"]}>
  238. <a href="https://github.com/Yidadaa" target="_blank">
  239. <IconButton icon={<GithubIcon />} />
  240. </a>
  241. </div>
  242. </div>
  243. <div>
  244. <IconButton
  245. icon={<AddIcon />}
  246. text={"新的聊天"}
  247. onClick={createNewSession}
  248. />
  249. </div>
  250. </div>
  251. </div>
  252. <Chat key="chat" />
  253. </div>
  254. );
  255. }