@azx-pbc/axiom-react 0.0.0-stage → 0.0.6

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 (191) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +604 -2
  3. package/dist/FormErrorBoundary.d.ts +37 -0
  4. package/dist/FormErrorBoundary.d.ts.map +1 -0
  5. package/dist/FormErrorBoundary.js +54 -0
  6. package/dist/FormErrorBoundary.js.map +1 -0
  7. package/dist/FormProvider.d.ts +197 -0
  8. package/dist/FormProvider.d.ts.map +1 -0
  9. package/dist/FormProvider.js +149 -0
  10. package/dist/FormProvider.js.map +1 -0
  11. package/dist/field-error.d.ts +154 -0
  12. package/dist/field-error.d.ts.map +1 -0
  13. package/dist/field-error.js +2 -0
  14. package/dist/field-error.js.map +1 -0
  15. package/dist/field-props.d.ts +362 -0
  16. package/dist/field-props.d.ts.map +1 -0
  17. package/dist/field-props.js +64 -0
  18. package/dist/field-props.js.map +1 -0
  19. package/dist/form-context.d.ts +31 -0
  20. package/dist/form-context.d.ts.map +1 -0
  21. package/dist/form-context.js +13 -0
  22. package/dist/form-context.js.map +1 -0
  23. package/dist/form-store.d.ts +784 -0
  24. package/dist/form-store.d.ts.map +1 -0
  25. package/dist/form-store.js +2179 -0
  26. package/dist/form-store.js.map +1 -0
  27. package/dist/hints.d.ts +60 -0
  28. package/dist/hints.d.ts.map +1 -0
  29. package/dist/hints.js +158 -0
  30. package/dist/hints.js.map +1 -0
  31. package/dist/index.d.ts +57 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +36 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/render/field-value.d.ts +92 -0
  36. package/dist/render/field-value.d.ts.map +1 -0
  37. package/dist/render/field-value.js +124 -0
  38. package/dist/render/field-value.js.map +1 -0
  39. package/dist/render/nodes.d.ts +303 -0
  40. package/dist/render/nodes.d.ts.map +1 -0
  41. package/dist/render/nodes.js +325 -0
  42. package/dist/render/nodes.js.map +1 -0
  43. package/dist/render/schema.d.ts +30 -0
  44. package/dist/render/schema.d.ts.map +1 -0
  45. package/dist/render/schema.js +40 -0
  46. package/dist/render/schema.js.map +1 -0
  47. package/dist/render/value-text.d.ts +59 -0
  48. package/dist/render/value-text.d.ts.map +1 -0
  49. package/dist/render/value-text.js +104 -0
  50. package/dist/render/value-text.js.map +1 -0
  51. package/dist/store/array-bindings.d.ts +110 -0
  52. package/dist/store/array-bindings.d.ts.map +1 -0
  53. package/dist/store/array-bindings.js +181 -0
  54. package/dist/store/array-bindings.js.map +1 -0
  55. package/dist/store/bindings.d.ts +12 -0
  56. package/dist/store/bindings.d.ts.map +1 -0
  57. package/dist/store/bindings.js +16 -0
  58. package/dist/store/bindings.js.map +1 -0
  59. package/dist/store/buffer-state.d.ts +42 -0
  60. package/dist/store/buffer-state.d.ts.map +1 -0
  61. package/dist/store/buffer-state.js +51 -0
  62. package/dist/store/buffer-state.js.map +1 -0
  63. package/dist/store/configure.d.ts +19 -0
  64. package/dist/store/configure.d.ts.map +1 -0
  65. package/dist/store/configure.js +28 -0
  66. package/dist/store/configure.js.map +1 -0
  67. package/dist/store/derive.d.ts +106 -0
  68. package/dist/store/derive.d.ts.map +1 -0
  69. package/dist/store/derive.js +417 -0
  70. package/dist/store/derive.js.map +1 -0
  71. package/dist/store/errors.d.ts +20 -0
  72. package/dist/store/errors.d.ts.map +1 -0
  73. package/dist/store/errors.js +101 -0
  74. package/dist/store/errors.js.map +1 -0
  75. package/dist/store/fields.d.ts +35 -0
  76. package/dist/store/fields.d.ts.map +1 -0
  77. package/dist/store/fields.js +81 -0
  78. package/dist/store/fields.js.map +1 -0
  79. package/dist/store/form-channel.d.ts +174 -0
  80. package/dist/store/form-channel.d.ts.map +1 -0
  81. package/dist/store/form-channel.js +411 -0
  82. package/dist/store/form-channel.js.map +1 -0
  83. package/dist/store/gates.d.ts +22 -0
  84. package/dist/store/gates.d.ts.map +1 -0
  85. package/dist/store/gates.js +44 -0
  86. package/dist/store/gates.js.map +1 -0
  87. package/dist/store/ids.d.ts +20 -0
  88. package/dist/store/ids.d.ts.map +1 -0
  89. package/dist/store/ids.js +28 -0
  90. package/dist/store/ids.js.map +1 -0
  91. package/dist/store/inputs.d.ts +114 -0
  92. package/dist/store/inputs.d.ts.map +1 -0
  93. package/dist/store/inputs.js +202 -0
  94. package/dist/store/inputs.js.map +1 -0
  95. package/dist/store/layout.d.ts +81 -0
  96. package/dist/store/layout.d.ts.map +1 -0
  97. package/dist/store/layout.js +285 -0
  98. package/dist/store/layout.js.map +1 -0
  99. package/dist/store/messages.d.ts +56 -0
  100. package/dist/store/messages.d.ts.map +1 -0
  101. package/dist/store/messages.js +157 -0
  102. package/dist/store/messages.js.map +1 -0
  103. package/dist/store/option-pages.d.ts +193 -0
  104. package/dist/store/option-pages.d.ts.map +1 -0
  105. package/dist/store/option-pages.js +598 -0
  106. package/dist/store/option-pages.js.map +1 -0
  107. package/dist/store/parse-print.d.ts +105 -0
  108. package/dist/store/parse-print.d.ts.map +1 -0
  109. package/dist/store/parse-print.js +415 -0
  110. package/dist/store/parse-print.js.map +1 -0
  111. package/dist/store/proposals.d.ts +114 -0
  112. package/dist/store/proposals.d.ts.map +1 -0
  113. package/dist/store/proposals.js +90 -0
  114. package/dist/store/proposals.js.map +1 -0
  115. package/dist/store/publish.d.ts +48 -0
  116. package/dist/store/publish.d.ts.map +1 -0
  117. package/dist/store/publish.js +96 -0
  118. package/dist/store/publish.js.map +1 -0
  119. package/dist/store/snapshots.d.ts +63 -0
  120. package/dist/store/snapshots.d.ts.map +1 -0
  121. package/dist/store/snapshots.js +154 -0
  122. package/dist/store/snapshots.js.map +1 -0
  123. package/dist/store/steps.d.ts +13 -0
  124. package/dist/store/steps.d.ts.map +1 -0
  125. package/dist/store/steps.js +31 -0
  126. package/dist/store/steps.js.map +1 -0
  127. package/dist/store/view.d.ts +124 -0
  128. package/dist/store/view.d.ts.map +1 -0
  129. package/dist/store/view.js +301 -0
  130. package/dist/store/view.js.map +1 -0
  131. package/dist/store/visibility.d.ts +68 -0
  132. package/dist/store/visibility.d.ts.map +1 -0
  133. package/dist/store/visibility.js +231 -0
  134. package/dist/store/visibility.js.map +1 -0
  135. package/dist/store/wizard-cursor.d.ts +43 -0
  136. package/dist/store/wizard-cursor.d.ts.map +1 -0
  137. package/dist/store/wizard-cursor.js +67 -0
  138. package/dist/store/wizard-cursor.js.map +1 -0
  139. package/dist/store/writes.d.ts +50 -0
  140. package/dist/store/writes.d.ts.map +1 -0
  141. package/dist/store/writes.js +91 -0
  142. package/dist/store/writes.js.map +1 -0
  143. package/dist/typed.d.ts +134 -0
  144. package/dist/typed.d.ts.map +1 -0
  145. package/dist/typed.js +74 -0
  146. package/dist/typed.js.map +1 -0
  147. package/dist/useAsyncOptions.d.ts +20 -0
  148. package/dist/useAsyncOptions.d.ts.map +1 -0
  149. package/dist/useAsyncOptions.js +70 -0
  150. package/dist/useAsyncOptions.js.map +1 -0
  151. package/dist/useBufferedInput.d.ts +69 -0
  152. package/dist/useBufferedInput.d.ts.map +1 -0
  153. package/dist/useBufferedInput.js +132 -0
  154. package/dist/useBufferedInput.js.map +1 -0
  155. package/dist/useField.d.ts +42 -0
  156. package/dist/useField.d.ts.map +1 -0
  157. package/dist/useField.js +63 -0
  158. package/dist/useField.js.map +1 -0
  159. package/dist/useFieldArray.d.ts +25 -0
  160. package/dist/useFieldArray.d.ts.map +1 -0
  161. package/dist/useFieldArray.js +18 -0
  162. package/dist/useFieldArray.js.map +1 -0
  163. package/dist/useForm.d.ts +20 -0
  164. package/dist/useForm.d.ts.map +1 -0
  165. package/dist/useForm.js +9 -0
  166. package/dist/useForm.js.map +1 -0
  167. package/dist/useFormState.d.ts +23 -0
  168. package/dist/useFormState.d.ts.map +1 -0
  169. package/dist/useFormState.js +11 -0
  170. package/dist/useFormState.js.map +1 -0
  171. package/dist/useGate.d.ts +15 -0
  172. package/dist/useGate.d.ts.map +1 -0
  173. package/dist/useGate.js +21 -0
  174. package/dist/useGate.js.map +1 -0
  175. package/dist/useLayout.d.ts +29 -0
  176. package/dist/useLayout.d.ts.map +1 -0
  177. package/dist/useLayout.js +59 -0
  178. package/dist/useLayout.js.map +1 -0
  179. package/dist/useMessage.d.ts +58 -0
  180. package/dist/useMessage.d.ts.map +1 -0
  181. package/dist/useMessage.js +62 -0
  182. package/dist/useMessage.js.map +1 -0
  183. package/dist/useWatch.d.ts +19 -0
  184. package/dist/useWatch.d.ts.map +1 -0
  185. package/dist/useWatch.js +17 -0
  186. package/dist/useWatch.js.map +1 -0
  187. package/dist/useWizard.d.ts +166 -0
  188. package/dist/useWizard.d.ts.map +1 -0
  189. package/dist/useWizard.js +554 -0
  190. package/dist/useWizard.js.map +1 -0
  191. package/package.json +62 -3
@@ -0,0 +1,554 @@
1
+ import { useCallback, useLayoutEffect, useSyncExternalStore } from 'react';
2
+ import { assertNever, sameWireValue } from '@azx-pbc/axiom-runtime';
3
+ import { sameMediaLimit, sameSuggestions, sameWireParams } from './store/derive.js';
4
+ import { fieldFacts } from './store/fields.js';
5
+ import { findStepView, wizardSteps } from './store/layout.js';
6
+ import { nextVisibleStepId, previousVisibleStepId } from './store/steps.js';
7
+ import { visitedWith } from './store/wizard-cursor.js';
8
+ import { useFormSession } from './useField.js';
9
+ import { useLayout } from './useLayout.js';
10
+ /**
11
+ * A wizard's cursor and its checkpoints, host-owned: the engine holds no step state.
12
+ *
13
+ * The cursor changes only through the returned setter (which `next` and `previous` use), so a
14
+ * value edit can never move the user to another page. Hidden steps are skipped in both
15
+ * directions: with the middle step hidden, next from step one is step three and previous from
16
+ * step three is step one.
17
+ *
18
+ * A step that becomes hidden after the cursor was placed on it is not rendered: the published
19
+ * cursor reconciles to the nearest visible step (the next one, then the previous, then the
20
+ * first) so a host never renders a blank page. `setStepId` refuses an id the artifact does not
21
+ * declare, and accepts a declared-but-hidden one, which the same reconciliation then moves.
22
+ *
23
+ * The checkpoint facts are derived from the same summary `useFormState` publishes and the same
24
+ * `useStep` blocker filter, so the bar, the footer and the form-level readiness cannot disagree.
25
+ * "Visited" is the session's own history: a step becomes visited when the cursor lands on it,
26
+ * which is why a clean page reads `complete` after the reader leaves it and not before.
27
+ */
28
+ export function useWizard(initialStepId) {
29
+ const views = useLayout();
30
+ const { store, candidate, wizard: cursor } = useFormSession('useWizard');
31
+ // One session cursor, read through the store subscription: every `useWizard` caller in this
32
+ // provider sees the same step and the same visited history, and a move from one moves both.
33
+ const { chosen, visited } = useSyncExternalStore(cursor.subscribe, cursor.getSnapshot, cursor.getSnapshot);
34
+ const steps = wizardSteps(views);
35
+ const stepIds = steps.map((step) => step.stepId);
36
+ const visible = new Set(steps.filter((step) => step.visible).map((step) => step.stepId));
37
+ const visibleSteps = steps.filter((step) => step.visible);
38
+ const visibleStepIds = visibleSteps.map((step) => step.stepId);
39
+ const canSeed = initialStepId !== undefined && stepIds.includes(initialStepId);
40
+ // Seed the shared session cursor once in a layout effect so render stays pure and every
41
+ // `useWizard` caller in the provider observes the same starting step before paint.
42
+ useLayoutEffect(() => {
43
+ if (canSeed && initialStepId !== undefined)
44
+ cursor.seed(initialStepId);
45
+ }, [cursor, canSeed, initialStepId]);
46
+ // Before the layout effect seeds the shared cursor (including during SSR where layout effects
47
+ // do not run), fall back to this caller's declared starting step so server HTML renders the
48
+ // requested page. Once the cursor is seeded (at provider mount via `initialStepId` or after a
49
+ // move), `chosen` is defined and every caller under one provider reads the same session step.
50
+ const chosenStep = chosen !== undefined && stepIds.includes(chosen) ? chosen : undefined;
51
+ const stepId = cursorOf(chosenStep ?? (canSeed ? initialStepId : undefined), stepIds, visible, visibleStepIds);
52
+ const facts = useCheckpointFacts(store, candidate, stepId, visibleSteps);
53
+ const currentFacts = facts.steps.find((step) => step.stepId === stepId);
54
+ const blockedBy = Object.freeze([
55
+ ...(currentFacts?.blockers ?? NO_BLOCKERS).map((blocker) => Object.freeze({ kind: 'field', blocker })),
56
+ ...gateProblemsOf(currentFacts?.gate),
57
+ ]);
58
+ const canAdvance = currentFacts !== undefined && blockedBy.length === 0;
59
+ const checkpoints = Object.freeze(visibleSteps.map((step, index) => {
60
+ const stepFacts = facts.steps[index];
61
+ const problems = stepFacts === undefined ? 0 : stepFacts.blockers.length + gateProblemCount(stepFacts.gate);
62
+ // The cursor's page outranks every other answer. A page the reader has not reached is
63
+ // unvisited even when its required fields are still empty: a checkpoint must not accuse a
64
+ // page nobody has been shown. Once visited, a remaining reason makes it blocked.
65
+ const status = step.stepId === stepId
66
+ ? 'current'
67
+ : !visited.has(step.stepId)
68
+ ? 'unvisited'
69
+ : problems > 0
70
+ ? 'blocked'
71
+ : 'complete';
72
+ return Object.freeze({
73
+ stepId: step.stepId,
74
+ titleKey: step.titleKey,
75
+ index,
76
+ status,
77
+ problemCount: problems,
78
+ });
79
+ }));
80
+ const setStepId = (next) => {
81
+ if (!steps.some((step) => step.stepId === next)) {
82
+ throw new Error(`@azx-pbc/axiom-react: wizard step ${JSON.stringify(next)} is not declared by the layout`);
83
+ }
84
+ cursor.update((state) => {
85
+ const from = state.chosen === undefined
86
+ ? stepId
87
+ : cursorOf(state.chosen, stepIds, visible, visibleStepIds);
88
+ const landed = cursorOf(next, stepIds, visible, visibleStepIds);
89
+ return {
90
+ chosen: next,
91
+ visited: visitedWith(visitedWith(state.visited, from), landed),
92
+ };
93
+ });
94
+ };
95
+ return {
96
+ stepIds,
97
+ visibleStepIds,
98
+ steps: checkpoints,
99
+ stepId,
100
+ canAdvance,
101
+ blockedBy,
102
+ setStepId,
103
+ next: () => {
104
+ // A move from a page that is not clean is refused, not thrown: a Next press is user
105
+ // intent, and the host states the reason. The move itself is computed from the cursor the
106
+ // state holds, so two calls in one handler advance twice rather than both reading the step
107
+ // this render started on. A move that lands nowhere leaves the cursor where it is: the
108
+ // last step has nowhere to go, and clearing it would place the user back on the first step.
109
+ if (!canAdvance)
110
+ return;
111
+ cursor.update((state) => {
112
+ const from = (state.chosen === undefined
113
+ ? stepId
114
+ : cursorOf(state.chosen, stepIds, visible, visibleStepIds)) ?? visibleStepIds[0];
115
+ if (from === undefined)
116
+ return state;
117
+ const to = nextVisibleStepId(stepIds, visible, from) ?? from;
118
+ // The page being left and the page being reached are both in the reader's history. A
119
+ // hidden page is never either one, so it cannot become `complete` by being skipped.
120
+ return { chosen: to, visited: visitedWith(visitedWith(state.visited, from), to) };
121
+ });
122
+ },
123
+ previous: () => {
124
+ cursor.update((state) => {
125
+ const from = state.chosen === undefined
126
+ ? stepId
127
+ : cursorOf(state.chosen, stepIds, visible, visibleStepIds);
128
+ if (from === undefined)
129
+ return state;
130
+ const to = previousVisibleStepId(stepIds, visible, from) ?? from;
131
+ return { chosen: to, visited: visitedWith(visitedWith(state.visited, from), to) };
132
+ });
133
+ },
134
+ nextVisibleStepId: (from) => nextVisibleStepId(stepIds, visible, from),
135
+ previousVisibleStepId: (from) => previousVisibleStepId(stepIds, visible, from),
136
+ };
137
+ }
138
+ /**
139
+ * The per-store cache one wizard's checkpoints read.
140
+ *
141
+ * `useSyncExternalStore` compares by identity, so a publish that moved no checkpoint fact must
142
+ * return the object the previous render held. The key is the visible step sequence; a layout
143
+ * change therefore starts a fresh answer while an edit to another page reuses this one.
144
+ */
145
+ const CHECKPOINT_FACTS = new WeakMap();
146
+ /**
147
+ * Subscribes one wizard to the form channel and publishes only the facts a checkpoint can move.
148
+ *
149
+ * A keystroke that changes a different page's values leaves every step's blockers array and gate
150
+ * view at the same identity (`stepBlockersOf` and `store.gateView` both preserve it), so the
151
+ * snapshot is reused and the consumer does not render. That is the same discipline `useStep`
152
+ * keeps for one step, applied to the whole sequence without calling a hook per step.
153
+ */
154
+ function useCheckpointFacts(store, candidate, stepId, visibleSteps) {
155
+ const subscribe = useCallback((onStoreChange) => store.subscribeForm(onStoreChange), [store]);
156
+ const key = `${stepId ?? ''}|${visibleSteps.map((step) => step.stepId).join(',')}`;
157
+ const getSnapshot = useCallback(() => checkpointFactsOf(store, candidate, visibleSteps, key, stepId), [store, candidate, visibleSteps, key, stepId]);
158
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
159
+ }
160
+ /** One render's checkpoint answer, reusing the previous object when no step fact moved. */
161
+ function checkpointFactsOf(store, candidate, visibleSteps, key, stepId) {
162
+ const current = store.isCurrentCandidate(candidate) ? candidate : undefined;
163
+ const summary = current === undefined ? store.getFormSummary() : store.getCandidateFormSummary(current);
164
+ const steps = visibleSteps.map((step) => Object.freeze({
165
+ stepId: step.stepId,
166
+ blockers: stepBlockersOf(store, step, summary),
167
+ gate: step.gateId === undefined ? undefined : store.gateView(current, step.gateId),
168
+ }));
169
+ const currentFacts = steps.find((step) => step.stepId === stepId);
170
+ const canAdvance = currentFacts !== undefined &&
171
+ currentFacts.blockers.length === 0 &&
172
+ gateProblemCount(currentFacts.gate) === 0;
173
+ let byKey = CHECKPOINT_FACTS.get(store);
174
+ if (byKey === undefined) {
175
+ byKey = new Map();
176
+ CHECKPOINT_FACTS.set(store, byKey);
177
+ }
178
+ const previous = byKey.get(key);
179
+ if (previous !== undefined &&
180
+ previous.facts.steps.length === steps.length &&
181
+ previous.facts.steps.every((step, index) => {
182
+ const next = steps[index];
183
+ return (next !== undefined &&
184
+ step.stepId === next.stepId &&
185
+ step.blockers === next.blockers &&
186
+ step.gate === next.gate);
187
+ }) &&
188
+ previous.facts.canAdvance === canAdvance) {
189
+ return previous.facts;
190
+ }
191
+ const facts = Object.freeze({
192
+ steps: Object.freeze(steps),
193
+ canAdvance,
194
+ });
195
+ byKey.set(key, { facts });
196
+ return facts;
197
+ }
198
+ const NO_GATE_PROBLEMS = Object.freeze([]);
199
+ /** How many reasons a step's own gate states; an open or absent gate states none. */
200
+ function gateProblemCount(gate) {
201
+ if (gate === undefined || gate.open)
202
+ return 0;
203
+ const failed = gate.items.filter((item) => item.status === 'FAIL').length;
204
+ return failed === 0 ? 1 : failed;
205
+ }
206
+ /**
207
+ * A step's own gate as reasons.
208
+ *
209
+ * A gate names no field, so it can never be one of a step's `StepBlocker`s; a step that declares
210
+ * the gate still owns its verdict. An open gate states nothing. A closed gate states each item
211
+ * that failed, and a closed gate with no failed item (an active gate the engine refused) states
212
+ * one reason under the gate's own id rather than reporting a block with no cause.
213
+ */
214
+ function gateProblemsOf(gate) {
215
+ if (gate === undefined || gate.open)
216
+ return NO_GATE_PROBLEMS;
217
+ const failed = gate.items.filter((item) => item.status === 'FAIL');
218
+ if (failed.length === 0) {
219
+ return Object.freeze([
220
+ Object.freeze({
221
+ kind: 'gate',
222
+ gateId: gate.gateId,
223
+ itemId: gate.gateId,
224
+ messageKey: gate.gateId,
225
+ }),
226
+ ]);
227
+ }
228
+ return Object.freeze(failed.map((item) => Object.freeze({
229
+ kind: 'gate',
230
+ gateId: gate.gateId,
231
+ itemId: item.itemId,
232
+ messageKey: item.messageKey,
233
+ ...(item.params === undefined ? {} : { params: item.params }),
234
+ })));
235
+ }
236
+ /**
237
+ * The published cursor: the chosen step when it still shows, else the nearest visible one.
238
+ *
239
+ * A hidden cursor moves forward first, so the flow continues where it was heading; a hidden
240
+ * last step falls back to the step before it, and a cursor that names no declared step starts
241
+ * from the first visible one.
242
+ */
243
+ function cursorOf(chosen, stepIds, visible, visibleStepIds) {
244
+ const first = visibleStepIds[0];
245
+ if (chosen === undefined)
246
+ return first;
247
+ if (visible.has(chosen))
248
+ return chosen;
249
+ if (!stepIds.includes(chosen))
250
+ return first;
251
+ return (nextVisibleStepId(stepIds, visible, chosen) ??
252
+ previousVisibleStepId(stepIds, visible, chosen) ??
253
+ first);
254
+ }
255
+ const NO_BLOCKERS = Object.freeze([]);
256
+ /**
257
+ * One wizard step, by the id the layout states.
258
+ *
259
+ * `blockers` and `ready` are derived from the same summary `useFormState` publishes and the
260
+ * same gate `useGate` presents, so a page cannot disagree with the form-level badge: the list
261
+ * is `summary.blockers` filtered by the fields this step's subtree places, never a second
262
+ * readiness rule. The placement is static per artifact, so it is read once; the filter walks
263
+ * only the fields in this step, and the per-summary index and per-step answer are memoised
264
+ * against the summary object, so an unrelated keystroke costs no scan and a publish that moved
265
+ * no summary fact reuses both.
266
+ *
267
+ * The gate is read from the same derived state `useGate` presents, so a step's gate and the
268
+ * form-level badge cannot disagree. The step's own visibility is the presentation channel's
269
+ * answer: a hidden step is skipped by the wizard's moves and never rendered by a host that
270
+ * checks it, and a hidden page reports `ready: false` with no blockers because there is no
271
+ * page to leave.
272
+ */
273
+ export function useStep(stepId) {
274
+ const views = useLayout();
275
+ const { store, candidate } = useFormSession('useStep');
276
+ const steps = wizardSteps(views);
277
+ const index = steps.findIndex((step) => step.stepId === stepId);
278
+ const step = findStepView(views, stepId);
279
+ const gateId = step?.gateId;
280
+ const visible = step?.visible ?? false;
281
+ const subscribe = useCallback((onStoreChange) => store.subscribeLayout(onStoreChange), [store]);
282
+ const getSnapshot = useCallback(() => snapshotOf(store, candidate, stepId, step, visible, gateId), [store, candidate, stepId, step, visible, gateId]);
283
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
284
+ return {
285
+ stepId,
286
+ visible,
287
+ index,
288
+ gateId,
289
+ gate: snapshot.gate,
290
+ blockers: snapshot.blockers,
291
+ ready: snapshot.ready,
292
+ };
293
+ }
294
+ const STEP_SNAPSHOTS = new WeakMap();
295
+ /**
296
+ * One step's published answer, reading the summary and gate from the store the way
297
+ * `useFormState` and `useGate` do.
298
+ *
299
+ * `useSyncExternalStore` compares by identity, so a publish that moved neither this step's
300
+ * blockers nor its gate returns the object the previous render already held; the component is
301
+ * not rendered again, exactly as the layout channel keeps its identity across an edit it does
302
+ * not read. The blockers array is itself borrowed from the previous summary when it states the
303
+ * same reasons, so an edit that changes another page's blockers cannot re-render this one.
304
+ */
305
+ function snapshotOf(store, candidate, stepId, step, visible, gateId) {
306
+ const current = store.isCurrentCandidate(candidate) ? candidate : undefined;
307
+ const summary = current === undefined ? store.getFormSummary() : store.getCandidateFormSummary(current);
308
+ const gate = gateId === undefined ? undefined : store.gateView(current, gateId);
309
+ const blockers = visible && step !== undefined ? stepBlockersOf(store, step, summary) : NO_BLOCKERS;
310
+ const ready = visible && blockers.length === 0 && (gate === undefined || gate.open);
311
+ let byStep = STEP_SNAPSHOTS.get(store);
312
+ if (byStep === undefined) {
313
+ byStep = new Map();
314
+ STEP_SNAPSHOTS.set(store, byStep);
315
+ }
316
+ const previous = byStep.get(stepId);
317
+ if (previous !== undefined &&
318
+ previous.blockers === blockers &&
319
+ previous.gate === gate &&
320
+ previous.visible === visible &&
321
+ previous.ready === ready) {
322
+ return previous.snapshot;
323
+ }
324
+ const snapshot = Object.freeze({ gate, blockers, ready, visible });
325
+ byStep.set(stepId, { blockers, gate, visible, ready, snapshot });
326
+ return snapshot;
327
+ }
328
+ /**
329
+ * The static field set of each step, keyed by artifact and step id.
330
+ *
331
+ * Placement is a declaration fact, not a per-pass answer: `visibleWhen` moves a step's chrome,
332
+ * never which fields it holds. The set therefore lives as long as the compiled artifact.
333
+ */
334
+ const PLACED_FIELDS = new WeakMap();
335
+ /** One summary's blockers grouped by field, built once per summary rather than once per step. */
336
+ const BLOCKER_INDEX = new WeakMap();
337
+ /** The per-step answer, so a step re-read during a render does not filter again. */
338
+ const STEP_BLOCKERS = new WeakMap();
339
+ /**
340
+ * The last answer each step stated, so a new summary that repeats it keeps the same array.
341
+ *
342
+ * `buildFormSummary` copies its blocker entries, so two summaries that state the same reasons
343
+ * hold different objects. Comparing the reasons here is what lets `useSyncExternalStore` see one
344
+ * answer: an edit on another page then leaves this step unrendered.
345
+ */
346
+ const LAST_STEP_BLOCKERS = new WeakMap();
347
+ /**
348
+ * The summary entries whose field this step places.
349
+ *
350
+ * The step's placed leaves are expanded through their declarations, so a member of a placed
351
+ * array or record is attributed to the page that painted the container. The index is built
352
+ * once per summary; this then reads only the fields this step holds, in summary order.
353
+ */
354
+ function stepBlockersOf(store, step, summary) {
355
+ let bySummary = STEP_BLOCKERS.get(store);
356
+ if (bySummary === undefined) {
357
+ bySummary = new WeakMap();
358
+ STEP_BLOCKERS.set(store, bySummary);
359
+ }
360
+ let byStep = bySummary.get(summary);
361
+ if (byStep === undefined) {
362
+ byStep = new Map();
363
+ bySummary.set(summary, byStep);
364
+ }
365
+ const cached = byStep.get(step.stepId);
366
+ if (cached !== undefined)
367
+ return cached;
368
+ const index = blockerIndex(summary);
369
+ const collected = [];
370
+ for (const fieldId of placedFieldIds(store.compiled, step)) {
371
+ const entries = index.get(fieldId);
372
+ if (entries === undefined)
373
+ continue;
374
+ for (const entry of entries)
375
+ collected.push(entry);
376
+ }
377
+ collected.sort((left, right) => left.order - right.order);
378
+ const next = collected.map((entry) => entry.blocker);
379
+ const last = lastBlockersOf(store);
380
+ const previous = last.get(step.stepId);
381
+ const blockers = previous !== undefined && sameStepBlockers(previous, next) ? previous : Object.freeze(next);
382
+ last.set(step.stepId, blockers);
383
+ byStep.set(step.stepId, blockers);
384
+ return blockers;
385
+ }
386
+ function lastBlockersOf(store) {
387
+ const cached = LAST_STEP_BLOCKERS.get(store);
388
+ if (cached !== undefined)
389
+ return cached;
390
+ const created = new Map();
391
+ LAST_STEP_BLOCKERS.set(store, created);
392
+ return created;
393
+ }
394
+ /** Whether two step blocker lists state the same reasons, so a new summary may reuse the array. */
395
+ function sameStepBlockers(left, right) {
396
+ return (left.length === right.length &&
397
+ left.every((blocker, index) => {
398
+ const other = right[index];
399
+ return other !== undefined && sameStepBlocker(blocker, other);
400
+ }));
401
+ }
402
+ function sameStepBlocker(left, right) {
403
+ if (left.kind !== right.kind)
404
+ return false;
405
+ switch (left.kind) {
406
+ case 'rule':
407
+ return (right.kind === 'rule' &&
408
+ left.ruleId === right.ruleId &&
409
+ left.fieldId === right.fieldId &&
410
+ left.rowKey === right.rowKey &&
411
+ left.messageKey === right.messageKey &&
412
+ sameWireParams(left.params, right.params) &&
413
+ sameSuggestions(left.suggestions, right.suggestions));
414
+ case 'required':
415
+ return (right.kind === 'required' &&
416
+ left.fieldId === right.fieldId &&
417
+ left.rowKey === right.rowKey &&
418
+ left.messageKey === right.messageKey &&
419
+ sameWireParams(left.params, right.params));
420
+ case 'cardinality':
421
+ return (right.kind === 'cardinality' &&
422
+ left.fieldId === right.fieldId &&
423
+ left.bound === right.bound &&
424
+ left.count === right.count &&
425
+ left.limit === right.limit);
426
+ case 'media':
427
+ return (right.kind === 'media' &&
428
+ left.fieldId === right.fieldId &&
429
+ left.member === right.member &&
430
+ left.value === right.value &&
431
+ left.index === right.index &&
432
+ left.rowKey === right.rowKey &&
433
+ sameMediaLimit(left.limit, right.limit));
434
+ case 'rowCount':
435
+ return (right.kind === 'rowCount' &&
436
+ left.fieldId === right.fieldId &&
437
+ left.expected === right.expected &&
438
+ left.actual === right.actual &&
439
+ left.source === right.source &&
440
+ left.reason === right.reason &&
441
+ left.ceiling === right.ceiling);
442
+ case 'invalidOption':
443
+ return (right.kind === 'invalidOption' &&
444
+ left.fieldId === right.fieldId &&
445
+ left.rowKey === right.rowKey &&
446
+ sameWireValue(left.value, right.value));
447
+ case 'unavailableOption':
448
+ return (right.kind === 'unavailableOption' &&
449
+ left.fieldId === right.fieldId &&
450
+ left.rowKey === right.rowKey &&
451
+ left.code === right.code &&
452
+ left.messageKey === right.messageKey &&
453
+ sameWireParams(left.params, right.params) &&
454
+ sameSuggestions(left.suggestions, right.suggestions));
455
+ default:
456
+ return assertNever(left);
457
+ }
458
+ }
459
+ /** One summary's field-named blockers, in summary order, indexed by the field they name. */
460
+ function blockerIndex(summary) {
461
+ const cached = BLOCKER_INDEX.get(summary);
462
+ if (cached !== undefined)
463
+ return cached;
464
+ const mutable = new Map();
465
+ for (const [order, blocker] of summary.blockers.entries()) {
466
+ if (!isStepBlocker(blocker))
467
+ continue;
468
+ const entries = mutable.get(blocker.fieldId);
469
+ const entry = Object.freeze({ blocker, order });
470
+ if (entries === undefined)
471
+ mutable.set(blocker.fieldId, [entry]);
472
+ else
473
+ entries.push(entry);
474
+ }
475
+ const index = new Map();
476
+ for (const [fieldId, entries] of mutable)
477
+ index.set(fieldId, Object.freeze(entries));
478
+ BLOCKER_INDEX.set(summary, index);
479
+ return index;
480
+ }
481
+ /** Whether a summary blocker names the field a step must hold to own it. */
482
+ function isStepBlocker(blocker) {
483
+ switch (blocker.kind) {
484
+ case 'rule':
485
+ case 'required':
486
+ case 'cardinality':
487
+ case 'scale':
488
+ case 'media':
489
+ case 'rowCount':
490
+ case 'invalidOption':
491
+ case 'unavailableOption':
492
+ return blocker.fieldId !== undefined;
493
+ case 'gate':
494
+ case 'screen':
495
+ return false;
496
+ default:
497
+ return assertNever(blocker);
498
+ }
499
+ }
500
+ /**
501
+ * Every field a step places, and every member those declarations hold.
502
+ *
503
+ * The walk is a step's own subtree, so the cost is the step rather than the report.
504
+ */
505
+ function placedFieldIds(compiled, step) {
506
+ let byStep = PLACED_FIELDS.get(compiled);
507
+ if (byStep === undefined) {
508
+ byStep = new Map();
509
+ PLACED_FIELDS.set(compiled, byStep);
510
+ }
511
+ const cached = byStep.get(step.stepId);
512
+ if (cached !== undefined)
513
+ return cached;
514
+ const facts = fieldFacts(compiled);
515
+ const ids = [];
516
+ const seen = new Set();
517
+ const addDeclaration = (fieldId) => {
518
+ if (seen.has(fieldId))
519
+ return;
520
+ seen.add(fieldId);
521
+ ids.push(fieldId);
522
+ for (const member of facts.get(fieldId)?.field.fields ?? [])
523
+ addDeclaration(member.fieldId);
524
+ };
525
+ for (const child of step.children)
526
+ visitPlacedFields(child, addDeclaration);
527
+ const placed = Object.freeze(ids);
528
+ byStep.set(step.stepId, placed);
529
+ return placed;
530
+ }
531
+ /** Calls `visit` for every field leaf a layout node places, depth first. */
532
+ function visitPlacedFields(node, visit) {
533
+ switch (node.kind) {
534
+ case 'field':
535
+ visit(node.fieldId);
536
+ return;
537
+ case 'section':
538
+ case 'group':
539
+ case 'step':
540
+ case 'wizard':
541
+ for (const child of node.children)
542
+ visitPlacedFields(child, visit);
543
+ return;
544
+ case 'heading':
545
+ case 'callout':
546
+ case 'divider':
547
+ case 'sop_block':
548
+ case 'summary_card':
549
+ return;
550
+ default:
551
+ return assertNever(node);
552
+ }
553
+ }
554
+ //# sourceMappingURL=useWizard.js.map