mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-18 03:41:44 +03:00
9bb7ccf651
* feat: add providers mocks * test(<ChatPage/>: add render test using providers
70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useState } from "react";
|
|
|
|
import { ChatHistory } from "@/app/chat/[chatId]/types";
|
|
|
|
import { ChatContextProps } from "./types";
|
|
|
|
export const ChatContext = createContext<ChatContextProps | undefined>(
|
|
undefined
|
|
);
|
|
|
|
export const ChatProvider = ({
|
|
children,
|
|
}: {
|
|
children: JSX.Element | JSX.Element[];
|
|
}): JSX.Element => {
|
|
const [history, setHistory] = useState<ChatHistory[]>([]);
|
|
|
|
const addToHistory = (message: ChatHistory) => {
|
|
setHistory((prevHistory) => [...prevHistory, message]);
|
|
};
|
|
|
|
const updateStreamingHistory = (streamedChat: ChatHistory): void => {
|
|
setHistory((prevHistory: ChatHistory[]) => {
|
|
const updatedHistory = prevHistory.find(
|
|
(item) => item.message_id === streamedChat.message_id
|
|
)
|
|
? prevHistory.map((item: ChatHistory) =>
|
|
item.message_id === streamedChat.message_id
|
|
? { ...item, assistant: item.assistant + streamedChat.assistant }
|
|
: item
|
|
)
|
|
: [...prevHistory, streamedChat];
|
|
|
|
return updatedHistory;
|
|
});
|
|
};
|
|
|
|
const updateHistory = (chat: ChatHistory): void => {
|
|
setHistory((prevHistory: ChatHistory[]) => {
|
|
const updatedHistory = prevHistory.find(
|
|
(item) => item.message_id === chat.message_id
|
|
)
|
|
? prevHistory.map((item: ChatHistory) =>
|
|
item.message_id === chat.message_id
|
|
? { ...item, assistant: chat.assistant }
|
|
: item
|
|
)
|
|
: [...prevHistory, chat];
|
|
|
|
return updatedHistory;
|
|
});
|
|
};
|
|
|
|
return (
|
|
<ChatContext.Provider
|
|
value={{
|
|
history,
|
|
setHistory,
|
|
addToHistory,
|
|
updateHistory,
|
|
updateStreamingHistory,
|
|
}}
|
|
>
|
|
{children}
|
|
</ChatContext.Provider>
|
|
);
|
|
};
|