chat.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. import { useDebounce, useDebouncedCallback } from "use-debounce";
  2. import { memo, useState, useRef, useEffect, useLayoutEffect } from "react";
  3. import SendWhiteIcon from "../icons/send-white.svg";
  4. import BrainIcon from "../icons/brain.svg";
  5. import ExportIcon from "../icons/share.svg";
  6. import ReturnIcon from "../icons/return.svg";
  7. import CopyIcon from "../icons/copy.svg";
  8. import DownloadIcon from "../icons/download.svg";
  9. import LoadingIcon from "../icons/three-dots.svg";
  10. import BotIcon from "../icons/bot.svg";
  11. import AddIcon from "../icons/add.svg";
  12. import DeleteIcon from "../icons/delete.svg";
  13. import MaxIcon from "../icons/max.svg";
  14. import MinIcon from "../icons/min.svg";
  15. import LightIcon from "../icons/light.svg";
  16. import DarkIcon from "../icons/dark.svg";
  17. import AutoIcon from "../icons/auto.svg";
  18. import BottomIcon from "../icons/bottom.svg";
  19. import {
  20. Message,
  21. SubmitKey,
  22. useChatStore,
  23. BOT_HELLO,
  24. ROLES,
  25. createMessage,
  26. useAccessStore,
  27. Theme,
  28. } from "../store";
  29. import {
  30. copyToClipboard,
  31. downloadAs,
  32. getEmojiUrl,
  33. isMobileScreen,
  34. selectOrCopy,
  35. autoGrowTextArea,
  36. getCSSVar,
  37. } from "../utils";
  38. import dynamic from "next/dynamic";
  39. import { ControllerPool } from "../requests";
  40. import { Prompt, usePromptStore } from "../store/prompt";
  41. import Locale from "../locales";
  42. import { IconButton } from "./button";
  43. import styles from "./home.module.scss";
  44. import chatStyle from "./chat.module.scss";
  45. import { Input, Modal, showModal } from "./ui-lib";
  46. const Markdown = dynamic(
  47. async () => memo((await import("./markdown")).Markdown),
  48. {
  49. loading: () => <LoadingIcon />,
  50. },
  51. );
  52. const Emoji = dynamic(async () => (await import("emoji-picker-react")).Emoji, {
  53. loading: () => <LoadingIcon />,
  54. });
  55. export function Avatar(props: { role: Message["role"] }) {
  56. const config = useChatStore((state) => state.config);
  57. if (props.role !== "user") {
  58. return (
  59. <div className="no-dark">
  60. <BotIcon className={styles["user-avtar"]} />
  61. </div>
  62. );
  63. }
  64. return (
  65. <div className={styles["user-avtar"]}>
  66. <Emoji unified={config.avatar} size={18} getEmojiUrl={getEmojiUrl} />
  67. </div>
  68. );
  69. }
  70. function exportMessages(messages: Message[], topic: string) {
  71. const mdText =
  72. `# ${topic}\n\n` +
  73. messages
  74. .map((m) => {
  75. return m.role === "user"
  76. ? `## ${Locale.Export.MessageFromYou}:\n${m.content}`
  77. : `## ${Locale.Export.MessageFromChatGPT}:\n${m.content.trim()}`;
  78. })
  79. .join("\n\n");
  80. const filename = `${topic}.md`;
  81. showModal({
  82. title: Locale.Export.Title,
  83. children: (
  84. <div className="markdown-body">
  85. <pre className={styles["export-content"]}>{mdText}</pre>
  86. </div>
  87. ),
  88. actions: [
  89. <IconButton
  90. key="copy"
  91. icon={<CopyIcon />}
  92. bordered
  93. text={Locale.Export.Copy}
  94. onClick={() => copyToClipboard(mdText)}
  95. />,
  96. <IconButton
  97. key="download"
  98. icon={<DownloadIcon />}
  99. bordered
  100. text={Locale.Export.Download}
  101. onClick={() => downloadAs(mdText, filename)}
  102. />,
  103. ],
  104. });
  105. }
  106. function PromptToast(props: {
  107. showToast?: boolean;
  108. showModal?: boolean;
  109. setShowModal: (_: boolean) => void;
  110. }) {
  111. const chatStore = useChatStore();
  112. const session = chatStore.currentSession();
  113. const context = session.context;
  114. const addContextPrompt = (prompt: Message) => {
  115. chatStore.updateCurrentSession((session) => {
  116. session.context.push(prompt);
  117. });
  118. };
  119. const removeContextPrompt = (i: number) => {
  120. chatStore.updateCurrentSession((session) => {
  121. session.context.splice(i, 1);
  122. });
  123. };
  124. const updateContextPrompt = (i: number, prompt: Message) => {
  125. chatStore.updateCurrentSession((session) => {
  126. session.context[i] = prompt;
  127. });
  128. };
  129. return (
  130. <div className={chatStyle["prompt-toast"]} key="prompt-toast">
  131. {props.showToast && (
  132. <div
  133. className={chatStyle["prompt-toast-inner"] + " clickable"}
  134. role="button"
  135. onClick={() => props.setShowModal(true)}
  136. >
  137. <BrainIcon />
  138. <span className={chatStyle["prompt-toast-content"]}>
  139. {Locale.Context.Toast(context.length)}
  140. </span>
  141. </div>
  142. )}
  143. {props.showModal && (
  144. <div className="modal-mask">
  145. <Modal
  146. title={Locale.Context.Edit}
  147. onClose={() => props.setShowModal(false)}
  148. actions={[
  149. <IconButton
  150. key="reset"
  151. icon={<CopyIcon />}
  152. bordered
  153. text={Locale.Memory.Reset}
  154. onClick={() =>
  155. confirm(Locale.Memory.ResetConfirm) &&
  156. chatStore.resetSession()
  157. }
  158. />,
  159. <IconButton
  160. key="copy"
  161. icon={<CopyIcon />}
  162. bordered
  163. text={Locale.Memory.Copy}
  164. onClick={() => copyToClipboard(session.memoryPrompt)}
  165. />,
  166. ]}
  167. >
  168. <>
  169. <div className={chatStyle["context-prompt"]}>
  170. {context.map((c, i) => (
  171. <div className={chatStyle["context-prompt-row"]} key={i}>
  172. <select
  173. value={c.role}
  174. className={chatStyle["context-role"]}
  175. onChange={(e) =>
  176. updateContextPrompt(i, {
  177. ...c,
  178. role: e.target.value as any,
  179. })
  180. }
  181. >
  182. {ROLES.map((r) => (
  183. <option key={r} value={r}>
  184. {r}
  185. </option>
  186. ))}
  187. </select>
  188. <Input
  189. value={c.content}
  190. type="text"
  191. className={chatStyle["context-content"]}
  192. rows={1}
  193. onInput={(e) =>
  194. updateContextPrompt(i, {
  195. ...c,
  196. content: e.currentTarget.value as any,
  197. })
  198. }
  199. />
  200. <IconButton
  201. icon={<DeleteIcon />}
  202. className={chatStyle["context-delete-button"]}
  203. onClick={() => removeContextPrompt(i)}
  204. bordered
  205. />
  206. </div>
  207. ))}
  208. <div className={chatStyle["context-prompt-row"]}>
  209. <IconButton
  210. icon={<AddIcon />}
  211. text={Locale.Context.Add}
  212. bordered
  213. className={chatStyle["context-prompt-button"]}
  214. onClick={() =>
  215. addContextPrompt({
  216. role: "system",
  217. content: "",
  218. date: "",
  219. })
  220. }
  221. />
  222. </div>
  223. </div>
  224. <div className={chatStyle["memory-prompt"]}>
  225. <div className={chatStyle["memory-prompt-title"]}>
  226. <span>
  227. {Locale.Memory.Title} ({session.lastSummarizeIndex} of{" "}
  228. {session.messages.length})
  229. </span>
  230. <label className={chatStyle["memory-prompt-action"]}>
  231. {Locale.Memory.Send}
  232. <input
  233. type="checkbox"
  234. checked={session.sendMemory}
  235. onChange={() =>
  236. chatStore.updateCurrentSession(
  237. (session) =>
  238. (session.sendMemory = !session.sendMemory),
  239. )
  240. }
  241. ></input>
  242. </label>
  243. </div>
  244. <div className={chatStyle["memory-prompt-content"]}>
  245. {session.memoryPrompt || Locale.Memory.EmptyContent}
  246. </div>
  247. </div>
  248. </>
  249. </Modal>
  250. </div>
  251. )}
  252. </div>
  253. );
  254. }
  255. function useSubmitHandler() {
  256. const config = useChatStore((state) => state.config);
  257. const submitKey = config.submitKey;
  258. const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  259. if (e.key !== "Enter") return false;
  260. if (e.key === "Enter" && e.nativeEvent.isComposing) return false;
  261. return (
  262. (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
  263. (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
  264. (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
  265. (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
  266. (config.submitKey === SubmitKey.Enter &&
  267. !e.altKey &&
  268. !e.ctrlKey &&
  269. !e.shiftKey &&
  270. !e.metaKey)
  271. );
  272. };
  273. return {
  274. submitKey,
  275. shouldSubmit,
  276. };
  277. }
  278. export function PromptHints(props: {
  279. prompts: Prompt[];
  280. onPromptSelect: (prompt: Prompt) => void;
  281. }) {
  282. if (props.prompts.length === 0) return null;
  283. return (
  284. <div className={styles["prompt-hints"]}>
  285. {props.prompts.map((prompt, i) => (
  286. <div
  287. className={styles["prompt-hint"]}
  288. key={prompt.title + i.toString()}
  289. onClick={() => props.onPromptSelect(prompt)}
  290. >
  291. <div className={styles["hint-title"]}>{prompt.title}</div>
  292. <div className={styles["hint-content"]}>{prompt.content}</div>
  293. </div>
  294. ))}
  295. </div>
  296. );
  297. }
  298. function useScrollToBottom() {
  299. // for auto-scroll
  300. const scrollRef = useRef<HTMLDivElement>(null);
  301. const [autoScroll, setAutoScroll] = useState(true);
  302. const scrollToBottom = () => {
  303. const dom = scrollRef.current;
  304. if (dom) {
  305. setTimeout(() => (dom.scrollTop = dom.scrollHeight), 1);
  306. }
  307. };
  308. // auto scroll
  309. useLayoutEffect(() => {
  310. autoScroll && scrollToBottom();
  311. });
  312. return {
  313. scrollRef,
  314. autoScroll,
  315. setAutoScroll,
  316. scrollToBottom,
  317. };
  318. }
  319. export function ChatActions(props: {
  320. showPromptModal: () => void;
  321. scrollToBottom: () => void;
  322. hitBottom: boolean;
  323. }) {
  324. const chatStore = useChatStore();
  325. const theme = chatStore.config.theme;
  326. function nextTheme() {
  327. const themes = [Theme.Auto, Theme.Light, Theme.Dark];
  328. const themeIndex = themes.indexOf(theme);
  329. const nextIndex = (themeIndex + 1) % themes.length;
  330. const nextTheme = themes[nextIndex];
  331. chatStore.updateConfig((config) => (config.theme = nextTheme));
  332. }
  333. return (
  334. <div className={chatStyle["chat-input-actions"]}>
  335. {!props.hitBottom && (
  336. <div
  337. className={`${chatStyle["chat-input-action"]} clickable`}
  338. onClick={props.scrollToBottom}
  339. >
  340. <BottomIcon />
  341. </div>
  342. )}
  343. {props.hitBottom && (
  344. <div
  345. className={`${chatStyle["chat-input-action"]} clickable`}
  346. onClick={props.showPromptModal}
  347. >
  348. <BrainIcon />
  349. </div>
  350. )}
  351. <div
  352. className={`${chatStyle["chat-input-action"]} clickable`}
  353. onClick={nextTheme}
  354. >
  355. {theme === Theme.Auto ? (
  356. <AutoIcon />
  357. ) : theme === Theme.Light ? (
  358. <LightIcon />
  359. ) : theme === Theme.Dark ? (
  360. <DarkIcon />
  361. ) : null}
  362. </div>
  363. </div>
  364. );
  365. }
  366. export function Chat(props: {
  367. showSideBar?: () => void;
  368. sideBarShowing?: boolean;
  369. }) {
  370. type RenderMessage = Message & { preview?: boolean };
  371. const chatStore = useChatStore();
  372. const [session, sessionIndex] = useChatStore((state) => [
  373. state.currentSession(),
  374. state.currentSessionIndex,
  375. ]);
  376. const fontSize = useChatStore((state) => state.config.fontSize);
  377. const inputRef = useRef<HTMLTextAreaElement>(null);
  378. const [userInput, setUserInput] = useState("");
  379. const [beforeInput, setBeforeInput] = useState("");
  380. const [isLoading, setIsLoading] = useState(false);
  381. const { submitKey, shouldSubmit } = useSubmitHandler();
  382. const { scrollRef, setAutoScroll, scrollToBottom } = useScrollToBottom();
  383. const [hitBottom, setHitBottom] = useState(false);
  384. const onChatBodyScroll = (e: HTMLElement) => {
  385. const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 20;
  386. setHitBottom(isTouchBottom);
  387. };
  388. // prompt hints
  389. const promptStore = usePromptStore();
  390. const [promptHints, setPromptHints] = useState<Prompt[]>([]);
  391. const onSearch = useDebouncedCallback(
  392. (text: string) => {
  393. setPromptHints(promptStore.search(text));
  394. },
  395. 100,
  396. { leading: true, trailing: true },
  397. );
  398. const onPromptSelect = (prompt: Prompt) => {
  399. setUserInput(prompt.content);
  400. setPromptHints([]);
  401. inputRef.current?.focus();
  402. };
  403. const scrollInput = () => {
  404. const dom = inputRef.current;
  405. if (!dom) return;
  406. const paddingBottomNum: number = parseInt(
  407. window.getComputedStyle(dom).paddingBottom,
  408. 10,
  409. );
  410. dom.scrollTop = dom.scrollHeight - dom.offsetHeight + paddingBottomNum;
  411. };
  412. // auto grow input
  413. const [inputRows, setInputRows] = useState(2);
  414. const measure = useDebouncedCallback(
  415. () => {
  416. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  417. const inputRows = Math.min(
  418. 5,
  419. Math.max(2 + Number(!isMobileScreen()), rows),
  420. );
  421. setInputRows(inputRows);
  422. },
  423. 100,
  424. {
  425. leading: true,
  426. trailing: true,
  427. },
  428. );
  429. // eslint-disable-next-line react-hooks/exhaustive-deps
  430. useEffect(measure, [userInput]);
  431. // only search prompts when user input is short
  432. const SEARCH_TEXT_LIMIT = 30;
  433. const onInput = (text: string) => {
  434. scrollInput();
  435. setUserInput(text);
  436. const n = text.trim().length;
  437. // clear search results
  438. if (n === 0) {
  439. setPromptHints([]);
  440. } else if (!chatStore.config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  441. // check if need to trigger auto completion
  442. if (text.startsWith("/")) {
  443. let searchText = text.slice(1);
  444. onSearch(searchText);
  445. }
  446. }
  447. };
  448. // submit user input
  449. const onUserSubmit = () => {
  450. if (userInput.length <= 0) return;
  451. setIsLoading(true);
  452. chatStore.onUserInput(userInput).then(() => setIsLoading(false));
  453. setBeforeInput(userInput);
  454. setUserInput("");
  455. setPromptHints([]);
  456. if (!isMobileScreen()) inputRef.current?.focus();
  457. setAutoScroll(true);
  458. };
  459. // stop response
  460. const onUserStop = (messageId: number) => {
  461. ControllerPool.stop(sessionIndex, messageId);
  462. };
  463. // check if should send message
  464. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  465. // if ArrowUp and no userInput
  466. if (e.key === "ArrowUp" && userInput.length <= 0) {
  467. setUserInput(beforeInput);
  468. e.preventDefault();
  469. return;
  470. }
  471. if (shouldSubmit(e)) {
  472. onUserSubmit();
  473. e.preventDefault();
  474. }
  475. };
  476. const onRightClick = (e: any, message: Message) => {
  477. // auto fill user input
  478. if (message.role === "user") {
  479. setUserInput(message.content);
  480. }
  481. // copy to clipboard
  482. if (selectOrCopy(e.currentTarget, message.content)) {
  483. e.preventDefault();
  484. }
  485. };
  486. const onResend = (botIndex: number) => {
  487. // find last user input message and resend
  488. for (let i = botIndex; i >= 0; i -= 1) {
  489. if (messages[i].role === "user") {
  490. setIsLoading(true);
  491. chatStore
  492. .onUserInput(messages[i].content)
  493. .then(() => setIsLoading(false));
  494. chatStore.updateCurrentSession((session) =>
  495. session.messages.splice(i, 2),
  496. );
  497. inputRef.current?.focus();
  498. return;
  499. }
  500. }
  501. };
  502. const config = useChatStore((state) => state.config);
  503. const context: RenderMessage[] = session.context.slice();
  504. const accessStore = useAccessStore();
  505. if (
  506. context.length === 0 &&
  507. session.messages.at(0)?.content !== BOT_HELLO.content
  508. ) {
  509. const copiedHello = Object.assign({}, BOT_HELLO);
  510. if (!accessStore.isAuthorized()) {
  511. copiedHello.content = Locale.Error.Unauthorized;
  512. }
  513. context.push(copiedHello);
  514. }
  515. // preview messages
  516. const messages = context
  517. .concat(session.messages as RenderMessage[])
  518. .concat(
  519. isLoading
  520. ? [
  521. {
  522. ...createMessage({
  523. role: "assistant",
  524. content: "……",
  525. }),
  526. preview: true,
  527. },
  528. ]
  529. : [],
  530. )
  531. .concat(
  532. userInput.length > 0 && config.sendPreviewBubble
  533. ? [
  534. {
  535. ...createMessage({
  536. role: "user",
  537. content: userInput,
  538. }),
  539. preview: true,
  540. },
  541. ]
  542. : [],
  543. );
  544. const [showPromptModal, setShowPromptModal] = useState(false);
  545. // Auto focus
  546. useEffect(() => {
  547. if (props.sideBarShowing && isMobileScreen()) return;
  548. inputRef.current?.focus();
  549. // eslint-disable-next-line react-hooks/exhaustive-deps
  550. }, []);
  551. return (
  552. <div className={styles.chat} key={session.id}>
  553. <div className={styles["window-header"]}>
  554. <div className={styles["window-header-title"]}>
  555. <div
  556. className={`${styles["window-header-main-title"]} ${styles["chat-body-title"]}`}
  557. onClickCapture={() => {
  558. const newTopic = prompt(Locale.Chat.Rename, session.topic);
  559. if (newTopic && newTopic !== session.topic) {
  560. chatStore.updateCurrentSession(
  561. (session) => (session.topic = newTopic!),
  562. );
  563. }
  564. }}
  565. >
  566. {session.topic}
  567. </div>
  568. <div className={styles["window-header-sub-title"]}>
  569. {Locale.Chat.SubTitle(session.messages.length)}
  570. </div>
  571. </div>
  572. <div className={styles["window-actions"]}>
  573. <div className={styles["window-action-button"] + " " + styles.mobile}>
  574. <IconButton
  575. icon={<ReturnIcon />}
  576. bordered
  577. title={Locale.Chat.Actions.ChatList}
  578. onClick={props?.showSideBar}
  579. />
  580. </div>
  581. <div className={styles["window-action-button"]}>
  582. <IconButton
  583. icon={<BrainIcon />}
  584. bordered
  585. title={Locale.Chat.Actions.CompressedHistory}
  586. onClick={() => {
  587. setShowPromptModal(true);
  588. }}
  589. />
  590. </div>
  591. <div className={styles["window-action-button"]}>
  592. <IconButton
  593. icon={<ExportIcon />}
  594. bordered
  595. title={Locale.Chat.Actions.Export}
  596. onClick={() => {
  597. exportMessages(
  598. session.messages.filter((msg) => !msg.isError),
  599. session.topic,
  600. );
  601. }}
  602. />
  603. </div>
  604. {!isMobileScreen() && (
  605. <div className={styles["window-action-button"]}>
  606. <IconButton
  607. icon={chatStore.config.tightBorder ? <MinIcon /> : <MaxIcon />}
  608. bordered
  609. onClick={() => {
  610. chatStore.updateConfig(
  611. (config) => (config.tightBorder = !config.tightBorder),
  612. );
  613. }}
  614. />
  615. </div>
  616. )}
  617. </div>
  618. <PromptToast
  619. showToast={!hitBottom}
  620. showModal={showPromptModal}
  621. setShowModal={setShowPromptModal}
  622. />
  623. </div>
  624. <div
  625. className={styles["chat-body"]}
  626. ref={scrollRef}
  627. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  628. onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
  629. onTouchStart={() => {
  630. inputRef.current?.blur();
  631. setAutoScroll(false);
  632. }}
  633. >
  634. {messages.map((message, i) => {
  635. const isUser = message.role === "user";
  636. return (
  637. <div
  638. key={i}
  639. className={
  640. isUser ? styles["chat-message-user"] : styles["chat-message"]
  641. }
  642. >
  643. <div className={styles["chat-message-container"]}>
  644. <div className={styles["chat-message-avatar"]}>
  645. <Avatar role={message.role} />
  646. </div>
  647. {(message.preview || message.streaming) && (
  648. <div className={styles["chat-message-status"]}>
  649. {Locale.Chat.Typing}
  650. </div>
  651. )}
  652. <div className={styles["chat-message-item"]}>
  653. {!isUser &&
  654. !(message.preview || message.content.length === 0) && (
  655. <div className={styles["chat-message-top-actions"]}>
  656. {message.streaming ? (
  657. <div
  658. className={styles["chat-message-top-action"]}
  659. onClick={() => onUserStop(message.id ?? i)}
  660. >
  661. {Locale.Chat.Actions.Stop}
  662. </div>
  663. ) : (
  664. <div
  665. className={styles["chat-message-top-action"]}
  666. onClick={() => onResend(i)}
  667. >
  668. {Locale.Chat.Actions.Retry}
  669. </div>
  670. )}
  671. <div
  672. className={styles["chat-message-top-action"]}
  673. onClick={() => copyToClipboard(message.content)}
  674. >
  675. {Locale.Chat.Actions.Copy}
  676. </div>
  677. </div>
  678. )}
  679. <Markdown
  680. content={message.content}
  681. loading={
  682. (message.preview || message.content.length === 0) &&
  683. !isUser
  684. }
  685. onContextMenu={(e) => onRightClick(e, message)}
  686. onDoubleClickCapture={() => {
  687. if (!isMobileScreen()) return;
  688. setUserInput(message.content);
  689. }}
  690. fontSize={fontSize}
  691. parentRef={scrollRef}
  692. />
  693. </div>
  694. {!isUser && !message.preview && (
  695. <div className={styles["chat-message-actions"]}>
  696. <div className={styles["chat-message-action-date"]}>
  697. {message.date.toLocaleString()}
  698. </div>
  699. </div>
  700. )}
  701. </div>
  702. </div>
  703. );
  704. })}
  705. </div>
  706. <div className={styles["chat-input-panel"]}>
  707. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  708. <ChatActions
  709. showPromptModal={() => setShowPromptModal(true)}
  710. scrollToBottom={scrollToBottom}
  711. hitBottom={hitBottom}
  712. />
  713. <div className={styles["chat-input-panel-inner"]}>
  714. <textarea
  715. ref={inputRef}
  716. className={styles["chat-input"]}
  717. placeholder={Locale.Chat.Input(submitKey)}
  718. onInput={(e) => onInput(e.currentTarget.value)}
  719. value={userInput}
  720. onKeyDown={onInputKeyDown}
  721. onFocus={() => setAutoScroll(true)}
  722. onBlur={() => {
  723. setAutoScroll(false);
  724. setTimeout(() => setPromptHints([]), 500);
  725. }}
  726. autoFocus={!props?.sideBarShowing}
  727. rows={inputRows}
  728. />
  729. <IconButton
  730. icon={<SendWhiteIcon />}
  731. text={Locale.Chat.Send}
  732. className={styles["chat-input-send"]}
  733. noDark
  734. onClick={onUserSubmit}
  735. />
  736. </div>
  737. </div>
  738. </div>
  739. );
  740. }