chat.tsx 22 KB

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