import React, { useState } from "react"; import { base44 } from "@/api/base44Client"; import { useQuery } from "@tanstack/react-query"; import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { MessageSquare, Plus, Users } from "lucide-react"; import ConversationList from "@/components/messaging/ConversationList"; import MessageThread from "@/components/messaging/MessageThread"; import MessageInput from "@/components/messaging/MessageInput"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; import PageHeader from "@/components/common/PageHeader"; export default function Messages() { const [selectedConversation, setSelectedConversation] = useState(null); const [activeTab, setActiveTab] = useState("all"); const [showNewConversation, setShowNewConversation] = useState(false); const [conversationMode, setConversationMode] = useState("individual"); const [newConvData, setNewConvData] = useState({ type: "client-vendor", subject: "", participant_id: "", participant_name: "", participant_role: "client", group_type: "all-active-staff", event_id: "" }); const { data: user } = useQuery({ queryKey: ['current-user'], queryFn: () => base44.auth.me(), }); const { data: staff } = useQuery({ queryKey: ['staff-list'], queryFn: () => base44.entities.Staff.list(), initialData: [], }); const { data: businesses } = useQuery({ queryKey: ['businesses-list'], queryFn: () => base44.entities.Business.list(), initialData: [], }); const { data: events } = useQuery({ queryKey: ['events-list'], queryFn: () => base44.entities.Event.list('-date'), initialData: [], }); const { data: conversations, refetch: refetchConversations } = useQuery({ queryKey: ['conversations'], queryFn: () => base44.entities.Conversation.list('-last_message_at'), initialData: [], }); const { data: messages, refetch: refetchMessages } = useQuery({ queryKey: ['messages', selectedConversation?.id], queryFn: () => base44.entities.Message.filter({ conversation_id: selectedConversation?.id }), initialData: [], enabled: !!selectedConversation?.id }); const filteredConversations = conversations.filter(conv => { if (activeTab === "all") return conv.status === "active"; if (activeTab === "groups") return conv.is_group && conv.status === "active"; return conv.conversation_type === activeTab && conv.status === "active"; }); const getAvailableParticipants = () => { const convType = newConvData.type; if (convType === "client-vendor" || convType === "client-admin") { return businesses.map(b => ({ id: b.id, name: b.business_name, role: "client", contact: b.contact_name })); } else if (convType === "staff-client" || convType === "staff-admin") { return staff.map(s => ({ id: s.id, name: s.employee_name, role: "staff", position: s.position })); } else if (convType === "vendor-admin") { return businesses.map(b => ({ id: b.id, name: b.business_name, role: "vendor", contact: b.contact_name })); } return []; }; const handleParticipantSelect = (participantId) => { const participants = getAvailableParticipants(); const selected = participants.find(p => p.id === participantId); if (selected) { setNewConvData({ ...newConvData, participant_id: selected.id, participant_name: selected.name, participant_role: selected.role }); } }; const getGroupParticipants = () => { if (newConvData.group_type === "all-active-staff") { return staff.map(s => ({ id: s.id, name: s.employee_name, role: "staff" })); } else if (newConvData.group_type === "event-staff" && newConvData.event_id) { const event = events.find(e => e.id === newConvData.event_id); if (event && event.assigned_staff) { return event.assigned_staff.map(s => ({ id: s.staff_id, name: s.staff_name, role: "staff" })); } } return []; }; const handleCreateConversation = async () => { if (!newConvData.subject) return; let participants = []; let conversationType = ""; let groupName = ""; let isGroup = false; let relatedTo = null; let relatedType = null; if (conversationMode === "individual") { if (!newConvData.participant_id) return; participants = [ { id: user?.id, name: user?.full_name || user?.email, role: "admin" }, { id: newConvData.participant_id, name: newConvData.participant_name, role: newConvData.participant_role } ]; conversationType = newConvData.type; } else { const groupParticipants = getGroupParticipants(); if (groupParticipants.length === 0) return; participants = [ { id: user?.id, name: user?.full_name || user?.email, role: "admin" }, ...groupParticipants ]; isGroup = true; if (newConvData.group_type === "all-active-staff") { conversationType = "group-staff"; groupName = "All Active Staff"; } else if (newConvData.group_type === "event-staff") { conversationType = "group-event-staff"; const event = events.find(e => e.id === newConvData.event_id); groupName = event ? `${event.event_name} Team` : "Event Team"; relatedTo = newConvData.event_id; relatedType = "event"; } } const newConv = await base44.entities.Conversation.create({ participants, conversation_type: conversationType, is_group: isGroup, group_name: groupName, subject: newConvData.subject, status: "active", related_to: relatedTo, related_type: relatedType }); setShowNewConversation(false); setConversationMode("individual"); setNewConvData({ type: "client-vendor", subject: "", participant_id: "", participant_name: "", participant_role: "client", group_type: "all-active-staff", event_id: "" }); refetchConversations(); setSelectedConversation(newConv); }; const availableParticipants = getAvailableParticipants(); const groupParticipants = conversationMode === "group" ? getGroupParticipants() : []; return (
{selectedConversation.group_name} • {selectedConversation.participants?.length || 0} members
) : ({selectedConversation.participants?.map(p => p.name).join(', ')}
)}Select a conversation to start messaging