home.tsx 7.7 KB

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