style: clean up code formatting and remove trailing whitespace

refactor: reorganize imports and fix type imports
This commit is contained in:
Evandro Fonseca Junior committed 2025-11-11 03:08:36 -03:00
1 parent d3704602fd
commit 47c04de6fe
232 files changed
+7140 -4798

No files matched your search

+29 -15
View File
@@ -1,10 +1,10 @@
"use client";
import { useState, useEffect, useCallback, useRef } from "react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useSocket } from "./use-socket";
import { useSession } from "./use-session";
import { useCallback, useEffect, useRef, useState } from "react";
import type { Socket } from "socket.io-client";
import { useSession } from "./use-session";
import { useSocket } from "./use-socket";
export interface ChatMessage {
_id: string;
@@ -109,7 +109,9 @@ export function useChat(chatId: string | null): UseChatReturn {
queryKey: ["chat-messages", chatId, page],
queryFn: async () => {
if (!chatId) return null;
const res = await fetch(`/api/chats/${chatId}/messages?page=${page}&limit=50`);
const res = await fetch(
`/api/chats/${chatId}/messages?page=${page}&limit=50`,
);
if (!res.ok) throw new Error("Erro ao buscar mensagens");
return res.json();
},
@@ -126,7 +128,9 @@ export function useChat(chatId: string | null): UseChatReturn {
// Adicionar mensagens antigas no início
setMessages((prev) => [...messagesData.messages, ...prev]);
}
setHasMore(messagesData.pagination.page < messagesData.pagination.totalPages);
setHasMore(
messagesData.pagination.page < messagesData.pagination.totalPages,
);
}
}, [messagesData, page]);
@@ -139,25 +143,33 @@ export function useChat(chatId: string | null): UseChatReturn {
// Listener para novas mensagens
const handleNewMessage = (data: { message: ChatMessage }) => {
console.log("[CHAT-DEBUG] useChat: new_message recebido", { message: data.message });
console.log("[CHAT-DEBUG] useChat: new_message recebido", {
message: data.message,
});
// Verificar se a mensagem já existe para evitar duplicação
setMessages((prev) => {
const messageExists = prev.some((msg) => msg._id === data.message._id);
if (messageExists) {
console.log("[CHAT-DEBUG] Mensagem já existe, ignorando duplicação", { messageId: data.message._id });
console.log("[CHAT-DEBUG] Mensagem já existe, ignorando duplicação", {
messageId: data.message._id,
});
return prev;
}
return [...prev, data.message];
});
// Invalidar cache
queryClient.invalidateQueries({ queryKey: ["chat-messages", chatId] });
queryClient.invalidateQueries({ queryKey: ["chat", chatId] });
};
// Listener para indicador de digitação
const handleTyping = (data: { user_id: number; username: string; is_typing: boolean }) => {
const handleTyping = (data: {
user_id: number;
username: string;
is_typing: boolean;
}) => {
if (data.user_id !== user?.userId) {
setOtherUserTyping(data.is_typing);
}
@@ -177,7 +189,11 @@ export function useChat(chatId: string | null): UseChatReturn {
async (content: string) => {
if (!chatId || !socket || !isConnected || !content.trim()) return;
console.log("[CHAT-DEBUG] sendMessage chamado", { chatId, content, socketConnected: isConnected });
console.log("[CHAT-DEBUG] sendMessage chamado", {
chatId,
content,
socketConnected: isConnected,
});
setIsSending(true);
try {
@@ -202,7 +218,7 @@ export function useChat(chatId: string | null): UseChatReturn {
setIsSending(false);
}
},
[chatId, socket, isConnected, user]
[chatId, socket, isConnected],
);
// Função para carregar mais mensagens
@@ -211,7 +227,6 @@ export function useChat(chatId: string | null): UseChatReturn {
setPage((prev) => prev + 1);
}, [hasMore, isLoadingMessages]);
return {
chat: chatData?.chat || null,
messages,
@@ -230,7 +245,7 @@ export function useChat(chatId: string | null): UseChatReturn {
export function useChatTyping(
socket: Socket | null,
isConnected: boolean,
chatId: string | null
chatId: string | null,
) {
const [isTyping, setIsTyping] = useState(false);
const typingTimeoutRef = useRef<NodeJS.Timeout | null>(null);
@@ -272,4 +287,3 @@ export function useChatTyping(
return { handleTypingStart, handleTypingStop };
}