File size: 8,482 Bytes
a6bdbe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 * 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 (
    <div className="patient-builder-container">
      <div className="headerButtonsContainer">
      <button className="back-button" onClick={onBack}>
          <i className="material-icons back-button-icon">keyboard_arrow_left</i>
          Back
      </button>
      <button className="details-button" onClick={() => setIsDetailsPopupOpen(true)}>
          <i className="material-icons code-block-icon">code</i>&nbsp;
          Details about this Demo
      </button>
      </div>
      <div className="frame">
        <div className="selection-section">
          <div className="header2">Select a Patient</div>
          <div className="patient-list">
            {patients.map((patient) => {
              const isSelected = selectedPatient && selectedPatient.id === patient.id;
              return (
                <div
                  key={patient.id}
                  className="patient-card"
                >
                  <div
                    className={`patient-video-container ${isSelected ? "selected" : ""}`}
                    onClick={() => setSelectedPatient(patient)}
                  >
                    <img
                      src={patient.img}
                      className="patient-img"
                      alt={patient.name}
                      draggable="false"
                      onDragStart={(e) => e.preventDefault()}
                      style={{ opacity: isSelected && !isVideoLoading ? 0 : 1 }}
                    />
                    {isSelected && (
                      <video
                        key={patient.id}
                        src={patient.video}
                        className="patient-video"
                        autoPlay
                        muted
                        loop
                        onCanPlay={() => setIsVideoLoading(false)}
                        style={{ opacity: isVideoLoading ? 0 : 1 }}
                      />
                    )}
                    <div className="ehr-label" onClick={(e) => { e.stopPropagation(); openPopup(patient); }}>
                      Synthetic Health Record (FHIR)
                    </div>
                  </div>
                  <div className="patient-info">
                    <div className="category-value">
                      {patient.name}, {patient.age} years old, {patient.gender}
                    </div>
                    <div className="category-value">
                      Existing condition: {patient.existing_condition}
                    </div>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
        <div className="selection-section">
          <div className="header2">Explore a Condition</div>
          <div className="lighttext">
          In this demonstration, a persona, simulated using Gemini 2.5 Flash, will interact with an AI agent, built with MedGemma. 
          Neither the simulated persona nor the AI agent have been provided the diagnosis for the current condition (selected below). 
          The AI agent facilitates structured information-gathering, designed to usefully collect and summarize the patient's symptoms. 
          For the purposes of this demonstration, the AI agent also has access to elements of the patient's health record (provided as FHIR resources).
          </div>
          <div className="condition-list">
            {conditions.map((cond) => {
              const isDisabled =
                cond.name === "Serotonin Syndrome" &&
                selectedPatient &&
                selectedPatient.existing_condition !== "Depression";
              return (
                <div
                  key={cond.name}
                  className={`condition-card lighttext ${
                    selectedCondition === cond.name ? "selected" : ""
                  } ${isDisabled ? "disabled" : ""}`}
                  onClick={
                    !isDisabled
                      ? () => setSelectedCondition(cond.name)
                      : undefined
                  }
                >
                  <div><strong>{cond.name}</strong></div>
                  <div>{cond.description}</div>
                </div>
              );
            })}
          </div>
        </div>
        <button
          className="info-button"
          onClick={handleGo}
          disabled={!(selectedPatient && selectedCondition)}
        >
          Launch simulation
        </button>
      </div>
      {isPopupOpen && (
        <div className="popup-overlay" onClick={closePopup}>
          <div
            className="popup-content json-popup-content"
            onClick={(e) => e.stopPropagation()}
          >
            <h2>Synthetic Electronic Health Record</h2>
            <span>This is a sample of the patient’s electronic health record, shown in a standard (FHIR) format. This FHIR record, like the patient, was generated solely for the purposes of this demo.</span>
            <div className="json-viewer-container">
              <JsonViewer value={popupJson} theme="monokai" />
            </div>
            <button className="popup-button" onClick={closePopup}>
              Close
            </button>
          </div>
        </div>
      )}
       <DetailsPopup
        isOpen={isDetailsPopupOpen}
        onClose={() => setIsDetailsPopupOpen(false)}
      />
    </div>
  );
};

export default PatientBuilder;