home.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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({
  120. behavior: "smooth",
  121. block: "end",
  122. });
  123. });
  124. return (
  125. <div className={styles.chat} key={session.topic}>
  126. <div className={styles["chat-header"]}>
  127. <div>
  128. <div className={styles["chat-header-title"]}>{session.topic}</div>
  129. <div className={styles["chat-header-sub-title"]}>
  130. 与 ChatGPT 的 {session.messages.length} 条对话
  131. </div>
  132. </div>
  133. <div className={styles["chat-actions"]}>
  134. <div className={styles["chat-action-button"]}>
  135. <IconButton icon={<BrainIcon />} bordered />
  136. </div>
  137. <div className={styles["chat-action-button"]}>
  138. <IconButton icon={<ExportIcon />} bordered />
  139. </div>
  140. </div>
  141. </div>
  142. <div className={styles["chat-body"]}>
  143. {messages.map((message, i) => {
  144. const isUser = message.role === "user";
  145. return (
  146. <div
  147. key={i}
  148. className={
  149. isUser ? styles["chat-message-user"] : styles["chat-message"]
  150. }
  151. >
  152. <div className={styles["chat-message-container"]}>
  153. <div className={styles["chat-message-avatar"]}>
  154. <Avatar role={message.role} />
  155. </div>
  156. {message.preview && (
  157. <div className={styles["chat-message-status"]}>正在输入…</div>
  158. )}
  159. <div className={styles["chat-message-item"]}>
  160. {message.preview && !isUser ? (
  161. <LoadingIcon />
  162. ) : (
  163. <div className="markdown-body">
  164. <ReactMarkdown>{message.content}</ReactMarkdown>
  165. </div>
  166. )}
  167. </div>
  168. {!isUser && !message.preview && (
  169. <div className={styles["chat-message-actions"]}>
  170. <div className={styles["chat-message-action-date"]}>
  171. {message.date.toLocaleString()}
  172. </div>
  173. </div>
  174. )}
  175. </div>
  176. </div>
  177. );
  178. })}
  179. <span ref={latestMessageRef} style={{ opacity: 0 }}>
  180. -
  181. </span>
  182. </div>
  183. <div className={styles["chat-input-panel"]}>
  184. <div className={styles["chat-input-panel-inner"]}>
  185. <textarea
  186. className={styles["chat-input"]}
  187. placeholder="输入消息,Ctrl + Enter 发送"
  188. rows={3}
  189. onInput={(e) => setUserInput(e.currentTarget.value)}
  190. value={userInput}
  191. onKeyDown={(e) => onInputKeyDown(e as any)}
  192. />
  193. <IconButton
  194. icon={<SendWhiteIcon />}
  195. text={"发送"}
  196. className={styles["chat-input-send"]}
  197. onClick={onUserSubmit}
  198. />
  199. </div>
  200. </div>
  201. </div>
  202. );
  203. }
  204. export function Home() {
  205. const [createNewSession] = useChatStore((state) => [state.newSession]);
  206. return (
  207. <div className={styles.container}>
  208. <div className={styles.sidebar}>
  209. <div className={styles["sidebar-header"]}>
  210. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  211. <div className={styles["sidebar-sub-title"]}>
  212. Build your own AI assistant.
  213. </div>
  214. <div className={styles["sidebar-logo"]}>
  215. <ChatGptIcon />
  216. </div>
  217. </div>
  218. <div className={styles["sidebar-body"]}>
  219. <ChatList />
  220. </div>
  221. <div className={styles["sidebar-tail"]}>
  222. <div className={styles["sidebar-actions"]}>
  223. <div className={styles["sidebar-action"]}>
  224. <IconButton icon={<SettingsIcon />} />
  225. </div>
  226. <div className={styles["sidebar-action"]}>
  227. <a href="https://github.com/Yidadaa" target="_blank">
  228. <IconButton icon={<GithubIcon />} />
  229. </a>
  230. </div>
  231. </div>
  232. <div>
  233. <IconButton
  234. icon={<AddIcon />}
  235. text={"新的聊天"}
  236. onClick={createNewSession}
  237. />
  238. </div>
  239. </div>
  240. </div>
  241. <Chat key="chat" />
  242. </div>
  243. );
  244. }