hide chat
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
getChatList,
|
||||
getChatMessages,
|
||||
createChat,
|
||||
markChatAsRead,
|
||||
} from "../service/Service";
|
||||
import { CreateChatRequest } from "../types/ProfileTypes";
|
||||
|
||||
// Get chat list
|
||||
export const useChatList = (options?: { enabled?: boolean }) => {
|
||||
return useQuery({
|
||||
queryKey: ["chat", "list"],
|
||||
queryFn: getChatList,
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
enabled: options?.enabled ?? true,
|
||||
});
|
||||
};
|
||||
|
||||
// Get chat messages
|
||||
export const useChatMessages = (
|
||||
chatId: string,
|
||||
options?: { enabled?: boolean }
|
||||
) => {
|
||||
return useQuery({
|
||||
queryKey: ["chat", "messages", chatId],
|
||||
queryFn: () => getChatMessages(chatId),
|
||||
enabled: options?.enabled ?? !!chatId,
|
||||
staleTime: 1000 * 60 * 1, // 1 minute
|
||||
});
|
||||
};
|
||||
|
||||
// Create new chat
|
||||
export const useCreateChat = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (chatData: CreateChatRequest) => createChat(chatData),
|
||||
onSuccess: () => {
|
||||
// Invalidate chat list to refetch
|
||||
queryClient.invalidateQueries({ queryKey: ["chat", "list"] });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
// Mark chat as read
|
||||
export const useMarkChatAsRead = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (chatId: string) => {
|
||||
console.log("📤 ارسال درخواست markAsRead برای:", chatId);
|
||||
return markChatAsRead(chatId);
|
||||
},
|
||||
onSuccess: (_, chatId) => {
|
||||
console.log(
|
||||
"✅ پیامها با موفقیت به عنوان خوانده شده علامتگذاری شدند:",
|
||||
chatId
|
||||
);
|
||||
// Update chat list to reflect read status
|
||||
queryClient.invalidateQueries({ queryKey: ["chat", "list"] });
|
||||
// Update messages to mark as read
|
||||
queryClient.invalidateQueries({ queryKey: ["chat", "messages", chatId] });
|
||||
},
|
||||
onError: (error, chatId) => {
|
||||
console.error("❌ خطا در markAsRead:", chatId, error);
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user