chat.tsx 23 KB

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