fix: state management - get current conv messages with selectAtom
This commit is contained in:
parent
315b85591f
commit
e7e932059a
@ -5,14 +5,24 @@ import ChatItem from "../ChatItem";
|
|||||||
import { ChatMessage } from "@/_models/ChatMessage";
|
import { ChatMessage } from "@/_models/ChatMessage";
|
||||||
import useChatMessages from "@/_hooks/useChatMessages";
|
import useChatMessages from "@/_hooks/useChatMessages";
|
||||||
import {
|
import {
|
||||||
currentChatMessagesAtom,
|
chatMessages,
|
||||||
|
getActiveConvoIdAtom,
|
||||||
showingTyping,
|
showingTyping,
|
||||||
} from "@/_helpers/JotaiWrapper";
|
} from "@/_helpers/JotaiWrapper";
|
||||||
import { useAtomValue } from "jotai";
|
import { useAtomValue } from "jotai";
|
||||||
|
import { selectAtom } from "jotai/utils";
|
||||||
import LoadingIndicator from "../LoadingIndicator";
|
import LoadingIndicator from "../LoadingIndicator";
|
||||||
|
|
||||||
const ChatBody: React.FC = () => {
|
const ChatBody: React.FC = () => {
|
||||||
const messages = useAtomValue(currentChatMessagesAtom);
|
const activeConversationId = useAtomValue(getActiveConvoIdAtom) ?? "";
|
||||||
|
const messageList = useAtomValue(
|
||||||
|
selectAtom(
|
||||||
|
chatMessages,
|
||||||
|
useCallback((v) => v[activeConversationId], [activeConversationId])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const [content, setContent] = useState<React.JSX.Element[]>([]);
|
||||||
|
|
||||||
const isTyping = useAtomValue(showingTyping);
|
const isTyping = useAtomValue(showingTyping);
|
||||||
const [offset, setOffset] = useState(0);
|
const [offset, setOffset] = useState(0);
|
||||||
const { loading, hasMore } = useChatMessages(offset);
|
const { loading, hasMore } = useChatMessages(offset);
|
||||||
@ -35,13 +45,18 @@ const ChatBody: React.FC = () => {
|
|||||||
[loading, hasMore]
|
[loading, hasMore]
|
||||||
);
|
);
|
||||||
|
|
||||||
const content = messages.map((message, index) => {
|
React.useEffect(() => {
|
||||||
if (messages.length === index + 1) {
|
const list = messageList?.map((message, index) => {
|
||||||
// @ts-ignore
|
if (messageList?.length === index + 1) {
|
||||||
return <ChatItem ref={lastPostRef} message={message} key={message.id} />;
|
return (
|
||||||
}
|
// @ts-ignore
|
||||||
return <ChatItem message={message} key={message.id} />;
|
<ChatItem ref={lastPostRef} message={message} key={message.id} />
|
||||||
});
|
);
|
||||||
|
}
|
||||||
|
return <ChatItem message={message} key={message.id} />;
|
||||||
|
});
|
||||||
|
setContent(list);
|
||||||
|
}, [messageList, lastPostRef]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col-reverse flex-1 py-4 overflow-y-auto scroll">
|
<div className="flex flex-col-reverse flex-1 py-4 overflow-y-auto scroll">
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
import {
|
import {
|
||||||
addNewMessageAtom,
|
addNewMessageAtom,
|
||||||
currentChatMessagesAtom,
|
chatMessages,
|
||||||
currentConversationAtom,
|
currentConversationAtom,
|
||||||
currentPromptAtom,
|
currentPromptAtom,
|
||||||
currentStreamingMessageAtom,
|
currentStreamingMessageAtom,
|
||||||
|
getActiveConvoIdAtom,
|
||||||
showingTyping,
|
showingTyping,
|
||||||
updateMessageAtom,
|
updateMessageAtom,
|
||||||
} from "@/_helpers/JotaiWrapper";
|
} from "@/_helpers/JotaiWrapper";
|
||||||
|
|
||||||
import { useAtom, useAtomValue, useSetAtom } from "jotai";
|
import { useAtom, useAtomValue, useSetAtom } from "jotai";
|
||||||
|
import { selectAtom } from "jotai/utils";
|
||||||
import { DataService } from "../../shared/coreService";
|
import { DataService } from "../../shared/coreService";
|
||||||
import {
|
import {
|
||||||
MessageSenderType,
|
MessageSenderType,
|
||||||
@ -16,13 +18,21 @@ import {
|
|||||||
toChatMessage,
|
toChatMessage,
|
||||||
} from "@/_models/ChatMessage";
|
} from "@/_models/ChatMessage";
|
||||||
import { executeSerial } from "@/_services/pluginService";
|
import { executeSerial } from "@/_services/pluginService";
|
||||||
|
import { useCallback } from "react";
|
||||||
|
|
||||||
export default function useSendChatMessage() {
|
export default function useSendChatMessage() {
|
||||||
const currentConvo = useAtomValue(currentConversationAtom);
|
const currentConvo = useAtomValue(currentConversationAtom);
|
||||||
const updateStreamMessage = useSetAtom(currentStreamingMessageAtom);
|
const updateStreamMessage = useSetAtom(currentStreamingMessageAtom);
|
||||||
const addNewMessage = useSetAtom(addNewMessageAtom);
|
const addNewMessage = useSetAtom(addNewMessageAtom);
|
||||||
const updateMessage = useSetAtom(updateMessageAtom);
|
const updateMessage = useSetAtom(updateMessageAtom);
|
||||||
const chatMessagesHistory = useAtomValue(currentChatMessagesAtom);
|
const activeConversationId = useAtomValue(getActiveConvoIdAtom) ?? "";
|
||||||
|
|
||||||
|
const chatMessagesHistory = useAtomValue(
|
||||||
|
selectAtom(
|
||||||
|
chatMessages,
|
||||||
|
useCallback((v) => v[activeConversationId], [activeConversationId])
|
||||||
|
)
|
||||||
|
);
|
||||||
const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom);
|
const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom);
|
||||||
const [, setIsTyping] = useAtom(showingTyping);
|
const [, setIsTyping] = useAtom(showingTyping);
|
||||||
const sendChatMessage = async () => {
|
const sendChatMessage = async () => {
|
||||||
@ -120,6 +130,11 @@ export default function useSendChatMessage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updateMessage(
|
||||||
|
responseChatMessage.id,
|
||||||
|
responseChatMessage.conversationId,
|
||||||
|
answer
|
||||||
|
);
|
||||||
await executeSerial(DataService.UPDATE_MESSAGE, {
|
await executeSerial(DataService.UPDATE_MESSAGE, {
|
||||||
...newResponse,
|
...newResponse,
|
||||||
message: answer,
|
message: answer,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user