@echoxyz/sonar-react 0.1.4 → 0.2.1

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,23 @@
1
1
  # @echoxyz/sonar-react
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [a043774]
8
+ - @echoxyz/sonar-core@0.2.0
9
+
10
+ ## 0.2.0
11
+
12
+ ### Minor Changes
13
+
14
+ - 77db07a: Change authenticated to bool, add ready state
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [77db07a]
19
+ - @echoxyz/sonar-core@0.1.5
20
+
3
21
  ## 0.1.4
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -45,8 +45,8 @@ export function AppRoot({ children }: { children: React.ReactNode }) {
45
45
  import { useSonarAuth } from "@echoxyz/sonar-react";
46
46
 
47
47
  export function LoginButton() {
48
- const { login, authenticated } = useSonarAuth();
49
- if (authenticated()) {
48
+ const { login, authenticated, ready } = useSonarAuth();
49
+ if (!ready || authenticated) {
50
50
  return null;
51
51
  }
52
52
  return <button onClick={() => login()}>Sign in with Echo</button>;
@@ -86,19 +86,18 @@ import { useSonarAuth, useSonarClient } from "@echoxyz/sonar-react";
86
86
  import { EntityType } from "@echoxyz/sonar-core";
87
87
 
88
88
  export function Example() {
89
- const { authenticated } = useSonarAuth();
89
+ const { authenticated, ready } = useSonarAuth();
90
90
  const client = useSonarClient();
91
+ const { walletAddress } = useWallet();
91
92
 
92
93
  useEffect(() => {
93
- if (!authenticated()) {
94
+ if (!ready || !authenticated) {
94
95
  return;
95
96
  }
96
97
 
97
98
  (async () => {
98
- const { Entities } = await client.listAvailableEntities({ saleUUID: "<your-sale-uuid>" });
99
- if (Entities.length === 0) return;
99
+ const { Entity: entity } = await client.readEntity({ saleUUID: "<your-sale-uuid>", walletAddress });
100
100
 
101
- const entity = Entities[0];
102
101
  const pre = await client.prePurchaseCheck({
103
102
  saleUUID: "<your-sale-uuid>",
104
103
  entityUUID: entity.EntityUUID,
@@ -118,11 +117,11 @@ export function Example() {
118
117
 
119
118
  const alloc = await client.fetchAllocation({
120
119
  saleUUID: "<your-sale-uuid>",
121
- walletAddress: "0x1234...abcd" as `0x${string}`,
120
+ walletAddress,
122
121
  });
123
122
  console.log(alloc);
124
123
  })();
125
- }, [authenticated, client]);
124
+ }, [authenticated, client, ready]);
126
125
 
127
126
  return null;
128
127
  }
@@ -138,7 +137,7 @@ export function Example() {
138
137
  - `apiURL?: string` (default: `https://api.echo.xyz`) – API base URL.
139
138
  - `tokenStorageKey?: string` (default: `sonar:auth-token`) – Browser storage key for the access token.
140
139
 
141
- - `useSonarAuth()` → `{ authenticated, token?, login(), completeOAuth({ code, state }), logout() }`
140
+ - `useSonarAuth()` → `{ authenticated, ready, token?, login(), completeOAuth({ code, state }), logout() }`
142
141
 
143
142
  - `useSonarClient()` → low-level `SonarClient` instance.
144
143
 
package/dist/index.cjs CHANGED
@@ -33,13 +33,20 @@ var import_jsx_runtime = require("react/jsx-runtime");
33
33
  var AuthContext = (0, import_react.createContext)(void 0);
34
34
  var ClientContext = (0, import_react.createContext)(void 0);
35
35
  function SonarProvider({ children, config }) {
36
+ const [authState, setAuthState] = (0, import_react.useState)({ token: void 0, ready: false });
36
37
  const client = (0, import_react.useMemo)(() => {
37
38
  return (0, import_sonar_core.createClient)({
38
39
  saleUUID: config.saleUUID,
39
40
  apiURL: config.apiURL,
40
- tokenKey: config.tokenStorageKey
41
+ tokenKey: config.tokenStorageKey,
42
+ onTokenChange: (nextToken) => {
43
+ setAuthState({ token: nextToken ?? void 0, ready: true });
44
+ }
41
45
  });
42
46
  }, [config.apiURL, config.saleUUID, config.tokenStorageKey]);
47
+ (0, import_react.useEffect)(() => {
48
+ setAuthState({ token: client.getToken() ?? void 0, ready: true });
49
+ }, [client]);
43
50
  const login = (0, import_react.useCallback)(async () => {
44
51
  const { codeVerifier, codeChallenge, state } = await (0, import_sonar_core.generatePKCEParams)();
45
52
  if (typeof window === "undefined") {
@@ -80,16 +87,17 @@ function SonarProvider({ children, config }) {
80
87
  );
81
88
  const logout = (0, import_react.useCallback)(() => {
82
89
  client.clear();
83
- }, []);
90
+ }, [client]);
84
91
  const authValue = (0, import_react.useMemo)(
85
92
  () => ({
86
93
  login,
87
94
  logout,
88
- authenticated: () => Boolean(client.getToken()),
89
- token: client.getToken() ?? void 0,
95
+ authenticated: Boolean(authState.token),
96
+ ready: authState.ready,
97
+ token: authState.token,
90
98
  completeOAuth
91
99
  }),
92
- [client, login, completeOAuth, logout]
100
+ [authState, login, completeOAuth, logout]
93
101
  );
94
102
  const clientValue = (0, import_react.useMemo)(() => ({ client }), [client]);
95
103
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value: authValue, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ClientContext.Provider, { value: clientValue, children }) });
package/dist/index.d.cts CHANGED
@@ -15,7 +15,8 @@ type SonarProviderConfig = {
15
15
  tokenStorageKey?: string;
16
16
  };
17
17
  type AuthContextValue = {
18
- authenticated: () => boolean;
18
+ authenticated: boolean;
19
+ ready: boolean;
19
20
  token?: string;
20
21
  login: () => Promise<void>;
21
22
  completeOAuth: (args: {
package/dist/index.d.ts CHANGED
@@ -15,7 +15,8 @@ type SonarProviderConfig = {
15
15
  tokenStorageKey?: string;
16
16
  };
17
17
  type AuthContextValue = {
18
- authenticated: () => boolean;
18
+ authenticated: boolean;
19
+ ready: boolean;
19
20
  token?: string;
20
21
  login: () => Promise<void>;
21
22
  completeOAuth: (args: {
package/dist/index.js CHANGED
@@ -1,17 +1,24 @@
1
1
  // src/react.tsx
2
2
  import { buildAuthorizationUrl, createClient, generatePKCEParams } from "@echoxyz/sonar-core";
3
- import { createContext, useCallback, useContext, useMemo } from "react";
3
+ import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
  var AuthContext = createContext(void 0);
6
6
  var ClientContext = createContext(void 0);
7
7
  function SonarProvider({ children, config }) {
8
+ const [authState, setAuthState] = useState({ token: void 0, ready: false });
8
9
  const client = useMemo(() => {
9
10
  return createClient({
10
11
  saleUUID: config.saleUUID,
11
12
  apiURL: config.apiURL,
12
- tokenKey: config.tokenStorageKey
13
+ tokenKey: config.tokenStorageKey,
14
+ onTokenChange: (nextToken) => {
15
+ setAuthState({ token: nextToken ?? void 0, ready: true });
16
+ }
13
17
  });
14
18
  }, [config.apiURL, config.saleUUID, config.tokenStorageKey]);
19
+ useEffect(() => {
20
+ setAuthState({ token: client.getToken() ?? void 0, ready: true });
21
+ }, [client]);
15
22
  const login = useCallback(async () => {
16
23
  const { codeVerifier, codeChallenge, state } = await generatePKCEParams();
17
24
  if (typeof window === "undefined") {
@@ -52,16 +59,17 @@ function SonarProvider({ children, config }) {
52
59
  );
53
60
  const logout = useCallback(() => {
54
61
  client.clear();
55
- }, []);
62
+ }, [client]);
56
63
  const authValue = useMemo(
57
64
  () => ({
58
65
  login,
59
66
  logout,
60
- authenticated: () => Boolean(client.getToken()),
61
- token: client.getToken() ?? void 0,
67
+ authenticated: Boolean(authState.token),
68
+ ready: authState.ready,
69
+ token: authState.token,
62
70
  completeOAuth
63
71
  }),
64
- [client, login, completeOAuth, logout]
72
+ [authState, login, completeOAuth, logout]
65
73
  );
66
74
  const clientValue = useMemo(() => ({ client }), [client]);
67
75
  return /* @__PURE__ */ jsx(AuthContext.Provider, { value: authValue, children: /* @__PURE__ */ jsx(ClientContext.Provider, { value: clientValue, children }) });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@echoxyz/sonar-react",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -16,12 +16,17 @@
16
16
  "react": ">=18"
17
17
  },
18
18
  "dependencies": {
19
- "@echoxyz/sonar-core": "0.1.4"
19
+ "@echoxyz/sonar-core": "0.2.0"
20
20
  },
21
21
  "devDependencies": {
22
+ "@testing-library/react": "^16.0.0",
23
+ "@types/react": "^18",
24
+ "@types/react-dom": "^18",
25
+ "jsdom": "^27.0.0",
26
+ "react": "^18",
27
+ "react-dom": "^18",
22
28
  "tsup": "^8.0.0",
23
- "vitest": "^2.0.0",
24
- "@types/react": "^18"
29
+ "vitest": "^2.0.0"
25
30
  },
26
31
  "repository": {
27
32
  "type": "git",