/** * Copyright 2025 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import React, { useState, useEffect } from "react"; import "./PatientBuilder.css"; import { JsonViewer } from "@textea/json-viewer"; // updated import import DetailsPopup from "../DetailsPopup/DetailsPopup"; // Global caching function to load patients & conditions once let cachedPatientsAndConditions = null; function getPatientsAndConditions() { if (cachedPatientsAndConditions) return Promise.resolve(cachedPatientsAndConditions); return fetch("/assets/patients_and_conditions.json") .then((response) => response.json()) .then((data) => { cachedPatientsAndConditions = data; return data; }); } const PatientBuilder = ({ selectedPatient, selectedCondition, setSelectedPatient, setSelectedCondition, onNext, onBack, }) => { const [patients, setPatients] = useState([]); const [conditions, setConditions] = useState([]); const [hoveredPatient, setHoveredPatient] = useState(null); const [isVideoLoading, setIsVideoLoading] = useState(false); const [isPopupOpen, setIsPopupOpen] = useState(false); const [popupJson, setPopupJson] = useState(null); const [isDetailsPopupOpen, setIsDetailsPopupOpen] = useState(false); useEffect(() => { getPatientsAndConditions() .then((data) => { setPatients(data.patients); setConditions(data.conditions); }) .catch((error) => console.error("Error fetching patients and conditions:", error) ); }, []); useEffect(() => { if ( selectedPatient && selectedPatient.existing_condition !== "depression" && selectedCondition === "Serotonin Syndrome" ) { setSelectedCondition(null); } }, [selectedPatient]); // When a new patient is selected, set the video to a loading state // to ensure the placeholder image is shown. useEffect(() => { if (selectedPatient) { setIsVideoLoading(true); } }, [selectedPatient]); const handleGo = () => { if (selectedPatient && selectedCondition) { onNext(); } }; const openPopup = (patient) => { if (patient && patient.fhirFile) { fetch(patient.fhirFile) .then((response) => response.json()) .then((json) => { setPopupJson(json); setIsPopupOpen(true); }) .catch((error) => console.error("Error fetching FHIR JSON:", error)); } }; const closePopup = () => { setIsPopupOpen(false); setPopupJson(null); }; return (