2023-09-12 19:00:46 +03:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
2023-08-11 11:06:20 +03:00
|
|
|
import { useParams } from "next/navigation";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
|
|
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
2023-09-12 19:00:46 +03:00
|
|
|
import { useNotificationApi } from "@/lib/api/notification/useNotificationApi";
|
2023-08-11 11:06:20 +03:00
|
|
|
import { useChatContext } from "@/lib/context";
|
|
|
|
|
2023-09-12 19:00:46 +03:00
|
|
|
import { getChatNotificationsQueryKey } from "../utils/getChatNotificationsQueryKey";
|
2023-09-12 18:44:15 +03:00
|
|
|
import { getMessagesFromChatItems } from "../utils/getMessagesFromChatItems";
|
|
|
|
import { getNotificationsFromChatItems } from "../utils/getNotificationsFromChatItems";
|
2023-09-07 18:23:31 +03:00
|
|
|
|
2023-08-11 11:06:20 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useSelectedChatPage = () => {
|
2023-09-12 19:00:46 +03:00
|
|
|
const { setMessages, setNotifications, notifications } = useChatContext();
|
2023-09-12 18:44:15 +03:00
|
|
|
const { getChatItems } = useChatApi();
|
2023-09-12 19:00:46 +03:00
|
|
|
const { getChatNotifications } = useNotificationApi();
|
2023-08-11 11:06:20 +03:00
|
|
|
const params = useParams();
|
2023-09-12 19:00:46 +03:00
|
|
|
|
2023-08-11 11:06:20 +03:00
|
|
|
const chatId = params?.chatId as string | undefined;
|
|
|
|
|
2023-09-12 19:00:46 +03:00
|
|
|
const chatNotificationsQueryKey = getChatNotificationsQueryKey(chatId ?? "");
|
|
|
|
const { data: fetchedNotifications = [] } = useQuery({
|
|
|
|
queryKey: [chatNotificationsQueryKey],
|
|
|
|
enabled: notifications.length > 0,
|
|
|
|
queryFn: () => {
|
|
|
|
if (chatId === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return getChatNotifications(chatId);
|
|
|
|
},
|
|
|
|
refetchInterval: () => {
|
|
|
|
if (notifications.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const hasAPendingNotification = notifications.find(
|
|
|
|
(item) => item.status === "Pending"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (hasAPendingNotification) {
|
|
|
|
//30 seconds
|
|
|
|
return 30_000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (fetchedNotifications.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setNotifications(fetchedNotifications);
|
|
|
|
}, [fetchedNotifications]);
|
|
|
|
|
2023-08-11 11:06:20 +03:00
|
|
|
useEffect(() => {
|
|
|
|
const fetchHistory = async () => {
|
|
|
|
if (chatId === undefined) {
|
2023-09-12 18:44:15 +03:00
|
|
|
setMessages([]);
|
|
|
|
setNotifications([]);
|
2023-08-11 11:06:20 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:44:15 +03:00
|
|
|
const chatItems = await getChatItems(chatId);
|
2023-08-11 11:06:20 +03:00
|
|
|
|
2023-09-12 18:44:15 +03:00
|
|
|
setMessages(getMessagesFromChatItems(chatItems));
|
|
|
|
setNotifications(getNotificationsFromChatItems(chatItems));
|
2023-08-11 11:06:20 +03:00
|
|
|
};
|
|
|
|
void fetchHistory();
|
2023-09-12 19:00:46 +03:00
|
|
|
}, [chatId]);
|
2023-08-11 11:06:20 +03:00
|
|
|
};
|