@dreamboard-games/ui-sdk 0.0.42 → 0.0.45

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 (166) hide show
  1. package/dist/components/ActionButton.d.ts.map +1 -1
  2. package/dist/components/ActionButton.js +2 -1
  3. package/dist/components/Card.d.ts +1 -1
  4. package/dist/components/Card.d.ts.map +1 -1
  5. package/dist/components/DiceRoller.d.ts +3 -2
  6. package/dist/components/DiceRoller.d.ts.map +1 -1
  7. package/dist/components/DiceRoller.js +4 -13
  8. package/dist/components/ErrorBoundary.d.ts.map +1 -1
  9. package/dist/components/ErrorBoundary.js +94 -2
  10. package/dist/components/InteractionForm.d.ts +1 -1
  11. package/dist/components/InteractionForm.d.ts.map +1 -1
  12. package/dist/components/InteractionForm.js +29 -15
  13. package/dist/components/PrimaryActionButton.d.ts.map +1 -1
  14. package/dist/components/PrimaryActionButton.js +7 -6
  15. package/dist/components/ResourceCounter.d.ts +59 -25
  16. package/dist/components/ResourceCounter.d.ts.map +1 -1
  17. package/dist/components/ResourceCounter.js +106 -115
  18. package/dist/components/Toast.d.ts +13 -6
  19. package/dist/components/Toast.d.ts.map +1 -1
  20. package/dist/components/Toast.js +10 -5
  21. package/dist/components/board/HexGrid.js +6 -6
  22. package/dist/components/board/target-layer.d.ts +18 -2
  23. package/dist/components/board/target-layer.d.ts.map +1 -1
  24. package/dist/components/board/target-layer.js +20 -3
  25. package/dist/components/index.d.ts +3 -4
  26. package/dist/components/index.d.ts.map +1 -1
  27. package/dist/components/index.js +3 -4
  28. package/dist/components/surfaces/InboxSurface.d.ts.map +1 -1
  29. package/dist/components/surfaces/InboxSurface.js +2 -6
  30. package/dist/components/surfaces/PlayerCardsSurface.js +2 -2
  31. package/dist/components/surfaces/internal/CardZoneRoutedForm.d.ts +7 -0
  32. package/dist/components/surfaces/internal/CardZoneRoutedForm.d.ts.map +1 -0
  33. package/dist/components/surfaces/internal/CardZoneRoutedForm.js +9 -0
  34. package/dist/components/surfaces/internal/DefaultInteractionButton.d.ts.map +1 -1
  35. package/dist/components/surfaces/internal/DefaultInteractionButton.js +5 -8
  36. package/dist/components/surfaces/internal/useCardZoneInteractions.d.ts +2 -2
  37. package/dist/components/surfaces/internal/useCardZoneInteractions.d.ts.map +1 -1
  38. package/dist/components/surfaces/internal/useCardZoneInteractions.js +19 -43
  39. package/dist/context/InteractionDraftContext.d.ts +11 -2
  40. package/dist/context/InteractionDraftContext.d.ts.map +1 -1
  41. package/dist/context/InteractionDraftContext.js +41 -4
  42. package/dist/defaults/components.d.ts +0 -5
  43. package/dist/defaults/components.d.ts.map +1 -1
  44. package/dist/defaults/components.js +7 -11
  45. package/dist/hooks/useBoardInteractions.d.ts +35 -12
  46. package/dist/hooks/useBoardInteractions.d.ts.map +1 -1
  47. package/dist/hooks/useBoardInteractions.js +186 -82
  48. package/dist/hooks/useInteractionHandle.d.ts +1 -1
  49. package/dist/hooks/useInteractionHandle.d.ts.map +1 -1
  50. package/dist/hooks/useInteractionHandle.js +12 -27
  51. package/dist/index.d.ts +11 -17
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +5 -14
  54. package/dist/primitives/board.d.ts +53 -3
  55. package/dist/primitives/board.d.ts.map +1 -1
  56. package/dist/primitives/board.js +65 -41
  57. package/dist/primitives/dialog-lifecycle.d.ts +17 -0
  58. package/dist/primitives/dialog-lifecycle.d.ts.map +1 -0
  59. package/dist/primitives/dialog-lifecycle.js +24 -0
  60. package/dist/primitives/dice.d.ts +31 -0
  61. package/dist/primitives/dice.d.ts.map +1 -0
  62. package/dist/primitives/dice.js +33 -0
  63. package/dist/primitives/game.d.ts +55 -0
  64. package/dist/primitives/game.d.ts.map +1 -0
  65. package/dist/primitives/game.js +101 -0
  66. package/dist/primitives/index.d.ts +7 -4
  67. package/dist/primitives/index.d.ts.map +1 -1
  68. package/dist/primitives/index.js +7 -4
  69. package/dist/primitives/interaction-form-binding.d.ts +12 -0
  70. package/dist/primitives/interaction-form-binding.d.ts.map +1 -0
  71. package/dist/primitives/interaction-form-binding.js +14 -0
  72. package/dist/primitives/interaction-submit.d.ts +23 -0
  73. package/dist/primitives/interaction-submit.d.ts.map +1 -0
  74. package/dist/primitives/interaction-submit.js +41 -0
  75. package/dist/primitives/interaction.d.ts +76 -6
  76. package/dist/primitives/interaction.d.ts.map +1 -1
  77. package/dist/primitives/interaction.js +210 -26
  78. package/dist/primitives/player-roster.d.ts +2 -1
  79. package/dist/primitives/player-roster.d.ts.map +1 -1
  80. package/dist/primitives/prompt.d.ts +36 -11
  81. package/dist/primitives/prompt.d.ts.map +1 -1
  82. package/dist/primitives/prompt.js +29 -17
  83. package/dist/primitives/ui.d.ts +9 -0
  84. package/dist/primitives/ui.d.ts.map +1 -0
  85. package/dist/primitives/ui.js +7 -0
  86. package/dist/primitives/zone.d.ts +111 -5
  87. package/dist/primitives/zone.d.ts.map +1 -1
  88. package/dist/primitives/zone.js +349 -9
  89. package/dist/reducer.d.ts +2 -14
  90. package/dist/reducer.d.ts.map +1 -1
  91. package/dist/reducer.js +1 -14
  92. package/dist/runtime/createPluginRuntimeAPI.js +1 -1
  93. package/dist/types/hex-color.d.ts +7 -0
  94. package/dist/types/hex-color.d.ts.map +1 -0
  95. package/dist/types/hex-color.js +13 -0
  96. package/dist/types/player-state.d.ts +28 -14
  97. package/dist/types/player-state.d.ts.map +1 -1
  98. package/dist/types/plugin-state.d.ts +9 -3
  99. package/dist/types/plugin-state.d.ts.map +1 -1
  100. package/dist/ui-contract.d.ts +119 -14
  101. package/dist/ui-contract.d.ts.map +1 -1
  102. package/dist/ui-contract.js +4 -3
  103. package/dist/ui-sdk.d.ts +1637 -1245
  104. package/dist/utils/interaction-inputs.d.ts +8 -5
  105. package/dist/utils/interaction-inputs.d.ts.map +1 -1
  106. package/dist/utils/interaction-inputs.js +82 -14
  107. package/dist/utils/interaction-router.d.ts +31 -0
  108. package/dist/utils/interaction-router.d.ts.map +1 -0
  109. package/dist/utils/interaction-router.js +114 -0
  110. package/package.json +2 -2
  111. package/src/components/ActionButton.tsx +2 -1
  112. package/src/components/Card.tsx +1 -1
  113. package/src/components/DiceRoller.tsx +13 -22
  114. package/src/components/ErrorBoundary.test.tsx +19 -0
  115. package/src/components/ErrorBoundary.tsx +113 -24
  116. package/src/components/InteractionForm.test.tsx +24 -0
  117. package/src/components/InteractionForm.tsx +48 -23
  118. package/src/components/PrimaryActionButton.tsx +19 -5
  119. package/src/components/ResourceCounter.test.tsx +13 -13
  120. package/src/components/ResourceCounter.tsx +238 -244
  121. package/src/components/Toast.tsx +23 -10
  122. package/src/components/__fixtures__/ResourceCounter.fixture.tsx +70 -169
  123. package/src/components/board/HexGrid.tsx +6 -6
  124. package/src/components/board/target-layer.ts +44 -5
  125. package/src/components/index.ts +17 -10
  126. package/src/components/surfaces/InboxSurface.tsx +7 -5
  127. package/src/components/surfaces/PlayerCardsSurface.tsx +6 -6
  128. package/src/components/surfaces/internal/CardZoneRoutedForm.tsx +35 -0
  129. package/src/components/surfaces/internal/DefaultInteractionButton.tsx +17 -7
  130. package/src/components/surfaces/internal/useCardZoneInteractions.ts +25 -67
  131. package/src/context/InteractionDraftContext.tsx +51 -5
  132. package/src/defaults/components.tsx +12 -50
  133. package/src/defaults/defaults.test.tsx +1 -50
  134. package/src/hooks/useBoardInteractions.test.tsx +240 -17
  135. package/src/hooks/useBoardInteractions.ts +330 -105
  136. package/src/hooks/useInteractionHandle.ts +23 -28
  137. package/src/index.test.ts +60 -40
  138. package/src/index.ts +30 -36
  139. package/src/primitives/board.test.tsx +73 -0
  140. package/src/primitives/board.tsx +191 -40
  141. package/src/primitives/dialog-lifecycle.ts +58 -0
  142. package/src/primitives/dice.test.tsx +47 -0
  143. package/src/primitives/dice.tsx +79 -0
  144. package/src/primitives/game.test.tsx +98 -0
  145. package/src/primitives/game.tsx +213 -0
  146. package/src/primitives/index.ts +84 -0
  147. package/src/primitives/interaction-form-binding.tsx +56 -0
  148. package/src/primitives/interaction-submit.ts +90 -0
  149. package/src/primitives/interaction.test.tsx +396 -0
  150. package/src/primitives/interaction.tsx +451 -31
  151. package/src/primitives/player-roster.tsx +2 -1
  152. package/src/primitives/prompt.test.tsx +94 -3
  153. package/src/primitives/prompt.tsx +87 -48
  154. package/src/primitives/ui.test.tsx +131 -0
  155. package/src/primitives/ui.tsx +13 -0
  156. package/src/primitives/zone.test.tsx +305 -0
  157. package/src/primitives/zone.tsx +660 -12
  158. package/src/reducer.ts +7 -20
  159. package/src/runtime/createPluginRuntimeAPI.ts +1 -1
  160. package/src/types/hex-color.ts +20 -0
  161. package/src/types/player-state.ts +36 -18
  162. package/src/types/plugin-state.ts +10 -3
  163. package/src/ui-contract.ts +253 -21
  164. package/src/utils/interaction-inputs.test.ts +400 -0
  165. package/src/utils/interaction-inputs.ts +113 -11
  166. package/src/utils/interaction-router.ts +200 -0
@@ -1,8 +1,36 @@
1
- import { createContext, useContext, useMemo, type HTMLAttributes } from "react";
1
+ import {
2
+ createContext,
3
+ useContext,
4
+ useMemo,
5
+ type ButtonHTMLAttributes,
6
+ type HTMLAttributes,
7
+ type ReactNode,
8
+ } from "react";
9
+ import type { ViewCard } from "@dreamboard/sdk-types";
10
+ import { useInteractionUiStore } from "../context/InteractionDraftContext.js";
11
+ import { usePluginSession } from "../context/PluginSessionContext.js";
2
12
  import { usePluginState } from "../context/PluginStateContext.js";
3
- import type { ZoneHandlesSnapshot } from "../types/plugin-state.js";
13
+ import { useRuntimeContext } from "../context/RuntimeContext.js";
14
+ import type {
15
+ InteractionDescriptor,
16
+ ZoneHandlesSnapshot,
17
+ } from "../types/plugin-state.js";
4
18
  import type { ZoneKey } from "../ui-contract.js";
5
19
  import {
20
+ inputByTarget,
21
+ interactionInputKeys,
22
+ } from "../utils/interaction-inputs.js";
23
+ import {
24
+ claimInteractionSubmit,
25
+ clearInteractionRoute,
26
+ markInteractionPending,
27
+ routeInteractionTarget,
28
+ shouldRouteInteractionPending,
29
+ } from "../utils/interaction-router.js";
30
+ import { useGameActionError } from "./game.js";
31
+ import { runInteractionAction } from "./interaction-submit.js";
32
+ import {
33
+ composeEventHandlers,
6
34
  renderPrimitive,
7
35
  type PrimitiveCommonProps,
8
36
  } from "./primitive-props.js";
@@ -17,8 +45,61 @@ interface ZoneCardContextValue {
17
45
  cardId: string;
18
46
  }
19
47
 
48
+ /**
49
+ * An item rendered by a zone primitive. Discriminated by `hidden`:
50
+ *
51
+ * - `hidden: false` — fully hydrated from the zone projection. Carries the
52
+ * authored card type, properties, and reducer-projected interaction state.
53
+ * - `hidden: true` — the zone snapshot exposes the card id but withholds its
54
+ * contents (e.g. opponent zones, or a zone that is projected count-only).
55
+ * The render contract is honest about not knowing the type; authors must
56
+ * narrow on `hidden` before reading `cardType` / `properties`.
57
+ *
58
+ * Replaces the previous silent fallback that widened `cardType` to the
59
+ * untyped string `"unknown"` — see SDK Design Principles §2 (strong contracts
60
+ * over comments).
61
+ */
62
+ export type ZoneCardRenderItem<
63
+ CardIdValue extends string = string,
64
+ CardTypeValue extends string = string,
65
+ Properties extends Record<string, unknown> = Record<string, unknown>,
66
+ > =
67
+ | HydratedZoneCardRenderItem<CardIdValue, CardTypeValue, Properties>
68
+ | HiddenZoneCardRenderItem<CardIdValue>;
69
+
70
+ export interface HydratedZoneCardRenderItem<
71
+ CardIdValue extends string = string,
72
+ CardTypeValue extends string = string,
73
+ Properties extends Record<string, unknown> = Record<string, unknown>,
74
+ > extends ViewCard<CardIdValue, CardTypeValue, Properties> {
75
+ zone: string;
76
+ index: number;
77
+ hidden: false;
78
+ playable: boolean;
79
+ interactions: readonly InteractionDescriptor[];
80
+ }
81
+
82
+ export interface HiddenZoneCardRenderItem<CardIdValue extends string = string> {
83
+ id: CardIdValue;
84
+ zone: string;
85
+ index: number;
86
+ hidden: true;
87
+ }
88
+
89
+ export interface ZonePileContextValue {
90
+ zone: string;
91
+ label: string;
92
+ count: number;
93
+ cards: readonly string[];
94
+ items: readonly ZoneCardRenderItem[];
95
+ hasVisibleCards: boolean;
96
+ isHidden: boolean;
97
+ description: string | null;
98
+ }
99
+
20
100
  const ZoneContext = createContext<ZoneContextValue | null>(null);
21
101
  const ZoneCardContext = createContext<ZoneCardContextValue | null>(null);
102
+ const ZonePileContext = createContext<ZonePileContextValue | null>(null);
22
103
 
23
104
  export function useZonePrimitiveContext(): ZoneContextValue {
24
105
  const value = useContext(ZoneContext);
@@ -28,10 +109,24 @@ export function useZonePrimitiveContext(): ZoneContextValue {
28
109
  return value;
29
110
  }
30
111
 
112
+ export function useOptionalZonePrimitiveContext(): ZoneContextValue | null {
113
+ return useContext(ZoneContext);
114
+ }
115
+
31
116
  export function useZoneCardContext(): ZoneCardContextValue | null {
32
117
  return useContext(ZoneCardContext);
33
118
  }
34
119
 
120
+ export function useZonePileContext(): ZonePileContextValue {
121
+ const value = useContext(ZonePileContext);
122
+ if (!value) {
123
+ throw new Error(
124
+ "Zone pile primitives must be rendered inside <Zone.PileRoot>.",
125
+ );
126
+ }
127
+ return value;
128
+ }
129
+
35
130
  export interface ZoneRootProps<Zone extends string = ZoneKey>
36
131
  extends PrimitiveCommonProps,
37
132
  HTMLAttributes<HTMLElement> {
@@ -62,33 +157,63 @@ export function ZoneRoot<Zone extends string = ZoneKey>({
62
157
  );
63
158
  }
64
159
 
65
- export type ZoneListProps = PrimitiveCommonProps & HTMLAttributes<HTMLElement>;
160
+ export interface ZoneListProps
161
+ extends Omit<PrimitiveCommonProps, "children">,
162
+ Omit<HTMLAttributes<HTMLElement>, "children"> {
163
+ children?: ReactNode | ((card: ZoneCardRenderItem) => ReactNode);
164
+ empty?: ReactNode;
165
+ sort?: (a: ZoneCardRenderItem, b: ZoneCardRenderItem) => number;
166
+ }
66
167
 
67
- export function ZoneList({ children, ...props }: ZoneListProps) {
168
+ export function ZoneList({ children, empty, sort, ...props }: ZoneListProps) {
68
169
  const { zone, snapshot } = useZonePrimitiveContext();
170
+ const cards = (snapshot?.cardIds ?? []).map((cardId, index) =>
171
+ createZoneCardRenderItem(zone, snapshot, cardId, index),
172
+ );
173
+ const items = sort ? [...cards].sort(sort) : cards;
69
174
  const isEmpty = (snapshot?.cardIds.length ?? 0) === 0;
175
+ const renderedChildren =
176
+ typeof children === "function"
177
+ ? isEmpty
178
+ ? empty
179
+ : items.map((item) => (
180
+ <ZoneItem key={item.id} card={item}>
181
+ {children(item)}
182
+ </ZoneItem>
183
+ ))
184
+ : isEmpty && empty !== undefined
185
+ ? empty
186
+ : children;
70
187
  return renderPrimitive("div", {
71
188
  ...props,
72
189
  role: props.role ?? "list",
73
190
  "data-dreamboard-zone-list": "",
74
191
  "data-zone": zone,
75
192
  "data-empty": isEmpty || undefined,
76
- children,
193
+ children: renderedChildren,
77
194
  });
78
195
  }
79
196
 
80
197
  export interface ZoneItemProps
81
198
  extends PrimitiveCommonProps,
82
199
  HTMLAttributes<HTMLElement> {
83
- card: string;
200
+ card: string | ZoneCardRenderItem;
84
201
  }
85
202
 
86
203
  export function ZoneItem({ card, children, ...props }: ZoneItemProps) {
87
204
  const { zone, snapshot } = useZonePrimitiveContext();
88
- const playable = snapshot?.playableByCardId[card] ?? [];
205
+ const item =
206
+ typeof card === "string"
207
+ ? createZoneCardRenderItem(
208
+ zone,
209
+ snapshot,
210
+ card,
211
+ indexOfCard(snapshot, card),
212
+ )
213
+ : card;
89
214
  const cardContext = useMemo<ZoneCardContextValue>(
90
- () => ({ zone, cardId: card }),
91
- [card, zone],
215
+ () => ({ zone, cardId: item.id }),
216
+ [item.id, zone],
92
217
  );
93
218
  return (
94
219
  <ZoneCardContext.Provider value={cardContext}>
@@ -97,17 +222,540 @@ export function ZoneItem({ card, children, ...props }: ZoneItemProps) {
97
222
  role: props.role ?? "listitem",
98
223
  "data-dreamboard-zone-item": "",
99
224
  "data-zone": zone,
100
- "data-card-id": card,
101
- "data-playable":
102
- playable.some((descriptor) => descriptor.available) || undefined,
225
+ "data-card-id": item.id,
226
+ "data-card-type": item.hidden ? undefined : item.cardType,
227
+ "data-card-index": item.index,
228
+ "data-card-hidden": item.hidden || undefined,
229
+ "data-playable": item.hidden ? undefined : item.playable || undefined,
103
230
  children,
104
231
  })}
105
232
  </ZoneCardContext.Provider>
106
233
  );
107
234
  }
108
235
 
236
+ export interface ZoneCardAtProps<Zone extends string = ZoneKey>
237
+ extends Omit<ZoneItemProps, "card" | "children"> {
238
+ zone?: Zone;
239
+ index: number;
240
+ children?: ReactNode | ((card: ZoneCardRenderItem) => ReactNode);
241
+ empty?: ReactNode;
242
+ }
243
+
244
+ function ZoneCardAtContent<Zone extends string = ZoneKey>({
245
+ index,
246
+ children,
247
+ empty = null,
248
+ ...props
249
+ }: Omit<ZoneCardAtProps<Zone>, "zone">) {
250
+ const { zone, snapshot } = useZonePrimitiveContext();
251
+ const cardIndex = resolveZoneCardIndex(snapshot, index);
252
+ if (cardIndex === null) return <>{empty}</>;
253
+
254
+ const cardId = snapshot?.cardIds[cardIndex];
255
+ if (!cardId) return <>{empty}</>;
256
+
257
+ const card = createZoneCardRenderItem(zone, snapshot, cardId, cardIndex);
258
+ return (
259
+ <ZoneItem card={card} {...props}>
260
+ {typeof children === "function" ? children(card) : children}
261
+ </ZoneItem>
262
+ );
263
+ }
264
+
265
+ export function ZoneCardAt<Zone extends string = ZoneKey>({
266
+ zone,
267
+ ...props
268
+ }: ZoneCardAtProps<Zone>) {
269
+ if (zone) {
270
+ return (
271
+ <ZoneRoot zone={zone}>
272
+ <ZoneCardAtContent {...props} />
273
+ </ZoneRoot>
274
+ );
275
+ }
276
+ return <ZoneCardAtContent {...props} />;
277
+ }
278
+
279
+ export type ZoneTopCardProps<Zone extends string = ZoneKey> = Omit<
280
+ ZoneCardAtProps<Zone>,
281
+ "index"
282
+ >;
283
+
284
+ export function ZoneTopCard<Zone extends string = ZoneKey>(
285
+ props: ZoneTopCardProps<Zone>,
286
+ ) {
287
+ return <ZoneCardAt {...props} index={0} />;
288
+ }
289
+
290
+ export type ZoneCardActionExtraInputs =
291
+ | Record<string, unknown>
292
+ | ((cardId: string) => Record<string, unknown>);
293
+
294
+ export interface ZoneCardActionProps<Card extends string = string>
295
+ extends PrimitiveCommonProps,
296
+ Omit<ButtonHTMLAttributes<HTMLButtonElement>, "onSelect"> {
297
+ card?: Card;
298
+ interaction?: string;
299
+ input?: string;
300
+ extraInputs?: ZoneCardActionExtraInputs;
301
+ onSelect?: (result: ZoneCardActionResult) => void;
302
+ onSelectError?: (error: unknown) => void;
303
+ }
304
+
305
+ export type ZoneCardActionResult =
306
+ | { status: "none" }
307
+ | {
308
+ status: "pending";
309
+ interactionKey: string;
310
+ descriptor: InteractionDescriptor;
311
+ missingInputs: readonly string[];
312
+ }
313
+ | {
314
+ status: "submitted";
315
+ interactionKey: string;
316
+ descriptor: InteractionDescriptor;
317
+ };
318
+
319
+ export function ZoneCardAction<Card extends string = string>({
320
+ card,
321
+ interaction,
322
+ input,
323
+ extraInputs,
324
+ onSelect,
325
+ onSelectError,
326
+ disabled,
327
+ onClick,
328
+ children,
329
+ ...props
330
+ }: ZoneCardActionProps<Card>) {
331
+ const { controllingPlayerId } = usePluginSession();
332
+ const runtime = useRuntimeContext();
333
+ const store = useInteractionUiStore();
334
+ const contextCard = useZoneCardContext();
335
+ const { snapshot } = useZonePrimitiveContext();
336
+ const gameActionError = useGameActionError();
337
+ const cardId = card ?? (contextCard?.cardId as Card | undefined);
338
+ const route = useZoneCardActionRoute(cardId, snapshot, interaction, input);
339
+ const isDisabled =
340
+ disabled ??
341
+ (!cardId ||
342
+ !route.descriptor ||
343
+ !route.inputKey ||
344
+ route.ambiguous ||
345
+ !route.descriptor.available);
346
+
347
+ return renderPrimitive("button", {
348
+ type: "button",
349
+ ...props,
350
+ children,
351
+ disabled: isDisabled,
352
+ "aria-disabled": isDisabled,
353
+ "data-dreamboard-zone-card-action": "",
354
+ "data-card-id": cardId,
355
+ "data-interaction-id": route.descriptor?.interactionId,
356
+ "data-interaction-key": route.descriptor?.interactionKey ?? interaction,
357
+ "data-input-name": route.inputKey ?? input,
358
+ "data-eligible": Boolean(route.descriptor && route.inputKey),
359
+ "data-ambiguous": route.ambiguous || undefined,
360
+ "data-disabled": isDisabled || undefined,
361
+ onClick: composeEventHandlers(onClick, () => {
362
+ if (
363
+ isDisabled ||
364
+ !cardId ||
365
+ !route.descriptor ||
366
+ !route.inputKey ||
367
+ !controllingPlayerId
368
+ ) {
369
+ return;
370
+ }
371
+ const descriptor = route.descriptor;
372
+ const inputKey = route.inputKey;
373
+ void runInteractionAction(
374
+ async (): Promise<ZoneCardActionResult> => {
375
+ const { params, readiness } = routeInteractionTarget(
376
+ store,
377
+ descriptor,
378
+ {
379
+ inputKey,
380
+ value: cardId,
381
+ extraInputs: resolveCardActionExtraInputs(extraInputs, cardId),
382
+ },
383
+ );
384
+ if (shouldRouteInteractionPending(descriptor, readiness)) {
385
+ markInteractionPending(store, descriptor);
386
+ return {
387
+ status: "pending",
388
+ interactionKey: descriptor.interactionKey,
389
+ descriptor,
390
+ missingInputs: readiness.missingInputs,
391
+ };
392
+ }
393
+ if (!claimInteractionSubmit(store, descriptor)) {
394
+ return {
395
+ status: "submitted",
396
+ interactionKey: descriptor.interactionKey,
397
+ descriptor,
398
+ };
399
+ }
400
+ try {
401
+ await runtime.submitInteraction(
402
+ controllingPlayerId,
403
+ descriptor.interactionId,
404
+ params,
405
+ );
406
+ clearInteractionRoute(store, descriptor);
407
+ return {
408
+ status: "submitted",
409
+ interactionKey: descriptor.interactionKey,
410
+ descriptor,
411
+ };
412
+ } finally {
413
+ store.setSubmitting(descriptor.interactionKey, false);
414
+ }
415
+ },
416
+ {
417
+ onSuccess: onSelect,
418
+ onError: onSelectError ?? gameActionError ?? undefined,
419
+ },
420
+ );
421
+ }),
422
+ });
423
+ }
424
+
425
+ export interface ZonePileRootProps<Zone extends string = ZoneKey>
426
+ extends Omit<ZoneRootProps<Zone>, "children"> {
427
+ label: string;
428
+ children?: ReactNode;
429
+ hiddenDescription?: string | null;
430
+ emptyDescription?: string | null;
431
+ visibleDescription?: ((count: number) => string) | null;
432
+ }
433
+
434
+ export function ZonePileRoot<Zone extends string = ZoneKey>({
435
+ zone,
436
+ label,
437
+ hiddenDescription = null,
438
+ emptyDescription = null,
439
+ visibleDescription = null,
440
+ children,
441
+ ...props
442
+ }: ZonePileRootProps<Zone>) {
443
+ return (
444
+ <ZoneRoot zone={zone} {...props}>
445
+ <ZonePileProvider
446
+ emptyDescription={emptyDescription}
447
+ hiddenDescription={hiddenDescription}
448
+ label={label}
449
+ visibleDescription={visibleDescription}
450
+ >
451
+ {children}
452
+ </ZonePileProvider>
453
+ </ZoneRoot>
454
+ );
455
+ }
456
+
457
+ interface ZonePileProviderProps {
458
+ label: string;
459
+ children?: ReactNode;
460
+ hiddenDescription: string | null;
461
+ emptyDescription: string | null;
462
+ visibleDescription: ((count: number) => string) | null;
463
+ }
464
+
465
+ function ZonePileProvider({
466
+ label,
467
+ hiddenDescription,
468
+ emptyDescription,
469
+ visibleDescription,
470
+ children,
471
+ }: ZonePileProviderProps) {
472
+ const { zone, snapshot } = useZonePrimitiveContext();
473
+ // Snapshot is the single source of truth for what's in a pile. A zone that
474
+ // isn't in the current phase's projection scope (snapshot === null) is
475
+ // treated as hidden — author should change the reducer projection rather
476
+ // than inject ids in the UI.
477
+ const cards = snapshot?.cardIds ?? [];
478
+ const items = cards.map((cardId, index) =>
479
+ createZoneCardRenderItem(zone, snapshot, cardId, index),
480
+ );
481
+ const count = cards.length;
482
+ const isHidden = snapshot === null;
483
+ // PileCards iterates whatever the snapshot exposes. Items are tagged
484
+ // `hidden: true | false` so the author's `renderCard` discriminates on
485
+ // honest data — including the "id without contents" case — rather than
486
+ // receiving a lying ViewCard with `cardType: "unknown"`.
487
+ const hasVisibleCards = items.length > 0;
488
+ const description = isHidden
489
+ ? hiddenDescription
490
+ : hasVisibleCards
491
+ ? (visibleDescription?.(count) ?? null)
492
+ : emptyDescription;
493
+ const value = useMemo<ZonePileContextValue>(
494
+ () => ({
495
+ zone,
496
+ label,
497
+ count,
498
+ cards,
499
+ items,
500
+ hasVisibleCards,
501
+ isHidden,
502
+ description,
503
+ }),
504
+ [cards, count, description, hasVisibleCards, isHidden, items, label, zone],
505
+ );
506
+
507
+ return (
508
+ <ZonePileContext.Provider value={value}>
509
+ {children}
510
+ </ZonePileContext.Provider>
511
+ );
512
+ }
513
+
514
+ export interface ZonePileTriggerProps
515
+ extends PrimitiveCommonProps,
516
+ ButtonHTMLAttributes<HTMLButtonElement> {}
517
+
518
+ export function ZonePileTrigger({ children, ...props }: ZonePileTriggerProps) {
519
+ const pile = useZonePileContext();
520
+ return renderPrimitive("button", {
521
+ type: "button",
522
+ ...props,
523
+ "aria-label": props["aria-label"] ?? `Show ${pile.label} pile`,
524
+ "data-dreamboard-zone-pile-trigger": "",
525
+ "data-zone": pile.zone,
526
+ "data-card-count": pile.count,
527
+ "data-hidden": pile.isHidden || undefined,
528
+ children,
529
+ });
530
+ }
531
+
532
+ export type ZonePileLabelProps = PrimitiveCommonProps &
533
+ HTMLAttributes<HTMLElement>;
534
+
535
+ export function ZonePileLabel({ children, ...props }: ZonePileLabelProps) {
536
+ const pile = useZonePileContext();
537
+ return renderPrimitive("span", {
538
+ ...props,
539
+ "data-dreamboard-zone-pile-label": "",
540
+ "data-zone": pile.zone,
541
+ children: children ?? pile.label,
542
+ });
543
+ }
544
+
545
+ export type ZonePileCountProps = PrimitiveCommonProps &
546
+ HTMLAttributes<HTMLElement>;
547
+
548
+ export function ZonePileCount({ children, ...props }: ZonePileCountProps) {
549
+ const pile = useZonePileContext();
550
+ return renderPrimitive("span", {
551
+ ...props,
552
+ "data-dreamboard-zone-pile-count": "",
553
+ "data-zone": pile.zone,
554
+ "data-card-count": pile.count,
555
+ children: children ?? `${pile.count} cards`,
556
+ });
557
+ }
558
+
559
+ export type ZonePileDescriptionProps = PrimitiveCommonProps &
560
+ HTMLAttributes<HTMLElement>;
561
+
562
+ export function ZonePileDescription({
563
+ children,
564
+ ...props
565
+ }: ZonePileDescriptionProps) {
566
+ const pile = useZonePileContext();
567
+ const description = children ?? pile.description;
568
+ if (description === null || description === undefined) return null;
569
+
570
+ return renderPrimitive("span", {
571
+ ...props,
572
+ "data-dreamboard-zone-pile-description": "",
573
+ "data-zone": pile.zone,
574
+ children: description,
575
+ });
576
+ }
577
+
578
+ export interface ZonePileCardsProps extends Omit<ZoneListProps, "children"> {
579
+ renderCard: (card: ZoneCardRenderItem) => ReactNode;
580
+ }
581
+
582
+ export function ZonePileCards({ renderCard, ...props }: ZonePileCardsProps) {
583
+ const pile = useZonePileContext();
584
+ if (!pile.hasVisibleCards) return null;
585
+
586
+ return (
587
+ <ZoneList {...props}>
588
+ {pile.items.map((card) => (
589
+ <ZoneItem key={card.id} card={card}>
590
+ {renderCard(card)}
591
+ </ZoneItem>
592
+ ))}
593
+ </ZoneList>
594
+ );
595
+ }
596
+
597
+ function indexOfCard(
598
+ snapshot: ZoneHandlesSnapshot | null,
599
+ cardId: string,
600
+ ): number {
601
+ return snapshot?.cardIds.indexOf(cardId) ?? -1;
602
+ }
603
+
604
+ function resolveZoneCardIndex(
605
+ snapshot: ZoneHandlesSnapshot | null,
606
+ index: number,
607
+ ): number | null {
608
+ const count = snapshot?.cardIds.length ?? 0;
609
+ const resolved = index < 0 ? count + index : index;
610
+ return resolved >= 0 && resolved < count ? resolved : null;
611
+ }
612
+
613
+ function createZoneCardRenderItem(
614
+ zone: string,
615
+ snapshot: ZoneHandlesSnapshot | null,
616
+ cardId: string,
617
+ index: number,
618
+ ): ZoneCardRenderItem {
619
+ const card = parseViewCard(snapshot?.cardsById[cardId]);
620
+ if (card === null) {
621
+ // The snapshot exposes this card id but not its contents. Surface that
622
+ // honestly via the `hidden: true` variant instead of fabricating a
623
+ // ViewCard with a fake `cardType: "unknown"`.
624
+ return { id: cardId, zone, index, hidden: true };
625
+ }
626
+ const interactions = snapshot?.playableByCardId[cardId] ?? [];
627
+ return {
628
+ ...card,
629
+ id: cardId,
630
+ zone,
631
+ index,
632
+ hidden: false,
633
+ playable: interactions.some((descriptor) => descriptor.available),
634
+ interactions,
635
+ };
636
+ }
637
+
638
+ function useZoneCardActionRoute(
639
+ cardId: string | undefined,
640
+ snapshot: ZoneHandlesSnapshot | null,
641
+ interaction: string | undefined,
642
+ input: string | undefined,
643
+ ): {
644
+ descriptor: InteractionDescriptor | null;
645
+ inputKey: string | null;
646
+ ambiguous: boolean;
647
+ } {
648
+ return useMemo(() => {
649
+ if (!cardId || !snapshot) {
650
+ return { descriptor: null, inputKey: null, ambiguous: false };
651
+ }
652
+ const interactions = snapshot.playableByCardId[cardId] ?? [];
653
+ if (interaction) {
654
+ const descriptor =
655
+ interactions.find(
656
+ (candidate) =>
657
+ candidate.interactionKey === interaction ||
658
+ candidate.interactionId === interaction,
659
+ ) ?? null;
660
+ return {
661
+ descriptor,
662
+ inputKey: descriptor
663
+ ? inputKeyForCardAction(descriptor, cardId, input)
664
+ : null,
665
+ ambiguous: false,
666
+ };
667
+ }
668
+ const matches = interactions.flatMap((descriptor) => {
669
+ if (!descriptor.available) return [];
670
+ const inputKey = inputKeyForCardAction(descriptor, cardId, input);
671
+ return inputKey ? [{ descriptor, inputKey }] : [];
672
+ });
673
+ if (matches.length !== 1) {
674
+ return {
675
+ descriptor: matches[0]?.descriptor ?? null,
676
+ inputKey: matches[0]?.inputKey ?? null,
677
+ ambiguous: matches.length > 1,
678
+ };
679
+ }
680
+ const match = matches[0];
681
+ if (!match) {
682
+ return { descriptor: null, inputKey: null, ambiguous: false };
683
+ }
684
+ return {
685
+ descriptor: match.descriptor,
686
+ inputKey: match.inputKey,
687
+ ambiguous: false,
688
+ };
689
+ }, [cardId, input, interaction, snapshot]);
690
+ }
691
+
692
+ function inputKeyForCardAction(
693
+ descriptor: InteractionDescriptor,
694
+ cardId: string,
695
+ explicitInput?: string,
696
+ ): string | null {
697
+ if (explicitInput) {
698
+ const input = descriptor.inputs.find(
699
+ (candidate) => candidate.key === explicitInput,
700
+ );
701
+ return input?.domain.type === "target" &&
702
+ input.domain.targetKind === "card" &&
703
+ (input.domain.eligibleTargets === undefined ||
704
+ input.domain.eligibleTargets.includes(cardId))
705
+ ? input.key
706
+ : null;
707
+ }
708
+ const targetInput = inputByTarget(descriptor, "card", cardId);
709
+ if (targetInput) return targetInput.key;
710
+ if (interactionInputKeys(descriptor).includes("cardId")) {
711
+ return descriptor.inputs.find((candidate) => candidate.key === "cardId")
712
+ ? "cardId"
713
+ : null;
714
+ }
715
+ return null;
716
+ }
717
+
718
+ function resolveCardActionExtraInputs(
719
+ extraInputs: ZoneCardActionExtraInputs | undefined,
720
+ cardId: string,
721
+ ): Record<string, unknown> {
722
+ return typeof extraInputs === "function"
723
+ ? extraInputs(cardId)
724
+ : (extraInputs ?? {});
725
+ }
726
+
727
+ function parseViewCard(serialized: string | undefined): ViewCard | null {
728
+ if (!serialized) return null;
729
+ try {
730
+ const parsed = JSON.parse(serialized) as Partial<ViewCard>;
731
+ if (typeof parsed.id !== "string" || typeof parsed.cardType !== "string") {
732
+ return null;
733
+ }
734
+ return {
735
+ ...parsed,
736
+ id: parsed.id,
737
+ cardType: parsed.cardType,
738
+ properties:
739
+ parsed.properties && typeof parsed.properties === "object"
740
+ ? parsed.properties
741
+ : {},
742
+ };
743
+ } catch {
744
+ return null;
745
+ }
746
+ }
747
+
109
748
  export const Zone = {
110
749
  Root: ZoneRoot,
111
750
  List: ZoneList,
112
751
  Item: ZoneItem,
752
+ CardAt: ZoneCardAt,
753
+ TopCard: ZoneTopCard,
754
+ CardAction: ZoneCardAction,
755
+ PileRoot: ZonePileRoot,
756
+ PileTrigger: ZonePileTrigger,
757
+ PileLabel: ZonePileLabel,
758
+ PileCount: ZonePileCount,
759
+ PileDescription: ZonePileDescription,
760
+ PileCards: ZonePileCards,
113
761
  };