mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-29 10:14:00 +01:00
improve useSubmitHook
This commit is contained in:
parent
29f8fe75f3
commit
fb910d0a52
|
@ -12,10 +12,10 @@ import CrossIcon from '@icon/CrossIcon';
|
||||||
import RefreshIcon from '@icon/RefreshIcon';
|
import RefreshIcon from '@icon/RefreshIcon';
|
||||||
import DownChevronArrow from '@icon/DownChevronArrow';
|
import DownChevronArrow from '@icon/DownChevronArrow';
|
||||||
|
|
||||||
import { MessageInterface } from '@type/chat';
|
|
||||||
|
|
||||||
import useSubmit from '@hooks/useSubmit';
|
import useSubmit from '@hooks/useSubmit';
|
||||||
|
|
||||||
|
import { MessageInterface } from '@type/chat';
|
||||||
|
|
||||||
const MessageContent = ({
|
const MessageContent = ({
|
||||||
role,
|
role,
|
||||||
content,
|
content,
|
||||||
|
@ -91,6 +91,13 @@ const ContentView = ({
|
||||||
setMessages(updatedMessages);
|
setMessages(updatedMessages);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleRefresh = () => {
|
||||||
|
const updatedMessages = JSON.parse(JSON.stringify(messages));
|
||||||
|
updatedMessages.splice(updatedMessages.length - 1, 1);
|
||||||
|
setMessages(updatedMessages);
|
||||||
|
handleSubmit();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className='markdown prose w-full break-words dark:prose-invert dark'>
|
<div className='markdown prose w-full break-words dark:prose-invert dark'>
|
||||||
|
@ -161,11 +168,7 @@ const ContentView = ({
|
||||||
{isDelete || (
|
{isDelete || (
|
||||||
<>
|
<>
|
||||||
{role === 'assistant' && messageIndex === messages?.length - 1 && (
|
{role === 'assistant' && messageIndex === messages?.length - 1 && (
|
||||||
<RefreshButton
|
<RefreshButton onClick={handleRefresh} />
|
||||||
onClick={() => {
|
|
||||||
handleSubmit(true);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)}
|
)}
|
||||||
{messageIndex !== 0 && (
|
{messageIndex !== 0 && (
|
||||||
<UpButton onClick={() => handleMove('up')} />
|
<UpButton onClick={() => 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(() => {
|
useEffect(() => {
|
||||||
if (textareaRef.current) {
|
if (textareaRef.current) {
|
||||||
textareaRef.current.style.height = 'auto';
|
textareaRef.current.style.height = 'auto';
|
||||||
|
@ -308,28 +339,29 @@ const EditView = ({
|
||||||
}}
|
}}
|
||||||
value={_content}
|
value={_content}
|
||||||
onInput={handleInput}
|
onInput={handleInput}
|
||||||
rows={sticky ? 10 : 1}
|
rows={1}
|
||||||
></textarea>
|
></textarea>
|
||||||
<div className='text-center mt-2 flex justify-center'>
|
<div className='text-center mt-2 flex justify-center'>
|
||||||
|
{sticky && (
|
||||||
|
<button
|
||||||
|
className='btn relative btn-primary mr-2'
|
||||||
|
onClick={handleSaveAndSubmit}
|
||||||
|
>
|
||||||
|
<div className='flex items-center justify-center gap-2'>
|
||||||
|
Save and Submit
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
|
||||||
<button
|
<button
|
||||||
className='btn relative btn-primary mr-2'
|
className={`btn relative mr-2 ${
|
||||||
onClick={() => {
|
sticky ? 'btn-neutral' : 'btn-primary'
|
||||||
if (_content === '') return;
|
}`}
|
||||||
const updatedMessages: MessageInterface[] = JSON.parse(
|
onClick={handleSave}
|
||||||
JSON.stringify(messages)
|
|
||||||
);
|
|
||||||
if (sticky) {
|
|
||||||
updatedMessages.push({ role: inputRole, content: _content });
|
|
||||||
_setContent('');
|
|
||||||
} else {
|
|
||||||
updatedMessages[messageIndex].content = _content;
|
|
||||||
setIsEdit(false);
|
|
||||||
}
|
|
||||||
setMessages(updatedMessages);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<div className='flex items-center justify-center gap-2'>Save</div>
|
<div className='flex items-center justify-center gap-2'>Save</div>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
{sticky || (
|
{sticky || (
|
||||||
<button
|
<button
|
||||||
className='btn relative btn-neutral'
|
className='btn relative btn-neutral'
|
||||||
|
|
|
@ -7,48 +7,35 @@ import { parseEventSource } from '@api/helper';
|
||||||
|
|
||||||
const useSubmit = () => {
|
const useSubmit = () => {
|
||||||
const [error, setError] = useState<string>('');
|
const [error, setError] = useState<string>('');
|
||||||
const [messages, apiFree, apiKey, setMessages, setGenerating, generating] =
|
const [apiFree, apiKey, setMessages, setGenerating, generating] = useStore(
|
||||||
useStore((state) => [
|
(state) => [
|
||||||
state.messages,
|
|
||||||
state.apiFree,
|
state.apiFree,
|
||||||
state.apiKey,
|
state.apiKey,
|
||||||
state.setMessages,
|
state.setMessages,
|
||||||
state.setGenerating,
|
state.setGenerating,
|
||||||
state.generating,
|
state.generating,
|
||||||
]);
|
]
|
||||||
|
);
|
||||||
|
|
||||||
const handleSubmit = async (refresh?: boolean) => {
|
const handleSubmit = async () => {
|
||||||
if (generating) return;
|
if (generating) return;
|
||||||
|
const messages = useStore.getState().messages;
|
||||||
|
|
||||||
const updatedMessages: MessageInterface[] = JSON.parse(
|
const updatedMessages: MessageInterface[] = JSON.parse(
|
||||||
JSON.stringify(messages)
|
JSON.stringify(messages)
|
||||||
);
|
);
|
||||||
if (refresh) {
|
|
||||||
updatedMessages[updatedMessages.length - 1] = {
|
updatedMessages.push({ role: 'assistant', content: '' });
|
||||||
role: 'assistant',
|
|
||||||
content: '',
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
updatedMessages.push({ role: 'assistant', content: '' });
|
|
||||||
}
|
|
||||||
setMessages(updatedMessages);
|
setMessages(updatedMessages);
|
||||||
setGenerating(true);
|
setGenerating(true);
|
||||||
let stream;
|
let stream;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (apiFree) {
|
if (apiFree) {
|
||||||
if (refresh)
|
stream = await getChatCompletionStreamFree(messages);
|
||||||
stream = await getChatCompletionStreamFree(
|
|
||||||
updatedMessages.slice(0, updatedMessages.length - 1)
|
|
||||||
);
|
|
||||||
else stream = await getChatCompletionStreamFree(messages);
|
|
||||||
} else if (apiKey) {
|
} else if (apiKey) {
|
||||||
if (refresh)
|
stream = await getChatCompletionStreamFree(messages);
|
||||||
stream = await getChatCompletionStreamCustom(
|
|
||||||
apiKey,
|
|
||||||
updatedMessages.slice(0, updatedMessages.length - 1)
|
|
||||||
);
|
|
||||||
else stream = await getChatCompletionStreamFree(messages);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stream) {
|
if (stream) {
|
||||||
|
|
Loading…
Reference in a new issue