mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-18 03:41:44 +03:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
|
import { useEffect } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { CHATS_DATA_KEY } from "@/lib/api/chat/config";
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
|
import { useChatsContext } from "@/lib/context/ChatsProvider/hooks/useChatsContext";
|
|
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
|
import { useToast } from "@/lib/hooks";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useChatsList = () => {
|
|
const { t } = useTranslation(["chat"]);
|
|
|
|
const { setAllChats, setIsLoading } = useChatsContext();
|
|
const { publish } = useToast();
|
|
const { getChats } = useChatApi();
|
|
const { session } = useSupabase();
|
|
|
|
const fetchAllChats = async () => {
|
|
if (session) {
|
|
try {
|
|
const response = await getChats();
|
|
|
|
return response.reverse();
|
|
} catch (error) {
|
|
console.error(error);
|
|
publish({
|
|
variant: "danger",
|
|
text: t("errorFetching", { ns: "chat" }),
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
const { data: chats, isLoading } = useQuery({
|
|
queryKey: [CHATS_DATA_KEY],
|
|
queryFn: fetchAllChats,
|
|
});
|
|
|
|
useEffect(() => {
|
|
setAllChats(chats ?? []);
|
|
}, [chats]);
|
|
|
|
useEffect(() => {
|
|
setIsLoading(isLoading);
|
|
}, [isLoading]);
|
|
};
|