@echoxyz/sonar-react 0.6.2 → 0.8.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/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @echoxyz/sonar-react
2
2
 
3
+ ## 0.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5b96860: Add listAvailableEntities / useSonarEntities
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [5b96860]
12
+ - Updated dependencies [ef7e0f9]
13
+ - @echoxyz/sonar-core@0.7.0
14
+
15
+ ## 0.7.0
16
+
17
+ ### Minor Changes
18
+
19
+ - f47a532: Replace EntityUUID + ObfuscatedEntityID in the API interface with a single EntityID
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies [f47a532]
24
+ - @echoxyz/sonar-core@0.6.0
25
+
3
26
  ## 0.6.2
4
27
 
5
28
  ### Patch Changes
package/README.md CHANGED
@@ -114,22 +114,23 @@ const ExampleEntityPanel = () => {
114
114
  </div>
115
115
  );
116
116
  };
117
-
118
117
  ```
119
118
 
119
+ If you want to fetch all entities associated with the logged in user, you can use the `useSonarEntities` hook.
120
+
120
121
  5. Implement the purchase flow
121
122
 
122
123
  ```tsx
123
124
  function Example({
124
- entityUUID,
125
+ entityID,
125
126
  walletAddress,
126
127
  }: {
127
- entityUUID: string;
128
+ entityID: string;
128
129
  walletAddress: string;
129
130
  }) {
130
131
  const sonarPurchaser = useSonarPurchase({
131
132
  saleUUID: sonarConfig.saleUUID,
132
- entityUUID,
133
+ entityID,
133
134
  entityType,
134
135
  walletAddress,
135
136
  });
package/dist/index.cjs CHANGED
@@ -23,6 +23,7 @@ __export(index_exports, {
23
23
  SonarProvider: () => SonarProvider,
24
24
  useSonarAuth: () => useSonarAuth,
25
25
  useSonarClient: () => useSonarClient,
26
+ useSonarEntities: () => useSonarEntities,
26
27
  useSonarEntity: () => useSonarEntity,
27
28
  useSonarPurchase: () => useSonarPurchase
28
29
  });
@@ -191,9 +192,66 @@ function useSonarEntity(args) {
191
192
  error: state.error
192
193
  };
193
194
  }
195
+ function useSonarEntities(args) {
196
+ const { authenticated, ready } = useSonarAuth();
197
+ const client = useSonarClient();
198
+ if (!args.saleUUID) {
199
+ throw new Error("saleUUID is required");
200
+ }
201
+ const saleUUID = args.saleUUID;
202
+ const [state, setState] = (0, import_react2.useState)({
203
+ loading: false,
204
+ hasFetched: false
205
+ });
206
+ const fullyConnected = ready && authenticated;
207
+ const refetch = (0, import_react2.useCallback)(async () => {
208
+ if (!fullyConnected) {
209
+ return;
210
+ }
211
+ setState((s) => ({ ...s, loading: true }));
212
+ try {
213
+ const resp = await client.listAvailableEntities({
214
+ saleUUID
215
+ });
216
+ setState({
217
+ loading: false,
218
+ entities: resp.Entities,
219
+ error: void 0,
220
+ hasFetched: true
221
+ });
222
+ } catch (err) {
223
+ const error = err instanceof Error ? err : new Error(String(err));
224
+ setState({ loading: false, entities: void 0, error, hasFetched: true });
225
+ }
226
+ }, [client, saleUUID, fullyConnected]);
227
+ const reset = (0, import_react2.useCallback)(() => {
228
+ setState({
229
+ loading: false,
230
+ hasFetched: false,
231
+ entities: void 0,
232
+ error: void 0
233
+ });
234
+ }, []);
235
+ (0, import_react2.useEffect)(() => {
236
+ if (fullyConnected) {
237
+ refetch();
238
+ }
239
+ }, [fullyConnected, refetch]);
240
+ (0, import_react2.useEffect)(() => {
241
+ if (ready && !authenticated) {
242
+ reset();
243
+ }
244
+ }, [ready, authenticated, reset]);
245
+ return {
246
+ authenticated,
247
+ loading: state.loading,
248
+ entities: state.entities,
249
+ error: state.error
250
+ };
251
+ }
194
252
  function useSonarPurchase(args) {
195
253
  const saleUUID = args.saleUUID;
196
- const entityUUID = args.entityUUID;
254
+ const entityID = args.entityID;
197
255
  const walletAddress = args.walletAddress;
198
256
  const client = useSonarClient();
199
257
  const [state, setState] = (0, import_react2.useState)({
@@ -204,10 +262,10 @@ function useSonarPurchase(args) {
204
262
  const generatePurchasePermit = (0, import_react2.useCallback)(() => {
205
263
  return client.generatePurchasePermit({
206
264
  saleUUID,
207
- entityUUID,
265
+ entityID,
208
266
  walletAddress
209
267
  });
210
- }, [client, saleUUID, entityUUID, walletAddress]);
268
+ }, [client, saleUUID, entityID, walletAddress]);
211
269
  (0, import_react2.useEffect)(() => {
212
270
  const fetchPurchaseData = async () => {
213
271
  setState({
@@ -218,7 +276,7 @@ function useSonarPurchase(args) {
218
276
  try {
219
277
  const response = await client.prePurchaseCheck({
220
278
  saleUUID,
221
- entityUUID,
279
+ entityID,
222
280
  walletAddress
223
281
  });
224
282
  if (response.ReadyToPurchase) {
@@ -247,7 +305,7 @@ function useSonarPurchase(args) {
247
305
  }
248
306
  };
249
307
  fetchPurchaseData();
250
- }, [saleUUID, entityUUID, walletAddress, client, generatePurchasePermit]);
308
+ }, [saleUUID, entityID, walletAddress, client, generatePurchasePermit]);
251
309
  return state;
252
310
  }
253
311
  // Annotate the CommonJS export names for ESM import in node:
@@ -255,6 +313,7 @@ function useSonarPurchase(args) {
255
313
  SonarProvider,
256
314
  useSonarAuth,
257
315
  useSonarClient,
316
+ useSonarEntities,
258
317
  useSonarEntity,
259
318
  useSonarPurchase
260
319
  });
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React from 'react';
3
- import { SonarClient, EntityDetails, GeneratePurchasePermitResponse, PrePurchaseFailureReason } from '@echoxyz/sonar-core';
3
+ import { SonarClient, EntityDetails, GeneratePurchasePermitResponse, PrePurchaseFailureReason, Hex } from '@echoxyz/sonar-core';
4
4
 
5
5
  type SonarProviderProps = {
6
6
  children: React.ReactNode;
@@ -38,6 +38,16 @@ declare function useSonarEntity(args: {
38
38
  saleUUID: string;
39
39
  walletAddress?: string;
40
40
  }): UseSonarEntityResult;
41
+ type UseSonarEntitiesResult = {
42
+ authenticated: boolean;
43
+ loading: boolean;
44
+ entities?: EntityDetails[];
45
+ error?: Error;
46
+ };
47
+ declare function useSonarEntities(args: {
48
+ saleUUID: string;
49
+ walletAddress?: string;
50
+ }): UseSonarEntitiesResult;
41
51
  type UseSonarPurchaseResultReadyToPurchase = {
42
52
  loading: false;
43
53
  readyToPurchase: true;
@@ -64,8 +74,8 @@ type UseSonarPurchaseResultLoading = {
64
74
  type UseSonarPurchaseResult = UseSonarPurchaseResultLoading | UseSonarPurchaseResultReadyToPurchase | UseSonarPurchaseResultNotReadyToPurchase | UseSonarPurchaseResultError;
65
75
  declare function useSonarPurchase(args: {
66
76
  saleUUID: string;
67
- entityUUID: string;
77
+ entityID: Hex;
68
78
  walletAddress: string;
69
79
  }): UseSonarPurchaseResult;
70
80
 
71
- export { SonarProvider, type SonarProviderConfig, type UseSonarEntityResult, type UseSonarPurchaseResult, type UseSonarPurchaseResultError, type UseSonarPurchaseResultLoading, type UseSonarPurchaseResultNotReadyToPurchase, type UseSonarPurchaseResultReadyToPurchase, useSonarAuth, useSonarClient, useSonarEntity, useSonarPurchase };
81
+ export { SonarProvider, type SonarProviderConfig, type UseSonarEntitiesResult, type UseSonarEntityResult, type UseSonarPurchaseResult, type UseSonarPurchaseResultError, type UseSonarPurchaseResultLoading, type UseSonarPurchaseResultNotReadyToPurchase, type UseSonarPurchaseResultReadyToPurchase, useSonarAuth, useSonarClient, useSonarEntities, useSonarEntity, useSonarPurchase };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React from 'react';
3
- import { SonarClient, EntityDetails, GeneratePurchasePermitResponse, PrePurchaseFailureReason } from '@echoxyz/sonar-core';
3
+ import { SonarClient, EntityDetails, GeneratePurchasePermitResponse, PrePurchaseFailureReason, Hex } from '@echoxyz/sonar-core';
4
4
 
5
5
  type SonarProviderProps = {
6
6
  children: React.ReactNode;
@@ -38,6 +38,16 @@ declare function useSonarEntity(args: {
38
38
  saleUUID: string;
39
39
  walletAddress?: string;
40
40
  }): UseSonarEntityResult;
41
+ type UseSonarEntitiesResult = {
42
+ authenticated: boolean;
43
+ loading: boolean;
44
+ entities?: EntityDetails[];
45
+ error?: Error;
46
+ };
47
+ declare function useSonarEntities(args: {
48
+ saleUUID: string;
49
+ walletAddress?: string;
50
+ }): UseSonarEntitiesResult;
41
51
  type UseSonarPurchaseResultReadyToPurchase = {
42
52
  loading: false;
43
53
  readyToPurchase: true;
@@ -64,8 +74,8 @@ type UseSonarPurchaseResultLoading = {
64
74
  type UseSonarPurchaseResult = UseSonarPurchaseResultLoading | UseSonarPurchaseResultReadyToPurchase | UseSonarPurchaseResultNotReadyToPurchase | UseSonarPurchaseResultError;
65
75
  declare function useSonarPurchase(args: {
66
76
  saleUUID: string;
67
- entityUUID: string;
77
+ entityID: Hex;
68
78
  walletAddress: string;
69
79
  }): UseSonarPurchaseResult;
70
80
 
71
- export { SonarProvider, type SonarProviderConfig, type UseSonarEntityResult, type UseSonarPurchaseResult, type UseSonarPurchaseResultError, type UseSonarPurchaseResultLoading, type UseSonarPurchaseResultNotReadyToPurchase, type UseSonarPurchaseResultReadyToPurchase, useSonarAuth, useSonarClient, useSonarEntity, useSonarPurchase };
81
+ export { SonarProvider, type SonarProviderConfig, type UseSonarEntitiesResult, type UseSonarEntityResult, type UseSonarPurchaseResult, type UseSonarPurchaseResultError, type UseSonarPurchaseResultLoading, type UseSonarPurchaseResultNotReadyToPurchase, type UseSonarPurchaseResultReadyToPurchase, useSonarAuth, useSonarClient, useSonarEntities, useSonarEntity, useSonarPurchase };
package/dist/index.js CHANGED
@@ -163,9 +163,66 @@ function useSonarEntity(args) {
163
163
  error: state.error
164
164
  };
165
165
  }
166
+ function useSonarEntities(args) {
167
+ const { authenticated, ready } = useSonarAuth();
168
+ const client = useSonarClient();
169
+ if (!args.saleUUID) {
170
+ throw new Error("saleUUID is required");
171
+ }
172
+ const saleUUID = args.saleUUID;
173
+ const [state, setState] = useState2({
174
+ loading: false,
175
+ hasFetched: false
176
+ });
177
+ const fullyConnected = ready && authenticated;
178
+ const refetch = useCallback2(async () => {
179
+ if (!fullyConnected) {
180
+ return;
181
+ }
182
+ setState((s) => ({ ...s, loading: true }));
183
+ try {
184
+ const resp = await client.listAvailableEntities({
185
+ saleUUID
186
+ });
187
+ setState({
188
+ loading: false,
189
+ entities: resp.Entities,
190
+ error: void 0,
191
+ hasFetched: true
192
+ });
193
+ } catch (err) {
194
+ const error = err instanceof Error ? err : new Error(String(err));
195
+ setState({ loading: false, entities: void 0, error, hasFetched: true });
196
+ }
197
+ }, [client, saleUUID, fullyConnected]);
198
+ const reset = useCallback2(() => {
199
+ setState({
200
+ loading: false,
201
+ hasFetched: false,
202
+ entities: void 0,
203
+ error: void 0
204
+ });
205
+ }, []);
206
+ useEffect2(() => {
207
+ if (fullyConnected) {
208
+ refetch();
209
+ }
210
+ }, [fullyConnected, refetch]);
211
+ useEffect2(() => {
212
+ if (ready && !authenticated) {
213
+ reset();
214
+ }
215
+ }, [ready, authenticated, reset]);
216
+ return {
217
+ authenticated,
218
+ loading: state.loading,
219
+ entities: state.entities,
220
+ error: state.error
221
+ };
222
+ }
166
223
  function useSonarPurchase(args) {
167
224
  const saleUUID = args.saleUUID;
168
- const entityUUID = args.entityUUID;
225
+ const entityID = args.entityID;
169
226
  const walletAddress = args.walletAddress;
170
227
  const client = useSonarClient();
171
228
  const [state, setState] = useState2({
@@ -176,10 +233,10 @@ function useSonarPurchase(args) {
176
233
  const generatePurchasePermit = useCallback2(() => {
177
234
  return client.generatePurchasePermit({
178
235
  saleUUID,
179
- entityUUID,
236
+ entityID,
180
237
  walletAddress
181
238
  });
182
- }, [client, saleUUID, entityUUID, walletAddress]);
239
+ }, [client, saleUUID, entityID, walletAddress]);
183
240
  useEffect2(() => {
184
241
  const fetchPurchaseData = async () => {
185
242
  setState({
@@ -190,7 +247,7 @@ function useSonarPurchase(args) {
190
247
  try {
191
248
  const response = await client.prePurchaseCheck({
192
249
  saleUUID,
193
- entityUUID,
250
+ entityID,
194
251
  walletAddress
195
252
  });
196
253
  if (response.ReadyToPurchase) {
@@ -219,13 +276,14 @@ function useSonarPurchase(args) {
219
276
  }
220
277
  };
221
278
  fetchPurchaseData();
222
- }, [saleUUID, entityUUID, walletAddress, client, generatePurchasePermit]);
279
+ }, [saleUUID, entityID, walletAddress, client, generatePurchasePermit]);
223
280
  return state;
224
281
  }
225
282
  export {
226
283
  SonarProvider,
227
284
  useSonarAuth,
228
285
  useSonarClient,
286
+ useSonarEntities,
229
287
  useSonarEntity,
230
288
  useSonarPurchase
231
289
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@echoxyz/sonar-react",
3
- "version": "0.6.2",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -16,7 +16,7 @@
16
16
  "react": ">=18"
17
17
  },
18
18
  "dependencies": {
19
- "@echoxyz/sonar-core": "0.5.0"
19
+ "@echoxyz/sonar-core": "0.7.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@testing-library/react": "^16.0.0",