quivr/frontend/app/chat/[chatId]/hooks/useSelectedChatPage.ts

34 lines
923 B
TypeScript
Raw Normal View History

import { useParams } from "next/navigation";
import { useEffect } from "react";
import { useChatApi } from "@/lib/api/chat/useChatApi";
import { useChatContext } from "@/lib/context";
import { getMessagesFromChatHistory } from "../utils/getMessagesFromChatHistory";
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useSelectedChatPage = () => {
const { setHistory } = useChatContext();
const { getHistory } = useChatApi();
const params = useParams();
const chatId = params?.chatId as string | undefined;
useEffect(() => {
const fetchHistory = async () => {
if (chatId === undefined) {
setHistory([]);
return;
}
const chatHistory = await getHistory(chatId);
if (chatHistory.length > 0) {
setHistory(getMessagesFromChatHistory(chatHistory));
}
};
void fetchHistory();
}, [chatId, setHistory]);
};