import React, { useState, useRef } from "react"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { MessageCircle, Calendar, Users, Info, MessageSquare, Video, Phone, Briefcase, Plus, ChevronDown, Loader2, ArrowDownUp, Printer, Building2 } from "lucide-react"; import { format, parseISO } from "date-fns"; import { Badge } from "@/components/ui/badge"; import { Communication, ProjectApi, CommunicationCreateRequest } from "@/types"; import { getCommunicationsByProjectId, createCommunication } from "@/services/communicationApi"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { cn } from "@/lib/utils"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { useNavigate } from "react-router-dom"; import { projectService } from "@/lib/api"; import CustomDialog from "@/components/CustomDialog"; import { toast } from "@/lib/custom-toast"; import SlateRichTextEditor from "@/components/custom/SlateRichTextEditor"; interface ProjectCommunicationTrailSectionProps { projectId: number; } const ProjectCommunicationTrailSection: React.FC = ({ projectId }) => { const [sortOrder, setSortOrder] = useState<"desc" | "asc">("desc"); // Newest first by default const [printMode, setPrintMode] = useState(false); const printRef = useRef(null); const navigate = useNavigate(); const queryClient = useQueryClient(); // Add dialog state const [isAddDialogOpen, setIsAddDialogOpen] = useState(false); const [submitLoading, setSubmitLoading] = useState(false); const [formData, setFormData] = useState>({ topic: '', details: '', decisionMade: '', mode: 'In-person', participants: '', communicationDate: new Date().toISOString(), projectId: projectId, }); // Fetch project details to get the project name const { data: project } = useQuery({ queryKey: ['project', projectId], queryFn: () => projectService.getById(projectId), enabled: !!projectId }); // Get project name const projectName = project?.projectName || `Project #${projectId}`; // Fetch communications const { data: communicationsResponse = { data: [], success: true }, isLoading, isError, refetch } = useQuery({ queryKey: ["communications", projectId], queryFn: () => getCommunicationsByProjectId(projectId), }); // Add debug logs console.log("Communications response:", communicationsResponse); // Extract communications array from response with defensive coding const communications = // If response has data property and it's an array (communicationsResponse && 'data' in communicationsResponse && Array.isArray(communicationsResponse.data)) ? communicationsResponse.data // If response is directly an array (unexpected format but handle it) : Array.isArray(communicationsResponse) ? communicationsResponse // Default to empty array for safety : []; console.log("Extracted communications array:", communications); // Get communication icon based on mode const getCommunicationIcon = (mode: string) => { const m = mode.toLowerCase(); if (m.includes('virtual') || m.includes('online') || m.includes('video')) { return