diff --git a/src/app/(authenticated)/chat/_components/users-list.tsx b/src/app/(authenticated)/chat/_components/users-list.tsx index 81d97c4f..4caebfca 100644 --- a/src/app/(authenticated)/chat/_components/users-list.tsx +++ b/src/app/(authenticated)/chat/_components/users-list.tsx @@ -155,7 +155,11 @@ export function UsersList({ onUserDoubleClick, className }: UsersListProps) { // Estados computados const onlineUserIds = new Set(presenceData?.onlineUserIds ?? []) const allUsers = usersData ?? [] - const allColaborators = allUsers.filter((user: User) => user.setor !== 'Sistema') + const allColaborators = allUsers.filter((user: User) => + user.setor !== 'Sistema' && + user.setor !== 'Sem setor' && + user.setor !== 'TESTE' + ) const usersBySector = allColaborators.reduce((acc: Record, user: User) => { const sector = user.setor ?? 'Sem Setor' diff --git a/src/components/chat/ChatRoom.tsx b/src/components/chat/ChatRoom.tsx index f231afd6..e8998386 100644 --- a/src/components/chat/ChatRoom.tsx +++ b/src/components/chat/ChatRoom.tsx @@ -283,8 +283,7 @@ export function ChatRoom({ roomId = "global", className }: ChatRoomProps) { {/* Área de Mensagens */}
{/* Indicador de carregamento no topo */} @@ -369,43 +368,46 @@ export function ChatRoom({ roomId = "global", className }: ChatRoomProps) {
- {/* Formulário de Envio - Fixo na parte inferior */} -
-
+ {/* Formulário de Envio - Sempre na parte inferior */} +
+ {/* Preview da imagem selecionada */} {selectedImageUrl && ( -
+
undefined} // Já foi feito no onImageUploaded onRemove={() => setSelectedImageUrl(null)} - className="mb-2" + className="max-w-xs" />
)} -
- { - setInputMessage(e.target.value) - handleTyping() - }} - placeholder="Digite sua mensagem..." - className="flex-1" - maxLength={2000} - disabled={!isConnected} - /> +
+
+ { + setInputMessage(e.target.value) + handleTyping() + }} + placeholder="Digite sua mensagem..." + className="min-h-[44px] resize-none" + maxLength={2000} + disabled={!isConnected} + /> +
diff --git a/src/components/chat/ChatSidebar.tsx b/src/components/chat/ChatSidebar.tsx index 2d449028..2bad02a0 100644 --- a/src/components/chat/ChatSidebar.tsx +++ b/src/components/chat/ChatSidebar.tsx @@ -45,6 +45,12 @@ export function ChatSidebar({ currentRoomId, onRoomChange, className }: ChatSide { enabled: !!clerkUser?.id } ) + // Buscar conversas ativas + const activeConversationsQuery = api.chatMessage.getActiveConversations.useQuery( + undefined, + { enabled: !!clerkUser?.id } + ) + // Chat global sempre disponível const globalRoom: Room = { id: 'global', @@ -67,60 +73,149 @@ export function ChatSidebar({ currentRoomId, onRoomChange, className }: ChatSide allRooms.push(...groupRooms) } + const activeConversations = activeConversationsQuery.data ?? [] + const isLoading = userGroupsQuery.isLoading || activeConversationsQuery.isLoading + return ( -
-
+
+ {/* Header */} +

Salas

-
- {userGroupsQuery.isLoading ? ( -
- {[1, 2, 3].map((i) => ( -
- ))} -
- ) : ( -
- {allRooms.map((room) => ( - - ))} + + ))} +
)} + + {/* Todas as Salas */} +
+
+

+ Todas as Salas +

+
+ + {isLoading ? ( +
+ {[1, 2, 3].map((i) => ( +
+ ))} +
+ ) : ( +
+ {allRooms.map((room) => ( + + ))} +
+ )} +
) diff --git a/src/server/api/routers/chat-message.ts b/src/server/api/routers/chat-message.ts index 5d5d1725..03b40744 100644 --- a/src/server/api/routers/chat-message.ts +++ b/src/server/api/routers/chat-message.ts @@ -203,4 +203,108 @@ export const chatMessageRouter = createTRPCRouter({ orderBy: { createdAt: 'desc' }, }) }), + + // Buscar conversas ativas (salas com mensagens recentes) + getActiveConversations: protectedProcedure + .query(async ({ ctx }) => { + const userId = ctx.auth.userId + + // Buscar últimas mensagens de todas as salas que o usuário tem acesso + // 1. Chat global (sempre disponível) + // 2. Grupos onde o usuário é membro + + const conversations: Array<{ + roomId: string + roomName: string + roomType: 'global' | 'group' + lastMessage: { + content: string | null + createdAt: Date + user: { + firstName: string | null + lastName: string | null + } + } | null + memberCount?: number + }> = [] + + // Adicionar chat global + const globalLastMessage = await ctx.db.chat_message.findFirst({ + where: { roomId: 'global' }, + orderBy: { createdAt: 'desc' }, + include: { + user: { + select: { + firstName: true, + lastName: true, + }, + }, + }, + }) + + conversations.push({ + roomId: 'global', + roomName: 'Chat Global', + roomType: 'global', + lastMessage: globalLastMessage ? { + content: globalLastMessage.content, + createdAt: globalLastMessage.createdAt, + user: globalLastMessage.user, + } : null, + }) + + // Buscar grupos com mensagens recentes + const userGroups = await ctx.db.chat_group.findMany({ + where: { + isActive: true, + members: { + some: { userId }, + }, + }, + include: { + members: true, + _count: { + select: { members: true }, + }, + }, + }) + + // Para cada grupo, buscar a última mensagem + for (const group of userGroups) { + const lastMessage = await ctx.db.chat_message.findFirst({ + where: { roomId: `group_${group.id}` }, + orderBy: { createdAt: 'desc' }, + include: { + user: { + select: { + firstName: true, + lastName: true, + }, + }, + }, + }) + + conversations.push({ + roomId: `group_${group.id}`, + roomName: group.name, + roomType: 'group', + lastMessage: lastMessage ? { + content: lastMessage.content, + createdAt: lastMessage.createdAt, + user: lastMessage.user, + } : null, + memberCount: group._count.members, + }) + } + + // Filtrar apenas conversas que têm mensagens e ordenar por data da última mensagem + return conversations + .filter(conv => conv.lastMessage !== null) + .sort((a, b) => { + const dateA = a.lastMessage?.createdAt ?? new Date(0) + const dateB = b.lastMessage?.createdAt ?? new Date(0) + return dateB.getTime() - dateA.getTime() // Mais recentes primeiro + }) + .slice(0, 10) // Limitar a 10 conversas ativas + }), })