2023-08-11 11:06:20 +03:00
|
|
|
import { useParams } from "next/navigation";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
|
|
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
|
|
|
import { useChatContext } from "@/lib/context";
|
|
|
|
|
2023-09-07 18:23:31 +03:00
|
|
|
import { getMessagesFromChatHistory } from "../utils/getMessagesFromChatHistory";
|
|
|
|
|
2023-08-11 11:06:20 +03:00
|
|
|
// 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) {
|
2023-09-07 18:23:31 +03:00
|
|
|
setHistory(getMessagesFromChatHistory(chatHistory));
|
2023-08-11 11:06:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
void fetchHistory();
|
|
|
|
}, [chatId, setHistory]);
|
|
|
|
};
|