mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-23 14:31:34 +03:00
e2c1a027b0
Issue: https://github.com/StanGirard/quivr/issues/1888 - Add Spinner when history is loading - Change chat messages fetching logic - Add cha view new design Demo: https://github.com/StanGirard/quivr/assets/63923024/c4341ccf-bacd-4720-9aa1-127dd557a75c
45 lines
1.2 KiB
TypeScript
45 lines
1.2 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 { 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 fetchAllChats = async () => {
|
|
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]);
|
|
};
|