@stamprally/react 0.9.0 → 0.10.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.
Files changed (2) hide show
  1. package/README.md +11 -84
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,102 +1,29 @@
1
1
  # @stamprally/react
2
2
 
3
- React 19 integration for [`@stamprally/core`](https://www.npmjs.com/package/@stamprally/core).
4
- The package provides the `useStampRally` hook with external-store updates,
5
- optimistic stamp acquisition, persistence, reward redemption, and recovery-code
6
- support.
7
-
8
- ## Install
9
-
10
- ```sh
11
- npm install @stamprally/core @stamprally/react react
12
- ```
13
-
14
- Or with pnpm:
15
-
16
- ```sh
17
- pnpm add @stamprally/core @stamprally/react react
18
- ```
19
-
20
- This package supports React `>=19.0.0 <20.0.0`.
21
-
22
- ## Quick start
3
+ React integration for `@stamprally/core`. `useStampRally` subscribes to immutable client state and exposes check-in, reward-claim, synchronization, and user-switching operations.
23
4
 
24
5
  ```tsx
25
6
  import { useMemo } from "react";
26
- import {
27
- LocalStorageAdapter,
28
- StampRallyClient,
29
- type RallyConfig,
30
- } from "@stamprally/core";
7
+ import { StampRallyClient, type PublicRallyConfig } from "@stamprally/core";
31
8
  import { useStampRally } from "@stamprally/react";
32
9
 
33
- const config: RallyConfig = {
10
+ const config: PublicRallyConfig = {
34
11
  id: "city-tour",
35
- stamps: [
36
- {
37
- id: "station",
38
- name: "Central Station",
39
- condition: { type: "instant" },
40
- },
41
- ],
12
+ version: "0.9.1",
13
+ title: "City Tour",
14
+ spots: [{ id: "station", orderIndex: 0, name: "Station", conditions: [{ type: "passcode" }] }],
15
+ rewards: [],
42
16
  };
43
17
 
44
18
  export function Rally() {
45
- const client = useMemo(
46
- () => new StampRallyClient(config, new LocalStorageAdapter()),
47
- [],
48
- );
49
- const { state, isLoading, isPending, error, acquire } = useStampRally(client);
50
-
19
+ const client = useMemo(() => new StampRallyClient(config), []);
20
+ const { state, isLoading, onCheckIn } = useStampRally(client);
51
21
  if (isLoading) return <p>Loading…</p>;
52
-
53
- return (
54
- <section>
55
- <p>
56
- {state?.records.length ?? 0} stamp(s) collected
57
- </p>
58
- <button
59
- disabled={isPending}
60
- onClick={() => void acquire("station", { type: "instant" })}
61
- >
62
- Collect stamp
63
- </button>
64
- {error !== null && <p role="alert">Could not collect the stamp.</p>}
65
- </section>
66
- );
22
+ return <button onClick={() => void onCheckIn("station", "ARRIVED")}>{state?.records.length ?? 0} checked in</button>;
67
23
  }
68
24
  ```
69
25
 
70
- The hook initializes the client when needed, subscribes to state changes, and
71
- keeps the UI synchronized with persisted state. Keep the `StampRallyClient`
72
- instance stable, for example with `useMemo`, so that the hook does not switch
73
- clients on every render.
74
-
75
- ## Hook return value
76
-
77
- `useStampRally(client)` returns:
78
-
79
- - `state`: the current `StampRallyState`, or `null` while it is not initialized.
80
- - `isLoading`: whether the client is initializing or changing clients.
81
- - `isPending`: whether an acquisition, reset, redemption, or import is pending.
82
- - `error`: the latest typed engine/reward error or storage error.
83
- - `rewardsState`: the current reward states, or an empty array when rewards are not configured.
84
- - `acquire(stampId, context, now?)`: validates and persists a stamp acquisition.
85
- - `reset(now?)`: clears the persisted rally state.
86
- - `redeem(rewardId, options?)`: redeems an available reward, optionally with a staff passcode and ID.
87
- - `exportRecoveryCode()`: exports confirmed stamp and reward progress.
88
- - `importRecoveryCode(token)`: restores a rally-scoped recovery code and returns whether it was valid.
89
-
90
- Acquisitions are shown optimistically while persistence is pending. Engine
91
- validation and storage failures roll the optimistic state back and are exposed
92
- through the returned `error` value and the rejected promise where applicable.
93
-
94
- ## Browser and server rendering
95
-
96
- The hook uses React's external-store API and provides a `null` server snapshot.
97
- The core package's browser adapters and detectors access browser globals only
98
- when called, so applications can choose a different `StampStorage` for server
99
- rendering, tests, or non-browser environments.
26
+ The hook return value includes `state`, `isLoading`, `error`, `onCheckIn`, `onClaimReward`, `onSync`, `switchUser`, and `clearUserState`.
100
27
 
101
28
  ## License
102
29
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stamprally/react",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "React hooks for @stamprally/core.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -31,7 +31,7 @@
31
31
  }
32
32
  },
33
33
  "dependencies": {
34
- "@stamprally/core": "0.9.0"
34
+ "@stamprally/core": "0.10.0"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "react": "^18.0.0 || ^19.0.0",