fix bug message
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
getChatList,
|
||||
getChatMessages,
|
||||
createChat,
|
||||
markChatAsRead,
|
||||
getChatDetail,
|
||||
} from "../service/Service";
|
||||
import { CreateChatRequest } from "../types/ProfileTypes";
|
||||
|
||||
@@ -17,19 +17,6 @@ export const useChatList = (options?: { enabled?: boolean }) => {
|
||||
});
|
||||
};
|
||||
|
||||
// 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();
|
||||
@@ -52,6 +39,17 @@ export const useMarkChatAsRead = () => {
|
||||
console.log("📤 ارسال درخواست markAsRead برای:", chatId);
|
||||
return markChatAsRead(chatId);
|
||||
},
|
||||
retry: (failureCount, error: unknown) => {
|
||||
// اگر ۴۰۴ است، retry نکن
|
||||
const axiosError = error as { response?: { status?: number } };
|
||||
if (axiosError?.response?.status === 404) {
|
||||
console.log("ℹ️ چت یافت نشد یا قبلاً mark شده - retry نمیکنیم");
|
||||
return false;
|
||||
}
|
||||
// برای سایر خطاها، حداکثر ۱ بار retry کن
|
||||
return failureCount < 1;
|
||||
},
|
||||
retryDelay: 1000,
|
||||
onSuccess: (_, chatId) => {
|
||||
console.log(
|
||||
"✅ پیامها با موفقیت به عنوان خوانده شده علامتگذاری شدند:",
|
||||
@@ -62,8 +60,21 @@ export const useMarkChatAsRead = () => {
|
||||
// Update messages to mark as read
|
||||
queryClient.invalidateQueries({ queryKey: ["chat", "messages", chatId] });
|
||||
},
|
||||
onError: (error, chatId) => {
|
||||
onError: (error: unknown, chatId) => {
|
||||
console.error("❌ خطا در markAsRead:", chatId, error);
|
||||
// اگر ۴۰۴ دریافت کردیم، شاید چت وجود ندارد یا قبلاً mark شده
|
||||
const axiosError = error as { response?: { status?: number } };
|
||||
if (axiosError?.response?.status === 404) {
|
||||
console.log("ℹ️ چت یافت نشد یا قبلاً mark شده:", chatId);
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const useChatDetail = (chatId?: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["chat", "detail", chatId],
|
||||
queryFn: () => getChatDetail(chatId!),
|
||||
enabled: !!chatId,
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user