@dreamboard-games/sdk 0.2.0
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.
- package/LICENSE.md +96 -0
- package/README.md +12 -0
- package/dist/HandView-ncJIVLhN.d.ts +193 -0
- package/dist/ResourceCounter-CTREyF73.d.ts +102 -0
- package/dist/ThemeProvider-fy0_QzgO.d.ts +99 -0
- package/dist/bundle-TIZcw8LB.d.ts +281 -0
- package/dist/cards-Sl3b40Mv.d.ts +13 -0
- package/dist/chunk-7YAHLYBR.js +481 -0
- package/dist/chunk-7YAHLYBR.js.map +1 -0
- package/dist/chunk-FDNZTDD6.js +8085 -0
- package/dist/chunk-FDNZTDD6.js.map +1 -0
- package/dist/chunk-GKKBPPSW.js +598 -0
- package/dist/chunk-GKKBPPSW.js.map +1 -0
- package/dist/chunk-I46YJSOD.js +1 -0
- package/dist/chunk-I46YJSOD.js.map +1 -0
- package/dist/chunk-KAELH4KC.js +104 -0
- package/dist/chunk-KAELH4KC.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +10 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-T3ZKNUZ7.js +1 -0
- package/dist/chunk-T3ZKNUZ7.js.map +1 -0
- package/dist/chunk-T52J5RMF.js +1 -0
- package/dist/chunk-T52J5RMF.js.map +1 -0
- package/dist/chunk-TDSWKVZ4.js +5401 -0
- package/dist/chunk-TDSWKVZ4.js.map +1 -0
- package/dist/chunk-U5C6BONG.js +34 -0
- package/dist/chunk-U5C6BONG.js.map +1 -0
- package/dist/chunk-VDXOF4FW.js +69 -0
- package/dist/chunk-VDXOF4FW.js.map +1 -0
- package/dist/chunk-VFTAA4WO.js +115 -0
- package/dist/chunk-VFTAA4WO.js.map +1 -0
- package/dist/chunk-WN74KVNY.js +17 -0
- package/dist/chunk-WN74KVNY.js.map +1 -0
- package/dist/chunk-WYPQ3GG5.js +10990 -0
- package/dist/chunk-WYPQ3GG5.js.map +1 -0
- package/dist/components-D5ZRE2Hl.d.ts +1451 -0
- package/dist/generated/runtime/primitives.d.ts +12 -0
- package/dist/generated/runtime/primitives.js +180 -0
- package/dist/generated/runtime/primitives.js.map +1 -0
- package/dist/generated/runtime-api.d.ts +3 -0
- package/dist/generated/runtime-api.js +2 -0
- package/dist/generated/runtime-api.js.map +1 -0
- package/dist/generated/runtime.d.ts +14 -0
- package/dist/generated/runtime.js +18 -0
- package/dist/generated/runtime.js.map +1 -0
- package/dist/generated/workspace-contract.d.ts +14 -0
- package/dist/generated/workspace-contract.js +14 -0
- package/dist/generated/workspace-contract.js.map +1 -0
- package/dist/hex-board-view-D_07hO6O.d.ts +933 -0
- package/dist/hex-color-MhOyuY-o.d.ts +8 -0
- package/dist/index-BwqPQtBu.d.ts +1433 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/reducer-bundle-abi.d.ts +1083 -0
- package/dist/infrastructure/reducer-bundle-abi.js +14 -0
- package/dist/infrastructure/reducer-bundle-abi.js.map +1 -0
- package/dist/infrastructure/workspace-codegen.d.ts +53 -0
- package/dist/infrastructure/workspace-codegen.js +44 -0
- package/dist/infrastructure/workspace-codegen.js.map +1 -0
- package/dist/manifest-contract-BNHVGFtU.d.ts +9 -0
- package/dist/package-set.d.ts +13 -0
- package/dist/package-set.js +12 -0
- package/dist/package-set.js.map +1 -0
- package/dist/primitive-props-DpKs-GCr.d.ts +11 -0
- package/dist/reducer.d.ts +3786 -0
- package/dist/reducer.js +8131 -0
- package/dist/reducer.js.map +1 -0
- package/dist/runtime/primitives.d.ts +226 -0
- package/dist/runtime/primitives.js +180 -0
- package/dist/runtime/primitives.js.map +1 -0
- package/dist/runtime/types/runtime-api.d.ts +1 -0
- package/dist/runtime/types/runtime-api.js +2 -0
- package/dist/runtime/types/runtime-api.js.map +1 -0
- package/dist/runtime/workspace-contract.d.ts +172 -0
- package/dist/runtime/workspace-contract.js +14 -0
- package/dist/runtime/workspace-contract.js.map +1 -0
- package/dist/runtime-api-3dshj6kK.d.ts +101 -0
- package/dist/runtime-api-DWxvTr-O.d.ts +379 -0
- package/dist/runtime.d.ts +58 -0
- package/dist/runtime.js +13 -0
- package/dist/runtime.js.map +1 -0
- package/dist/slots-1GPGihk8.d.ts +8 -0
- package/dist/testing.d.ts +149 -0
- package/dist/testing.js +513 -0
- package/dist/testing.js.map +1 -0
- package/dist/types.d.ts +496 -0
- package/dist/types.js +28 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/components.d.ts +16 -0
- package/dist/ui/components.js +192 -0
- package/dist/ui/components.js.map +1 -0
- package/dist/ui/defaults.d.ts +19 -0
- package/dist/ui/defaults.js +104 -0
- package/dist/ui/defaults.js.map +1 -0
- package/dist/ui/plugin-styles.css +250 -0
- package/dist/ui/types/player-state.d.ts +365 -0
- package/dist/ui/types/player-state.js +1 -0
- package/dist/ui/types/player-state.js.map +1 -0
- package/dist/ui-contract-iQfTtUSL.d.ts +1161 -0
- package/dist/ui.d.ts +320 -0
- package/dist/ui.js +253 -0
- package/dist/ui.js.map +1 -0
- package/package.json +199 -0
- package/src/generated/reducer-contract/builders.ts +90 -0
- package/src/generated/reducer-contract/version.ts +9 -0
- package/src/generated/reducer-contract/wire.ts +100 -0
- package/src/generated/reducer-contract/zod.ts +101 -0
- package/src/generated/runtime/primitives.ts +2 -0
- package/src/generated/runtime-api.ts +5 -0
- package/src/generated/runtime.ts +35 -0
- package/src/generated/workspace-contract.ts +2 -0
- package/src/index.ts +7 -0
- package/src/infrastructure/reducer-bundle-abi.ts +8 -0
- package/src/infrastructure/reducer-contract/bundle.ts +37 -0
- package/src/infrastructure/workspace-codegen/hex-geometry.ts +69 -0
- package/src/infrastructure/workspace-codegen/index.ts +64 -0
- package/src/infrastructure/workspace-codegen/manifest-contract.ts +6632 -0
- package/src/infrastructure/workspace-codegen/manifest-validation.ts +795 -0
- package/src/infrastructure/workspace-codegen/ownership.ts +131 -0
- package/src/infrastructure/workspace-codegen/preset-card-sets.ts +169 -0
- package/src/infrastructure/workspace-codegen/seeds.ts +1705 -0
- package/src/infrastructure/workspace-codegen.ts +1 -0
- package/src/package-set.ts +19 -0
- package/src/reducer/authoring/contract.ts +157 -0
- package/src/reducer/authoring/effect.ts +224 -0
- package/src/reducer/authoring/game.ts +23 -0
- package/src/reducer/authoring/interaction.ts +98 -0
- package/src/reducer/authoring/phase.ts +300 -0
- package/src/reducer/authoring/types.ts +70 -0
- package/src/reducer/authoring/validation.ts +382 -0
- package/src/reducer/authoring/view-stage.ts +68 -0
- package/src/reducer/authoring.ts +29 -0
- package/src/reducer/bundle/ingress-bundle.ts +491 -0
- package/src/reducer/bundle/trusted/engine-instruction-resolver.ts +254 -0
- package/src/reducer/bundle/trusted/flow-instruction-resolver.ts +73 -0
- package/src/reducer/bundle/trusted/instruction-runner.ts +414 -0
- package/src/reducer/bundle/trusted/interaction-authorization.ts +137 -0
- package/src/reducer/bundle/trusted/interaction-collectors.ts +859 -0
- package/src/reducer/bundle/trusted/interaction-decision.ts +747 -0
- package/src/reducer/bundle/trusted/interaction-resolver.ts +95 -0
- package/src/reducer/bundle/trusted/interaction-types.ts +171 -0
- package/src/reducer/bundle/trusted/lifecycle-runner.ts +427 -0
- package/src/reducer/bundle/trusted/projection-builder.ts +356 -0
- package/src/reducer/bundle/trusted/projection-context.ts +39 -0
- package/src/reducer/bundle/trusted/rng-sampler.ts +150 -0
- package/src/reducer/bundle/trusted/runtime-registry.ts +120 -0
- package/src/reducer/bundle/trusted/runtime-scope.ts +336 -0
- package/src/reducer/bundle/trusted/simultaneous-player.ts +97 -0
- package/src/reducer/bundle/trusted/stage-resolver.ts +87 -0
- package/src/reducer/bundle/trusted/static-projection.ts +116 -0
- package/src/reducer/bundle/trusted/trusted-runtime-args.ts +97 -0
- package/src/reducer/bundle/trusted/trusted-runtime-result.ts +39 -0
- package/src/reducer/bundle/trusted/trusted-setup-profiles.ts +43 -0
- package/src/reducer/bundle/trusted/trusted-state-codec.ts +48 -0
- package/src/reducer/bundle/trusted-bundle.ts +97 -0
- package/src/reducer/bundle/types.ts +171 -0
- package/src/reducer/bundle.ts +2 -0
- package/src/reducer/client-param-schemas.ts +57 -0
- package/src/reducer/compose.ts +34 -0
- package/src/reducer/core/runtime-input.ts +30 -0
- package/src/reducer/core/runtime-instruction.ts +59 -0
- package/src/reducer/core/types.ts +62 -0
- package/src/reducer/definition-index.ts +277 -0
- package/src/reducer/derived.ts +106 -0
- package/src/reducer/effects.ts +92 -0
- package/src/reducer/engine/runtime-instruction-engine.ts +155 -0
- package/src/reducer/ingress/decode-runtime-input.ts +7 -0
- package/src/reducer/ingress/decode-session-state.ts +9 -0
- package/src/reducer/ingress/encode-session-state.ts +6 -0
- package/src/reducer/ingress/input-codec.ts +18 -0
- package/src/reducer/ingress/phase-schemas.ts +62 -0
- package/src/reducer/ingress/raw-types.ts +107 -0
- package/src/reducer/ingress/runtime-codec.ts +14 -0
- package/src/reducer/ingress/runtime-payload.ts +13 -0
- package/src/reducer/ingress/session-codec.ts +392 -0
- package/src/reducer/ingress/types.ts +6 -0
- package/src/reducer/inputs/boardInput.ts +217 -0
- package/src/reducer/inputs/boardTarget.ts +190 -0
- package/src/reducer/inputs/cardInput.ts +86 -0
- package/src/reducer/inputs/cardTarget.ts +101 -0
- package/src/reducer/inputs/choiceTarget.ts +104 -0
- package/src/reducer/inputs/defineInputs.ts +71 -0
- package/src/reducer/inputs/formInput.ts +809 -0
- package/src/reducer/inputs/many.ts +120 -0
- package/src/reducer/inputs/promptInput.ts +87 -0
- package/src/reducer/inputs/rngInput.ts +58 -0
- package/src/reducer/inputs/targetRule.ts +123 -0
- package/src/reducer/inputs.ts +41 -0
- package/src/reducer/model/definition.ts +1072 -0
- package/src/reducer/model/extract.ts +745 -0
- package/src/reducer/model/manifest.ts +570 -0
- package/src/reducer/model/queries.ts +641 -0
- package/src/reducer/model/runtime.ts +264 -0
- package/src/reducer/model/spec.ts +1386 -0
- package/src/reducer/model/table.ts +260 -0
- package/src/reducer/model.ts +7 -0
- package/src/reducer/ops.ts +1034 -0
- package/src/reducer/parse-utils.ts +28 -0
- package/src/reducer/per-player.ts +422 -0
- package/src/reducer/rng.ts +69 -0
- package/src/reducer/schema-helpers.ts +185 -0
- package/src/reducer/setup-bootstrap-helpers.ts +171 -0
- package/src/reducer/setup-bootstrap.ts +481 -0
- package/src/reducer/table-ops.ts +2671 -0
- package/src/reducer/table-queries.ts +372 -0
- package/src/reducer/transaction.ts +120 -0
- package/src/reducer.ts +314 -0
- package/src/runtime/primitives.ts +1 -0
- package/src/runtime/types/runtime-api.ts +1 -0
- package/src/runtime/workspace-contract.ts +32 -0
- package/src/runtime-internal/components/InteractionForm.tsx +1309 -0
- package/src/runtime-internal/components/PluginRuntime.tsx +103 -0
- package/src/runtime-internal/components/board/target-layer.ts +70 -0
- package/src/runtime-internal/context/ClientParamSchemaContext.tsx +44 -0
- package/src/runtime-internal/context/InteractionDraftContext.tsx +279 -0
- package/src/runtime-internal/context/PluginSessionContext.tsx +47 -0
- package/src/runtime-internal/context/PluginStateContext.tsx +262 -0
- package/src/runtime-internal/context/RuntimeContext.tsx +96 -0
- package/src/runtime-internal/defaults/components.tsx +409 -0
- package/src/runtime-internal/defaults/index.ts +11 -0
- package/src/runtime-internal/errors/ValidationError.ts +29 -0
- package/src/runtime-internal/hooks/useActivePlayers.ts +33 -0
- package/src/runtime-internal/hooks/useBoardInteractions.ts +665 -0
- package/src/runtime-internal/hooks/useGameSelector.ts +105 -0
- package/src/runtime-internal/hooks/useGameView.ts +9 -0
- package/src/runtime-internal/hooks/useInteractionByKey.ts +354 -0
- package/src/runtime-internal/hooks/useInteractionHandle.ts +438 -0
- package/src/runtime-internal/hooks/useIsMyTurn.ts +20 -0
- package/src/runtime-internal/hooks/useLobby.ts +76 -0
- package/src/runtime-internal/hooks/useMe.ts +48 -0
- package/src/runtime-internal/hooks/usePlayerInfo.ts +28 -0
- package/src/runtime-internal/hooks/usePlayerTurnOrder.ts +23 -0
- package/src/runtime-internal/hooks/usePluginRuntime.ts +147 -0
- package/src/runtime-internal/hooks/useSeatInbox.ts +61 -0
- package/src/runtime-internal/hooks/useSimultaneousPhase.ts +10 -0
- package/src/runtime-internal/index.ts +42 -0
- package/src/runtime-internal/internal.ts +43 -0
- package/src/runtime-internal/plugin-styles.css +250 -0
- package/src/runtime-internal/primitives/board.tsx +459 -0
- package/src/runtime-internal/primitives/dialog-lifecycle.ts +58 -0
- package/src/runtime-internal/primitives/dice.tsx +79 -0
- package/src/runtime-internal/primitives/game-ui-provider.tsx +35 -0
- package/src/runtime-internal/primitives/game.tsx +387 -0
- package/src/runtime-internal/primitives/hand-intent-adapter.ts +147 -0
- package/src/runtime-internal/primitives/hand-surface.tsx +594 -0
- package/src/runtime-internal/primitives/index.ts +196 -0
- package/src/runtime-internal/primitives/interaction-form-binding.tsx +56 -0
- package/src/runtime-internal/primitives/interaction-submit.ts +90 -0
- package/src/runtime-internal/primitives/interaction.tsx +987 -0
- package/src/runtime-internal/primitives/phase.tsx +43 -0
- package/src/runtime-internal/primitives/player-roster.tsx +302 -0
- package/src/runtime-internal/primitives/primitive-props.tsx +101 -0
- package/src/runtime-internal/primitives/prompt.tsx +255 -0
- package/src/runtime-internal/primitives/ui.tsx +60 -0
- package/src/runtime-internal/primitives/zone.tsx +791 -0
- package/src/runtime-internal/reducer.ts +30 -0
- package/src/runtime-internal/runtime/createPluginRuntimeAPI.ts +605 -0
- package/src/runtime-internal/types/plugin-state.ts +508 -0
- package/src/runtime-internal/types/reducer-state.ts +24 -0
- package/src/runtime-internal/types/runtime-api.ts +114 -0
- package/src/runtime-internal/ui-contract.ts +519 -0
- package/src/runtime-internal/utils/card-intent-adapter.ts +546 -0
- package/src/runtime-internal/utils/interaction-inputs.ts +492 -0
- package/src/runtime-internal/utils/interaction-labels.ts +23 -0
- package/src/runtime-internal/utils/interaction-router.ts +273 -0
- package/src/runtime-internal/utils/interaction-status.ts +74 -0
- package/src/runtime-internal/workspace-contract.ts +1170 -0
- package/src/runtime.ts +34 -0
- package/src/testing/create-expect-api.ts +352 -0
- package/src/testing/create-test-runtime.ts +381 -0
- package/src/testing/definitions.ts +127 -0
- package/src/testing/index.ts +3 -0
- package/src/testing.ts +1 -0
- package/src/type-stubs/manifest-contract.d.ts +42 -0
- package/src/type-stubs/manifest-contract.js +72 -0
- package/src/type-stubs/ui-contract.d.ts +5 -0
- package/src/type-stubs/ui-contract.js +1 -0
- package/src/types/authoring-card-properties.type-test.ts +266 -0
- package/src/types/authoring.ts +1282 -0
- package/src/types/cards.ts +19 -0
- package/src/types/contracts.ts +1550 -0
- package/src/types/generated-helpers.ts +35 -0
- package/src/types/index.ts +147 -0
- package/src/types/slots.ts +11 -0
- package/src/types.ts +1 -0
- package/src/ui/components/ActionButton.tsx +97 -0
- package/src/ui/components/ActionPanel.tsx +315 -0
- package/src/ui/components/Card.tsx +378 -0
- package/src/ui/components/CardDragSurface.tsx +1076 -0
- package/src/ui/components/ChromeSuppressionContext.tsx +70 -0
- package/src/ui/components/CostDisplay.tsx +145 -0
- package/src/ui/components/DiceRoller.tsx +581 -0
- package/src/ui/components/Drawer.tsx +180 -0
- package/src/ui/components/ErrorBoundary.tsx +275 -0
- package/src/ui/components/GameEndDisplay.tsx +398 -0
- package/src/ui/components/GameSkeleton.tsx +260 -0
- package/src/ui/components/Hand.tsx +468 -0
- package/src/ui/components/HandDock.tsx +299 -0
- package/src/ui/components/HandView.tsx +441 -0
- package/src/ui/components/MobileHandTray.tsx +381 -0
- package/src/ui/components/MoreActions.tsx +143 -0
- package/src/ui/components/PhaseIndicator.tsx +341 -0
- package/src/ui/components/PlayArea.tsx +146 -0
- package/src/ui/components/PrimaryActionButton.tsx +336 -0
- package/src/ui/components/PrimaryButton.tsx +45 -0
- package/src/ui/components/ResourceCounter.tsx +270 -0
- package/src/ui/components/StagingZone.tsx +134 -0
- package/src/ui/components/ThemedButton.tsx +113 -0
- package/src/ui/components/Toast.tsx +264 -0
- package/src/ui/components/board/HexGrid.tsx +1294 -0
- package/src/ui/components/board/NetworkGraph.tsx +476 -0
- package/src/ui/components/board/SlotSystem.tsx +388 -0
- package/src/ui/components/board/SquareGrid.tsx +1165 -0
- package/src/ui/components/board/TrackBoard.tsx +496 -0
- package/src/ui/components/board/ZoneMap.tsx +448 -0
- package/src/ui/components/board/hex-board-view.ts +123 -0
- package/src/ui/components/board/index.ts +142 -0
- package/src/ui/components/board/interaction-accessibility.ts +21 -0
- package/src/ui/components/board/target-layer.ts +66 -0
- package/src/ui/components/card-render-content.type-test.ts +27 -0
- package/src/ui/components/hand-layout-math.ts +163 -0
- package/src/ui/components/hand-pointer-engine.ts +413 -0
- package/src/ui/components/index.ts +245 -0
- package/src/ui/components.ts +1 -0
- package/src/ui/defaults/components.tsx +106 -0
- package/src/ui/defaults/index.ts +8 -0
- package/src/ui/defaults.ts +1 -0
- package/src/ui/errors/ValidationError.ts +29 -0
- package/src/ui/helpers/cards.ts +19 -0
- package/src/ui/helpers/track-board.ts +211 -0
- package/src/ui/hooks/useBoardTopology.ts +316 -0
- package/src/ui/hooks/useCards.ts +10 -0
- package/src/ui/hooks/useHandCardPointer.ts +381 -0
- package/src/ui/hooks/useHandLayout.ts +378 -0
- package/src/ui/hooks/useHandPresentation.ts +121 -0
- package/src/ui/hooks/useHexBoard.ts +74 -0
- package/src/ui/hooks/useHexGrid.ts +185 -0
- package/src/ui/hooks/useIsMobile.ts +35 -0
- package/src/ui/hooks/usePanZoom.ts +278 -0
- package/src/ui/hooks/useSquareBoard.ts +124 -0
- package/src/ui/hooks/useSquareGrid.ts +328 -0
- package/src/ui/index.ts +98 -0
- package/src/ui/internal/ui/alert.tsx +51 -0
- package/src/ui/internal/ui/button.tsx +58 -0
- package/src/ui/internal/ui/dialog.tsx +134 -0
- package/src/ui/internal/ui/input.tsx +21 -0
- package/src/ui/internal/ui/label.tsx +21 -0
- package/src/ui/internal/ui/select.tsx +129 -0
- package/src/ui/internal/ui/tooltip.tsx +54 -0
- package/src/ui/internal/ui/utils.ts +5 -0
- package/src/ui/plugin-styles.css +250 -0
- package/src/ui/primitives/dialog-lifecycle.ts +58 -0
- package/src/ui/primitives/dice.tsx +79 -0
- package/src/ui/primitives/primitive-props.tsx +101 -0
- package/src/ui/theme/ThemeProvider.tsx +252 -0
- package/src/ui/theme/board.ts +61 -0
- package/src/ui/theme/css-vars.ts +105 -0
- package/src/ui/theme/derive.ts +240 -0
- package/src/ui/theme/index.ts +61 -0
- package/src/ui/theme/presets/arcade.ts +261 -0
- package/src/ui/theme/presets/studio.ts +261 -0
- package/src/ui/theme/presets/tabletop.ts +266 -0
- package/src/ui/theme/tokens.ts +392 -0
- package/src/ui/types/hex-color.ts +20 -0
- package/src/ui/types/player-state.ts +463 -0
- package/src/ui/types/tiled-board.ts +785 -0
- package/src/ui/types/visual-state.ts +137 -0
- package/src/ui.ts +1 -0
|
@@ -0,0 +1,1309 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useEffect,
|
|
3
|
+
useId,
|
|
4
|
+
useMemo,
|
|
5
|
+
useState,
|
|
6
|
+
type ButtonHTMLAttributes,
|
|
7
|
+
type CSSProperties,
|
|
8
|
+
type FormEvent,
|
|
9
|
+
type ReactNode,
|
|
10
|
+
} from "react";
|
|
11
|
+
import * as AccordionPrimitive from "@radix-ui/react-accordion";
|
|
12
|
+
import {
|
|
13
|
+
Input,
|
|
14
|
+
Label,
|
|
15
|
+
Select,
|
|
16
|
+
SelectContent,
|
|
17
|
+
SelectItem,
|
|
18
|
+
SelectTrigger,
|
|
19
|
+
surfaceStyle,
|
|
20
|
+
useTheme,
|
|
21
|
+
useThemeCssVars,
|
|
22
|
+
} from "../../ui.js";
|
|
23
|
+
import type {
|
|
24
|
+
DraftValidation,
|
|
25
|
+
InteractionHandle,
|
|
26
|
+
InteractionParamsShape,
|
|
27
|
+
} from "../hooks/useInteractionHandle.js";
|
|
28
|
+
import type {
|
|
29
|
+
InteractionChoiceOption,
|
|
30
|
+
InteractionDescriptor,
|
|
31
|
+
InteractionInputDescriptor,
|
|
32
|
+
InputDomain,
|
|
33
|
+
} from "../types/plugin-state.js";
|
|
34
|
+
import { interactionLabel } from "../utils/interaction-labels.js";
|
|
35
|
+
import {
|
|
36
|
+
inputTargetKind,
|
|
37
|
+
isResolvedTargetDomain,
|
|
38
|
+
isTargetDomain,
|
|
39
|
+
resolveInputDomain,
|
|
40
|
+
resolveInteractionInputs,
|
|
41
|
+
toggleManyValue,
|
|
42
|
+
} from "../utils/interaction-inputs.js";
|
|
43
|
+
import { isInteractionAvailable } from "../utils/interaction-status.js";
|
|
44
|
+
import { useChromeSuppression, ThemedButton } from "../../ui.js";
|
|
45
|
+
|
|
46
|
+
export interface InteractionFieldRenderProps<
|
|
47
|
+
Params extends InteractionParamsShape = InteractionParamsShape,
|
|
48
|
+
Key extends keyof Params & string = keyof Params & string,
|
|
49
|
+
> {
|
|
50
|
+
descriptor: InteractionDescriptor;
|
|
51
|
+
input: InteractionInputDescriptor & { key: Key };
|
|
52
|
+
handle: InteractionHandle<Params>;
|
|
53
|
+
value: Params[Key] | undefined;
|
|
54
|
+
setValue: (value: Params[Key]) => void;
|
|
55
|
+
clearValue: () => void;
|
|
56
|
+
errors: readonly string[];
|
|
57
|
+
missing: boolean;
|
|
58
|
+
disabled: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type InteractionFieldRenderMap<
|
|
62
|
+
Params extends InteractionParamsShape = InteractionParamsShape,
|
|
63
|
+
> = Partial<{
|
|
64
|
+
[K in keyof Params & string]: (
|
|
65
|
+
props: InteractionFieldRenderProps<Params, K>,
|
|
66
|
+
) => ReactNode;
|
|
67
|
+
}>;
|
|
68
|
+
|
|
69
|
+
export interface InteractionSlotComponentProps {
|
|
70
|
+
children?: ReactNode;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type InteractionButtonSlotProps = InteractionSlotComponentProps &
|
|
74
|
+
Omit<
|
|
75
|
+
ButtonHTMLAttributes<HTMLButtonElement>,
|
|
76
|
+
"children" | "disabled" | "type" | "value"
|
|
77
|
+
>;
|
|
78
|
+
|
|
79
|
+
export interface InteractionTargetSlotProps extends Omit<
|
|
80
|
+
ButtonHTMLAttributes<HTMLButtonElement>,
|
|
81
|
+
| "children"
|
|
82
|
+
| "disabled"
|
|
83
|
+
| "aria-disabled"
|
|
84
|
+
| "aria-pressed"
|
|
85
|
+
| "onClick"
|
|
86
|
+
| "type"
|
|
87
|
+
| "value"
|
|
88
|
+
> {
|
|
89
|
+
value: string;
|
|
90
|
+
children?: ReactNode;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface InteractionOptionsSlotProps {
|
|
94
|
+
children: (option: { value: string | null; label: string }) => ReactNode;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface InteractionCardsSlotProps {
|
|
98
|
+
children: (card: { id: string }) => ReactNode;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface InteractionValueSlotProps {
|
|
102
|
+
children: (value: unknown) => ReactNode;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface InteractionInputSlot {
|
|
106
|
+
Field: (props: InteractionSlotComponentProps) => ReactNode;
|
|
107
|
+
Default: (props: InteractionSlotComponentProps) => ReactNode;
|
|
108
|
+
Target: (props: InteractionTargetSlotProps) => ReactNode;
|
|
109
|
+
Card: (props: InteractionTargetSlotProps) => ReactNode;
|
|
110
|
+
Cards: (props: InteractionCardsSlotProps) => ReactNode;
|
|
111
|
+
Options: (props: InteractionOptionsSlotProps) => ReactNode;
|
|
112
|
+
Value: (props: InteractionValueSlotProps) => ReactNode;
|
|
113
|
+
Label: (props: InteractionSlotComponentProps) => ReactNode;
|
|
114
|
+
Message: (props: InteractionSlotComponentProps) => ReactNode;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type InteractionInputRenderMap = Record<
|
|
118
|
+
string,
|
|
119
|
+
(slot: InteractionInputSlot) => ReactNode
|
|
120
|
+
>;
|
|
121
|
+
|
|
122
|
+
export interface InteractionSubmitSlot {
|
|
123
|
+
Button: (props: InteractionButtonSlotProps) => ReactNode;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface InteractionFieldProps<
|
|
127
|
+
Params extends InteractionParamsShape = InteractionParamsShape,
|
|
128
|
+
Key extends keyof Params & string = keyof Params & string,
|
|
129
|
+
> {
|
|
130
|
+
descriptor: InteractionDescriptor;
|
|
131
|
+
inputKey: Key;
|
|
132
|
+
handle: InteractionHandle<Params>;
|
|
133
|
+
errors?: readonly string[];
|
|
134
|
+
missing?: boolean;
|
|
135
|
+
disabled?: boolean;
|
|
136
|
+
render?: InteractionFieldRenderMap<Params>[Key];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface InteractionFormProps<
|
|
140
|
+
Params extends InteractionParamsShape = InteractionParamsShape,
|
|
141
|
+
DefaultedKeys extends keyof Params & string = never,
|
|
142
|
+
> {
|
|
143
|
+
descriptor: InteractionDescriptor;
|
|
144
|
+
handle: InteractionHandle<Params, DefaultedKeys>;
|
|
145
|
+
fields?: ReadonlyArray<keyof Params & string>;
|
|
146
|
+
hiddenFields?: ReadonlyArray<keyof Params & string>;
|
|
147
|
+
renderFields?: InteractionFieldRenderMap<Params>;
|
|
148
|
+
inputs?: InteractionInputRenderMap;
|
|
149
|
+
submit?: (
|
|
150
|
+
slot: InteractionSubmitSlot,
|
|
151
|
+
descriptor: InteractionDescriptor,
|
|
152
|
+
) => ReactNode;
|
|
153
|
+
title?: ReactNode;
|
|
154
|
+
description?: ReactNode;
|
|
155
|
+
submitLabel?: ReactNode;
|
|
156
|
+
cancelLabel?: ReactNode;
|
|
157
|
+
onCancel?: () => void;
|
|
158
|
+
onSubmitSuccess?: () => void;
|
|
159
|
+
disabled?: boolean;
|
|
160
|
+
accordion?: boolean;
|
|
161
|
+
defaultOpen?: boolean;
|
|
162
|
+
style?: CSSProperties;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const EMPTY_FIELD_ERRORS: readonly string[] = [];
|
|
166
|
+
|
|
167
|
+
export function InteractionForm<
|
|
168
|
+
Params extends InteractionParamsShape = InteractionParamsShape,
|
|
169
|
+
DefaultedKeys extends keyof Params & string = never,
|
|
170
|
+
>({
|
|
171
|
+
descriptor,
|
|
172
|
+
handle,
|
|
173
|
+
fields,
|
|
174
|
+
hiddenFields,
|
|
175
|
+
renderFields,
|
|
176
|
+
inputs,
|
|
177
|
+
submit: renderSubmit,
|
|
178
|
+
title,
|
|
179
|
+
description,
|
|
180
|
+
submitLabel,
|
|
181
|
+
cancelLabel = "Cancel",
|
|
182
|
+
onCancel,
|
|
183
|
+
onSubmitSuccess,
|
|
184
|
+
disabled = false,
|
|
185
|
+
accordion = true,
|
|
186
|
+
defaultOpen = false,
|
|
187
|
+
style,
|
|
188
|
+
}: InteractionFormProps<Params, DefaultedKeys>) {
|
|
189
|
+
const theme = useTheme();
|
|
190
|
+
const fallbackLabel = interactionLabel(descriptor);
|
|
191
|
+
const formId = useId();
|
|
192
|
+
useChromeSuppression(formId, true);
|
|
193
|
+
const [pending, setPending] = useState(false);
|
|
194
|
+
const [validation, setValidation] = useState<DraftValidation<Params> | null>(
|
|
195
|
+
null,
|
|
196
|
+
);
|
|
197
|
+
const [formError, setFormError] = useState<string | null>(null);
|
|
198
|
+
const [accordionOpen, setAccordionOpen] = useState(defaultOpen);
|
|
199
|
+
const hidden = useMemo(() => new Set(hiddenFields ?? []), [hiddenFields]);
|
|
200
|
+
const visibleInputs = useMemo(() => {
|
|
201
|
+
if (inputs) {
|
|
202
|
+
const rendered = new Set(Object.keys(inputs));
|
|
203
|
+
return resolveInteractionInputs(
|
|
204
|
+
descriptor,
|
|
205
|
+
handle.values as Readonly<Record<string, unknown>>,
|
|
206
|
+
).filter((input) => rendered.has(input.key));
|
|
207
|
+
}
|
|
208
|
+
const allowed = fields ? new Set(fields) : null;
|
|
209
|
+
return defaultFormInputs(
|
|
210
|
+
descriptor,
|
|
211
|
+
handle.values as Readonly<Record<string, unknown>>,
|
|
212
|
+
).filter((input) => {
|
|
213
|
+
const key = input.key as keyof Params & string;
|
|
214
|
+
if (allowed && !allowed.has(key)) return false;
|
|
215
|
+
return !hidden.has(key);
|
|
216
|
+
});
|
|
217
|
+
}, [descriptor, fields, hidden, handle.values, inputs]);
|
|
218
|
+
|
|
219
|
+
const currentValidation = validation;
|
|
220
|
+
const fieldErrors = (currentValidation?.fieldErrors ?? {}) as Partial<
|
|
221
|
+
Record<keyof Params & string, readonly string[]>
|
|
222
|
+
>;
|
|
223
|
+
const missing = new Set<string>(currentValidation?.missing ?? []);
|
|
224
|
+
const formErrors = [
|
|
225
|
+
...(currentValidation?.formErrors ?? []),
|
|
226
|
+
...(formError ? [formError] : []),
|
|
227
|
+
];
|
|
228
|
+
const isDisabled = disabled || pending || !isInteractionAvailable(descriptor);
|
|
229
|
+
const useAccordion = accordion && visibleInputs.length > 0;
|
|
230
|
+
|
|
231
|
+
useEffect(() => {
|
|
232
|
+
setAccordionOpen(defaultOpen);
|
|
233
|
+
}, [defaultOpen, descriptor.interactionId]);
|
|
234
|
+
|
|
235
|
+
const containerStyle: CSSProperties = {
|
|
236
|
+
...surfaceStyle(theme, { tone: "card" }),
|
|
237
|
+
display: "flex",
|
|
238
|
+
flexDirection: "column",
|
|
239
|
+
gap: theme.space[2],
|
|
240
|
+
padding: theme.space[3],
|
|
241
|
+
minWidth: "min(100%, 280px)",
|
|
242
|
+
boxSizing: "border-box",
|
|
243
|
+
fontFamily: theme.typography.fontFamily.body,
|
|
244
|
+
...style,
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const submit = async (event: FormEvent) => {
|
|
248
|
+
event.preventDefault();
|
|
249
|
+
if (isDisabled) return;
|
|
250
|
+
const nextValidation = handle.validateDraft();
|
|
251
|
+
setValidation(nextValidation);
|
|
252
|
+
setFormError(null);
|
|
253
|
+
if (!nextValidation.ok) {
|
|
254
|
+
setAccordionOpen(true);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
setPending(true);
|
|
258
|
+
try {
|
|
259
|
+
await handle.submitDraft();
|
|
260
|
+
onSubmitSuccess?.();
|
|
261
|
+
setAccordionOpen(defaultOpen);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
setAccordionOpen(true);
|
|
264
|
+
setFormError(
|
|
265
|
+
error instanceof Error
|
|
266
|
+
? error.message
|
|
267
|
+
: "Interaction submission failed",
|
|
268
|
+
);
|
|
269
|
+
} finally {
|
|
270
|
+
setPending(false);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const header = (
|
|
275
|
+
<div style={{ display: "flex", flexDirection: "column", gap: 2 }}>
|
|
276
|
+
<strong
|
|
277
|
+
style={{
|
|
278
|
+
fontFamily: theme.typography.fontFamily.display,
|
|
279
|
+
fontSize: theme.typography.fontSize.md,
|
|
280
|
+
color: theme.semantic.text.primary,
|
|
281
|
+
}}
|
|
282
|
+
>
|
|
283
|
+
{title ?? fallbackLabel}
|
|
284
|
+
</strong>
|
|
285
|
+
{description ? (
|
|
286
|
+
<span
|
|
287
|
+
style={{
|
|
288
|
+
fontSize: theme.typography.fontSize.sm,
|
|
289
|
+
color: theme.semantic.text.muted,
|
|
290
|
+
}}
|
|
291
|
+
>
|
|
292
|
+
{description}
|
|
293
|
+
</span>
|
|
294
|
+
) : null}
|
|
295
|
+
</div>
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
const fieldsContent =
|
|
299
|
+
visibleInputs.length > 0 ? (
|
|
300
|
+
<div
|
|
301
|
+
style={{
|
|
302
|
+
display: "flex",
|
|
303
|
+
flexDirection: "column",
|
|
304
|
+
gap: theme.space[3],
|
|
305
|
+
}}
|
|
306
|
+
>
|
|
307
|
+
{visibleInputs.map((input) => {
|
|
308
|
+
const key = input.key as keyof Params & string;
|
|
309
|
+
const inputRenderer = inputs?.[key];
|
|
310
|
+
return (
|
|
311
|
+
<InteractionField<Params>
|
|
312
|
+
key={input.key}
|
|
313
|
+
descriptor={descriptor}
|
|
314
|
+
inputKey={key}
|
|
315
|
+
handle={handle}
|
|
316
|
+
errors={fieldErrors[key] ?? []}
|
|
317
|
+
missing={missing.has(key)}
|
|
318
|
+
disabled={isDisabled}
|
|
319
|
+
render={
|
|
320
|
+
inputRenderer
|
|
321
|
+
? ({ input, errors, missing, disabled }) =>
|
|
322
|
+
inputRenderer(
|
|
323
|
+
createInteractionInputSlot({
|
|
324
|
+
descriptor,
|
|
325
|
+
input,
|
|
326
|
+
handle,
|
|
327
|
+
errors,
|
|
328
|
+
missing,
|
|
329
|
+
disabled,
|
|
330
|
+
}),
|
|
331
|
+
)
|
|
332
|
+
: renderFields?.[key]
|
|
333
|
+
}
|
|
334
|
+
/>
|
|
335
|
+
);
|
|
336
|
+
})}
|
|
337
|
+
</div>
|
|
338
|
+
) : null;
|
|
339
|
+
|
|
340
|
+
if (visibleInputs.length === 0 && !handle.isReady) {
|
|
341
|
+
throw new Error(
|
|
342
|
+
`InteractionForm '${descriptor.interactionKey}' has required inputs that cannot be rendered by the default form. Provide renderFields, select on a surface, or use a default-renderable input domain.`,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const formErrorContent =
|
|
347
|
+
formErrors.length > 0 ? (
|
|
348
|
+
<div
|
|
349
|
+
role="alert"
|
|
350
|
+
style={{
|
|
351
|
+
display: "flex",
|
|
352
|
+
flexDirection: "column",
|
|
353
|
+
gap: theme.space[1],
|
|
354
|
+
color: theme.semantic.intent.danger.solid,
|
|
355
|
+
fontSize: theme.typography.fontSize.sm,
|
|
356
|
+
}}
|
|
357
|
+
>
|
|
358
|
+
{formErrors.map((error) => (
|
|
359
|
+
<span key={error}>{error}</span>
|
|
360
|
+
))}
|
|
361
|
+
</div>
|
|
362
|
+
) : null;
|
|
363
|
+
|
|
364
|
+
const actions = (
|
|
365
|
+
<div
|
|
366
|
+
style={{
|
|
367
|
+
display: "flex",
|
|
368
|
+
gap: theme.space[2],
|
|
369
|
+
justifyContent: "flex-end",
|
|
370
|
+
}}
|
|
371
|
+
>
|
|
372
|
+
{onCancel ? (
|
|
373
|
+
<ThemedButton
|
|
374
|
+
type="button"
|
|
375
|
+
variant="secondary"
|
|
376
|
+
size="sm"
|
|
377
|
+
disabled={pending}
|
|
378
|
+
onClick={onCancel}
|
|
379
|
+
className="h-9 px-3 text-sm"
|
|
380
|
+
>
|
|
381
|
+
{cancelLabel}
|
|
382
|
+
</ThemedButton>
|
|
383
|
+
) : null}
|
|
384
|
+
{renderSubmit ? (
|
|
385
|
+
renderSubmit(
|
|
386
|
+
{
|
|
387
|
+
Button: ({ children, ...buttonProps }) => (
|
|
388
|
+
<ThemedButton
|
|
389
|
+
type="submit"
|
|
390
|
+
variant="primary"
|
|
391
|
+
size="sm"
|
|
392
|
+
disabled={isDisabled}
|
|
393
|
+
className="h-9 px-3 text-sm"
|
|
394
|
+
{...buttonProps}
|
|
395
|
+
>
|
|
396
|
+
{pending
|
|
397
|
+
? "Submitting..."
|
|
398
|
+
: (children ?? submitLabel ?? fallbackLabel)}
|
|
399
|
+
</ThemedButton>
|
|
400
|
+
),
|
|
401
|
+
},
|
|
402
|
+
descriptor,
|
|
403
|
+
)
|
|
404
|
+
) : (
|
|
405
|
+
<ThemedButton
|
|
406
|
+
type="submit"
|
|
407
|
+
variant="primary"
|
|
408
|
+
size="sm"
|
|
409
|
+
disabled={isDisabled}
|
|
410
|
+
className="h-9 px-3 text-sm"
|
|
411
|
+
>
|
|
412
|
+
{pending ? "Submitting..." : (submitLabel ?? fallbackLabel)}
|
|
413
|
+
</ThemedButton>
|
|
414
|
+
)}
|
|
415
|
+
</div>
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
const body = (
|
|
419
|
+
<>
|
|
420
|
+
{fieldsContent}
|
|
421
|
+
{formErrorContent}
|
|
422
|
+
{actions}
|
|
423
|
+
</>
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
return (
|
|
427
|
+
<form
|
|
428
|
+
data-interaction-form
|
|
429
|
+
data-interaction-id={descriptor.interactionId}
|
|
430
|
+
onSubmit={(event) => void submit(event)}
|
|
431
|
+
style={containerStyle}
|
|
432
|
+
>
|
|
433
|
+
{useAccordion ? (
|
|
434
|
+
<AccordionPrimitive.Root
|
|
435
|
+
type="single"
|
|
436
|
+
collapsible
|
|
437
|
+
value={accordionOpen ? "fields" : undefined}
|
|
438
|
+
onValueChange={(value: string) =>
|
|
439
|
+
setAccordionOpen(value === "fields")
|
|
440
|
+
}
|
|
441
|
+
>
|
|
442
|
+
<AccordionPrimitive.Item value="fields">
|
|
443
|
+
<AccordionPrimitive.Header style={{ margin: 0 }}>
|
|
444
|
+
<AccordionPrimitive.Trigger
|
|
445
|
+
style={{
|
|
446
|
+
alignItems: "center",
|
|
447
|
+
appearance: "none",
|
|
448
|
+
background: "transparent",
|
|
449
|
+
border: 0,
|
|
450
|
+
color: theme.semantic.text.primary,
|
|
451
|
+
cursor: "pointer",
|
|
452
|
+
display: "flex",
|
|
453
|
+
fontFamily: theme.typography.fontFamily.display,
|
|
454
|
+
fontSize: theme.typography.fontSize.md,
|
|
455
|
+
fontWeight: theme.typography.fontWeight.bold,
|
|
456
|
+
justifyContent: "space-between",
|
|
457
|
+
padding: 0,
|
|
458
|
+
textAlign: "left",
|
|
459
|
+
width: "100%",
|
|
460
|
+
}}
|
|
461
|
+
>
|
|
462
|
+
<span>{title ?? fallbackLabel}</span>
|
|
463
|
+
<span aria-hidden>{accordionOpen ? "−" : "+"}</span>
|
|
464
|
+
</AccordionPrimitive.Trigger>
|
|
465
|
+
</AccordionPrimitive.Header>
|
|
466
|
+
<AccordionPrimitive.Content>
|
|
467
|
+
<div
|
|
468
|
+
style={{
|
|
469
|
+
display: "flex",
|
|
470
|
+
flexDirection: "column",
|
|
471
|
+
gap: theme.space[2],
|
|
472
|
+
marginTop: theme.space[2],
|
|
473
|
+
}}
|
|
474
|
+
>
|
|
475
|
+
{description ? (
|
|
476
|
+
<span
|
|
477
|
+
style={{
|
|
478
|
+
fontSize: theme.typography.fontSize.sm,
|
|
479
|
+
color: theme.semantic.text.muted,
|
|
480
|
+
}}
|
|
481
|
+
>
|
|
482
|
+
{description}
|
|
483
|
+
</span>
|
|
484
|
+
) : null}
|
|
485
|
+
{body}
|
|
486
|
+
</div>
|
|
487
|
+
</AccordionPrimitive.Content>
|
|
488
|
+
</AccordionPrimitive.Item>
|
|
489
|
+
</AccordionPrimitive.Root>
|
|
490
|
+
) : (
|
|
491
|
+
<>
|
|
492
|
+
{header}
|
|
493
|
+
{body}
|
|
494
|
+
</>
|
|
495
|
+
)}
|
|
496
|
+
</form>
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function createInteractionInputSlot<
|
|
501
|
+
Params extends InteractionParamsShape,
|
|
502
|
+
Key extends keyof Params & string,
|
|
503
|
+
>({
|
|
504
|
+
descriptor,
|
|
505
|
+
input,
|
|
506
|
+
handle,
|
|
507
|
+
errors,
|
|
508
|
+
missing,
|
|
509
|
+
disabled,
|
|
510
|
+
}: {
|
|
511
|
+
descriptor: InteractionDescriptor;
|
|
512
|
+
input: InteractionInputDescriptor & { key: Key };
|
|
513
|
+
handle: InteractionHandle<Params>;
|
|
514
|
+
errors: readonly string[];
|
|
515
|
+
missing: boolean;
|
|
516
|
+
disabled: boolean;
|
|
517
|
+
}): InteractionInputSlot {
|
|
518
|
+
const value = handle.values[input.key] as Params[Key] | undefined;
|
|
519
|
+
const resolvedInput = resolveInputDomain(
|
|
520
|
+
input,
|
|
521
|
+
handle.values as Readonly<Record<string, unknown>>,
|
|
522
|
+
) as InteractionInputDescriptor & { key: Key };
|
|
523
|
+
|
|
524
|
+
const targetButton = ({
|
|
525
|
+
value: targetValue,
|
|
526
|
+
children,
|
|
527
|
+
kind,
|
|
528
|
+
...buttonProps
|
|
529
|
+
}: InteractionTargetSlotProps & { kind: "card" | "target" }) => {
|
|
530
|
+
const domain = resolvedInput.domain;
|
|
531
|
+
const eligible =
|
|
532
|
+
isResolvedTargetDomain(domain) &&
|
|
533
|
+
domain.eligibleTargets.includes(targetValue);
|
|
534
|
+
const selection = isTargetDomain(domain) ? domain.selection : undefined;
|
|
535
|
+
const currentValue = handle.values[input.key];
|
|
536
|
+
const selected =
|
|
537
|
+
selection?.mode === "many"
|
|
538
|
+
? Array.isArray(currentValue) &&
|
|
539
|
+
currentValue.map(String).includes(targetValue)
|
|
540
|
+
: currentValue !== undefined && String(currentValue) === targetValue;
|
|
541
|
+
const isDisabled = disabled || !eligible;
|
|
542
|
+
const dataAttribute =
|
|
543
|
+
kind === "card"
|
|
544
|
+
? { "data-dreamboard-interaction-card-slot": "" }
|
|
545
|
+
: { "data-dreamboard-interaction-target-slot": "" };
|
|
546
|
+
return (
|
|
547
|
+
<button
|
|
548
|
+
type="button"
|
|
549
|
+
disabled={isDisabled}
|
|
550
|
+
aria-disabled={isDisabled}
|
|
551
|
+
aria-pressed={selected}
|
|
552
|
+
data-input-name={input.key}
|
|
553
|
+
data-target-kind={inputTargetKind(domain)}
|
|
554
|
+
data-target-value={targetValue}
|
|
555
|
+
data-eligible={eligible}
|
|
556
|
+
data-selected={selected || undefined}
|
|
557
|
+
data-disabled={isDisabled || undefined}
|
|
558
|
+
{...dataAttribute}
|
|
559
|
+
{...buttonProps}
|
|
560
|
+
onClick={() => {
|
|
561
|
+
if (isDisabled) return;
|
|
562
|
+
const nextValue =
|
|
563
|
+
selection?.mode === "many"
|
|
564
|
+
? toggleManyValue(currentValue, targetValue, selection)
|
|
565
|
+
: targetValue;
|
|
566
|
+
handle.setInput(input.key, nextValue as Params[Key]);
|
|
567
|
+
}}
|
|
568
|
+
>
|
|
569
|
+
{children ?? targetValue}
|
|
570
|
+
</button>
|
|
571
|
+
);
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
return {
|
|
575
|
+
Field: () => (
|
|
576
|
+
<DefaultInteractionField
|
|
577
|
+
descriptor={descriptor}
|
|
578
|
+
input={resolvedInput}
|
|
579
|
+
handle={handle}
|
|
580
|
+
value={value}
|
|
581
|
+
setValue={(next) => handle.setInput(input.key, next)}
|
|
582
|
+
clearValue={() => handle.clearInput(input.key)}
|
|
583
|
+
errors={errors}
|
|
584
|
+
missing={missing}
|
|
585
|
+
disabled={disabled}
|
|
586
|
+
/>
|
|
587
|
+
),
|
|
588
|
+
Default: ({ children }) => {
|
|
589
|
+
const hasDefault = "defaultValue" in input;
|
|
590
|
+
const isDisabled = disabled || !hasDefault;
|
|
591
|
+
return (
|
|
592
|
+
<button
|
|
593
|
+
type="button"
|
|
594
|
+
disabled={isDisabled}
|
|
595
|
+
aria-disabled={isDisabled}
|
|
596
|
+
data-dreamboard-interaction-default-slot=""
|
|
597
|
+
data-input-name={input.key}
|
|
598
|
+
data-disabled={isDisabled || undefined}
|
|
599
|
+
onClick={() => {
|
|
600
|
+
if (isDisabled) return;
|
|
601
|
+
handle.setInput(input.key, input.defaultValue as Params[Key]);
|
|
602
|
+
}}
|
|
603
|
+
>
|
|
604
|
+
{children ?? "Use default"}
|
|
605
|
+
</button>
|
|
606
|
+
);
|
|
607
|
+
},
|
|
608
|
+
Target: (props) => targetButton({ ...props, kind: "target" }),
|
|
609
|
+
Card: (props) => targetButton({ ...props, kind: "card" }),
|
|
610
|
+
Cards: ({ children }) => {
|
|
611
|
+
const domain = resolvedInput.domain;
|
|
612
|
+
const targets = isResolvedTargetDomain(domain)
|
|
613
|
+
? domain.eligibleTargets
|
|
614
|
+
: [];
|
|
615
|
+
return <>{targets.map((id) => children({ id }))}</>;
|
|
616
|
+
},
|
|
617
|
+
Options: ({ children }) => {
|
|
618
|
+
const domain = resolvedInput.domain;
|
|
619
|
+
const choices =
|
|
620
|
+
domain.type === "choice" || domain.type === "choiceList"
|
|
621
|
+
? (domain.choices ?? [])
|
|
622
|
+
: [];
|
|
623
|
+
return <>{choices.map((choice) => children(choice))}</>;
|
|
624
|
+
},
|
|
625
|
+
Value: ({ children }) => <>{children(value)}</>,
|
|
626
|
+
Label: ({ children }) => <>{children ?? labelForInput(resolvedInput)}</>,
|
|
627
|
+
Message: ({ children }) => {
|
|
628
|
+
const message = children ?? errors[0] ?? (missing ? "Required" : null);
|
|
629
|
+
return message ? (
|
|
630
|
+
<span role="alert" data-dreamboard-interaction-message-slot="">
|
|
631
|
+
{message}
|
|
632
|
+
</span>
|
|
633
|
+
) : null;
|
|
634
|
+
},
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
export function InteractionField<
|
|
639
|
+
Params extends InteractionParamsShape = InteractionParamsShape,
|
|
640
|
+
Key extends keyof Params & string = keyof Params & string,
|
|
641
|
+
>({
|
|
642
|
+
descriptor,
|
|
643
|
+
inputKey,
|
|
644
|
+
handle,
|
|
645
|
+
errors = EMPTY_FIELD_ERRORS,
|
|
646
|
+
missing = false,
|
|
647
|
+
disabled = false,
|
|
648
|
+
render,
|
|
649
|
+
}: InteractionFieldProps<Params, Key>) {
|
|
650
|
+
const input = descriptor.inputs.find(
|
|
651
|
+
(candidate) => candidate.key === inputKey,
|
|
652
|
+
);
|
|
653
|
+
if (!input) return null;
|
|
654
|
+
const typedInput = resolveInputDomain(
|
|
655
|
+
input,
|
|
656
|
+
handle.values as Readonly<Record<string, unknown>>,
|
|
657
|
+
) as InteractionInputDescriptor & { key: Key };
|
|
658
|
+
const value = handle.values[inputKey] as Params[Key] | undefined;
|
|
659
|
+
const props: InteractionFieldRenderProps<Params, Key> = {
|
|
660
|
+
descriptor,
|
|
661
|
+
input: typedInput,
|
|
662
|
+
handle,
|
|
663
|
+
value,
|
|
664
|
+
setValue: (next) => handle.setInput(inputKey, next),
|
|
665
|
+
clearValue: () => handle.clearInput(inputKey),
|
|
666
|
+
errors,
|
|
667
|
+
missing,
|
|
668
|
+
disabled,
|
|
669
|
+
};
|
|
670
|
+
if (render) return <>{render(props)}</>;
|
|
671
|
+
return <DefaultInteractionField {...props} />;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
export function hasDefaultInteractionFormFields(
|
|
675
|
+
descriptor: Pick<InteractionDescriptor, "inputs">,
|
|
676
|
+
): boolean {
|
|
677
|
+
return defaultFormInputs(descriptor).length > 0;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
export function defaultFormInputs(
|
|
681
|
+
descriptor: Pick<InteractionDescriptor, "inputs">,
|
|
682
|
+
values: Readonly<Record<string, unknown>> = {},
|
|
683
|
+
): InteractionInputDescriptor[] {
|
|
684
|
+
return resolveInteractionInputs(descriptor, values).filter((input) => {
|
|
685
|
+
switch (input.domain.type) {
|
|
686
|
+
case "choice":
|
|
687
|
+
case "choiceList":
|
|
688
|
+
case "resourceMap":
|
|
689
|
+
case "boundedNumber":
|
|
690
|
+
return true;
|
|
691
|
+
case "cardTarget":
|
|
692
|
+
case "boardTarget":
|
|
693
|
+
return input.domain.selection?.mode === "many";
|
|
694
|
+
}
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
function DefaultInteractionField<
|
|
699
|
+
Params extends InteractionParamsShape,
|
|
700
|
+
Key extends keyof Params & string,
|
|
701
|
+
>(props: InteractionFieldRenderProps<Params, Key>) {
|
|
702
|
+
const { input } = props;
|
|
703
|
+
switch (input.domain.type) {
|
|
704
|
+
case "choice":
|
|
705
|
+
if (input.domain.selection?.mode === "many") {
|
|
706
|
+
return (
|
|
707
|
+
<ChoiceListField
|
|
708
|
+
{...props}
|
|
709
|
+
domain={{
|
|
710
|
+
type: "choiceList",
|
|
711
|
+
choices: input.domain.choices,
|
|
712
|
+
min: input.domain.selection.min ?? 0,
|
|
713
|
+
max: input.domain.selection.max,
|
|
714
|
+
selection: input.domain.selection,
|
|
715
|
+
}}
|
|
716
|
+
/>
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
return <ChoiceField {...props} domain={input.domain} />;
|
|
720
|
+
case "choiceList":
|
|
721
|
+
return <ChoiceListField {...props} domain={input.domain} />;
|
|
722
|
+
case "resourceMap":
|
|
723
|
+
return <ResourceMapField {...props} domain={input.domain} />;
|
|
724
|
+
case "boundedNumber":
|
|
725
|
+
return <BoundedNumberField {...props} domain={input.domain} />;
|
|
726
|
+
case "cardTarget":
|
|
727
|
+
case "boardTarget":
|
|
728
|
+
return <TargetSummaryField {...props} domain={input.domain} />;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
function FieldFrame({
|
|
733
|
+
label,
|
|
734
|
+
controlId,
|
|
735
|
+
errors,
|
|
736
|
+
missing,
|
|
737
|
+
children,
|
|
738
|
+
}: {
|
|
739
|
+
label: ReactNode;
|
|
740
|
+
controlId?: string;
|
|
741
|
+
errors: readonly string[];
|
|
742
|
+
missing: boolean;
|
|
743
|
+
children: ReactNode;
|
|
744
|
+
}) {
|
|
745
|
+
const theme = useTheme();
|
|
746
|
+
const messages = errors.length > 0 ? errors : missing ? ["Required"] : [];
|
|
747
|
+
return (
|
|
748
|
+
<div
|
|
749
|
+
style={{
|
|
750
|
+
display: "flex",
|
|
751
|
+
flexDirection: "column",
|
|
752
|
+
gap: theme.space[1],
|
|
753
|
+
fontSize: theme.typography.fontSize.sm,
|
|
754
|
+
color: theme.semantic.text.primary,
|
|
755
|
+
}}
|
|
756
|
+
>
|
|
757
|
+
<Label
|
|
758
|
+
htmlFor={controlId}
|
|
759
|
+
style={{
|
|
760
|
+
color: theme.semantic.text.primary,
|
|
761
|
+
fontWeight: theme.typography.fontWeight.bold,
|
|
762
|
+
}}
|
|
763
|
+
>
|
|
764
|
+
{label}
|
|
765
|
+
</Label>
|
|
766
|
+
{children}
|
|
767
|
+
{messages.length > 0 ? (
|
|
768
|
+
<span
|
|
769
|
+
role="alert"
|
|
770
|
+
style={{
|
|
771
|
+
display: "flex",
|
|
772
|
+
flexDirection: "column",
|
|
773
|
+
gap: 2,
|
|
774
|
+
color: theme.semantic.intent.danger.solid,
|
|
775
|
+
fontSize: theme.typography.fontSize.xs,
|
|
776
|
+
}}
|
|
777
|
+
>
|
|
778
|
+
{messages.map((message) => (
|
|
779
|
+
<span key={message}>{message}</span>
|
|
780
|
+
))}
|
|
781
|
+
</span>
|
|
782
|
+
) : null}
|
|
783
|
+
</div>
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
function ChoiceOptionLabel({ choice }: { choice: InteractionChoiceOption }) {
|
|
788
|
+
const theme = useTheme();
|
|
789
|
+
return (
|
|
790
|
+
<span
|
|
791
|
+
style={{
|
|
792
|
+
display: "inline-flex",
|
|
793
|
+
alignItems: "center",
|
|
794
|
+
gap: 6,
|
|
795
|
+
minWidth: 0,
|
|
796
|
+
}}
|
|
797
|
+
>
|
|
798
|
+
<span
|
|
799
|
+
style={{
|
|
800
|
+
display: "inline-flex",
|
|
801
|
+
alignItems: "center",
|
|
802
|
+
gap: 6,
|
|
803
|
+
minWidth: 0,
|
|
804
|
+
}}
|
|
805
|
+
>
|
|
806
|
+
{choice.icon ? (
|
|
807
|
+
<span aria-hidden style={{ fontSize: "1.1em", lineHeight: 1 }}>
|
|
808
|
+
{choice.icon}
|
|
809
|
+
</span>
|
|
810
|
+
) : null}
|
|
811
|
+
<span style={{ overflow: "hidden", textOverflow: "ellipsis" }}>
|
|
812
|
+
{choice.label}
|
|
813
|
+
</span>
|
|
814
|
+
</span>
|
|
815
|
+
{choice.badge ? (
|
|
816
|
+
<span
|
|
817
|
+
style={{
|
|
818
|
+
borderRadius: 999,
|
|
819
|
+
background: theme.semantic.surface.inset,
|
|
820
|
+
color: theme.semantic.text.muted,
|
|
821
|
+
fontSize: theme.typography.fontSize.xs,
|
|
822
|
+
fontWeight: theme.typography.fontWeight.bold,
|
|
823
|
+
lineHeight: 1,
|
|
824
|
+
padding: "3px 6px",
|
|
825
|
+
whiteSpace: "nowrap",
|
|
826
|
+
}}
|
|
827
|
+
>
|
|
828
|
+
{choice.badge}
|
|
829
|
+
</span>
|
|
830
|
+
) : null}
|
|
831
|
+
</span>
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function ChoiceDescription({ choice }: { choice?: InteractionChoiceOption }) {
|
|
836
|
+
const theme = useTheme();
|
|
837
|
+
const message = choice?.disabledReason ?? choice?.description;
|
|
838
|
+
if (!message) return null;
|
|
839
|
+
return (
|
|
840
|
+
<span
|
|
841
|
+
style={{
|
|
842
|
+
color: choice?.disabledReason
|
|
843
|
+
? theme.semantic.intent.danger.solid
|
|
844
|
+
: theme.semantic.text.muted,
|
|
845
|
+
fontSize: theme.typography.fontSize.xs,
|
|
846
|
+
}}
|
|
847
|
+
>
|
|
848
|
+
{message}
|
|
849
|
+
</span>
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
const NULL_CHOICE_SELECT_VALUE = "__dreamboard_null_choice__";
|
|
854
|
+
|
|
855
|
+
function choiceRenderKey(choice: InteractionChoiceOption): string {
|
|
856
|
+
return choice.value === null ? NULL_CHOICE_SELECT_VALUE : choice.value;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
function encodeChoiceSelectValue(value: unknown): string | undefined {
|
|
860
|
+
if (value === null) return NULL_CHOICE_SELECT_VALUE;
|
|
861
|
+
return typeof value === "string" ? value : undefined;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
function decodeChoiceSelectValue(value: string): string | null {
|
|
865
|
+
return value === NULL_CHOICE_SELECT_VALUE ? null : value;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
function ChoiceField<
|
|
869
|
+
Params extends InteractionParamsShape,
|
|
870
|
+
Key extends keyof Params & string,
|
|
871
|
+
>({
|
|
872
|
+
input,
|
|
873
|
+
value,
|
|
874
|
+
setValue,
|
|
875
|
+
errors,
|
|
876
|
+
missing,
|
|
877
|
+
disabled,
|
|
878
|
+
domain,
|
|
879
|
+
}: InteractionFieldRenderProps<Params, Key> & {
|
|
880
|
+
domain: Extract<InputDomain, { type: "choice" }>;
|
|
881
|
+
}) {
|
|
882
|
+
const theme = useTheme();
|
|
883
|
+
const themeCssVars = useThemeCssVars();
|
|
884
|
+
const choices = domain.choices ?? [];
|
|
885
|
+
const controlId = useId();
|
|
886
|
+
const selectedChoice =
|
|
887
|
+
typeof value === "string" || value === null
|
|
888
|
+
? choices.find((choice) => choice.value === value)
|
|
889
|
+
: undefined;
|
|
890
|
+
if (choices.length > 0 && choices.length <= 3) {
|
|
891
|
+
return (
|
|
892
|
+
<FieldFrame
|
|
893
|
+
label={labelForInput(input)}
|
|
894
|
+
errors={errors}
|
|
895
|
+
missing={missing}
|
|
896
|
+
>
|
|
897
|
+
<span
|
|
898
|
+
style={{
|
|
899
|
+
display: "inline-flex",
|
|
900
|
+
flexWrap: "wrap",
|
|
901
|
+
gap: theme.space[1],
|
|
902
|
+
}}
|
|
903
|
+
>
|
|
904
|
+
{choices.map((choice) => {
|
|
905
|
+
const selected = value === choice.value;
|
|
906
|
+
return (
|
|
907
|
+
<ThemedButton
|
|
908
|
+
key={choiceRenderKey(choice)}
|
|
909
|
+
type="button"
|
|
910
|
+
variant={selected ? "primary" : "secondary"}
|
|
911
|
+
size="sm"
|
|
912
|
+
disabled={disabled || choice.disabled}
|
|
913
|
+
aria-pressed={selected}
|
|
914
|
+
title={choice.disabledReason ?? choice.description}
|
|
915
|
+
onClick={() => setValue(choice.value as Params[Key])}
|
|
916
|
+
className="h-8 px-3 text-sm"
|
|
917
|
+
>
|
|
918
|
+
<ChoiceOptionLabel choice={choice} />
|
|
919
|
+
</ThemedButton>
|
|
920
|
+
);
|
|
921
|
+
})}
|
|
922
|
+
</span>
|
|
923
|
+
</FieldFrame>
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
return (
|
|
927
|
+
<FieldFrame
|
|
928
|
+
label={labelForInput(input)}
|
|
929
|
+
controlId={controlId}
|
|
930
|
+
errors={errors}
|
|
931
|
+
missing={missing}
|
|
932
|
+
>
|
|
933
|
+
<Select
|
|
934
|
+
disabled={disabled}
|
|
935
|
+
value={encodeChoiceSelectValue(value)}
|
|
936
|
+
onValueChange={(next: string) =>
|
|
937
|
+
setValue(decodeChoiceSelectValue(next) as Params[Key])
|
|
938
|
+
}
|
|
939
|
+
>
|
|
940
|
+
<SelectTrigger id={controlId} size="sm" className="w-full bg-white">
|
|
941
|
+
<span data-slot="select-value">
|
|
942
|
+
{selectedChoice ? (
|
|
943
|
+
<ChoiceOptionLabel choice={selectedChoice} />
|
|
944
|
+
) : (
|
|
945
|
+
<span style={{ color: theme.semantic.text.muted }}>
|
|
946
|
+
Choose...
|
|
947
|
+
</span>
|
|
948
|
+
)}
|
|
949
|
+
</span>
|
|
950
|
+
</SelectTrigger>
|
|
951
|
+
<SelectContent
|
|
952
|
+
style={{
|
|
953
|
+
...themeCssVars,
|
|
954
|
+
fontFamily: theme.typography.fontFamily.body,
|
|
955
|
+
}}
|
|
956
|
+
>
|
|
957
|
+
{choices.map((choice) => (
|
|
958
|
+
<SelectItem
|
|
959
|
+
key={choiceRenderKey(choice)}
|
|
960
|
+
value={choiceRenderKey(choice)}
|
|
961
|
+
textValue={choice.label}
|
|
962
|
+
disabled={choice.disabled}
|
|
963
|
+
>
|
|
964
|
+
<ChoiceOptionLabel choice={choice} />
|
|
965
|
+
</SelectItem>
|
|
966
|
+
))}
|
|
967
|
+
</SelectContent>
|
|
968
|
+
</Select>
|
|
969
|
+
<ChoiceDescription choice={selectedChoice} />
|
|
970
|
+
</FieldFrame>
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
function ChoiceListField<
|
|
975
|
+
Params extends InteractionParamsShape,
|
|
976
|
+
Key extends keyof Params & string,
|
|
977
|
+
>({
|
|
978
|
+
input,
|
|
979
|
+
value,
|
|
980
|
+
setValue,
|
|
981
|
+
errors,
|
|
982
|
+
missing,
|
|
983
|
+
disabled,
|
|
984
|
+
domain,
|
|
985
|
+
}: InteractionFieldRenderProps<Params, Key> & {
|
|
986
|
+
domain: Extract<InputDomain, { type: "choiceList" }>;
|
|
987
|
+
}) {
|
|
988
|
+
const theme = useTheme();
|
|
989
|
+
const selected = new Set(Array.isArray(value) ? (value as string[]) : []);
|
|
990
|
+
const min =
|
|
991
|
+
domain.selection?.mode === "many"
|
|
992
|
+
? (domain.selection.min ?? 0)
|
|
993
|
+
: domain.min;
|
|
994
|
+
const max =
|
|
995
|
+
(domain.selection?.mode === "many" ? domain.selection.max : domain.max) ??
|
|
996
|
+
domain.choices?.length ??
|
|
997
|
+
Number.POSITIVE_INFINITY;
|
|
998
|
+
const toggle = (choice: string) => {
|
|
999
|
+
const next = new Set(selected);
|
|
1000
|
+
if (next.has(choice)) next.delete(choice);
|
|
1001
|
+
else if (next.size < max) next.add(choice);
|
|
1002
|
+
setValue([...next] as Params[Key]);
|
|
1003
|
+
};
|
|
1004
|
+
const meta =
|
|
1005
|
+
min || Number.isFinite(max)
|
|
1006
|
+
? `Pick ${min ?? 0}${Number.isFinite(max) ? `-${max}` : "+"}`
|
|
1007
|
+
: undefined;
|
|
1008
|
+
return (
|
|
1009
|
+
<FieldFrame
|
|
1010
|
+
label={
|
|
1011
|
+
<span
|
|
1012
|
+
style={{
|
|
1013
|
+
display: "flex",
|
|
1014
|
+
justifyContent: "space-between",
|
|
1015
|
+
gap: theme.space[2],
|
|
1016
|
+
}}
|
|
1017
|
+
>
|
|
1018
|
+
<span>{labelForInput(input)}</span>
|
|
1019
|
+
{meta ? (
|
|
1020
|
+
<span style={{ color: theme.semantic.text.muted }}>{meta}</span>
|
|
1021
|
+
) : null}
|
|
1022
|
+
</span>
|
|
1023
|
+
}
|
|
1024
|
+
errors={errors}
|
|
1025
|
+
missing={missing}
|
|
1026
|
+
>
|
|
1027
|
+
<span style={{ display: "flex", flexWrap: "wrap", gap: theme.space[1] }}>
|
|
1028
|
+
{(domain.choices ?? []).map((choice) => {
|
|
1029
|
+
const value = choice.value as string;
|
|
1030
|
+
const checked = selected.has(value);
|
|
1031
|
+
return (
|
|
1032
|
+
<ThemedButton
|
|
1033
|
+
key={value}
|
|
1034
|
+
type="button"
|
|
1035
|
+
variant={checked ? "primary" : "secondary"}
|
|
1036
|
+
size="sm"
|
|
1037
|
+
disabled={
|
|
1038
|
+
disabled ||
|
|
1039
|
+
choice.disabled ||
|
|
1040
|
+
(!checked && selected.size >= max)
|
|
1041
|
+
}
|
|
1042
|
+
aria-pressed={checked}
|
|
1043
|
+
title={choice.disabledReason ?? choice.description}
|
|
1044
|
+
onClick={() => toggle(value)}
|
|
1045
|
+
className="h-8 px-3 text-sm"
|
|
1046
|
+
>
|
|
1047
|
+
<ChoiceOptionLabel choice={choice} />
|
|
1048
|
+
</ThemedButton>
|
|
1049
|
+
);
|
|
1050
|
+
})}
|
|
1051
|
+
</span>
|
|
1052
|
+
</FieldFrame>
|
|
1053
|
+
);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
function ResourceMapField<
|
|
1057
|
+
Params extends InteractionParamsShape,
|
|
1058
|
+
Key extends keyof Params & string,
|
|
1059
|
+
>({
|
|
1060
|
+
input,
|
|
1061
|
+
value,
|
|
1062
|
+
setValue,
|
|
1063
|
+
errors,
|
|
1064
|
+
missing,
|
|
1065
|
+
disabled,
|
|
1066
|
+
domain,
|
|
1067
|
+
}: InteractionFieldRenderProps<Params, Key> & {
|
|
1068
|
+
domain: Extract<InputDomain, { type: "resourceMap" }>;
|
|
1069
|
+
}) {
|
|
1070
|
+
const theme = useTheme();
|
|
1071
|
+
const current: Record<string, unknown> = isRecord(value) ? value : {};
|
|
1072
|
+
const update = (
|
|
1073
|
+
resourceId: string,
|
|
1074
|
+
delta: number,
|
|
1075
|
+
min: number,
|
|
1076
|
+
max: number,
|
|
1077
|
+
) => {
|
|
1078
|
+
const previous = numberOrZero(current[resourceId]);
|
|
1079
|
+
const next = Math.max(min, Math.min(max, previous + delta));
|
|
1080
|
+
setValue({ ...current, [resourceId]: next } as Params[Key]);
|
|
1081
|
+
};
|
|
1082
|
+
return (
|
|
1083
|
+
<FieldFrame label={labelForInput(input)} errors={errors} missing={missing}>
|
|
1084
|
+
<span
|
|
1085
|
+
style={{
|
|
1086
|
+
display: "grid",
|
|
1087
|
+
gridTemplateColumns: "repeat(auto-fit, minmax(140px, 1fr))",
|
|
1088
|
+
gap: theme.space[2],
|
|
1089
|
+
}}
|
|
1090
|
+
>
|
|
1091
|
+
{(domain.resources ?? []).map((resource) => {
|
|
1092
|
+
const min = resource.min ?? 0;
|
|
1093
|
+
const max = resource.max ?? 0;
|
|
1094
|
+
const amount = numberOrZero(current[resource.resourceId]);
|
|
1095
|
+
return (
|
|
1096
|
+
<span
|
|
1097
|
+
key={resource.resourceId}
|
|
1098
|
+
style={{
|
|
1099
|
+
display: "grid",
|
|
1100
|
+
gridTemplateColumns: "minmax(0, 1fr) auto auto auto",
|
|
1101
|
+
alignItems: "center",
|
|
1102
|
+
gap: theme.space[1],
|
|
1103
|
+
padding: theme.space[2],
|
|
1104
|
+
borderRadius: theme.radius.md,
|
|
1105
|
+
background: theme.semantic.surface.inset,
|
|
1106
|
+
}}
|
|
1107
|
+
>
|
|
1108
|
+
<span
|
|
1109
|
+
style={{
|
|
1110
|
+
minWidth: 0,
|
|
1111
|
+
display: "inline-flex",
|
|
1112
|
+
alignItems: "center",
|
|
1113
|
+
gap: theme.space[1],
|
|
1114
|
+
}}
|
|
1115
|
+
>
|
|
1116
|
+
{resource.icon ? (
|
|
1117
|
+
<span aria-hidden style={{ fontSize: "1.1em" }}>
|
|
1118
|
+
{resource.icon}
|
|
1119
|
+
</span>
|
|
1120
|
+
) : null}
|
|
1121
|
+
{resource.label ?? humanize(input.key)}
|
|
1122
|
+
</span>
|
|
1123
|
+
<StepperButton
|
|
1124
|
+
label={`Decrease ${resource.label ?? resource.resourceId}`}
|
|
1125
|
+
disabled={disabled || amount <= min}
|
|
1126
|
+
onClick={() => update(resource.resourceId, -1, min, max)}
|
|
1127
|
+
>
|
|
1128
|
+
-
|
|
1129
|
+
</StepperButton>
|
|
1130
|
+
<span
|
|
1131
|
+
style={{
|
|
1132
|
+
minWidth: "2ch",
|
|
1133
|
+
textAlign: "center",
|
|
1134
|
+
fontVariantNumeric: "tabular-nums",
|
|
1135
|
+
}}
|
|
1136
|
+
>
|
|
1137
|
+
{amount}
|
|
1138
|
+
</span>
|
|
1139
|
+
<StepperButton
|
|
1140
|
+
label={`Increase ${resource.label ?? resource.resourceId}`}
|
|
1141
|
+
disabled={disabled || amount >= max}
|
|
1142
|
+
onClick={() => update(resource.resourceId, 1, min, max)}
|
|
1143
|
+
>
|
|
1144
|
+
+
|
|
1145
|
+
</StepperButton>
|
|
1146
|
+
</span>
|
|
1147
|
+
);
|
|
1148
|
+
})}
|
|
1149
|
+
</span>
|
|
1150
|
+
</FieldFrame>
|
|
1151
|
+
);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
function BoundedNumberField<
|
|
1155
|
+
Params extends InteractionParamsShape,
|
|
1156
|
+
Key extends keyof Params & string,
|
|
1157
|
+
>({
|
|
1158
|
+
input,
|
|
1159
|
+
value,
|
|
1160
|
+
setValue,
|
|
1161
|
+
errors,
|
|
1162
|
+
missing,
|
|
1163
|
+
disabled,
|
|
1164
|
+
domain,
|
|
1165
|
+
}: InteractionFieldRenderProps<Params, Key> & {
|
|
1166
|
+
domain: Extract<InputDomain, { type: "boundedNumber" }>;
|
|
1167
|
+
}) {
|
|
1168
|
+
const theme = useTheme();
|
|
1169
|
+
const min = domain.min ?? 0;
|
|
1170
|
+
const max = domain.max ?? Number.POSITIVE_INFINITY;
|
|
1171
|
+
const step = domain.step ?? 1;
|
|
1172
|
+
const current = typeof value === "number" ? value : min;
|
|
1173
|
+
const controlId = useId();
|
|
1174
|
+
const update = (next: number) =>
|
|
1175
|
+
setValue(Math.max(min, Math.min(max, next)) as Params[Key]);
|
|
1176
|
+
return (
|
|
1177
|
+
<FieldFrame
|
|
1178
|
+
label={labelForInput(input)}
|
|
1179
|
+
controlId={controlId}
|
|
1180
|
+
errors={errors}
|
|
1181
|
+
missing={missing}
|
|
1182
|
+
>
|
|
1183
|
+
<span
|
|
1184
|
+
style={{
|
|
1185
|
+
display: "inline-flex",
|
|
1186
|
+
alignItems: "center",
|
|
1187
|
+
gap: theme.space[1],
|
|
1188
|
+
}}
|
|
1189
|
+
>
|
|
1190
|
+
<StepperButton
|
|
1191
|
+
label={`Decrease ${input.key}`}
|
|
1192
|
+
disabled={disabled || current <= min}
|
|
1193
|
+
onClick={() => update(current - step)}
|
|
1194
|
+
>
|
|
1195
|
+
-
|
|
1196
|
+
</StepperButton>
|
|
1197
|
+
<Input
|
|
1198
|
+
id={controlId}
|
|
1199
|
+
type="number"
|
|
1200
|
+
min={min}
|
|
1201
|
+
max={Number.isFinite(max) ? max : undefined}
|
|
1202
|
+
step={step}
|
|
1203
|
+
value={current}
|
|
1204
|
+
disabled={disabled}
|
|
1205
|
+
onChange={(event) => update(Number(event.target.value))}
|
|
1206
|
+
className="h-9 w-[8ch] px-2 text-center text-sm md:text-sm"
|
|
1207
|
+
/>
|
|
1208
|
+
<StepperButton
|
|
1209
|
+
label={`Increase ${input.key}`}
|
|
1210
|
+
disabled={disabled || current >= max}
|
|
1211
|
+
onClick={() => update(current + step)}
|
|
1212
|
+
>
|
|
1213
|
+
+
|
|
1214
|
+
</StepperButton>
|
|
1215
|
+
</span>
|
|
1216
|
+
</FieldFrame>
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
function TargetSummaryField<
|
|
1221
|
+
Params extends InteractionParamsShape,
|
|
1222
|
+
Key extends keyof Params & string,
|
|
1223
|
+
>({
|
|
1224
|
+
input,
|
|
1225
|
+
value,
|
|
1226
|
+
errors,
|
|
1227
|
+
missing,
|
|
1228
|
+
domain,
|
|
1229
|
+
}: InteractionFieldRenderProps<Params, Key> & {
|
|
1230
|
+
domain: Extract<InputDomain, { type: "cardTarget" | "boardTarget" }>;
|
|
1231
|
+
}) {
|
|
1232
|
+
const target = inputTargetKind(domain) ?? "target";
|
|
1233
|
+
return (
|
|
1234
|
+
<FieldFrame label={labelForInput(input)} errors={errors} missing={missing}>
|
|
1235
|
+
<span>
|
|
1236
|
+
{Array.isArray(value)
|
|
1237
|
+
? value.length > 0
|
|
1238
|
+
? `${value.length} selected: ${value.join(", ")}`
|
|
1239
|
+
: `Select ${targetSelectionLabel(domain)}.`
|
|
1240
|
+
: typeof value === "string"
|
|
1241
|
+
? value
|
|
1242
|
+
: `Select a ${target} on the board.`}
|
|
1243
|
+
</span>
|
|
1244
|
+
</FieldFrame>
|
|
1245
|
+
);
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
function targetSelectionLabel(domain: InputDomain): string {
|
|
1249
|
+
const target = inputTargetKind(domain) ?? "target";
|
|
1250
|
+
const selection = isTargetDomain(domain) ? domain.selection : undefined;
|
|
1251
|
+
if (selection?.mode !== "many") return `a ${target}`;
|
|
1252
|
+
const min = selection.min ?? 0;
|
|
1253
|
+
if (selection.max !== undefined && min === selection.max) {
|
|
1254
|
+
return `${min} ${target}${min === 1 ? "" : "s"}`;
|
|
1255
|
+
}
|
|
1256
|
+
return `${min}${selection.max ? `-${selection.max}` : "+"} ${target}s`;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
function StepperButton({
|
|
1260
|
+
label,
|
|
1261
|
+
disabled,
|
|
1262
|
+
onClick,
|
|
1263
|
+
children,
|
|
1264
|
+
}: {
|
|
1265
|
+
label: string;
|
|
1266
|
+
disabled: boolean;
|
|
1267
|
+
onClick: () => void;
|
|
1268
|
+
children: ReactNode;
|
|
1269
|
+
}) {
|
|
1270
|
+
return (
|
|
1271
|
+
<ThemedButton
|
|
1272
|
+
type="button"
|
|
1273
|
+
variant="secondary"
|
|
1274
|
+
size="sm"
|
|
1275
|
+
aria-label={label}
|
|
1276
|
+
disabled={disabled}
|
|
1277
|
+
onClick={onClick}
|
|
1278
|
+
className="h-8 w-8 text-sm"
|
|
1279
|
+
>
|
|
1280
|
+
{children}
|
|
1281
|
+
</ThemedButton>
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
function labelForInput(input: InteractionInputDescriptor): string {
|
|
1286
|
+
if (input.domain.type === "choice") {
|
|
1287
|
+
const exact = input.domain.choices?.find(
|
|
1288
|
+
(choice) => choice.value === input.defaultValue,
|
|
1289
|
+
);
|
|
1290
|
+
if (exact?.label && input.key === exact.value) return exact.label;
|
|
1291
|
+
}
|
|
1292
|
+
return humanize(input.key);
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
function humanize(key: string): string {
|
|
1296
|
+
return key
|
|
1297
|
+
.replace(/Id$/, "")
|
|
1298
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
1299
|
+
.replace(/[-_]+/g, " ")
|
|
1300
|
+
.replace(/\b\w/g, (letter) => letter.toUpperCase());
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
1304
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
function numberOrZero(value: unknown): number {
|
|
1308
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
1309
|
+
}
|