From a326be76a144e876d6662981212be8eaa6f99cbf Mon Sep 17 00:00:00 2001 From: Ruan Bueno Date: Tue, 23 Sep 2025 16:05:04 -0300 Subject: [PATCH] fix: corrigindo conversas ativas + barra de pesquisa --- src/components/chat/ChatSidebar.tsx | 63 ++++++++++++++++---- src/server/api/routers/chat-message.ts | 82 +++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 13 deletions(-) diff --git a/src/components/chat/ChatSidebar.tsx b/src/components/chat/ChatSidebar.tsx index 708aa461..f8a40623 100644 --- a/src/components/chat/ChatSidebar.tsx +++ b/src/components/chat/ChatSidebar.tsx @@ -70,13 +70,19 @@ export function ChatSidebar({ currentRoomId, onRoomChange, onUserDoubleClick, cl // Buscar todos os colaboradores const { data: usersData, - isLoading: isLoadingUsers + isLoading: isLoadingUsers, + error: usersError } = api.user.listForChat.useQuery({ - search: searchTerm || undefined, + search: searchTerm.trim() || undefined, }, { enabled: activeTab === "colaboradores" }) + // Log de erro se houver + if (usersError) { + console.error('Erro ao buscar usuários:', usersError) + } + // Buscar status de presença online const { data: presenceData } = useQuery({ queryKey: ['online-users'], @@ -116,19 +122,32 @@ export function ChatSidebar({ currentRoomId, onRoomChange, onUserDoubleClick, cl }, {} as Record) // Filtrar colaboradores por busca - const filteredCollaborators = searchTerm.trim() - ? allColaborators.filter((user: User) => - formatUserName(user).toLowerCase().includes(searchTerm.toLowerCase()) ?? - user.email.toLowerCase().includes(searchTerm.toLowerCase()) ?? - user.setor?.toLowerCase().includes(searchTerm.toLowerCase()) ?? - user.enterprise.toLowerCase().includes(searchTerm.toLowerCase()) - ) - : allColaborators + const filteredCollaborators = (() => { + try { + const trimmedSearch = searchTerm.trim().toLowerCase() + if (!trimmedSearch) return allColaborators + + return allColaborators.filter((user: User) => { + const userName = formatUserName(user).toLowerCase() + const email = user.email.toLowerCase() + const setor = user.setor?.toLowerCase() ?? '' + const enterprise = user.enterprise.toLowerCase() + + return userName.includes(trimmedSearch) || + email.includes(trimmedSearch) || + setor.includes(trimmedSearch) || + enterprise.includes(trimmedSearch) + }) + } catch (error) { + console.error('Erro ao filtrar colaboradores:', error) + return allColaborators + } + })() const activeConversations = (activeConversationsData ?? []) as Array<{ roomId: string roomName: string - roomType: 'global' | 'group' + roomType: 'global' | 'group' | 'private' lastMessage: { content: string | null createdAt: Date @@ -268,6 +287,8 @@ export function ChatSidebar({ currentRoomId, onRoomChange, onUserDoubleClick, cl
{conversation.roomType === 'global' ? ( + ) : conversation.roomType === 'private' ? ( + ) : ( )} @@ -394,8 +415,18 @@ export function ChatSidebar({ currentRoomId, onRoomChange, onUserDoubleClick, cl setSearchTerm(e.target.value)} + onChange={(e) => { + try { + // Limitar tamanho e remover caracteres potencialmente problemáticos + const value = e.target.value.slice(0, 100).replace(/[<>]/g, '') + setSearchTerm(value) + } catch (error) { + console.error('Erro ao atualizar busca:', error) + setSearchTerm('') + } + }} className="pl-9 h-9" + maxLength={100} />
@@ -412,6 +443,14 @@ export function ChatSidebar({ currentRoomId, onRoomChange, onUserDoubleClick, cl ))} + ) : usersError ? ( +
+
⚠️
+

Erro ao carregar colaboradores

+

+ Não foi possível carregar a lista de colaboradores. +

+
) : filteredCollaborators.length === 0 ? (
diff --git a/src/server/api/routers/chat-message.ts b/src/server/api/routers/chat-message.ts index 03b40744..74ac9f88 100644 --- a/src/server/api/routers/chat-message.ts +++ b/src/server/api/routers/chat-message.ts @@ -216,7 +216,7 @@ export const chatMessageRouter = createTRPCRouter({ const conversations: Array<{ roomId: string roomName: string - roomType: 'global' | 'group' + roomType: 'global' | 'group' | 'private' lastMessage: { content: string | null createdAt: Date @@ -297,6 +297,86 @@ export const chatMessageRouter = createTRPCRouter({ }) } + // Buscar chats privados com mensagens recentes + // Buscar todas as mensagens privadas distintas por roomId + const privateRoomIds = await ctx.db.chat_message.findMany({ + where: { + roomId: { + startsWith: 'private_', + }, + OR: [ + { userId }, // Mensagens enviadas pelo usuário atual + { + roomId: { + contains: `user_${userId}`, // RoomIds que contenham o ID do usuário atual + } + } + ] + }, + select: { + roomId: true, + }, + distinct: ['roomId'], + }) + + // Para cada roomId único, buscar a última mensagem + const uniquePrivateRoomIds = [...new Set(privateRoomIds.map(msg => msg.roomId))] + + for (const roomId of uniquePrivateRoomIds) { + const lastMessage = await ctx.db.chat_message.findFirst({ + where: { roomId }, + orderBy: { createdAt: 'desc' }, + include: { + user: { + select: { + firstName: true, + lastName: true, + id: true, + }, + }, + }, + }) + + if (lastMessage) { + // Extrair o ID do outro usuário do roomId + const idPattern = /user_[^_]+/g + const matches = roomId.match(idPattern) ?? [] + const otherUserId = matches.find(id => id !== `user_${userId}`)?.replace('user_', '') + + if (otherUserId) { + // Buscar informações do outro usuário + const otherUser = await ctx.db.user.findUnique({ + where: { id: otherUserId }, + select: { + firstName: true, + lastName: true, + email: true, + }, + }) + + if (otherUser) { + const otherUserName = [otherUser.firstName, otherUser.lastName] + .filter(Boolean) + .join(' ') || otherUser.email + + conversations.push({ + roomId, + roomName: otherUserName, + roomType: 'private' as const, + lastMessage: { + content: lastMessage.content, + createdAt: lastMessage.createdAt, + user: { + firstName: lastMessage.user.firstName, + lastName: lastMessage.user.lastName, + }, + }, + }) + } + } + } + } + // Filtrar apenas conversas que têm mensagens e ordenar por data da última mensagem return conversations .filter(conv => conv.lastMessage !== null)