import { useState, useMemo, useRef } from "react"; import { useQuery, useMutation } from "@tanstack/react-query"; import { TopAppBar } from "@/components/layout/top-app-bar"; import { Sidebar } from "@/components/layout/sidebar"; import { PatientTable } from "@/components/patients/patient-table"; import { AddPatientModal } from "@/components/patients/add-patient-modal"; import { FileUploadZone } from "@/components/file-upload/file-upload-zone"; import { Button } from "@/components/ui/button"; import { Plus, RefreshCw, File, FilePlus } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { PatientUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas"; import { apiRequest, queryClient } from "@/lib/queryClient"; import { useAuth } from "@/hooks/use-auth"; import { z } from "zod"; import useExtractPdfData from "@/hooks/use-extractPdfData"; import { useLocation } from "wouter"; const PatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject ).omit({ appointments: true, }); type Patient = z.infer; const insertPatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject ).omit({ id: true, createdAt: true, userId: true, }); type InsertPatient = z.infer; // Type for the ref to access modal methods type AddPatientModalRef = { shouldSchedule: boolean; navigateToSchedule: (patientId: number) => void; }; export default function PatientsPage() { const { toast } = useToast(); const { user } = useAuth(); const [isAddPatientOpen, setIsAddPatientOpen] = useState(false); const [currentPatient, setCurrentPatient] = useState( undefined ); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const addPatientModalRef = useRef(null); // File upload states const [uploadedFile, setUploadedFile] = useState(null); const [isUploading, setIsUploading] = useState(false); const [isExtracting, setIsExtracting] = useState(false); const { mutate: extractPdf } = useExtractPdfData(); const [location, navigate] = useLocation(); // Add patient mutation const addPatientMutation = useMutation({ mutationFn: async (patient: InsertPatient) => { const res = await apiRequest("POST", "/api/patients/", patient); return res.json(); }, onSuccess: (newPatient) => { setIsAddPatientOpen(false); queryClient.invalidateQueries({ queryKey: ["/api/patients/"] }); toast({ title: "Success", description: "Patient added successfully!", variant: "default", }); // If the add patient modal wants to proceed to scheduling, redirect to appointments page if (addPatientModalRef.current?.shouldSchedule) { addPatientModalRef.current.navigateToSchedule(newPatient.id); } }, onError: (error) => { toast({ title: "Error", description: `Failed to add patient: ${error.message}`, variant: "destructive", }); }, }); const toggleMobileMenu = () => { setIsMobileMenuOpen(!isMobileMenuOpen); }; const handleAddPatient = (patient: InsertPatient) => { if (user) { addPatientMutation.mutate({ ...patient, userId: user.id, }); } }; const isLoading = addPatientMutation.isPending; // File upload handling const handleFileUpload = (file: File) => { setIsUploading(true); setUploadedFile(file); toast({ title: "File Selected", description: `${file.name} is ready for processing.`, variant: "default", }); setIsUploading(false); }; // data extraction const handleExtract = () => { setIsExtracting(true); if (!uploadedFile) { return toast({ title: "Error", description: "Please upload a PDF", variant: "destructive", }); } extractPdf(uploadedFile, { onSuccess: (data) => { setIsExtracting(false); toast({ title: "Success Pdf Data Extracted", description: `Name: ${data.name}, Member ID: ${data.memberId}, DOB: ${data.dob}`, variant: "default", }); const params = new URLSearchParams({ name: data.name, memberId: data.memberId, dob: data.dob, }); navigate( `/claims?name=${encodeURIComponent(data.name)}&memberId=${data.memberId}&dob=${data.dob}` ); }, }); }; return (

Patients

Manage patient records and information

{/* File Upload Zone */}
Upload Patient Document
{/* Patients Table */} Patient Records View and manage all patient information {/* Add/Edit Patient Modal */}
); }