mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-25 21:34:00 +01:00
abstract initialiseNewChat
This commit is contained in:
parent
5572f20a6a
commit
8a9dac15f7
20
src/App.tsx
20
src/App.tsx
|
@ -7,13 +7,14 @@ import ConfigMenu from './components/ConfigMenu';
|
||||||
|
|
||||||
import useSaveToLocalStorage from '@hooks/useSaveToLocalStorage';
|
import useSaveToLocalStorage from '@hooks/useSaveToLocalStorage';
|
||||||
import useUpdateCharts from '@hooks/useUpdateChats';
|
import useUpdateCharts from '@hooks/useUpdateChats';
|
||||||
|
import useInitialiseNewChat from '@hooks/useInitialiseNewChat';
|
||||||
|
|
||||||
import { ChatInterface, MessageInterface } from '@type/chat';
|
import { ChatInterface } from '@type/chat';
|
||||||
import { defaultSystemMessage } from '@constants/chat';
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
useSaveToLocalStorage();
|
useSaveToLocalStorage();
|
||||||
useUpdateCharts();
|
useUpdateCharts();
|
||||||
|
const initialiseNewChat = useInitialiseNewChat();
|
||||||
|
|
||||||
const [setChats, setMessages, setCurrentChatIndex] = useStore((state) => [
|
const [setChats, setMessages, setCurrentChatIndex] = useStore((state) => [
|
||||||
state.setChats,
|
state.setChats,
|
||||||
|
@ -21,21 +22,6 @@ function App() {
|
||||||
state.setCurrentChatIndex,
|
state.setCurrentChatIndex,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const initialiseNewChat = () => {
|
|
||||||
const message: MessageInterface = {
|
|
||||||
role: 'system',
|
|
||||||
content: defaultSystemMessage,
|
|
||||||
};
|
|
||||||
setChats([
|
|
||||||
{
|
|
||||||
title: 'New Chat',
|
|
||||||
messages: [message],
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
setMessages([message]);
|
|
||||||
setCurrentChatIndex(0);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// localStorage.removeItem('chats');
|
// localStorage.removeItem('chats');
|
||||||
const storedChats = localStorage.getItem('chats');
|
const storedChats = localStorage.getItem('chats');
|
||||||
|
|
|
@ -45,6 +45,7 @@ const ChatContent = () => {
|
||||||
while (reading) {
|
while (reading) {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
|
|
||||||
|
try {
|
||||||
const result = parseEventSource(new TextDecoder().decode(value));
|
const result = parseEventSource(new TextDecoder().decode(value));
|
||||||
|
|
||||||
if (result === '[DONE]' || done) {
|
if (result === '[DONE]' || done) {
|
||||||
|
@ -65,6 +66,9 @@ const ChatContent = () => {
|
||||||
updatedMessages[updatedMessages.length - 1].content += resultString;
|
updatedMessages[updatedMessages.length - 1].content += resultString;
|
||||||
setMessages(updatedMessages);
|
setMessages(updatedMessages);
|
||||||
}
|
}
|
||||||
|
} catch (e: unknown) {
|
||||||
|
console.log((e as Error).message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,6 +7,8 @@ import DeleteIcon from '@icon/DeleteIcon';
|
||||||
import TickIcon from '@icon/TickIcon';
|
import TickIcon from '@icon/TickIcon';
|
||||||
import CrossIcon from '@icon/CrossIcon';
|
import CrossIcon from '@icon/CrossIcon';
|
||||||
|
|
||||||
|
import useInitialiseNewChat from '@hooks/useInitialiseNewChat';
|
||||||
|
|
||||||
const ChatHistoryList = () => {
|
const ChatHistoryList = () => {
|
||||||
const [chats, setCurrentChatIndex, setMessages] = useStore((state) => [
|
const [chats, setCurrentChatIndex, setMessages] = useStore((state) => [
|
||||||
state.chats,
|
state.chats,
|
||||||
|
@ -63,6 +65,7 @@ const ChatHistory = ({
|
||||||
chatIndex: number;
|
chatIndex: number;
|
||||||
onClick?: React.MouseEventHandler<HTMLElement>;
|
onClick?: React.MouseEventHandler<HTMLElement>;
|
||||||
}) => {
|
}) => {
|
||||||
|
const initialiseNewChat = useInitialiseNewChat();
|
||||||
const [chats, setChats, currentChatIndex, setMessages, setCurrentChatIndex] =
|
const [chats, setChats, currentChatIndex, setMessages, setCurrentChatIndex] =
|
||||||
useStore((state) => [
|
useStore((state) => [
|
||||||
state.chats,
|
state.chats,
|
||||||
|
@ -86,13 +89,15 @@ const ChatHistory = ({
|
||||||
setIsEdit(false);
|
setIsEdit(false);
|
||||||
} else if (isDelete) {
|
} else if (isDelete) {
|
||||||
updatedChats.splice(chatIndex, 1);
|
updatedChats.splice(chatIndex, 1);
|
||||||
setCurrentChatIndex(-1);
|
if (updatedChats.length > 0) {
|
||||||
setMessages([]);
|
setCurrentChatIndex(0);
|
||||||
|
setMessages(updatedChats[0].messages);
|
||||||
|
setChats(updatedChats);
|
||||||
|
} else {
|
||||||
|
initialiseNewChat();
|
||||||
|
}
|
||||||
setIsDelete(false);
|
setIsDelete(false);
|
||||||
}
|
}
|
||||||
setTimeout(() => {
|
|
||||||
setChats(updatedChats);
|
|
||||||
}, 0);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCross = () => {
|
const handleCross = () => {
|
||||||
|
|
31
src/hooks/useInitialiseNewChat.ts
Normal file
31
src/hooks/useInitialiseNewChat.ts
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import React from 'react';
|
||||||
|
import useStore from '@store/store';
|
||||||
|
import { MessageInterface } from '@type/chat';
|
||||||
|
import { defaultSystemMessage } from '@constants/chat';
|
||||||
|
|
||||||
|
const useInitialiseNewChat = () => {
|
||||||
|
const [setChats, setMessages, setCurrentChatIndex] = useStore((state) => [
|
||||||
|
state.setChats,
|
||||||
|
state.setMessages,
|
||||||
|
state.setCurrentChatIndex,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const initialiseNewChat = () => {
|
||||||
|
const message: MessageInterface = {
|
||||||
|
role: 'system',
|
||||||
|
content: defaultSystemMessage,
|
||||||
|
};
|
||||||
|
setChats([
|
||||||
|
{
|
||||||
|
title: 'New Chat',
|
||||||
|
messages: [message],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
setMessages([message]);
|
||||||
|
setCurrentChatIndex(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
return initialiseNewChat;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useInitialiseNewChat;
|
Loading…
Reference in a new issue