2023-06-27 12:28:09 +03:00
|
|
|
import { usePathname } from "next/navigation";
|
|
|
|
import { useEffect, useState } from "react";
|
2023-08-07 15:13:41 +03:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-06-27 12:28:09 +03:00
|
|
|
|
2023-07-05 19:33:18 +03:00
|
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
|
|
|
import { useChatsContext } from "@/lib/context/ChatsProvider/hooks/useChatsContext";
|
|
|
|
import { useToast } from "@/lib/hooks";
|
2023-06-27 12:28:09 +03:00
|
|
|
import { useDevice } from "@/lib/hooks/useDevice";
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useChatsList = () => {
|
|
|
|
const { isMobile } = useDevice();
|
|
|
|
const [open, setOpen] = useState(!isMobile);
|
2023-08-07 15:13:41 +03:00
|
|
|
const { t } = useTranslation(['chat']);
|
2023-06-27 12:28:09 +03:00
|
|
|
|
|
|
|
const pathname = usePathname();
|
|
|
|
|
2023-07-05 19:33:18 +03:00
|
|
|
const { setAllChats } = useChatsContext();
|
|
|
|
const { publish } = useToast();
|
|
|
|
const { getChats } = useChatApi();
|
|
|
|
|
2023-06-27 12:28:09 +03:00
|
|
|
useEffect(() => {
|
2023-07-05 19:33:18 +03:00
|
|
|
const fetchAllChats = async () => {
|
|
|
|
try {
|
|
|
|
const response = await getChats();
|
|
|
|
setAllChats(response.reverse());
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
2023-08-07 15:13:41 +03:00
|
|
|
text: t("errorFetching",{ ns : 'chat'})
|
2023-07-05 19:33:18 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void fetchAllChats();
|
|
|
|
}, []);
|
2023-06-27 12:28:09 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setOpen(!isMobile);
|
|
|
|
}, [isMobile, pathname]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
open,
|
|
|
|
setOpen,
|
|
|
|
};
|
|
|
|
};
|