From fb910d0a52b459b1b2cd383cdfb060ab7d466928 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Sat, 4 Mar 2023 13:43:20 +0800 Subject: [PATCH] improve useSubmitHook --- .../ChatContent/Message/MessageContent.tsx | 78 +++++++++++++------ src/hooks/useSubmit.ts | 35 +++------ 2 files changed, 66 insertions(+), 47 deletions(-) diff --git a/src/components/Chat/ChatContent/Message/MessageContent.tsx b/src/components/Chat/ChatContent/Message/MessageContent.tsx index bf8bc4d..67c2d81 100644 --- a/src/components/Chat/ChatContent/Message/MessageContent.tsx +++ b/src/components/Chat/ChatContent/Message/MessageContent.tsx @@ -12,10 +12,10 @@ import CrossIcon from '@icon/CrossIcon'; import RefreshIcon from '@icon/RefreshIcon'; import DownChevronArrow from '@icon/DownChevronArrow'; -import { MessageInterface } from '@type/chat'; - import useSubmit from '@hooks/useSubmit'; +import { MessageInterface } from '@type/chat'; + const MessageContent = ({ role, content, @@ -91,6 +91,13 @@ const ContentView = ({ setMessages(updatedMessages); }; + const handleRefresh = () => { + const updatedMessages = JSON.parse(JSON.stringify(messages)); + updatedMessages.splice(updatedMessages.length - 1, 1); + setMessages(updatedMessages); + handleSubmit(); + }; + return ( <>
@@ -161,11 +168,7 @@ const ContentView = ({ {isDelete || ( <> {role === 'assistant' && messageIndex === messages?.length - 1 && ( - { - handleSubmit(true); - }} - /> + )} {messageIndex !== 0 && ( handleMove('up')} /> @@ -289,6 +292,34 @@ const EditView = ({ } }; + const handleSave = () => { + if (_content === '') return; + const updatedMessages: MessageInterface[] = JSON.parse( + JSON.stringify(messages) + ); + if (sticky) { + updatedMessages.push({ role: inputRole, content: _content }); + _setContent(''); + } else { + updatedMessages[messageIndex].content = _content; + setIsEdit(false); + } + setMessages(updatedMessages); + }; + + // only for sticky forms + const { handleSubmit } = useSubmit(); + const handleSaveAndSubmit = () => { + if (_content == '') return; + const updatedMessages: MessageInterface[] = JSON.parse( + JSON.stringify(messages) + ); + updatedMessages.push({ role: inputRole, content: _content }); + _setContent(''); + setMessages(updatedMessages); + handleSubmit(); + }; + useEffect(() => { if (textareaRef.current) { textareaRef.current.style.height = 'auto'; @@ -308,28 +339,29 @@ const EditView = ({ }} value={_content} onInput={handleInput} - rows={sticky ? 10 : 1} + rows={1} >
+ {sticky && ( + + )} + + {sticky || (