chat.tsx 22 KB

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