@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,78 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { Change, Changes } from "@photonhealth/client";
3
+ import { draftedNewPrescription, patientWriteOutcome } from "./lifecycleOutcomes.ts";
4
+
5
+ describe("patientWriteOutcome: which lifecycle event a patient write is", () => {
6
+ it("a patient without a record whose first name was added is created", () => {
7
+ const changes = changesWith({ applied: [applied("demographic.name.first", "ADDED")] });
8
+ expect(patientWriteOutcome({ hadRecord: false, changes })).toBe("created");
9
+ });
10
+
11
+ it("a patient that already had a record is updated, even when a first name is added", () => {
12
+ const changes = changesWith({ applied: [applied("demographic.name.first", "ADDED")] });
13
+ expect(patientWriteOutcome({ hadRecord: true, changes })).toBe("updated");
14
+ });
15
+
16
+ it("a patient found rather than added, with a field filled in, is updated", () => {
17
+ const changes = changesWith({ applied: [applied("contact.phone", "ADDED")] });
18
+ expect(patientWriteOutcome({ hadRecord: false, changes })).toBe("updated");
19
+ });
20
+
21
+ it("a first name added alongside a rejected field is updated, not created", () => {
22
+ const changes = changesWith({
23
+ applied: [applied("demographic.name.first", "ADDED")],
24
+ unapplied: [{ ...applied("contact.phone", "ADDED"), status: "REJECTED" }],
25
+ });
26
+ expect(patientWriteOutcome({ hadRecord: false, changes })).toBe("updated");
27
+ });
28
+
29
+ it("a write the server applied nothing from is unchanged", () => {
30
+ expect(patientWriteOutcome({ hadRecord: true, changes: changesWith({}) })).toBe("unchanged");
31
+ });
32
+ });
33
+
34
+ describe("draftedNewPrescription: whether a prescription's record is a new draft", () => {
35
+ it("is true when its treatment was added", () => {
36
+ expect(draftedNewPrescription(changesWith({ applied: [applied("treatment", "ADDED")] }))).toBe(true);
37
+ });
38
+
39
+ it("is false for a draft that was continued: its treatment was already there", () => {
40
+ expect(draftedNewPrescription(changesWith({ applied: [applied("instructions", "MODIFIED")] }))).toBe(false);
41
+ });
42
+
43
+ it("is false when the server rejected something in the same write", () => {
44
+ const changes = changesWith({
45
+ applied: [applied("treatment", "ADDED")],
46
+ unapplied: [{ ...applied("dispense.quantity", "ADDED"), status: "REJECTED" }],
47
+ });
48
+ expect(draftedNewPrescription(changes)).toBe(false);
49
+ });
50
+
51
+ it("is true alongside an advisory the server applied anyway", () => {
52
+ const changes = changesWith({
53
+ applied: [applied("treatment", "ADDED")],
54
+ unapplied: [{ ...applied("clinical.screeningAlerts", "ADDED"), severity: "ADVISORY" }],
55
+ });
56
+ expect(draftedNewPrescription(changes)).toBe(true);
57
+ });
58
+ });
59
+
60
+ function changesWith({ applied = [], unapplied = [] }: Partial<Changes>): Changes {
61
+ return { applied, unapplied };
62
+ }
63
+
64
+ function applied(key: string, operation: Change["operation"]): Change {
65
+ return {
66
+ key,
67
+ label: key,
68
+ status: "APPLIED",
69
+ severity: "INFO",
70
+ reason: null,
71
+ category: null,
72
+ operation,
73
+ options: [],
74
+ after: null,
75
+ inputText: null,
76
+ resolvedId: null,
77
+ };
78
+ }
@@ -0,0 +1,29 @@
1
+ import type { Changes } from "@photonhealth/client";
2
+
3
+ export type PatientWriteOutcome = "created" | "updated" | "unchanged";
4
+
5
+ export function patientWriteOutcome({
6
+ hadRecord,
7
+ changes,
8
+ }: {
9
+ hadRecord: boolean;
10
+ changes: Changes;
11
+ }): PatientWriteOutcome {
12
+ if (!changes.applied.length) return "unchanged";
13
+ return !hadRecord && addedWithNothingRejected(changes, FIELD_ONLY_A_NEW_PATIENT_ADDS) ? "created" : "updated";
14
+ }
15
+
16
+ export function draftedNewPrescription(changes: Changes): boolean {
17
+ return addedWithNothingRejected(changes, FIELD_ONLY_A_NEW_PRESCRIPTION_ADDS);
18
+ }
19
+
20
+ const FIELD_ONLY_A_NEW_PATIENT_ADDS = "demographic.name.first";
21
+ const FIELD_ONLY_A_NEW_PRESCRIPTION_ADDS = "treatment";
22
+
23
+ function addedWithNothingRejected(changes: Changes, key: string): boolean {
24
+ const nothingRejected = changes.unapplied.every((change) => change.status === "APPLIED");
25
+ const added = changes.applied.some(
26
+ (change) => change.key === key && change.status === "APPLIED" && change.operation === "ADDED",
27
+ );
28
+ return nothingRejected && added;
29
+ }
@@ -0,0 +1,52 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { environmentUrls, type Photon } from "@photonhealth/client";
3
+ import { fakeNetworkApiClient, photonOver } from "@photonhealth/client/testing";
4
+ import { MAX_QUEUED_EVENTS, Tracker } from "./tracker.ts";
5
+
6
+ // Sending — the token, providerId, analytics-api — is @photonhealth/client's (`ph.analytics.track`, its
7
+ // src/analytics/track.test.ts). The Tracker only holds events until the sign-in is accepted.
8
+
9
+ const photonWithTrackSpy = () => {
10
+ const ph = photonOver(fakeNetworkApiClient({}).client, environmentUrls("boson"), {
11
+ analytics: { url: "https://analytics-api.boson.health", token: async () => "tok", clientName: null },
12
+ });
13
+ const track = vi.spyOn(ph.analytics!, "track").mockResolvedValue();
14
+ return { ph, track };
15
+ };
16
+
17
+ const event = (n: number) => `Event ${String.fromCharCode(65 + (n % 26))}`;
18
+
19
+ describe("Tracker", () => {
20
+ it("holds events until the sign-in is accepted, then hands them to ph.analytics.track in order", async () => {
21
+ const { ph, track } = photonWithTrackSpy();
22
+ const tracker = new Tracker(ph);
23
+ tracker.track({ event: "Event A", properties: { n: 1 } });
24
+ tracker.track({ event: "Event B", properties: {} });
25
+ await new Promise((r) => setTimeout(r, 10));
26
+ expect(track).not.toHaveBeenCalled();
27
+ tracker.setReady(true);
28
+ expect(track.mock.calls).toEqual([
29
+ ["Event A", { n: 1 }],
30
+ ["Event B", {}],
31
+ ]);
32
+ tracker.track({ event: "Event C", properties: {} });
33
+ expect(track).toHaveBeenLastCalledWith("Event C", {});
34
+ });
35
+
36
+ it(`keeps at most ${MAX_QUEUED_EVENTS} while waiting`, () => {
37
+ const { ph, track } = photonWithTrackSpy();
38
+ const tracker = new Tracker(ph);
39
+ for (let i = 0; i < MAX_QUEUED_EVENTS + 10; i++) tracker.track({ event: event(i), properties: { i } });
40
+ tracker.setReady(true);
41
+ expect(track).toHaveBeenCalledTimes(MAX_QUEUED_EVENTS);
42
+ expect(track.mock.calls[0]).toEqual([event(0), { i: 0 }]);
43
+ });
44
+
45
+ it("holds nothing for an instance with nowhere to send", () => {
46
+ const ph: Photon = photonOver(fakeNetworkApiClient({}).client);
47
+ expect(ph.analytics).toBeNull();
48
+ const tracker = new Tracker(ph);
49
+ tracker.track({ event: "Event A", properties: {} });
50
+ expect(() => tracker.setReady(true)).not.toThrow();
51
+ });
52
+ });
@@ -0,0 +1,49 @@
1
+ import type { Analytics, AnalyticsProperties, Photon } from "@photonhealth/client";
2
+
3
+ export type { AnalyticsProperties };
4
+
5
+ export interface QueuedEvent {
6
+ event: string;
7
+ properties: AnalyticsProperties;
8
+ }
9
+
10
+ export type AnalyticsSend = (analytics: Analytics) => Promise<void>;
11
+
12
+ export const MAX_QUEUED_EVENTS = 50;
13
+
14
+ /** Holds the cards' events until the prescriber's sign-in is accepted, then sends them through `ph.analytics`. */
15
+ export class Tracker {
16
+ #ready = false;
17
+ #queue: AnalyticsSend[] = [];
18
+
19
+ constructor(private readonly ph: Photon) {}
20
+
21
+ setReady(ready: boolean) {
22
+ this.#ready = ready;
23
+ if (ready) this.#flush();
24
+ }
25
+
26
+ track({ event, properties }: QueuedEvent) {
27
+ this.send((analytics) => analytics.track(event, properties));
28
+ }
29
+
30
+ send(sendThroughAnalytics: AnalyticsSend) {
31
+ if (!this.ph.analytics) return;
32
+ if (this.#queue.length >= MAX_QUEUED_EVENTS) return;
33
+ this.#queue.push(sendThroughAnalytics);
34
+ if (this.#ready) this.#flush();
35
+ }
36
+
37
+ #flush() {
38
+ const analytics = this.ph.analytics;
39
+ for (const sendThroughAnalytics of this.#queue.splice(0)) if (analytics) void sendThroughAnalytics(analytics);
40
+ }
41
+ }
42
+
43
+ const trackersByPhoton = new WeakMap<Photon, Tracker>();
44
+
45
+ export function trackerFor(ph: Photon): Tracker {
46
+ let tracker = trackersByPhoton.get(ph);
47
+ if (!tracker) trackersByPhoton.set(ph, (tracker = new Tracker(ph)));
48
+ return tracker;
49
+ }
@@ -0,0 +1,106 @@
1
+ import { render } from "@testing-library/react";
2
+ import { useEffect, type ReactNode } from "react";
3
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
4
+ import type { LifecycleAnalytics } from "@photonhealth/client";
5
+ import { fakeNetworkApiClient } from "@photonhealth/client/testing";
6
+ import { PhotonAnalyticsProvider, PhotonProvider, usePhotonAnalytics } from "../index.ts";
7
+ import { reactPackage } from "../meta/version.ts";
8
+ import { AnalyticsWorkflow } from "./PhotonAnalyticsProvider.tsx";
9
+ import {
10
+ meAsPrescriber,
11
+ photonWithAnalytics,
12
+ recordAnalytics,
13
+ type RecordedAnalytics,
14
+ unmountAndRestoreFetch,
15
+ } from "../../test/analytics.fixtures.tsx";
16
+
17
+ let analytics: RecordedAnalytics;
18
+ beforeEach(() => {
19
+ analytics = recordAnalytics();
20
+ });
21
+ afterEach(unmountAndRestoreFetch);
22
+
23
+ const signedInClient = () => fakeNetworkApiClient({ Ping: () => ({ ping: "pong" }), ...meAsPrescriber }).client;
24
+
25
+ function SendsOnMount({ send }: { send: (lifecycle: LifecycleAnalytics) => Promise<void> }) {
26
+ const lifecycle = usePhotonAnalytics();
27
+ useEffect(() => {
28
+ void send(lifecycle);
29
+ }, [lifecycle, send]);
30
+ return null;
31
+ }
32
+
33
+ const deleteDraft = (lifecycle: LifecycleAnalytics) => lifecycle.draftPrescriptionDeleted("rx_1");
34
+
35
+ const inProvider = (children: ReactNode, { disableAnalytics }: { disableAnalytics?: boolean } = {}) => (
36
+ <PhotonProvider client={photonWithAnalytics(signedInClient())} disableAnalytics={disableAnalytics}>
37
+ {children}
38
+ </PhotonProvider>
39
+ );
40
+
41
+ const sent = () =>
42
+ analytics.bodies().map((body) => JSON.parse(body) as { event: string; properties: Record<string, unknown> });
43
+ const sentEvent = (name: string) => sent().find(({ event }) => event === name)!.properties;
44
+
45
+ describe("usePhotonAnalytics: ph.analytics's lifecycle events, sent as the cards send theirs", () => {
46
+ it("sends the event once the sign-in is accepted, with the record's properties and React's", async () => {
47
+ render(inProvider(<SendsOnMount send={deleteDraft} />));
48
+ await analytics.expectEventLog(["Draft Prescription Deleted"]);
49
+ expect(sentEvent("Draft Prescription Deleted")).toMatchObject({
50
+ prescriptionId: "rx_1",
51
+ category: "ctaClicked",
52
+ integration: "react",
53
+ packageName: reactPackage.name,
54
+ packageVersion: reactPackage.version,
55
+ providerId: "usr_prescriber_1",
56
+ timestamp: expect.any(String),
57
+ });
58
+ });
59
+
60
+ it("inside a card's workflow, carries that workflow's sessionId, the same as its Prescribe Workflow Viewed", async () => {
61
+ render(
62
+ inProvider(
63
+ <AnalyticsWorkflow card="PhotonOrder" viewedProperties={{}}>
64
+ <SendsOnMount send={deleteDraft} />
65
+ </AnalyticsWorkflow>,
66
+ ),
67
+ );
68
+ await analytics.expectEventLog(["Draft Prescription Deleted", "Prescribe Workflow Viewed"]);
69
+ const workflowSessionId = sentEvent("Prescribe Workflow Viewed").sessionId;
70
+ expect(workflowSessionId).toEqual(expect.any(String));
71
+ expect(sentEvent("Draft Prescription Deleted").sessionId).toBe(workflowSessionId);
72
+ });
73
+
74
+ it("outside a card, sends with no sessionId", async () => {
75
+ render(inProvider(<SendsOnMount send={deleteDraft} />));
76
+ await analytics.expectEventLog(["Draft Prescription Deleted"]);
77
+ expect(sentEvent("Draft Prescription Deleted").sessionId).toBeNull();
78
+ });
79
+
80
+ it("PhotonAnalyticsProvider's properties go on lifecycle events too, and win", async () => {
81
+ render(
82
+ inProvider(
83
+ <PhotonAnalyticsProvider properties={{ integration: "web-app", testRun: "react-tests" }}>
84
+ <SendsOnMount send={(lifecycle) => lifecycle.screeningAlertCanceled([], "Cancel")} />
85
+ </PhotonAnalyticsProvider>,
86
+ ),
87
+ );
88
+ await analytics.expectEventLog(["Screening Alert Canceled"]);
89
+ expect(sentEvent("Screening Alert Canceled")).toMatchObject({
90
+ integration: "web-app",
91
+ testRun: "react-tests",
92
+ screeningAlertCount: 0,
93
+ buttonText: "Cancel",
94
+ });
95
+ });
96
+
97
+ it("disableAnalytics sends nothing", async () => {
98
+ render(inProvider(<SendsOnMount send={deleteDraft} />, { disableAnalytics: true }));
99
+ await analytics.expectEventLog([]);
100
+ });
101
+
102
+ it("outside a PhotonProvider, it's there to call and sends nothing", async () => {
103
+ render(<SendsOnMount send={deleteDraft} />);
104
+ await analytics.expectEventLog([]);
105
+ });
106
+ });
@@ -0,0 +1,27 @@
1
+ import type { AnalyticsProperties } from "./tracker.ts";
2
+
3
+ interface CardProps {
4
+ order?: unknown;
5
+ patient?: unknown;
6
+ prescription?: unknown;
7
+ locked?: boolean;
8
+ fixed?: boolean;
9
+ }
10
+
11
+ export function viewedPropertiesFromCardProps(props: CardProps): AnalyticsProperties {
12
+ const order = asObject(props.order);
13
+ const linesToAdd = asObject(order?.prescriptions)?.add;
14
+ const lines = Array.isArray(linesToAdd) ? linesToAdd.map(asObject) : [];
15
+ return {
16
+ hasOrderPrefill: props.order != null,
17
+ hasPatientPrefill: props.patient != null || order?.patient != null,
18
+ hasPrescriptionPrefill: props.prescription != null,
19
+ numTemplateIds: lines.filter((line) => typeof line?.templateId === "string").length,
20
+ numPrescriptionIds: lines.filter((line) => typeof line?.id === "string").length,
21
+ locked: Boolean(props.locked),
22
+ fixed: Boolean(props.fixed),
23
+ };
24
+ }
25
+
26
+ const asObject = (value: unknown): Record<string, unknown> | null =>
27
+ value && typeof value === "object" ? (value as Record<string, unknown>) : null;