@photonhealth/react 2.0.0-rc.1 → 2.0.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. package/README.md +52 -9
  2. package/dist/adapters/tailwind.d.ts.map +1 -1
  3. package/dist/adapters/tailwind.js +7 -4
  4. package/dist/adapters/tailwind.js.map +1 -1
  5. package/dist/analytics/PhotonAnalyticsProvider.d.ts +19 -0
  6. package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
  7. package/dist/analytics/PhotonAnalyticsProvider.js +76 -0
  8. package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
  9. package/dist/analytics/cardLifecycle.d.ts +10 -0
  10. package/dist/analytics/cardLifecycle.d.ts.map +1 -0
  11. package/dist/analytics/cardLifecycle.js +43 -0
  12. package/dist/analytics/cardLifecycle.js.map +1 -0
  13. package/dist/analytics/fieldInteraction.d.ts +18 -0
  14. package/dist/analytics/fieldInteraction.d.ts.map +1 -0
  15. package/dist/analytics/fieldInteraction.js +102 -0
  16. package/dist/analytics/fieldInteraction.js.map +1 -0
  17. package/dist/analytics/lifecycleOutcomes.d.ts +8 -0
  18. package/dist/analytics/lifecycleOutcomes.d.ts.map +1 -0
  19. package/dist/analytics/lifecycleOutcomes.js +16 -0
  20. package/dist/analytics/lifecycleOutcomes.js.map +1 -0
  21. package/dist/analytics/tracker.d.ts +19 -0
  22. package/dist/analytics/tracker.d.ts.map +1 -0
  23. package/dist/analytics/tracker.js +41 -0
  24. package/dist/analytics/tracker.js.map +1 -0
  25. package/dist/analytics/viewed.d.ts +11 -0
  26. package/dist/analytics/viewed.d.ts.map +1 -0
  27. package/dist/analytics/viewed.js +16 -0
  28. package/dist/analytics/viewed.js.map +1 -0
  29. package/dist/components/PhotonOrder.d.ts +18 -18
  30. package/dist/components/PhotonOrder.d.ts.map +1 -1
  31. package/dist/components/PhotonOrder.js +34 -19
  32. package/dist/components/PhotonOrder.js.map +1 -1
  33. package/dist/components/PhotonPatient.d.ts +7 -35
  34. package/dist/components/PhotonPatient.d.ts.map +1 -1
  35. package/dist/components/PhotonPatient.js +6 -78
  36. package/dist/components/PhotonPatient.js.map +1 -1
  37. package/dist/components/PhotonPrescription.d.ts +15 -26
  38. package/dist/components/PhotonPrescription.d.ts.map +1 -1
  39. package/dist/components/PhotonPrescription.js +6 -360
  40. package/dist/components/PhotonPrescription.js.map +1 -1
  41. package/dist/components/format.d.ts +2 -2
  42. package/dist/components/format.js +2 -2
  43. package/dist/components/index.d.ts +1 -1
  44. package/dist/components/index.d.ts.map +1 -1
  45. package/dist/components/index.js +1 -1
  46. package/dist/components/index.js.map +1 -1
  47. package/dist/components/open.d.ts +11 -13
  48. package/dist/components/open.d.ts.map +1 -1
  49. package/dist/components/open.js +7 -2
  50. package/dist/components/open.js.map +1 -1
  51. package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
  52. package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
  53. package/dist/components/order/DraftOrderPrompt.js +26 -0
  54. package/dist/components/order/DraftOrderPrompt.js.map +1 -0
  55. package/dist/components/patient/PatientCard/PatientCard.d.ts +30 -0
  56. package/dist/components/patient/PatientCard/PatientCard.d.ts.map +1 -0
  57. package/dist/components/patient/PatientCard/PatientCard.js +101 -0
  58. package/dist/components/patient/PatientCard/PatientCard.js.map +1 -0
  59. package/dist/components/patient/PatientCard/usePatientToShow.d.ts +22 -0
  60. package/dist/components/patient/PatientCard/usePatientToShow.d.ts.map +1 -0
  61. package/dist/components/patient/PatientCard/usePatientToShow.js +40 -0
  62. package/dist/components/patient/PatientCard/usePatientToShow.js.map +1 -0
  63. package/dist/components/patient/PatientIntakeForm.d.ts +16 -0
  64. package/dist/components/patient/PatientIntakeForm.d.ts.map +1 -0
  65. package/dist/components/patient/PatientIntakeForm.js +114 -0
  66. package/dist/components/patient/PatientIntakeForm.js.map +1 -0
  67. package/dist/components/patient/PatientSearch.d.ts +12 -0
  68. package/dist/components/patient/PatientSearch.d.ts.map +1 -0
  69. package/dist/components/patient/PatientSearch.js +148 -0
  70. package/dist/components/patient/PatientSearch.js.map +1 -0
  71. package/dist/components/patient/{Benefits.d.ts → PatientSummary/BenefitsInputForm.d.ts} +2 -2
  72. package/dist/components/patient/PatientSummary/BenefitsInputForm.d.ts.map +1 -0
  73. package/dist/components/patient/{Benefits.js → PatientSummary/BenefitsInputForm.js} +6 -5
  74. package/dist/components/patient/PatientSummary/BenefitsInputForm.js.map +1 -0
  75. package/dist/components/patient/{Clinical.d.ts → PatientSummary/ClinicalInputForm.d.ts} +15 -16
  76. package/dist/components/patient/PatientSummary/ClinicalInputForm.d.ts.map +1 -0
  77. package/dist/components/patient/{Clinical.js → PatientSummary/ClinicalInputForm.js} +29 -28
  78. package/dist/components/patient/PatientSummary/ClinicalInputForm.js.map +1 -0
  79. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts +5 -0
  80. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts.map +1 -0
  81. package/dist/components/patient/PatientSummary/CollapsedSummary.js +30 -0
  82. package/dist/components/patient/PatientSummary/CollapsedSummary.js.map +1 -0
  83. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts +10 -0
  84. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts.map +1 -0
  85. package/dist/components/patient/PatientSummary/PatientDetailsForm.js +65 -0
  86. package/dist/components/patient/PatientSummary/PatientDetailsForm.js.map +1 -0
  87. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts +53 -0
  88. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts.map +1 -0
  89. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js +78 -0
  90. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js.map +1 -0
  91. package/dist/components/patient/PatientSummary/PatientSummary.d.ts +46 -0
  92. package/dist/components/patient/PatientSummary/PatientSummary.d.ts.map +1 -0
  93. package/dist/components/patient/PatientSummary/PatientSummary.js +57 -0
  94. package/dist/components/patient/PatientSummary/PatientSummary.js.map +1 -0
  95. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts +19 -0
  96. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts.map +1 -0
  97. package/dist/components/patient/PatientSummary/SummaryHeader.js +22 -0
  98. package/dist/components/patient/PatientSummary/SummaryHeader.js.map +1 -0
  99. package/dist/components/patient/fields.d.ts +7 -26
  100. package/dist/components/patient/fields.d.ts.map +1 -1
  101. package/dist/components/patient/fields.js +11 -52
  102. package/dist/components/patient/fields.js.map +1 -1
  103. package/dist/components/patient/parsePatientSearchQuery.d.ts +16 -0
  104. package/dist/components/patient/parsePatientSearchQuery.d.ts.map +1 -0
  105. package/dist/components/patient/{query.js → parsePatientSearchQuery.js} +3 -10
  106. package/dist/components/patient/parsePatientSearchQuery.js.map +1 -0
  107. package/dist/components/prescription/PrescriptionCard.d.ts +51 -0
  108. package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
  109. package/dist/components/prescription/PrescriptionCard.js +186 -0
  110. package/dist/components/prescription/PrescriptionCard.js.map +1 -0
  111. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
  112. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
  113. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
  114. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
  115. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
  116. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
  117. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
  118. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
  119. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
  120. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
  121. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
  122. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
  123. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
  124. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
  125. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
  126. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
  127. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
  128. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
  129. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
  130. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
  131. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
  132. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
  133. package/dist/components/{TreatmentPicker.js → prescription/TreatmentSearch/TreatmentPicker.js} +113 -105
  134. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
  135. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
  136. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
  137. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
  138. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
  139. package/dist/components/schemaErrors.d.ts +13 -0
  140. package/dist/components/schemaErrors.d.ts.map +1 -0
  141. package/dist/components/schemaErrors.js +25 -0
  142. package/dist/components/schemaErrors.js.map +1 -0
  143. package/dist/components/search.d.ts +1 -1
  144. package/dist/components/search.js +1 -1
  145. package/dist/index.d.ts +2 -0
  146. package/dist/index.d.ts.map +1 -1
  147. package/dist/index.js +1 -0
  148. package/dist/index.js.map +1 -1
  149. package/dist/meta/version.d.ts +5 -0
  150. package/dist/meta/version.d.ts.map +1 -0
  151. package/dist/meta/version.js +5 -0
  152. package/dist/meta/version.js.map +1 -0
  153. package/dist/primitives/PhotonProvider.d.ts +3 -1
  154. package/dist/primitives/PhotonProvider.d.ts.map +1 -1
  155. package/dist/primitives/PhotonProvider.js +23 -11
  156. package/dist/primitives/PhotonProvider.js.map +1 -1
  157. package/dist/primitives/defaults.d.ts.map +1 -1
  158. package/dist/primitives/defaults.js +6 -3
  159. package/dist/primitives/defaults.js.map +1 -1
  160. package/dist/primitives/photonContext.d.ts +3 -0
  161. package/dist/primitives/photonContext.d.ts.map +1 -0
  162. package/dist/primitives/photonContext.js +3 -0
  163. package/dist/primitives/photonContext.js.map +1 -0
  164. package/dist/primitives/types.d.ts +15 -3
  165. package/dist/primitives/types.d.ts.map +1 -1
  166. package/dist/primitives/types.js.map +1 -1
  167. package/dist/primitives/useProvidedPhoton.d.ts +8 -0
  168. package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
  169. package/dist/primitives/useProvidedPhoton.js +8 -0
  170. package/dist/primitives/useProvidedPhoton.js.map +1 -0
  171. package/dist/styles.css +45 -3
  172. package/dist/test-utils/contract.d.ts.map +1 -1
  173. package/dist/test-utils/contract.js +23 -2
  174. package/dist/test-utils/contract.js.map +1 -1
  175. package/package.json +8 -3
  176. package/src/adapters/tailwind.tsx +52 -24
  177. package/src/analytics/PhotonAnalyticsProvider.test.tsx +190 -0
  178. package/src/analytics/PhotonAnalyticsProvider.tsx +151 -0
  179. package/src/analytics/cardLifecycle.test.tsx +272 -0
  180. package/src/analytics/cardLifecycle.ts +44 -0
  181. package/src/analytics/fieldInteraction.test.tsx +186 -0
  182. package/src/analytics/fieldInteraction.tsx +148 -0
  183. package/src/analytics/lifecycleOutcomes.test.ts +78 -0
  184. package/src/analytics/lifecycleOutcomes.ts +29 -0
  185. package/src/analytics/tracker.test.ts +52 -0
  186. package/src/analytics/tracker.ts +49 -0
  187. package/src/analytics/usePhotonAnalytics.test.tsx +106 -0
  188. package/src/analytics/viewed.ts +27 -0
  189. package/src/components/PhotonOrder.test.tsx +394 -0
  190. package/src/components/PhotonOrder.tsx +79 -53
  191. package/src/components/PhotonPatient.tsx +15 -166
  192. package/src/components/PhotonPrescription.test.tsx +120 -0
  193. package/src/components/PhotonPrescription.tsx +23 -583
  194. package/src/components/ScreeningAlerts.test.tsx +81 -0
  195. package/src/components/format.ts +2 -2
  196. package/src/components/index.ts +1 -1
  197. package/src/components/open.ts +13 -16
  198. package/src/components/order/DraftOrderPrompt.test.tsx +77 -0
  199. package/src/components/order/DraftOrderPrompt.tsx +81 -0
  200. package/src/components/patient/PatientCard/PatientCard.test.tsx +255 -0
  201. package/src/components/patient/PatientCard/PatientCard.tsx +177 -0
  202. package/src/components/patient/PatientCard/usePatientToShow.ts +44 -0
  203. package/src/components/patient/PatientIntakeForm.test.tsx +262 -0
  204. package/src/components/patient/PatientIntakeForm.tsx +222 -0
  205. package/src/components/patient/PatientSearch.test.tsx +204 -0
  206. package/src/components/patient/PatientSearch.tsx +221 -0
  207. package/src/components/patient/PatientSummary/BenefitsInputForm.test.tsx +164 -0
  208. package/src/components/patient/{Benefits.tsx → PatientSummary/BenefitsInputForm.tsx} +24 -16
  209. package/src/components/patient/PatientSummary/ClinicalInputForm.test.tsx +247 -0
  210. package/src/components/patient/{Clinical.tsx → PatientSummary/ClinicalInputForm.tsx} +53 -48
  211. package/src/components/patient/PatientSummary/CollapsedSummary.test.tsx +90 -0
  212. package/src/components/patient/PatientSummary/CollapsedSummary.tsx +46 -0
  213. package/src/components/patient/PatientSummary/PatientDetailsForm.test.tsx +201 -0
  214. package/src/components/patient/PatientSummary/PatientDetailsForm.tsx +320 -0
  215. package/src/components/patient/PatientSummary/PatientDetailsSchema.test.ts +296 -0
  216. package/src/components/patient/PatientSummary/PatientDetailsSchema.ts +95 -0
  217. package/src/components/patient/PatientSummary/PatientSummary.test.tsx +194 -0
  218. package/src/components/patient/PatientSummary/PatientSummary.tsx +225 -0
  219. package/src/components/patient/PatientSummary/SummaryHeader.test.tsx +183 -0
  220. package/src/components/patient/PatientSummary/SummaryHeader.tsx +85 -0
  221. package/src/components/patient/fields.tsx +12 -124
  222. package/src/components/patient/parsePatientSearchQuery.test.ts +107 -0
  223. package/src/components/patient/{query.ts → parsePatientSearchQuery.ts} +4 -15
  224. package/src/components/prescription/PrescriptionCard.test.tsx +247 -0
  225. package/src/components/prescription/PrescriptionCard.tsx +361 -0
  226. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +82 -0
  227. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
  228. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +206 -0
  229. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
  230. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +453 -0
  231. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
  232. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
  233. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
  234. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +72 -0
  235. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
  236. package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
  237. package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +695 -0
  238. package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
  239. package/src/components/schemaErrors.test.ts +49 -0
  240. package/src/components/schemaErrors.ts +34 -0
  241. package/src/components/search.tsx +1 -1
  242. package/src/index.ts +2 -0
  243. package/src/meta/version.test.ts +11 -0
  244. package/src/meta/version.ts +4 -0
  245. package/src/primitives/PhotonProvider.tsx +30 -15
  246. package/src/primitives/defaults.tsx +44 -21
  247. package/src/primitives/photonContext.ts +4 -0
  248. package/src/primitives/types.ts +15 -3
  249. package/src/primitives/useProvidedPhoton.ts +21 -0
  250. package/src/styles.css +45 -3
  251. package/src/test-utils/contract.tsx +45 -2
  252. package/dist/components/TreatmentPicker.d.ts +0 -45
  253. package/dist/components/TreatmentPicker.d.ts.map +0 -1
  254. package/dist/components/TreatmentPicker.js.map +0 -1
  255. package/dist/components/patient/Benefits.d.ts.map +0 -1
  256. package/dist/components/patient/Benefits.js.map +0 -1
  257. package/dist/components/patient/Clinical.d.ts.map +0 -1
  258. package/dist/components/patient/Clinical.js.map +0 -1
  259. package/dist/components/patient/IntakeForm.d.ts +0 -10
  260. package/dist/components/patient/IntakeForm.d.ts.map +0 -1
  261. package/dist/components/patient/IntakeForm.js +0 -324
  262. package/dist/components/patient/IntakeForm.js.map +0 -1
  263. package/dist/components/patient/Summary.d.ts +0 -48
  264. package/dist/components/patient/Summary.d.ts.map +0 -1
  265. package/dist/components/patient/Summary.js +0 -80
  266. package/dist/components/patient/Summary.js.map +0 -1
  267. package/dist/components/patient/query.d.ts +0 -23
  268. package/dist/components/patient/query.d.ts.map +0 -1
  269. package/dist/components/patient/query.js.map +0 -1
  270. package/src/components/TreatmentPicker.tsx +0 -350
  271. package/src/components/patient/IntakeForm.tsx +0 -532
  272. package/src/components/patient/Summary.tsx +0 -310
@@ -0,0 +1,361 @@
1
+ import { useComponents, usePhoton } from "../../primitives/index.ts";
2
+ import {
3
+ classify,
4
+ Patient,
5
+ Prescription,
6
+ type PatientInput,
7
+ type PatientResult,
8
+ type PrescriptionEdits,
9
+ type PrescriptionInput,
10
+ type PrescriptionRecord,
11
+ type PrescriptionResult,
12
+ type PrescriptionTemplate,
13
+ } from "@photonhealth/client";
14
+ import { useLive, useOnChange } from "../../hooks/index.ts";
15
+ import { useEffect, useRef, useState, type ReactNode } from "react";
16
+ import { useHeldActivity } from "../activity.ts";
17
+ import { SearchResults } from "../search.tsx";
18
+ import { NotFound, openFailed, toOpen, useOpened } from "../open.ts";
19
+ import { treatmentHeading } from "../display.ts";
20
+ import { dispenseSummary } from "../format.ts";
21
+ import { ChangeList } from "../ChangeList.tsx";
22
+ import { PrescriptionDetailsForm } from "./PrescriptionDetailsForm/PrescriptionDetailsForm.tsx";
23
+ import { ScreeningAlerts } from "../ScreeningAlerts.tsx";
24
+ import { TreatmentSearch, type TreatmentChoice } from "./TreatmentSearch/TreatmentSearch.tsx";
25
+ import { usePhotonAnalytics } from "../../analytics/PhotonAnalyticsProvider.tsx";
26
+ import { useSendDraftPrescriptionAdded } from "../../analytics/cardLifecycle.ts";
27
+
28
+ /**
29
+ * One prescription line, bound to a live Prescription: PhotonPrescription's card (which adds the analytics workflow
30
+ * and the sign-in), and each line of PhotonOrder. Internal: the package index exports only PhotonPrescription.
31
+ */
32
+ export interface PrescriptionCardProps {
33
+ /**
34
+ * The prescription, one of:
35
+ * - a live `Prescription`: used as is; anything else holding it sees the same changes
36
+ * - its id, `"rx_…"` or `{ id }`: gets the prescription
37
+ * - its id with fields, `{ id, … }`: updates the prescription
38
+ * - fields without an id, e.g. `{ templateId }` or `{ treatment: { name } }`: upserts the prescription
39
+ */
40
+ prescription?: Prescription | PrescriptionInput | string | null;
41
+ /**
42
+ * Who it's for, one of:
43
+ * - a live `Patient`: used as is
44
+ * - its id, `"pat_…"` or `{ id }`: gets the patient
45
+ * - its id with fields, `{ id, … }`: updates the patient
46
+ * - fields without an id: upserts the patient
47
+ *
48
+ * Until it's known, a new line only looks drugs up (a line from an order already knows).
49
+ */
50
+ patient?: Patient | PatientInput | string | null;
51
+ /** Read-only (e.g. on a finalized order). A signed prescription is always read-only. */
52
+ locked?: boolean;
53
+ /** The prescriber signed it on this card — once per prescription. */
54
+ onSigned?: (prescription: Prescription) => void;
55
+ /**
56
+ * The card's prescription, once it has a record and each time that record changes from the backend
57
+ * (created, saved, signed).
58
+ */
59
+ onPrescription?: (prescription: Prescription) => void;
60
+ /** Shows a Remove button (an order's line). */
61
+ onRemove?: () => void;
62
+ /** Put the cursor in Treatment (a line the prescriber just added). */
63
+ autoFocus?: boolean;
64
+ /** Put the cursor in Treatment each time this changes (e.g. a patient was just picked). */
65
+ focusSignal?: number;
66
+ }
67
+
68
+ export function StatusBadge({ rx }: { rx: PrescriptionRecord | null }) {
69
+ const { Badge } = useComponents();
70
+ if (!rx) return null;
71
+ if (rx.signing.state === "SIGNED")
72
+ return (
73
+ <Badge tone="success" icon="check">
74
+ Signed
75
+ </Badge>
76
+ );
77
+ if (rx.signing.state === "INVALIDATED_SIGN") return <Badge tone="warning">Changed since signing</Badge>;
78
+ // No tag for screening: its alerts are listed above the sig. Nor for nothing found: network-api doesn't screen
79
+ // allergies without an RxNorm id (and doesn't say which), so "No alerts" could read as a clinical check that
80
+ // didn't happen.
81
+ return null;
82
+ }
83
+
84
+ // One empty list, so a line with no templates doesn't hand PrescriptionDetailsForm new options every render.
85
+ const NO_TEMPLATES: readonly PrescriptionTemplate[] = [];
86
+ // At module level, not inline: useOpened re-runs its server call when these functions change.
87
+ const prescriptionOf = (r: PrescriptionResult) => r.prescription;
88
+ const patientOf = (r: PatientResult) => r.patient;
89
+
90
+ export function PrescriptionCard({
91
+ patient,
92
+ prescription,
93
+ locked,
94
+ onSigned,
95
+ onPrescription,
96
+ onRemove,
97
+ autoFocus,
98
+ focusSignal,
99
+ }: PrescriptionCardProps) {
100
+ const { Alert, Button, Card, CardBody, CardHeader, Spinner, Stack } = useComponents();
101
+ const ph = usePhoton();
102
+ // A new line when the host gives no prescription (and none to open).
103
+ const shared = prescription instanceof Prescription ? prescription : null;
104
+ const given = toOpen<PrescriptionInput>(prescription, Boolean(shared));
105
+ const [own] = useState(() => (shared || given ? null : ph.prescription()));
106
+ const { opened, error: rxError } = useOpened(ph.prescription, given, prescriptionOf, "prescription");
107
+ // Who it's for, opened the same way; its id once it has a record.
108
+ const sharedPatient = patient instanceof Patient ? patient : null;
109
+ const { opened: openedPatient, error: patientError } = useOpened(
110
+ ph.patient,
111
+ toOpen<PatientInput>(patient, Boolean(sharedPatient)),
112
+ patientOf,
113
+ "patient",
114
+ );
115
+ const patientSnap = useLive(sharedPatient ?? openedPatient);
116
+ // Not given: leave it (a line from an order has its own). Given: its id once it has one; null clears it.
117
+ const patientId = patient === undefined ? undefined : patient === null ? null : patientSnap?.state?.id;
118
+ const openError = rxError ?? patientError;
119
+ const live = shared ?? opened ?? own;
120
+ const snap = useLive(live);
121
+ useOnChange(live, (rx) => rx.state, onPrescription);
122
+ useSendDraftPrescriptionAdded(live);
123
+ const analytics = usePhotonAnalytics();
124
+ const signedHere = useRef(new Set<string>()); // onSigned: once per prescription
125
+ useEffect(() => {
126
+ if (live && patientId !== undefined)
127
+ void live.sync({ patient: patientId ? { id: patientId } : null }).catch(() => {});
128
+ }, [live, patientId]);
129
+
130
+ const rx = snap?.state ?? null;
131
+ const changes = classify(snap?.changes ?? { applied: [], unapplied: [] });
132
+ const [signing, setSigning] = useState(false);
133
+ // The error showing came from Sign, which signs (its title says so); the next save's error is a save's again.
134
+ const [signFailed, setSignFailed] = useState(false);
135
+ const busy = signing || snap?.status === "saving" || snap?.status === "loading";
136
+ // Signing is per prescription, and a signed one is final here: no edits (any change would void the
137
+ // signature). A different drug or sig is a new line.
138
+ const readOnly = Boolean(locked) || Boolean(live?.signed);
139
+ // Sign (the signature) is offered once the server left nothing open. Screening alerts don't stop it: signing is the
140
+ // prescriber's acknowledgement of what they show (network-api can't acknowledge an alert yet).
141
+ const canSign = Boolean(rx) && changes.settled;
142
+ // The field whose change sent the call in flight shows a spinner — nowhere else, so nothing opens and
143
+ // closes while the prescriber works (held briefly, so it doesn't flicker).
144
+ const [sentFrom, setSentFrom] = useState<string | null>(null);
145
+ const working = snap?.status === "saving" || snap?.status === "loading";
146
+ if (sentFrom && !working) setSentFrom(null); // answered: a later call (e.g. signing) isn't that field's
147
+ const spinning = useHeldActivity(sentFrom);
148
+
149
+ const save = (edits: PrescriptionEdits, from?: string) => {
150
+ if (readOnly || !live) return;
151
+ setSignFailed(false);
152
+ // Its id known: update that prescription; before, sync what's known.
153
+ void (live.id ? live.update(edits) : live.sync(edits)).catch(() => {}); // a failure lands on the object (snap.error)
154
+ if (from && live.busy) setSentFrom(from);
155
+ };
156
+ const add = () => {
157
+ if (!live) return;
158
+ const alertsShownWhenSigning = openAlerts(rx);
159
+ setSigning(true);
160
+ setSignFailed(false);
161
+ void live
162
+ .sign()
163
+ .then(() => {
164
+ if (!live.signed || !live.id || signedHere.current.has(live.id)) return;
165
+ signedHere.current.add(live.id);
166
+ void analytics.prescriptionSigned({ id: live.id, screeningAlerts: alertsShownWhenSigning });
167
+ onSigned?.(live);
168
+ })
169
+ .catch(() => setSignFailed(true)) // a refusal or failure lands on the object (snap.error)
170
+ .finally(() => setSigning(false));
171
+ };
172
+ const pick = snap?.pick ?? null;
173
+ // A drug chosen, until the server answers: the picker stays as it was picked (a spinner on the drug's row), and
174
+ // the directions and dispense wait — a moment to see what was picked before the form changes under it.
175
+ const [choosing, setChoosing] = useState<TreatmentChoice | null>(null);
176
+ const chooseTreatment = (choice: TreatmentChoice) => {
177
+ if (!live) return;
178
+ setChoosing(choice);
179
+ void live
180
+ .choose(choice.option)
181
+ .catch(() => {}) // a failure lands on the object (snap.error)
182
+ .finally(() => setChoosing(null));
183
+ };
184
+
185
+ // The title is the resolved treatment — not what the provider is typing (or an unresolved choice).
186
+ const drugChoices = changes.needsChoice.filter((c) => c.key.startsWith("treatment"));
187
+ const otherChoices = changes.needsChoice.filter((c) => !c.key.startsWith("treatment"));
188
+ const treatmentPending = drugChoices.length > 0;
189
+ // Choosing among the drugs: the picker's up (none while a product chosen saves: it heads the card at once).
190
+ const pickerUp = treatmentPending && !choosing;
191
+ const resolvedName = (rx ? !treatmentPending : pick) && !choosing ? (live?.name ?? null) : null;
192
+ // Sig / dispense can be filled once a drug is picked (and its choice has saved) — before a patient too (kept
193
+ // until the line attaches).
194
+ const fieldsOpen = Boolean(rx || pick) && !choosing;
195
+ useEffect(() => {
196
+ if (fieldsOpen && !readOnly) live?.loadTemplates();
197
+ }, [live, fieldsOpen, readOnly]);
198
+ // "Lisinopril 10 mg tablet" over "Oral tablet · 10 mg" — the picked product, said once. A product chosen heads the
199
+ // card as soon as it's picked, from the option itself, while the server drafts it.
200
+ const heading = choosing
201
+ ? treatmentHeading(choosing.option.displayName, choosing.option.treatment)
202
+ : resolvedName
203
+ ? treatmentHeading(resolvedName, live?.treatment)
204
+ : null;
205
+ // The Treatment field is for finding a drug: shown until one is picked. A different drug is a new line.
206
+ const searching = !choosing && (!fieldsOpen || pickerUp);
207
+
208
+ // A failed call: shown where the drug list drops down (under Treatment), with the card's other messages — and so
209
+ // is a prescription this line stands in for that the server doesn't have (an order link's `rx=`), until it's filled.
210
+ const missing = live?.missing && !rx ? new NotFound("prescription", live.missing) : null;
211
+ const opening = openError ?? missing;
212
+ const error = snap?.error ?? opening;
213
+ const failure = error && (
214
+ <Alert
215
+ tone="critical"
216
+ title={
217
+ !snap?.error && opening
218
+ ? openFailed(opening === patientError ? "patient" : "prescription", opening)
219
+ : signFailed
220
+ ? "Couldn't sign this prescription"
221
+ : snap?.error?.kind === "refused" && !rx
222
+ ? "Can't start a new prescription"
223
+ : "Couldn't save this prescription"
224
+ }
225
+ description={error.message}
226
+ />
227
+ );
228
+ // The body above the form shows only when something is in it. A divided card draws a line above each section, so an
229
+ // empty one (Treatment closed once a drug is picked, nothing to say) would show as a double line. Keep this in step
230
+ // with what the body renders.
231
+ const hasBody =
232
+ readOnly ||
233
+ searching ||
234
+ Boolean(error) ||
235
+ otherChoices.length + changes.blocking.length + changes.advisories.length > 0 ||
236
+ Boolean(snap?.drafts) ||
237
+ openAlerts(rx).length > 0 ||
238
+ Boolean(choosing);
239
+
240
+ return (
241
+ <Card divided label={heading?.title ?? "Prescription"}>
242
+ <CardHeader
243
+ title={heading?.title ?? "Prescription"}
244
+ // The product in parts — a brand's ingredients, its form · strength, a controlled substance's schedule —
245
+ // with no NDC / RxNorm stand-ins.
246
+ description={heading?.detail}
247
+ action={
248
+ !locked &&
249
+ onRemove && (
250
+ <Button
251
+ variant="ghost"
252
+ size="sm"
253
+ tone="critical"
254
+ icon="trash"
255
+ aria-label="Remove prescription"
256
+ title="Remove prescription"
257
+ onClick={onRemove}
258
+ />
259
+ )
260
+ }
261
+ />
262
+ {hasBody && (
263
+ <CardBody>
264
+ <Stack gap="4">
265
+ {readOnly ? (
266
+ <>
267
+ {failure}
268
+ <ReadOnlyRx rx={rx} status={<StatusBadge rx={rx} />} />
269
+ <ScreeningAlerts alerts={openAlerts(rx)} />
270
+ </>
271
+ ) : (
272
+ <>
273
+ <TreatmentSearch
274
+ live={live}
275
+ rx={rx}
276
+ pickId={pick?.id ?? null}
277
+ drugChoices={drugChoices}
278
+ choosing={choosing}
279
+ open={searching}
280
+ busy={busy}
281
+ spinning={spinning}
282
+ autoFocus={autoFocus}
283
+ focusSignal={focusSignal}
284
+ save={save}
285
+ onChoose={chooseTreatment}
286
+ />
287
+ {/* Where the drug list drops down: what failed, what the server said, what screening found. */}
288
+ {failure}
289
+ <ChangeList
290
+ changes={{ ...changes, needsChoice: otherChoices }}
291
+ onChoose={(o) => void live?.choose(o).catch(() => {})}
292
+ busy={busy}
293
+ />
294
+ {/* The patient has several open prescriptions: this line continues the one picked. */}
295
+ {snap?.drafts && (
296
+ <SearchResults
297
+ label="Continue a draft"
298
+ busy={busy}
299
+ results={snap.drafts.map((d) => ({
300
+ key: d.id,
301
+ label: d.treatment.name,
302
+ description: [d.instructions, dispenseSummary(d)].filter(Boolean).join(" · ") || undefined,
303
+ }))}
304
+ onPick={(id) => void live?.continueDraft(id).catch(() => {})}
305
+ />
306
+ )}
307
+ {/* What screening found (an allergy, an interaction, a condition) — read before the directions. */}
308
+ <ScreeningAlerts alerts={openAlerts(rx)} />
309
+ {/* A product chosen, saving: a spinner where its directions will be. */}
310
+ {choosing && <Spinner />}
311
+ </>
312
+ )}
313
+ </Stack>
314
+ </CardBody>
315
+ )}
316
+ {/* Once a drug is set: the directions first, then quantity / unit / days / refills, then DAW — and Sign, which
317
+ signs it (before a patient too: signing then says a patient is required). */}
318
+ {!readOnly && (
319
+ <PrescriptionDetailsForm
320
+ live={live}
321
+ rx={rx}
322
+ open={fieldsOpen}
323
+ focusable={!searching}
324
+ templates={snap?.templates ?? NO_TEMPLATES}
325
+ spinning={spinning}
326
+ save={save}
327
+ status={<StatusBadge rx={rx} />}
328
+ onSign={add}
329
+ signing={signing}
330
+ signDisabled={snap?.status === "loading" || (Boolean(rx) && !canSign)}
331
+ />
332
+ )}
333
+ </Card>
334
+ );
335
+ }
336
+
337
+ /** A saved prescription: its instructions — with its status tag at the end of that line — then what's dispensed. */
338
+ export function ReadOnlyRx({ rx, status }: { rx: PrescriptionRecord | null; status: ReactNode }) {
339
+ const { Row, Stack, Text } = useComponents();
340
+ // Locked before there's anything in it: say so, rather than an empty card.
341
+ if (!rx) return <Text tone="tertiary">No prescription</Text>;
342
+ const dispense = dispenseSummary(rx);
343
+ const first = rx.instructions ? (
344
+ <span>{rx.instructions}</span>
345
+ ) : dispense ? (
346
+ <Text tone="secondary">{dispense}</Text>
347
+ ) : null;
348
+ return (
349
+ <Stack gap="1">
350
+ <Row gap="2" justify="between">
351
+ {first ?? <span />}
352
+ {status}
353
+ </Row>
354
+ {rx.instructions && dispense && <Text tone="secondary">{dispense}</Text>}
355
+ </Stack>
356
+ );
357
+ }
358
+
359
+ /** What screening found, until it's signed: the signature acknowledges them. */
360
+ const openAlerts = (rx: PrescriptionRecord | null) =>
361
+ rx?.signing.state === "SIGNED" ? [] : (rx?.screeningAlerts ?? []);
@@ -0,0 +1,82 @@
1
+ import { act, fireEvent, render, screen } from "@testing-library/react";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { DirectionsInput, type DirectionsInputProps } from "./DirectionsInput.tsx";
4
+
5
+ const directions = "Take 1 capsule by mouth 3 times per day";
6
+
7
+ const renderInput = (props: Partial<DirectionsInputProps> = {}) => {
8
+ const all: DirectionsInputProps = {
9
+ value: directions,
10
+ edited: false,
11
+ focusable: false,
12
+ onEdit: vi.fn(),
13
+ onCommit: vi.fn(),
14
+ ...props,
15
+ };
16
+ render(<DirectionsInput {...all} />);
17
+ return { ...all, input: screen.getByLabelText<HTMLTextAreaElement>("Directions (sig)") };
18
+ };
19
+
20
+ describe("DirectionsInput", () => {
21
+ it("shows the directions", () => {
22
+ const { input } = renderInput();
23
+ expect(input.value).toBe(directions);
24
+ });
25
+
26
+ describe("when I leave the field", () => {
27
+ it("hands off edited directions to be saved", () => {
28
+ const { input, onCommit } = renderInput({ edited: true });
29
+ fireEvent.blur(input);
30
+ expect(onCommit).toHaveBeenCalledOnce();
31
+ });
32
+
33
+ it("doesn't save directions I didn't change", () => {
34
+ const { input, onCommit } = renderInput();
35
+ fireEvent.blur(input);
36
+ expect(onCommit).not.toHaveBeenCalled();
37
+ });
38
+ });
39
+
40
+ describe("when I focus the field", () => {
41
+ it("selects an unedited value, so typing replaces it", () => {
42
+ const { input } = renderInput();
43
+ act(() => input.focus());
44
+ expect([input.selectionStart, input.selectionEnd]).toEqual([0, directions.length]);
45
+ });
46
+
47
+ it("doesn't select an edited value", () => {
48
+ const { input } = renderInput({ edited: true });
49
+ act(() => input.focus());
50
+ expect(input.selectionStart).toBe(input.selectionEnd);
51
+ });
52
+ });
53
+
54
+ describe("error message", () => {
55
+ const noteOf = (input: HTMLElement) => {
56
+ const note = input.getAttribute("aria-describedby");
57
+ return note ? document.getElementById(note)?.textContent : null;
58
+ };
59
+
60
+ it("shows the error message it's given under the field, and marks the field invalid", () => {
61
+ const { input } = renderInput({ errorMessage: "Required" });
62
+ expect(input.getAttribute("aria-invalid")).toBe("true");
63
+ expect(noteOf(input)).toBe("Required");
64
+ });
65
+
66
+ it("shows no errors when none are specified", () => {
67
+ const { input } = renderInput();
68
+ expect(input.getAttribute("aria-invalid")).toBeNull();
69
+ expect(noteOf(input) ?? null).toBeNull();
70
+ });
71
+ });
72
+
73
+ it("is where the card flow lands when focusable", () => {
74
+ const { input } = renderInput({ focusable: true });
75
+ expect(input.closest("[data-prx-focus]")).not.toBeNull();
76
+ });
77
+
78
+ it("isn't where the card flow lands when not focusable", () => {
79
+ const { input } = renderInput();
80
+ expect(input.closest("[data-prx-focus]")).toBeNull();
81
+ });
82
+ });
@@ -0,0 +1,41 @@
1
+ import { useComponents } from "../../../primitives/index.ts";
2
+ import { focusProps } from "../../flow.ts";
3
+ import { useFieldInteractions } from "../../../analytics/fieldInteraction.tsx";
4
+
5
+ /** A prescription's directions (sig). Its value is kept by PrescriptionDetailsForm, which decides what's saved. */
6
+ export interface DirectionsInputProps {
7
+ value: string;
8
+ /** What's wrong with it, shown under it. */
9
+ errorMessage?: string;
10
+ /** Typed since it was last committed. */
11
+ edited: boolean;
12
+ /** Where the card flow (← / →) lands, when Treatment isn't. */
13
+ focusable: boolean;
14
+ onEdit: (value: string) => void;
15
+ /** Leaving the field after editing it. */
16
+ onCommit: () => void;
17
+ }
18
+
19
+ export function DirectionsInput({ value, errorMessage, edited, focusable, onEdit, onCommit }: DirectionsInputProps) {
20
+ const { Textarea } = useComponents();
21
+ const directions = useFieldInteractions("add_prescription_form")("instructions", { isOptional: false });
22
+ return (
23
+ <div {...(focusable ? focusProps : {})} {...directions}>
24
+ <Textarea
25
+ label="Directions (sig)"
26
+ rows={2}
27
+ value={value}
28
+ invalid={Boolean(errorMessage)}
29
+ errorMessage={errorMessage}
30
+ // A value the server filled in (e.g. "Take as directed") is selected, so typing replaces it.
31
+ onFocus={(e) => {
32
+ if (!edited) e.currentTarget.select();
33
+ }}
34
+ onChange={(e) => onEdit(e.target.value)}
35
+ onBlur={() => {
36
+ if (edited) onCommit();
37
+ }}
38
+ />
39
+ </div>
40
+ );
41
+ }