quivr/frontend/lib/context/ChatProvider/ChatProvider.tsx
Mamadou DICKO 10af0c949a
feat: add notifications components (#1148)
* feat: rename ChatMessages to MessagesDialog

* feat: rename history to messages

* feat: add notifications to ChatContext

* feat: add getNotificationsFromChatHistory

* feat: add getMergedChatHistoryWithReducedNotifications

* refactor: update useActionBar

* refactor: update <ChatMessage />-n

* feat: update crawler and endpoint notifications content

* feat: display notifications

* test: update <MessageDialog /> tests

* feat: rename ChatMessage to QADisplay

* feat: rename ChatHistory to ChatMessage

* feat(upload): throw error when file missing

* feat: rename getMergedChatHistoryWithReducedNotifications to getMergedChatMessagesWithReducedNotifications

* feat: change history wording to message

* feat: move getFileIcon to lib

* refactor(NotificationDisplayer): move types to types.ts

* chore: improve ux

* feat: rename MessagesDialog to ChatDialogue
2023-09-12 17:44:15 +02:00

73 lines
1.9 KiB
TypeScript

"use client";
import { createContext, useState } from "react";
import { ChatMessage, Notification } 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 [messages, setMessages] = useState<ChatMessage[]>([]);
const [notifications, setNotifications] = useState<Notification[]>([]);
const addToHistory = (message: ChatMessage) => {
setMessages((prevHistory) => [...prevHistory, message]);
};
const updateStreamingHistory = (streamedChat: ChatMessage): void => {
setMessages((prevHistory: ChatMessage[]) => {
const updatedHistory = prevHistory.find(
(item) => item.message_id === streamedChat.message_id
)
? prevHistory.map((item: ChatMessage) =>
item.message_id === streamedChat.message_id
? { ...item, assistant: item.assistant + streamedChat.assistant }
: item
)
: [...prevHistory, streamedChat];
return updatedHistory;
});
};
const updateHistory = (chat: ChatMessage): void => {
setMessages((prevHistory: ChatMessage[]) => {
const updatedHistory = prevHistory.find(
(item) => item.message_id === chat.message_id
)
? prevHistory.map((item: ChatMessage) =>
item.message_id === chat.message_id
? { ...item, assistant: chat.assistant }
: item
)
: [...prevHistory, chat];
return updatedHistory;
});
};
return (
<ChatContext.Provider
value={{
messages,
setMessages,
addToHistory,
updateHistory,
updateStreamingHistory,
notifications,
setNotifications,
}}
>
{children}
</ChatContext.Provider>
);
};