@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,16 @@
1
+ import { useCallback, useLayoutEffect, useRef } from "react";
2
+
3
+ /**
4
+ * A stable function that always calls the latest `fn` — for handing host callbacks
5
+ * (onChange, …) to core objects created once. The ref is updated after
6
+ * render, never during it (react-hooks/refs).
7
+ */
8
+ export function useLatestCallback<A extends unknown[], R>(
9
+ fn: ((...args: A) => R) | undefined,
10
+ ): (...args: A) => R | undefined {
11
+ const ref = useRef(fn);
12
+ useLayoutEffect(() => {
13
+ ref.current = fn;
14
+ });
15
+ return useCallback((...args: A) => ref.current?.(...args), []);
16
+ }
@@ -0,0 +1,21 @@
1
+ import type { AnySnapshot, LiveObject } from "@photonhealth/client";
2
+ import { useSyncExternalStore } from "react";
3
+
4
+ interface Live<S> {
5
+ subscribe(listener: () => void): () => void;
6
+ readonly snapshot: S;
7
+ }
8
+
9
+ const noSubscribe = () => () => {};
10
+
11
+ /**
12
+ * Bind a live object from @photonhealth/client (a Patient, Prescription or Order): re-renders when anything
13
+ * changes it — this component, another card, an order's response. Returns its snapshot
14
+ * `{ state, status, error }`, or null without an object.
15
+ */
16
+ export function useLive<S>(obj: Live<S> | null | undefined): S | null;
17
+ /** Any of the three (e.g. an inspector over every object): the snapshot, typed as any of theirs. */
18
+ export function useLive(obj: LiveObject | null | undefined): AnySnapshot | null;
19
+ export function useLive(obj: Live<unknown> | null | undefined): unknown {
20
+ return useSyncExternalStore(obj ? obj.subscribe : noSubscribe, () => obj?.snapshot ?? null);
21
+ }
@@ -0,0 +1,34 @@
1
+ import { useEffect, useState } from "react";
2
+ import { useLatestCallback } from "./useLatestCallback.ts";
3
+
4
+ interface Live {
5
+ subscribe(listener: () => void): () => void;
6
+ }
7
+
8
+ /**
9
+ * A card's record event: call `fire(obj)` with its live object once it has a record (`select` returns
10
+ * something), then each time that record changes from the backend — compared by value, so a save that
11
+ * brings back the same record, or a saving / idle flip, doesn't count.
12
+ */
13
+ export function useOnChange<T extends Live>(
14
+ obj: T | null | undefined,
15
+ select: (obj: T) => unknown,
16
+ fire: ((obj: T) => void) | undefined,
17
+ ) {
18
+ const call = useLatestCallback(fire);
19
+ const pick = useLatestCallback(select);
20
+ // What was last reported for each object — so an effect that runs again (React's StrictMode mounts
21
+ // twice in development) doesn't report the same record twice.
22
+ const [reported] = useState(() => new WeakMap<T, string>());
23
+ useEffect(() => {
24
+ if (!obj) return;
25
+ const report = () => {
26
+ const key = JSON.stringify(pick(obj) ?? null);
27
+ if (key === (reported.get(obj) ?? "null")) return;
28
+ reported.set(obj, key);
29
+ call(obj);
30
+ };
31
+ report();
32
+ return obj.subscribe(report);
33
+ }, [obj, call, pick, reported]);
34
+ }
@@ -0,0 +1,11 @@
1
+ import type { SessionSnapshot } from "@photonhealth/client";
2
+ import { usePhoton } from "../primitives/index.ts";
3
+ import { useLive } from "./useLive.ts";
4
+
5
+ /**
6
+ * The prescriber's sign-in, for the host's own chrome (their name, their organization) — when the provider signs
7
+ * in (`clientId`). Null when the host signs in itself. Signing in is `usePhoton().session.signIn()`.
8
+ */
9
+ export function useSession(): SessionSnapshot | null {
10
+ return useLive(usePhoton().session);
11
+ }
package/src/index.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @photonhealth/react — Photon Rx for React. No design-system dependency:
3
+ * the cards render through a small set of building blocks that default to
4
+ * unstyled, page-inheriting HTML, and can be mapped to any design system.
5
+ *
6
+ * <PhotonProvider clientId="…" components={myComponents?}> — signs the prescriber in, then the cards call
7
+ * <PhotonProvider client={photon({ environment, auth })}> — or a client of the host's own
8
+ *
9
+ * primitives/ PhotonComponents (the building-block interface), defaultComponents, PhotonProvider
10
+ * hooks/ useLive (bind a live object)
11
+ * components/ PhotonOrder, PhotonPatient, PhotonPrescription — bound to @photonhealth/client's live objects (ids or objects in,
12
+ * events out)
13
+ *
14
+ * Optional: import "@photonhealth/react/styles.css" for structural styles that inherit the page.
15
+ */
16
+ export * from "./primitives/index.ts";
17
+ export { useLive, useSession } from "./hooks/index.ts";
18
+ export { PhotonAnalyticsProvider } from "./analytics/PhotonAnalyticsProvider.tsx";
19
+ export type { AnalyticsProperties } from "./analytics/tracker.ts";
20
+ export { PhotonOrder, PhotonPatient, PhotonPrescription } from "./components/index.ts";
21
+ export type { PhotonOrderProps, PhotonPatientProps, PhotonPrescriptionProps } from "./components/index.ts";
@@ -0,0 +1,10 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { describe, expect, it } from "vitest";
3
+ import { reactPackage } from "./version.ts";
4
+
5
+ describe("reactPackage", () => {
6
+ it("is package.json's name and version: bump both together", () => {
7
+ const manifest = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8"));
8
+ expect(reactPackage).toEqual({ name: manifest.name, version: manifest.version });
9
+ });
10
+ });
@@ -0,0 +1,4 @@
1
+ export const reactPackage: { readonly name: string; readonly version: string } = {
2
+ name: "@photonhealth/react",
3
+ version: "0.10.0",
4
+ };
@@ -0,0 +1,168 @@
1
+ import { type Photon, type PhotonEnvironment, type PhotonError, type SessionUser } from "@photonhealth/client";
2
+ import { PhotonContext } from "./photonContext.ts";
3
+ import { createContext, useContext, useEffect, useMemo, useState, useSyncExternalStore, type ReactNode } from "react";
4
+ import { useLatestCallback } from "../hooks/useLatestCallback.ts";
5
+ import { PhotonAnalyticsProvider } from "../analytics/PhotonAnalyticsProvider.tsx";
6
+ import { trackerFor } from "../analytics/tracker.ts";
7
+ import { defaultComponents } from "./defaults.tsx";
8
+ import type { PhotonComponents } from "./types.ts";
9
+ import { useProvidedPhoton } from "./useProvidedPhoton.ts";
10
+
11
+ const ComponentsContext = createContext<PhotonComponents>(defaultComponents);
12
+
13
+ /** What `onAuth` reports: network-api took the instance's token, or why not. */
14
+ export type PhotonAuthCheck = { ok: true; error: null } | { ok: false; error: PhotonError };
15
+
16
+ /**
17
+ * One ping per instance and sign-in, however many providers or renders ask (StrictMode runs effects twice). A new
18
+ * sign-in (`who`) is checked again, and so is one whose check failed when it's tried again (`attempt`).
19
+ */
20
+ const checks = new WeakMap<Photon, { who: unknown; attempt: number; check: Promise<PhotonAuthCheck> }>();
21
+ const checkAuth = (ph: Photon, who: unknown, attempt: number) => {
22
+ let known = checks.get(ph);
23
+ if (!known || known.who !== who || known.attempt !== attempt) {
24
+ const check = ph.ping().then(
25
+ (): PhotonAuthCheck => ({ ok: true, error: null }),
26
+ (error: PhotonError): PhotonAuthCheck => ({ ok: false, error }),
27
+ );
28
+ checks.set(ph, (known = { who, attempt, check }));
29
+ }
30
+ return known.check;
31
+ };
32
+
33
+ /**
34
+ * Why the sign-in's check failed — network-api refused the token (`unauthorized`), or couldn't be reached or answer —
35
+ * for the cards to say so; `retry` checks again (`checking` until it's answered).
36
+ */
37
+ interface CheckFailure {
38
+ error: PhotonError;
39
+ checking: boolean;
40
+ retry: () => void;
41
+ }
42
+ const CheckFailureContext = createContext<CheckFailure | null>(null);
43
+ /** The organization the cards are for, when the host names one. */
44
+ const OrganizationContext = createContext<string | null>(null);
45
+
46
+ /**
47
+ * One provider for Photon Rx components — and their sign-in: everything inside it can make web calls.
48
+ * clientId sign the prescriber in here (core's session: Auth0, PKCE, renewed tokens). Opportunistic: a
49
+ * sign-in coming back from Auth0 is finished and a stored one renewed, on load; otherwise the
50
+ * first card in the page offers "Sign in" (a click; never an unasked redirect).
51
+ * environment with `clientId`: which Photon environment — production (`photon`) unless said.
52
+ * organization sign in straight to this Photon organization (no picker); signed in to another, the first card
53
+ * offers to sign in to this one.
54
+ * client or a Photon client of the host's own (`photon({ environment, auth })` — e.g. `{ getToken }`
55
+ * when the host signs in itself).
56
+ * components optional — map any building blocks to your design system; the rest are unstyled defaults.
57
+ * onAuth optional — once signed in, whether network-api takes the token (one ping per instance).
58
+ * disableAnalytics optional — present or `true` turns analytics off; it's on by default.
59
+ *
60
+ * At any level of the page: a provider without a client inherits its parent's, and providers with the same
61
+ * `clientId` share one sign-in.
62
+ */
63
+ export function PhotonProvider({
64
+ client,
65
+ clientId,
66
+ environment = "photon",
67
+ organization,
68
+ components,
69
+ onAuth,
70
+ disableAnalytics,
71
+ children,
72
+ }: {
73
+ /** A Photon client of the host's own. Optional: a nested provider inherits its parent's. */
74
+ client?: Photon;
75
+ /** Sign the prescriber in with this Auth0 client (public, not a secret). */
76
+ clientId?: string;
77
+ /** With `clientId`: the Photon environment. Production unless said. */
78
+ environment?: PhotonEnvironment;
79
+ /** Sign in straight to this organization (`org_…`), skipping Auth0's picker. Optional: inherited. */
80
+ organization?: string;
81
+ components?: Partial<PhotonComponents>;
82
+ onAuth?: (auth: PhotonAuthCheck) => void;
83
+ disableAnalytics?: boolean;
84
+ children: ReactNode;
85
+ }) {
86
+ const outerComponents = useContext(ComponentsContext);
87
+ const outerPhoton = useContext(PhotonContext);
88
+ const outerFailure = useContext(CheckFailureContext);
89
+ const outerOrganization = useContext(OrganizationContext);
90
+ const blocks = useMemo(
91
+ () => (components ? { ...outerComponents, ...components } : outerComponents),
92
+ [outerComponents, components],
93
+ );
94
+ const providedPhoton = useProvidedPhoton({ client, clientId, environment, organization });
95
+
96
+ // Sign-in, opportunistically: once per page however many providers share it.
97
+ const session = providedPhoton?.session ?? null;
98
+ useEffect(() => void session?.start(), [session]);
99
+ const signedIn = useSyncExternalStore(session?.subscribe ?? noSubscribe, () =>
100
+ session ? (session.status === "signed-in" ? session.user : null) : true,
101
+ );
102
+
103
+ // Once signed in: does network-api take the token? Refused or unreachable, the cards say so (and offer to sign in
104
+ // again, or to try again).
105
+ const [failed, setFailed] = useState<PhotonError | null>(null);
106
+ const [attempt, setAttempt] = useState(0);
107
+ const [checked, setChecked] = useState(0);
108
+ const [signInAcceptedByNetworkApi, setSignInAcceptedByNetworkApi] = useState<SessionUser | true | null>(null);
109
+ const report = useLatestCallback(onAuth);
110
+ const listening = Boolean(onAuth) || Boolean(session);
111
+ useEffect(() => {
112
+ if (!providedPhoton || !listening || !signedIn) return;
113
+ let live = true;
114
+ void checkAuth(providedPhoton, signedIn, attempt).then((auth) => {
115
+ if (!live) return;
116
+ setFailed(auth.error);
117
+ setChecked(attempt);
118
+ setSignInAcceptedByNetworkApi(auth.ok ? signedIn : null);
119
+ report(auth);
120
+ });
121
+ return () => {
122
+ live = false;
123
+ };
124
+ }, [providedPhoton, listening, signedIn, attempt, report]);
125
+ const coreManagesSignIn = session !== null;
126
+ const signedInToOrganization = Boolean(signedIn && session?.user?.organization);
127
+ const networkApiAcceptedSignIn = signedIn !== null && signInAcceptedByNetworkApi === signedIn;
128
+ const analyticsReady = !coreManagesSignIn || (signedInToOrganization && networkApiAcceptedSignIn);
129
+ useEffect(() => {
130
+ if (providedPhoton) trackerFor(providedPhoton).setReady(analyticsReady);
131
+ }, [providedPhoton, analyticsReady]);
132
+
133
+ const failure = useMemo(
134
+ () => (failed ? { error: failed, checking: checked !== attempt, retry: () => setAttempt((n) => n + 1) } : null),
135
+ [failed, checked, attempt],
136
+ );
137
+
138
+ return (
139
+ <PhotonContext.Provider value={providedPhoton ?? outerPhoton}>
140
+ <CheckFailureContext.Provider value={providedPhoton ? failure : outerFailure}>
141
+ <OrganizationContext.Provider value={organization ?? outerOrganization}>
142
+ <ComponentsContext.Provider value={blocks}>
143
+ <PhotonAnalyticsProvider enabled={disableAnalytics === undefined ? undefined : !disableAnalytics}>
144
+ {children}
145
+ </PhotonAnalyticsProvider>
146
+ </ComponentsContext.Provider>
147
+ </OrganizationContext.Provider>
148
+ </CheckFailureContext.Provider>
149
+ </PhotonContext.Provider>
150
+ );
151
+ }
152
+
153
+ const noSubscribe = () => () => {};
154
+
155
+ export const useComponents = () => useContext(ComponentsContext);
156
+
157
+ /** The photon instance PhotonProvider was given (or made) — for a host's own calls next to the cards. */
158
+ export function usePhoton(): Photon {
159
+ const ph = useContext(PhotonContext);
160
+ if (!ph) throw new Error("Wrap Photon Rx components in <PhotonProvider clientId={…}> (or client={…}).");
161
+ return ph;
162
+ }
163
+
164
+ /** Why the sign-in's check failed, if it did (refused, or Photon unreachable) — for the cards. */
165
+ export const useCheckFailure = () => useContext(CheckFailureContext);
166
+
167
+ /** The organization the provider names, if it does — for the cards (signing in, and checking the sign-in's). */
168
+ export const useOrganization = () => useContext(OrganizationContext);