import { useState } from "react"; import { useQuery, useMutation } from "@tanstack/react-query"; import { Sidebar } from "@/components/layout/sidebar"; import { TopAppBar } from "@/components/layout/top-app-bar"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Phone, PhoneCall, Clock, User, Search, MessageSquare, X, } from "lucide-react"; import { SmsTemplateDialog } from "@/components/patient-connection/sms-template-diaog"; import { MessageThread } from "@/components/patient-connection/message-thread"; import { useToast } from "@/hooks/use-toast"; import { apiRequest } from "@/lib/queryClient"; import type { Patient } from "@repo/db/types"; export default function PatientConnectionPage() { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [searchTerm, setSearchTerm] = useState(""); const [selectedPatient, setSelectedPatient] = useState(null); const [isSmsDialogOpen, setIsSmsDialogOpen] = useState(false); const [showMessaging, setShowMessaging] = useState(false); const { toast } = useToast(); const toggleMobileMenu = () => { setIsMobileMenuOpen(!isMobileMenuOpen); }; const makeCallMutation = useMutation({ mutationFn: async ({ to, patientId, }: { to: string; patientId: number; }) => { return apiRequest("POST", "/api/twilio/make-call", { to, message: "Hello, this is a call from your dental office. We are calling to connect with you.", patientId, }); }, onSuccess: () => { toast({ title: "Call Initiated", description: "The call has been placed successfully.", }); }, onError: (error: any) => { toast({ title: "Call Failed", description: error.message || "Unable to place the call. Please try again.", variant: "destructive", }); }, }); // Fetch all patients from database const { data: patients = [], isLoading } = useQuery({ queryKey: ["/api/patients"], }); // Filter patients based on search term const filteredPatients = patients.filter((patient) => { if (!searchTerm) return false; const searchLower = searchTerm.toLowerCase(); const fullName = `${patient.firstName} ${patient.lastName}`.toLowerCase(); const phone = patient.phone?.toLowerCase() || ""; const patientId = patient.id?.toString(); return ( fullName.includes(searchLower) || phone.includes(searchLower) || patientId?.includes(searchLower) ); }); // Handle calling patient via Twilio const handleCall = (patient: Patient) => { if (patient.phone) { makeCallMutation.mutate({ to: patient.phone, patientId: Number(patient.id), }); } }; // Handle sending SMS const handleSMS = (patient: Patient) => { setSelectedPatient(patient); setIsSmsDialogOpen(true); }; // Handle opening messaging const handleOpenMessaging = (patient: Patient) => { setSelectedPatient(patient); setShowMessaging(true); }; // Handle closing messaging const handleCloseMessaging = () => { setShowMessaging(false); setSelectedPatient(null); }; // Sample call data const recentCalls = [ { id: 1, patientName: "John Bill", phoneNumber: "(555) 123-4567", callType: "Appointment Request", status: "Completed", duration: "3:45", time: "2 hours ago", }, { id: 2, patientName: "Emily Brown", phoneNumber: "(555) 987-6543", callType: "Insurance Question", status: "Follow-up Required", duration: "6:12", time: "4 hours ago", }, { id: 3, patientName: "Mike Johnson", phoneNumber: "(555) 456-7890", callType: "Prescription Refill", status: "Completed", duration: "2:30", time: "6 hours ago", }, ]; const callStats = [ { label: "Total Calls Today", value: "23", icon: , }, { label: "Answered Calls", value: "21", icon: , }, { label: "Average Call Time", value: "4:32", icon: , }, { label: "Active Patients", value: patients.length.toString(), icon: , }, ]; return (

Patient Connection

Search and communicate with patients

{/* Call Statistics */}
{callStats.map((stat, index) => (

{stat.label}

{stat.value}

{stat.icon}
))}
{/* Search and Actions */} Search Patients Search by name, phone number, or patient ID
setSearchTerm(e.target.value)} data-testid="input-patient-search" />
{/* Search Results */} {searchTerm && (
{isLoading ? (

Loading patients...

) : filteredPatients.length > 0 ? (

Search Results ({filteredPatients.length})

{filteredPatients.map((patient) => (

{patient.firstName} {patient.lastName}

{patient.phone || "No phone number"} • ID:{" "} {patient.id}

{patient.email && (

{patient.email}

)}
))}
) : (

No patients found matching "{searchTerm}"

)}
)}
{/* Recent Calls */} Recent Calls View and manage recent patient calls
{recentCalls.map((call) => (

{call.patientName}

{call.phoneNumber}

{call.callType}

Duration: {call.duration}

{call.status}

{call.time}

))}
{/* SMS Template Dialog */} {/* Messaging Interface */} {showMessaging && selectedPatient && (
)}
); }