@photonhealth/react 0.0.0-stage → 2.0.0-rc.2

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 (277) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +111 -2
  3. package/dist/adapters/index.d.ts +2 -0
  4. package/dist/adapters/index.d.ts.map +1 -0
  5. package/dist/adapters/index.js +2 -0
  6. package/dist/adapters/index.js.map +1 -0
  7. package/dist/adapters/tailwind.d.ts +3 -0
  8. package/dist/adapters/tailwind.d.ts.map +1 -0
  9. package/dist/adapters/tailwind.js +126 -0
  10. package/dist/adapters/tailwind.js.map +1 -0
  11. package/dist/analytics/PhotonAnalyticsProvider.d.ts +16 -0
  12. package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
  13. package/dist/analytics/PhotonAnalyticsProvider.js +57 -0
  14. package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
  15. package/dist/analytics/fieldInteraction.d.ts +18 -0
  16. package/dist/analytics/fieldInteraction.d.ts.map +1 -0
  17. package/dist/analytics/fieldInteraction.js +102 -0
  18. package/dist/analytics/fieldInteraction.js.map +1 -0
  19. package/dist/analytics/tracker.d.ts +17 -0
  20. package/dist/analytics/tracker.d.ts.map +1 -0
  21. package/dist/analytics/tracker.js +36 -0
  22. package/dist/analytics/tracker.js.map +1 -0
  23. package/dist/analytics/viewed.d.ts +11 -0
  24. package/dist/analytics/viewed.d.ts.map +1 -0
  25. package/dist/analytics/viewed.js +16 -0
  26. package/dist/analytics/viewed.js.map +1 -0
  27. package/dist/components/ChangeList.d.ts +21 -0
  28. package/dist/components/ChangeList.d.ts.map +1 -0
  29. package/dist/components/ChangeList.js +40 -0
  30. package/dist/components/ChangeList.js.map +1 -0
  31. package/dist/components/PhotonOrder.d.ts +46 -0
  32. package/dist/components/PhotonOrder.d.ts.map +1 -0
  33. package/dist/components/PhotonOrder.js +274 -0
  34. package/dist/components/PhotonOrder.js.map +1 -0
  35. package/dist/components/PhotonPatient.d.ts +48 -0
  36. package/dist/components/PhotonPatient.d.ts.map +1 -0
  37. package/dist/components/PhotonPatient.js +85 -0
  38. package/dist/components/PhotonPatient.js.map +1 -0
  39. package/dist/components/PhotonPrescription.d.ts +32 -0
  40. package/dist/components/PhotonPrescription.d.ts.map +1 -0
  41. package/dist/components/PhotonPrescription.js +11 -0
  42. package/dist/components/PhotonPrescription.js.map +1 -0
  43. package/dist/components/ScreeningAlerts.d.ts +6 -0
  44. package/dist/components/ScreeningAlerts.d.ts.map +1 -0
  45. package/dist/components/ScreeningAlerts.js +11 -0
  46. package/dist/components/ScreeningAlerts.js.map +1 -0
  47. package/dist/components/SignedIn.d.ts +12 -0
  48. package/dist/components/SignedIn.d.ts.map +1 -0
  49. package/dist/components/SignedIn.js +84 -0
  50. package/dist/components/SignedIn.js.map +1 -0
  51. package/dist/components/activity.d.ts +7 -0
  52. package/dist/components/activity.d.ts.map +1 -0
  53. package/dist/components/activity.js +24 -0
  54. package/dist/components/activity.js.map +1 -0
  55. package/dist/components/display.d.ts +90 -0
  56. package/dist/components/display.d.ts.map +1 -0
  57. package/dist/components/display.js +223 -0
  58. package/dist/components/display.js.map +1 -0
  59. package/dist/components/flow.d.ts +35 -0
  60. package/dist/components/flow.d.ts.map +1 -0
  61. package/dist/components/flow.js +53 -0
  62. package/dist/components/flow.js.map +1 -0
  63. package/dist/components/format.d.ts +26 -0
  64. package/dist/components/format.d.ts.map +1 -0
  65. package/dist/components/format.js +61 -0
  66. package/dist/components/format.js.map +1 -0
  67. package/dist/components/index.d.ts +6 -0
  68. package/dist/components/index.d.ts.map +1 -0
  69. package/dist/components/index.js +6 -0
  70. package/dist/components/index.js.map +1 -0
  71. package/dist/components/open.d.ts +47 -0
  72. package/dist/components/open.d.ts.map +1 -0
  73. package/dist/components/open.js +73 -0
  74. package/dist/components/open.js.map +1 -0
  75. package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
  76. package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
  77. package/dist/components/order/DraftOrderPrompt.js +26 -0
  78. package/dist/components/order/DraftOrderPrompt.js.map +1 -0
  79. package/dist/components/patient/Benefits.d.ts +25 -0
  80. package/dist/components/patient/Benefits.d.ts.map +1 -0
  81. package/dist/components/patient/Benefits.js +48 -0
  82. package/dist/components/patient/Benefits.js.map +1 -0
  83. package/dist/components/patient/Clinical.d.ts +56 -0
  84. package/dist/components/patient/Clinical.d.ts.map +1 -0
  85. package/dist/components/patient/Clinical.js +118 -0
  86. package/dist/components/patient/Clinical.js.map +1 -0
  87. package/dist/components/patient/IntakeForm.d.ts +10 -0
  88. package/dist/components/patient/IntakeForm.d.ts.map +1 -0
  89. package/dist/components/patient/IntakeForm.js +333 -0
  90. package/dist/components/patient/IntakeForm.js.map +1 -0
  91. package/dist/components/patient/Summary.d.ts +48 -0
  92. package/dist/components/patient/Summary.d.ts.map +1 -0
  93. package/dist/components/patient/Summary.js +90 -0
  94. package/dist/components/patient/Summary.js.map +1 -0
  95. package/dist/components/patient/fields.d.ts +42 -0
  96. package/dist/components/patient/fields.d.ts.map +1 -0
  97. package/dist/components/patient/fields.js +72 -0
  98. package/dist/components/patient/fields.js.map +1 -0
  99. package/dist/components/patient/query.d.ts +37 -0
  100. package/dist/components/patient/query.d.ts.map +1 -0
  101. package/dist/components/patient/query.js +70 -0
  102. package/dist/components/patient/query.js.map +1 -0
  103. package/dist/components/prescription/PrescriptionCard.d.ts +45 -0
  104. package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
  105. package/dist/components/prescription/PrescriptionCard.js +179 -0
  106. package/dist/components/prescription/PrescriptionCard.js.map +1 -0
  107. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
  108. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
  109. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
  110. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
  111. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
  112. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
  113. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
  114. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
  115. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
  116. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
  117. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
  118. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
  119. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
  120. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
  121. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
  122. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
  123. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
  124. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
  125. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
  126. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
  127. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
  128. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
  129. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js +287 -0
  130. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
  131. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
  132. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
  133. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
  134. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
  135. package/dist/components/schemaErrors.d.ts +12 -0
  136. package/dist/components/schemaErrors.d.ts.map +1 -0
  137. package/dist/components/schemaErrors.js +24 -0
  138. package/dist/components/schemaErrors.js.map +1 -0
  139. package/dist/components/search.d.ts +154 -0
  140. package/dist/components/search.d.ts.map +1 -0
  141. package/dist/components/search.js +295 -0
  142. package/dist/components/search.js.map +1 -0
  143. package/dist/hooks/index.d.ts +5 -0
  144. package/dist/hooks/index.d.ts.map +1 -0
  145. package/dist/hooks/index.js +5 -0
  146. package/dist/hooks/index.js.map +1 -0
  147. package/dist/hooks/useLatestCallback.d.ts +7 -0
  148. package/dist/hooks/useLatestCallback.d.ts.map +1 -0
  149. package/dist/hooks/useLatestCallback.js +14 -0
  150. package/dist/hooks/useLatestCallback.js.map +1 -0
  151. package/dist/hooks/useLive.d.ts +15 -0
  152. package/dist/hooks/useLive.d.ts.map +1 -0
  153. package/dist/hooks/useLive.js +6 -0
  154. package/dist/hooks/useLive.js.map +1 -0
  155. package/dist/hooks/useOnChange.d.ts +11 -0
  156. package/dist/hooks/useOnChange.d.ts.map +1 -0
  157. package/dist/hooks/useOnChange.js +28 -0
  158. package/dist/hooks/useOnChange.js.map +1 -0
  159. package/dist/hooks/useSession.d.ts +7 -0
  160. package/dist/hooks/useSession.d.ts.map +1 -0
  161. package/dist/hooks/useSession.js +10 -0
  162. package/dist/hooks/useSession.js.map +1 -0
  163. package/dist/index.d.ts +22 -0
  164. package/dist/index.d.ts.map +1 -0
  165. package/dist/index.js +20 -0
  166. package/dist/index.js.map +1 -0
  167. package/dist/meta/version.d.ts +5 -0
  168. package/dist/meta/version.d.ts.map +1 -0
  169. package/dist/meta/version.js +5 -0
  170. package/dist/meta/version.js.map +1 -0
  171. package/dist/primitives/PhotonProvider.d.ts +60 -0
  172. package/dist/primitives/PhotonProvider.d.ts.map +1 -0
  173. package/dist/primitives/PhotonProvider.js +103 -0
  174. package/dist/primitives/PhotonProvider.js.map +1 -0
  175. package/dist/primitives/defaults.d.ts +3 -0
  176. package/dist/primitives/defaults.d.ts.map +1 -0
  177. package/dist/primitives/defaults.js +70 -0
  178. package/dist/primitives/defaults.js.map +1 -0
  179. package/dist/primitives/icons.d.ts +6 -0
  180. package/dist/primitives/icons.d.ts.map +1 -0
  181. package/dist/primitives/icons.js +6 -0
  182. package/dist/primitives/icons.js.map +1 -0
  183. package/dist/primitives/index.d.ts +5 -0
  184. package/dist/primitives/index.d.ts.map +1 -0
  185. package/dist/primitives/index.js +5 -0
  186. package/dist/primitives/index.js.map +1 -0
  187. package/dist/primitives/photonContext.d.ts +3 -0
  188. package/dist/primitives/photonContext.d.ts.map +1 -0
  189. package/dist/primitives/photonContext.js +3 -0
  190. package/dist/primitives/photonContext.js.map +1 -0
  191. package/dist/primitives/select.d.ts +7 -0
  192. package/dist/primitives/select.d.ts.map +1 -0
  193. package/dist/primitives/select.js +16 -0
  194. package/dist/primitives/select.js.map +1 -0
  195. package/dist/primitives/types.d.ts +288 -0
  196. package/dist/primitives/types.d.ts.map +1 -0
  197. package/dist/primitives/types.js +3 -0
  198. package/dist/primitives/types.js.map +1 -0
  199. package/dist/primitives/useProvidedPhoton.d.ts +8 -0
  200. package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
  201. package/dist/primitives/useProvidedPhoton.js +8 -0
  202. package/dist/primitives/useProvidedPhoton.js.map +1 -0
  203. package/dist/styles.css +653 -0
  204. package/dist/test-utils/contract.d.ts +17 -0
  205. package/dist/test-utils/contract.d.ts.map +1 -0
  206. package/dist/test-utils/contract.js +282 -0
  207. package/dist/test-utils/contract.js.map +1 -0
  208. package/package.json +76 -4
  209. package/src/adapters/index.ts +1 -0
  210. package/src/adapters/tailwind.tsx +572 -0
  211. package/src/analytics/PhotonAnalyticsProvider.test.tsx +191 -0
  212. package/src/analytics/PhotonAnalyticsProvider.tsx +96 -0
  213. package/src/analytics/fieldInteraction.test.tsx +184 -0
  214. package/src/analytics/fieldInteraction.tsx +148 -0
  215. package/src/analytics/tracker.test.ts +52 -0
  216. package/src/analytics/tracker.ts +42 -0
  217. package/src/analytics/viewed.ts +27 -0
  218. package/src/components/ChangeList.tsx +85 -0
  219. package/src/components/PhotonOrder.test.tsx +256 -0
  220. package/src/components/PhotonOrder.tsx +508 -0
  221. package/src/components/PhotonPatient.tsx +188 -0
  222. package/src/components/PhotonPrescription.test.tsx +116 -0
  223. package/src/components/PhotonPrescription.tsx +45 -0
  224. package/src/components/ScreeningAlerts.tsx +28 -0
  225. package/src/components/SignedIn.tsx +144 -0
  226. package/src/components/activity.ts +23 -0
  227. package/src/components/display.ts +278 -0
  228. package/src/components/flow.ts +67 -0
  229. package/src/components/format.ts +73 -0
  230. package/src/components/index.ts +5 -0
  231. package/src/components/open.ts +100 -0
  232. package/src/components/order/DraftOrderPrompt.test.tsx +80 -0
  233. package/src/components/order/DraftOrderPrompt.tsx +81 -0
  234. package/src/components/patient/Benefits.tsx +133 -0
  235. package/src/components/patient/Clinical.tsx +215 -0
  236. package/src/components/patient/IntakeForm.test.tsx +89 -0
  237. package/src/components/patient/IntakeForm.tsx +564 -0
  238. package/src/components/patient/Summary.test.tsx +106 -0
  239. package/src/components/patient/Summary.tsx +330 -0
  240. package/src/components/patient/fields.tsx +158 -0
  241. package/src/components/patient/query.ts +83 -0
  242. package/src/components/prescription/PrescriptionCard.test.tsx +85 -0
  243. package/src/components/prescription/PrescriptionCard.tsx +347 -0
  244. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +85 -0
  245. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
  246. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +209 -0
  247. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
  248. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +456 -0
  249. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
  250. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
  251. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
  252. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +75 -0
  253. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
  254. package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
  255. package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +699 -0
  256. package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
  257. package/src/components/schemaErrors.test.ts +49 -0
  258. package/src/components/schemaErrors.ts +33 -0
  259. package/src/components/search.tsx +377 -0
  260. package/src/hooks/index.ts +4 -0
  261. package/src/hooks/useLatestCallback.ts +16 -0
  262. package/src/hooks/useLive.ts +21 -0
  263. package/src/hooks/useOnChange.ts +34 -0
  264. package/src/hooks/useSession.ts +11 -0
  265. package/src/index.ts +21 -0
  266. package/src/meta/version.test.ts +10 -0
  267. package/src/meta/version.ts +4 -0
  268. package/src/primitives/PhotonProvider.tsx +168 -0
  269. package/src/primitives/defaults.tsx +432 -0
  270. package/src/primitives/icons.tsx +37 -0
  271. package/src/primitives/index.ts +4 -0
  272. package/src/primitives/photonContext.ts +4 -0
  273. package/src/primitives/select.ts +15 -0
  274. package/src/primitives/types.ts +288 -0
  275. package/src/primitives/useProvidedPhoton.ts +21 -0
  276. package/src/styles.css +653 -0
  277. package/src/test-utils/contract.tsx +453 -0
@@ -0,0 +1,81 @@
1
+ import type { PrescriptionRecord } from "@photonhealth/client";
2
+ import { useState } from "react";
3
+ import { useComponents } from "../../primitives/index.ts";
4
+ import { treatmentHeading } from "../display.ts";
5
+ import { cardProps, focusProps } from "../flow.ts";
6
+ import { ReadOnlyRx, StatusBadge } from "../prescription/PrescriptionCard.tsx";
7
+
8
+ export interface DraftOrderPromptProps {
9
+ /** The patient's draft order: what's already on it, shown read-only until the prescriber decides. */
10
+ draft: { prescriptions: readonly PrescriptionRecord[] };
11
+ /** Go on with this draft: its prescriptions become the order's, and what's added goes on it. */
12
+ onContinue: () => void | Promise<unknown>;
13
+ /** Write a new order instead: the draft's prescriptions come off it, and the order starts over. */
14
+ onStartNew: () => void | Promise<unknown>;
15
+ }
16
+
17
+ /**
18
+ * A patient's draft order, found when they're chosen: its prescriptions, each on its own card as the order's lines
19
+ * are, then the question — continue it, or start a new order. Nothing changes until one is picked. Renders cards
20
+ * only, for PhotonOrder's flow.
21
+ */
22
+ export function DraftOrderPrompt({ draft, onContinue, onStartNew }: DraftOrderPromptProps) {
23
+ const { Button, Card, CardBody, CardFooter, CardHeader, Row, Stack, Text } = useComponents();
24
+ // Which choice is in flight: that button spins, and neither can be pressed again until it settles.
25
+ const [choosing, setChoosing] = useState<"continue" | "new" | null>(null);
26
+ const choose = (which: "continue" | "new", act: () => void | Promise<unknown>) => {
27
+ setChoosing(which);
28
+ // A failure lands on the order (snapshot.error), where the card shows it.
29
+ void new Promise((resolve) => resolve(act())).catch(() => {}).finally(() => setChoosing(null));
30
+ };
31
+
32
+ return (
33
+ <>
34
+ {draft.prescriptions.map((rx) => {
35
+ const heading = treatmentHeading(rx.treatment.name, rx.treatment);
36
+ return (
37
+ <div key={rx.id} {...cardProps}>
38
+ <Card divided label={heading.title}>
39
+ <CardHeader title={heading.title} description={heading.detail} />
40
+ <CardBody>
41
+ <ReadOnlyRx rx={rx} status={<StatusBadge rx={rx} />} />
42
+ </CardBody>
43
+ </Card>
44
+ </div>
45
+ );
46
+ })}
47
+ <div {...cardProps}>
48
+ <Card>
49
+ {/* The footer, not the body: a host's body text can be smaller (Photon Rx's is), and this is the card's
50
+ question. Stacked in `start`, so the buttons sit under it, on the left. */}
51
+ <CardFooter
52
+ start={
53
+ <Stack gap="3">
54
+ <Text>Would you like to continue this order?</Text>
55
+ <Row gap="2">
56
+ <span {...focusProps}>
57
+ <Button
58
+ loading={choosing === "continue"}
59
+ disabled={choosing !== null}
60
+ onClick={() => choose("continue", onContinue)}
61
+ >
62
+ Continue
63
+ </Button>
64
+ </span>
65
+ <Button
66
+ variant="secondary"
67
+ loading={choosing === "new"}
68
+ disabled={choosing !== null}
69
+ onClick={() => choose("new", onStartNew)}
70
+ >
71
+ Start a new order
72
+ </Button>
73
+ </Row>
74
+ </Stack>
75
+ }
76
+ />
77
+ </Card>
78
+ </div>
79
+ </>
80
+ );
81
+ }
@@ -0,0 +1,133 @@
1
+ /** The patient's insurance on file. */
2
+ import { useState } from "react";
3
+ import { useComponents } from "../../primitives/index.ts";
4
+ import { noAutofill } from "./fields.tsx";
5
+ import { TrackedField } from "../../analytics/fieldInteraction.tsx";
6
+
7
+ export type BenefitInput = { id?: string; bin?: string; pcn?: string; groupId?: string; memberId?: string };
8
+
9
+ /** Insurance on file: chips, "Add insurance" (BIN + member id required, PCN / group optional), "No insurance". */
10
+ export function BenefitsSection({
11
+ benefits,
12
+ saving,
13
+ onAdd,
14
+ onRemove,
15
+ onNone,
16
+ }: {
17
+ /** null: not recorded; []: "no insurance" recorded. */
18
+ benefits: { id: string; bin: string; pcn: string | null; groupId: string | null; memberId: string }[] | null;
19
+ saving: boolean;
20
+ onAdd: (benefit: BenefitInput) => void;
21
+ onRemove: (benefit: { id: string }) => void;
22
+ onNone: () => void;
23
+ }) {
24
+ const { Badge, Button, Grid, Input, Row, Stack, Text } = useComponents();
25
+ const [adding, setAdding] = useState(false);
26
+ const [bin, setBin] = useState("");
27
+ const [pcn, setPcn] = useState("");
28
+ const [groupId, setGroupId] = useState("");
29
+ const [memberId, setMemberId] = useState("");
30
+ const none = benefits?.length === 0;
31
+ const plans = benefits ?? [];
32
+ const canSave = Boolean(bin.trim() && memberId.trim());
33
+ const reset = () => {
34
+ setBin("");
35
+ setPcn("");
36
+ setGroupId("");
37
+ setMemberId("");
38
+ setAdding(false);
39
+ };
40
+
41
+ return (
42
+ <Stack gap="2">
43
+ <Row gap="2" justify="between">
44
+ <Row gap="2">
45
+ <strong>Insurance</strong>
46
+ {!adding && (
47
+ <Button
48
+ variant="ghost"
49
+ aria-label="Add insurance"
50
+ title="Add insurance"
51
+ onClick={() => setAdding(true)}
52
+ icon="add"
53
+ />
54
+ )}
55
+ {plans.map((b) => {
56
+ const label = [
57
+ `BIN ${b.bin}`,
58
+ b.pcn && `PCN ${b.pcn}`,
59
+ b.groupId && `Group ${b.groupId}`,
60
+ `Member ${b.memberId}`,
61
+ ]
62
+ .filter(Boolean)
63
+ .join(" · ");
64
+ return (
65
+ <Badge key={b.id} tone="neutral" onRemove={() => onRemove(b)} removeLabel={`Remove ${label}`}>
66
+ {label}
67
+ </Badge>
68
+ );
69
+ })}
70
+ {none && <Badge tone="success">No insurance</Badge>}
71
+ {benefits === null && !adding && <Text tone="tertiary">Not recorded</Text>}
72
+ </Row>
73
+ {!adding && (
74
+ <Row gap="1">
75
+ {!plans.length && !none && (
76
+ <Button variant="ghost" onClick={onNone} aria-label="Mark no insurance">
77
+ Mark none
78
+ </Button>
79
+ )}
80
+ </Row>
81
+ )}
82
+ </Row>
83
+ {adding && (
84
+ // A form, so Enter in any field saves the plan.
85
+ <form
86
+ {...noAutofill}
87
+ onSubmit={(e) => {
88
+ e.preventDefault();
89
+ if (!canSave) return;
90
+ onAdd({
91
+ bin: bin.trim(),
92
+ memberId: memberId.trim(),
93
+ pcn: pcn.trim() || undefined,
94
+ groupId: groupId.trim() || undefined,
95
+ });
96
+ reset();
97
+ }}
98
+ >
99
+ <Stack gap="2">
100
+ <TrackedField formName="update_patient_form" fieldName="insurance" isOptional>
101
+ <Grid min="72px" gap="2">
102
+ <Input
103
+ label="BIN"
104
+ value={bin}
105
+ onChange={(e) => setBin(e.target.value)}
106
+ inputMode="numeric"
107
+ autoFocus
108
+ {...noAutofill}
109
+ />
110
+ <Input label="PCN" value={pcn} onChange={(e) => setPcn(e.target.value)} {...noAutofill} />
111
+ <Input label="Group" value={groupId} onChange={(e) => setGroupId(e.target.value)} {...noAutofill} />
112
+ <Input
113
+ label="Member ID"
114
+ value={memberId}
115
+ onChange={(e) => setMemberId(e.target.value)}
116
+ {...noAutofill}
117
+ />
118
+ </Grid>
119
+ </TrackedField>
120
+ <Row justify="end" gap="2">
121
+ <Button variant="ghost" onClick={reset}>
122
+ Cancel
123
+ </Button>
124
+ <Button type="submit" variant="secondary" loading={saving} disabled={!canSave}>
125
+ Save insurance
126
+ </Button>
127
+ </Row>
128
+ </Stack>
129
+ </form>
130
+ )}
131
+ </Stack>
132
+ );
133
+ }
@@ -0,0 +1,215 @@
1
+ /** The patient's clinical lists — allergies, medications, diagnoses — and which server changes belong to them. */
2
+ import { type ChangeOption, type ClassifiedChanges } from "@photonhealth/client";
3
+ import { useRef, useState } from "react";
4
+ import { useComponents } from "../../primitives/index.ts";
5
+ import { useHeldActivity } from "../activity.ts";
6
+ import { ChangeList } from "../ChangeList.tsx";
7
+ import { afterPress, searchKeys, useTypeAhead, type SearchHandle } from "../search.tsx";
8
+ import { noAutofill } from "./fields.tsx";
9
+ import { TrackedField } from "../../analytics/fieldInteraction.tsx";
10
+
11
+ /** Which clinical list a server change belongs to (keys look like "clinical.allergies.add.0" / "allergies.0"). */
12
+ const CLINICAL_SECTIONS = ["allergies", "medications", "diagnoses"] as const;
13
+ export const sectionOf = (key: string) => CLINICAL_SECTIONS.find((section) => key.includes(section)) ?? null;
14
+ export const choicesIn = (choices: ClassifiedChanges["needsChoice"], section: (typeof CLINICAL_SECTIONS)[number]) =>
15
+ choices.filter((c) => sectionOf(c.key) === section);
16
+ /** Everything but what a clinical list shows under its own field — its choices and its problems (e.g. a rejected name). */
17
+ export const withoutInline = (changes: ClassifiedChanges): ClassifiedChanges => ({
18
+ ...changes,
19
+ needsChoice: changes.needsChoice.filter((c) => !sectionOf(c.key)),
20
+ blocking: changes.blocking.filter((c) => !sectionOf(c.key)),
21
+ });
22
+
23
+ /** An ICD-10 code ("J02.0") goes as a code; anything else as free text for the server to resolve. */
24
+ export const diagnosisRef = (text: string) =>
25
+ /^[A-TV-Z]\d{2}(\.[0-9A-Z]{1,4})?$/i.test(text) ? { icd10Code: text.toUpperCase() } : { name: text };
26
+
27
+ /**
28
+ * A clinical list (allergies, medications, diagnoses): chips for what's on file, plus "Add …" and "No …"
29
+ * (records none — `confirmedNone` is the record's `[]`; network-api can't read it back yet).
30
+ *
31
+ * "Add …" is a search field (search.tsx) — type-ahead, its spinner, its results (the server's choices, e.g.
32
+ * which penicillin) and keys. Each search is an add, by design: what's typed, once it matches one entry, is that
33
+ * entry — added then, as is a single row or the one match the server is sure of (core takes it). The field closes as soon as an entry lands, or once what's committed (Enter,
34
+ * leaving it, a choice) is answered. Escape closes it.
35
+ */
36
+ export function ClinicalList<T extends { id: string; name: string }>({
37
+ label,
38
+ fieldName,
39
+ items,
40
+ tone,
41
+ addLabel,
42
+ noneLabel,
43
+ noneAction,
44
+ confirmedNone,
45
+ unknownLabel,
46
+ pickedNames,
47
+ saving,
48
+ onAdd,
49
+ onRemove,
50
+ onNone,
51
+ choices,
52
+ problems = [],
53
+ onChoose,
54
+ }: {
55
+ label: string;
56
+ fieldName: "allergies" | "medications" | "diagnoses";
57
+ items: T[];
58
+ tone: "warning" | "neutral";
59
+ addLabel: string;
60
+ /** Offer "none" (e.g. No known allergies). Omit for lists where "none" isn't worth recording. */
61
+ noneLabel?: string;
62
+ /** The button that records "none" (e.g. "Mark NKDA") — an action, so it can't be read as a status. */
63
+ noneAction?: string;
64
+ /** "None" was recorded this session. */
65
+ confirmedNone?: boolean;
66
+ /** Shown when the server returns no real name (today allergies come back with name = id). */
67
+ unknownLabel: string;
68
+ /** Names the prescriber picked this session, by id — shown when the server returns only the id. */
69
+ pickedNames: Readonly<Record<string, string>>;
70
+ saving: boolean;
71
+ onAdd: (name: string) => void;
72
+ onRemove: (item: T) => void;
73
+ onNone?: () => void;
74
+ /** The server's choices for what was just entered (e.g. which penicillin) — shown right under the field. */
75
+ choices: ClassifiedChanges["needsChoice"];
76
+ /** What the server rejected for this list (e.g. a name nothing matches) — shown in the same place. */
77
+ problems?: ClassifiedChanges["blocking"];
78
+ onChoose: (option: ChangeOption) => void;
79
+ }) {
80
+ const { Badge, Button, Input, Row, Stack, Text } = useComponents();
81
+ const [adding, setAdding] = useState(false);
82
+ const [value, setValue] = useState("");
83
+ const [sent, setSent] = useState("");
84
+ // This list's call (an add, or a choice) is in flight: its field spins. Answered with nothing left to
85
+ // choose after the prescriber committed (not just paused typing), the entry is on the list: it closes.
86
+ const [asked, setAsked] = useState(false);
87
+ const [committed, setCommitted] = useState(false);
88
+ // How many entries were on the list when this field first sent: one more, and it's added.
89
+ const [before, setBefore] = useState<number | null>(null);
90
+ // Done with the field (state only — also from render, once an answer lands; a search still waiting finds
91
+ // it empty and sends nothing).
92
+ const reset = () => {
93
+ setValue("");
94
+ setSent("");
95
+ setCommitted(false);
96
+ setBefore(null);
97
+ setAdding(false);
98
+ };
99
+ if (asked && !saving) {
100
+ setAsked(false);
101
+ if (committed && !choices.length && !problems.length) reset(); // rejected: stays, to fix what's typed
102
+ }
103
+ if (adding && before !== null && items.length > before && !saving && !choices.length) reset();
104
+ // Spins while what it sent — and a match core takes after it — is in flight.
105
+ const spinning = useHeldActivity(adding && before !== null && saving ? "add" : null) === "add";
106
+ const results = useRef<SearchHandle | null>(null);
107
+ const field = useRef<HTMLDivElement>(null);
108
+ const none = Boolean(confirmedNone) && items.length === 0;
109
+
110
+ /** Add what's typed — unless it was just sent. */
111
+ const send = (name: string) => {
112
+ if (name === sent) return false;
113
+ setSent(name);
114
+ setAsked(true);
115
+ if (before === null) setBefore(items.length);
116
+ onAdd(name);
117
+ return true;
118
+ };
119
+ const typeAhead = useTypeAhead(() => value.trim() && send(value.trim()), { minChars: 3 });
120
+ const close = () => {
121
+ typeAhead.cancel();
122
+ reset();
123
+ };
124
+ /** Enter or leaving the field: add it now; once it's on the list, the field closes. */
125
+ const add = () => {
126
+ typeAhead.cancel();
127
+ const name = value.trim();
128
+ if (!name) return close();
129
+ setCommitted(true);
130
+ // Typing already added it, and that's answered: nothing left to wait for.
131
+ if (!send(name) && !saving && !choices.length && !problems.length) close();
132
+ };
133
+ const choose = (option: ChangeOption) => {
134
+ setCommitted(true);
135
+ setAsked(true);
136
+ onChoose(option);
137
+ };
138
+
139
+ return (
140
+ <Stack gap="2">
141
+ <Row gap="2" justify="between">
142
+ <Row gap="2">
143
+ <strong>{label}</strong>
144
+ {!adding && (
145
+ <Button variant="ghost" aria-label={addLabel} title={addLabel} onClick={() => setAdding(true)} icon="add" />
146
+ )}
147
+ {items.map((item) => {
148
+ const name = item.name && item.name !== item.id ? item.name : (pickedNames[item.id] ?? unknownLabel);
149
+ return (
150
+ <Badge key={item.id} tone={tone} onRemove={() => onRemove(item)} removeLabel={`Remove ${name}`}>
151
+ {name}
152
+ </Badge>
153
+ );
154
+ })}
155
+ {none && noneLabel && <Badge tone="success">{noneLabel}</Badge>}
156
+ {/* network-api can't tell "never asked" from "none": say which we know. */}
157
+ {noneLabel && !none && items.length === 0 && !adding && <Text tone="tertiary">Not recorded</Text>}
158
+ </Row>
159
+ {noneLabel && onNone && !adding && items.length === 0 && !none && (
160
+ <Button
161
+ variant="ghost"
162
+ onClick={onNone} // never disabled by another save: calls are queued
163
+ // "Mark none" on several lists: each named for its own.
164
+ aria-label={noneAction === "Mark none" ? `Mark no ${label.toLowerCase()}` : undefined}
165
+ >
166
+ {noneAction ?? noneLabel}
167
+ </Button>
168
+ )}
169
+ </Row>
170
+ {(adding || choices.length > 0 || problems.length > 0) && (
171
+ <div ref={field}>
172
+ <Stack gap="2">
173
+ {adding && (
174
+ <TrackedField formName="update_patient_form" fieldName={fieldName} isOptional>
175
+ <Input
176
+ label={addLabel}
177
+ value={value}
178
+ autoFocus
179
+ loading={spinning}
180
+ {...noAutofill}
181
+ onChange={(e) => {
182
+ setValue(e.target.value);
183
+ typeAhead.typed("add", e.target.value);
184
+ }}
185
+ onBlur={(e) => {
186
+ // Into its results isn't leaving; anywhere else sends what's typed (or closes an empty field).
187
+ if (field.current?.contains(e.relatedTarget) || results.current?.pointing?.()) return;
188
+ afterPress(add); // e.g. pressing "Mark none" below: it mustn't move before the click lands
189
+ }}
190
+ onKeyDown={(e) => {
191
+ if (searchKeys(e, results.current, value.trim() === sent && !saving)) return;
192
+ if (e.key === "Enter") {
193
+ e.preventDefault();
194
+ add();
195
+ }
196
+ if (e.key === "Escape") close();
197
+ }}
198
+ />
199
+ </TrackedField>
200
+ )}
201
+ {(choices.length > 0 || problems.length > 0) && (
202
+ <ChangeList
203
+ changes={{ needsChoice: choices, blocking: problems, advisories: [], applied: [], settled: false }}
204
+ onChoose={choose}
205
+ busy={saving}
206
+ handleRef={results}
207
+ onBack={() => field.current?.querySelector("input")?.focus()}
208
+ />
209
+ )}
210
+ </Stack>
211
+ </div>
212
+ )}
213
+ </Stack>
214
+ );
215
+ }
@@ -0,0 +1,89 @@
1
+ // @vitest-environment jsdom
2
+ import { fireEvent, render, screen } from "@testing-library/react";
3
+ import userEvent from "@testing-library/user-event";
4
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
5
+ import { fakeNetworkApiClient, noMatch } from "@photonhealth/client/testing";
6
+ import { PhotonPatient } from "../../index.ts";
7
+ import {
8
+ edited,
9
+ leaveField,
10
+ meAsPrescriber,
11
+ recordAnalytics,
12
+ type RecordedAnalytics,
13
+ typeInto,
14
+ unmountAndRestoreFetch,
15
+ visitedWithoutEditing,
16
+ withAnalytics,
17
+ } from "../../../test/analytics.fixtures.tsx";
18
+
19
+ let analytics: RecordedAnalytics;
20
+ beforeEach(() => {
21
+ analytics = recordAnalytics();
22
+ });
23
+ afterEach(unmountAndRestoreFetch);
24
+
25
+ const renderPatientCard = () =>
26
+ render(<PhotonPatient />, {
27
+ wrapper: withAnalytics(fakeNetworkApiClient({ ...meAsPrescriber, SyncPatient: noMatch }).client),
28
+ });
29
+
30
+ describe("PatientIntakeForm: Field Interaction", () => {
31
+ it("Find patient is find_patient's patientSearch: focus, one change, blur, and nothing typed", async () => {
32
+ renderPatientCard();
33
+ await typeInto("Find patient", "Zqxfirst Zqxlast");
34
+
35
+ await analytics.expectEventLog([
36
+ "Field Interaction patientSearch focus",
37
+ "Prescribe Workflow Viewed",
38
+ "Field Interaction patientSearch change",
39
+ "Field Interaction patientSearch blur",
40
+ ]);
41
+ expect(analytics.sentFor("patientSearch")).toEqual([
42
+ expect.objectContaining({ formName: "find_patient", hasValue: false, isOptional: false }),
43
+ expect.objectContaining({ hasValue: true }),
44
+ expect.objectContaining({ hasValue: true, changed: true, durationMs: expect.any(Number) }),
45
+ ]);
46
+ for (const body of analytics.bodies()) expect(body).not.toMatch(/Zqx/i);
47
+ });
48
+
49
+ it("adding a patient: each field visited sends focus, one change and blur as new_patient_form", async () => {
50
+ renderPatientCard();
51
+ await typeInto("Find patient", "Zqxfirst Zqxlast");
52
+ await userEvent.click(await screen.findByRole("option", { name: /Add new patient/ }));
53
+ await typeInto("First name", "Zqxa");
54
+ await typeInto("Last name", "Zqxb");
55
+ await userEvent.click(screen.getByLabelText("Date of birth"));
56
+ fireEvent.change(screen.getByLabelText("Date of birth"), { target: { value: "1990-01-01" } });
57
+ leaveField();
58
+ await userEvent.selectOptions(screen.getByRole("combobox", { name: "Sex" }), "FEMALE");
59
+ leaveField();
60
+ await typeInto("Phone", "5555550100");
61
+ await typeInto("Email", "zqx@example.com");
62
+
63
+ await analytics.expectEventLog([
64
+ "Field Interaction patientSearch focus",
65
+ "Prescribe Workflow Viewed",
66
+ "Field Interaction patientSearch change",
67
+ "Field Interaction patientSearch blur",
68
+ ...visitedWithoutEditing("firstName"),
69
+ ...visitedWithoutEditing("sex"),
70
+ ...edited("firstName"),
71
+ ...edited("lastName"),
72
+ ...edited("dateOfBirth"),
73
+ ...edited("sex"),
74
+ ...edited("phone"),
75
+ ...edited("email"),
76
+ ]);
77
+ const sent = analytics.fieldInteractions().filter((s) => s.fieldName !== "patientSearch");
78
+ expect(new Set(sent.map((s) => s.formName))).toEqual(new Set(["new_patient_form"]));
79
+ expect(Object.fromEntries(sent.map((s) => [s.fieldName, s.isOptional]))).toEqual({
80
+ firstName: false,
81
+ lastName: false,
82
+ dateOfBirth: false,
83
+ sex: false,
84
+ phone: true,
85
+ email: true,
86
+ });
87
+ for (const body of analytics.bodies()) expect(body).not.toMatch(/Zqx|1990|5555550100|FEMALE/i);
88
+ });
89
+ });