@hardkas/react 0.9.0-alpha → 0.9.2-alpha
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/README.md +70 -68
- package/dist/index.js +28 -23
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,68 +1,70 @@
|
|
|
1
|
-
# @hardkas/react
|
|
2
|
-
|
|
3
|
-
Zero-dependency React hooks for HardKAS.
|
|
4
|
-
|
|
5
|
-
> [!WARNING]
|
|
6
|
-
> **DO NOT import `@hardkas/sdk` in the browser!**
|
|
7
|
-
> The main SDK package contains Node.js specific libraries. Use `@hardkas/react` for your web UI.
|
|
8
|
-
|
|
9
|
-
## Installation
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm install @hardkas/react @hardkas/client
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## Setup Provider
|
|
16
|
-
|
|
17
|
-
Wrap your application in the `HardKASProvider`. This initializes the underlying `@hardkas/client` instance.
|
|
18
|
-
|
|
19
|
-
```tsx
|
|
20
|
-
import { HardKASProvider } from
|
|
21
|
-
|
|
22
|
-
function App() {
|
|
23
|
-
return (
|
|
24
|
-
<HardKASProvider baseUrl="http://127.0.0.1:7420" timeout={10000}>
|
|
25
|
-
<MyDApp />
|
|
26
|
-
</HardKASProvider>
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Hooks
|
|
32
|
-
|
|
33
|
-
### `useWallet`
|
|
34
|
-
|
|
35
|
-
Fetch wallet details.
|
|
36
|
-
|
|
37
|
-
```tsx
|
|
38
|
-
import { useWallet } from
|
|
39
|
-
|
|
40
|
-
function WalletView() {
|
|
41
|
-
const { data, loading, error } = useWallet(
|
|
42
|
-
|
|
43
|
-
if (loading) return <p>Loading...</p>;
|
|
44
|
-
if (error) return <p>Error: {error.message}</p>;
|
|
45
|
-
|
|
46
|
-
return <pre>{JSON.stringify(data, null, 2)}</pre>;
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### `useMutation`
|
|
51
|
-
|
|
52
|
-
Generic hook for executing state-changing transactions against the Dev API.
|
|
53
|
-
|
|
54
|
-
```tsx
|
|
55
|
-
import { useMutation } from
|
|
56
|
-
|
|
57
|
-
function SendKas() {
|
|
58
|
-
const { execute, loading } = useMutation(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
1
|
+
# @hardkas/react
|
|
2
|
+
|
|
3
|
+
Zero-dependency React hooks for HardKAS.
|
|
4
|
+
|
|
5
|
+
> [!WARNING]
|
|
6
|
+
> **DO NOT import `@hardkas/sdk` in the browser!**
|
|
7
|
+
> The main SDK package contains Node.js specific libraries. Use `@hardkas/react` for your web UI.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @hardkas/react @hardkas/client
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Setup Provider
|
|
16
|
+
|
|
17
|
+
Wrap your application in the `HardKASProvider`. This initializes the underlying `@hardkas/client` instance.
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { HardKASProvider } from "@hardkas/react";
|
|
21
|
+
|
|
22
|
+
function App() {
|
|
23
|
+
return (
|
|
24
|
+
<HardKASProvider baseUrl="http://127.0.0.1:7420" timeout={10000}>
|
|
25
|
+
<MyDApp />
|
|
26
|
+
</HardKASProvider>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Hooks
|
|
32
|
+
|
|
33
|
+
### `useWallet`
|
|
34
|
+
|
|
35
|
+
Fetch wallet details.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { useWallet } from "@hardkas/react";
|
|
39
|
+
|
|
40
|
+
function WalletView() {
|
|
41
|
+
const { data, loading, error } = useWallet("alice");
|
|
42
|
+
|
|
43
|
+
if (loading) return <p>Loading...</p>;
|
|
44
|
+
if (error) return <p>Error: {error.message}</p>;
|
|
45
|
+
|
|
46
|
+
return <pre>{JSON.stringify(data, null, 2)}</pre>;
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### `useMutation`
|
|
51
|
+
|
|
52
|
+
Generic hook for executing state-changing transactions against the Dev API.
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
import { useMutation } from "@hardkas/react";
|
|
56
|
+
|
|
57
|
+
function SendKas() {
|
|
58
|
+
const { execute, loading } = useMutation(
|
|
59
|
+
(client, vars: { to: string; amount: number }) => {
|
|
60
|
+
return client.txSimulate({ ...vars });
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<button onClick={() => execute({ to: "bob", amount: 100 })} disabled={loading}>
|
|
66
|
+
{loading ? "Sending..." : "Send to Bob"}
|
|
67
|
+
</button>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,11 @@ import { createContext, useContext, useMemo } from "react";
|
|
|
3
3
|
import { createClient } from "@hardkas/client";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
var HardKASContext = createContext(null);
|
|
6
|
-
var HardKASProvider = ({
|
|
6
|
+
var HardKASProvider = ({
|
|
7
|
+
children,
|
|
8
|
+
baseUrl,
|
|
9
|
+
timeout
|
|
10
|
+
}) => {
|
|
7
11
|
const client = useMemo(() => {
|
|
8
12
|
const config = {};
|
|
9
13
|
if (baseUrl !== void 0) config.baseUrl = baseUrl;
|
|
@@ -58,34 +62,35 @@ function useMutation(mutationFn) {
|
|
|
58
62
|
const [data, setData] = useState2(null);
|
|
59
63
|
const [error, setError] = useState2(null);
|
|
60
64
|
const [loading, setLoading] = useState2(false);
|
|
61
|
-
const execute = useCallback2(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
const execute = useCallback2(
|
|
66
|
+
async (variables) => {
|
|
67
|
+
setLoading(true);
|
|
68
|
+
setError(null);
|
|
69
|
+
try {
|
|
70
|
+
const response = await mutationFn(client, variables);
|
|
71
|
+
if (response.ok) {
|
|
72
|
+
setData(response.data);
|
|
73
|
+
} else {
|
|
74
|
+
setError(response);
|
|
75
|
+
}
|
|
76
|
+
return response;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
setError(err);
|
|
79
|
+
return { ok: false, code: "UNEXPECTED_ERROR", message: String(err) };
|
|
80
|
+
} finally {
|
|
81
|
+
setLoading(false);
|
|
70
82
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return { ok: false, code: "UNEXPECTED_ERROR", message: String(err) };
|
|
75
|
-
} finally {
|
|
76
|
-
setLoading(false);
|
|
77
|
-
}
|
|
78
|
-
}, [client, mutationFn]);
|
|
83
|
+
},
|
|
84
|
+
[client, mutationFn]
|
|
85
|
+
);
|
|
79
86
|
return { data, error, loading, execute };
|
|
80
87
|
}
|
|
81
88
|
|
|
82
89
|
// src/hooks/useWallet.ts
|
|
83
90
|
function useWallet(address) {
|
|
84
|
-
return useQuery(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
{ enabled: !!address }
|
|
88
|
-
);
|
|
91
|
+
return useQuery((client) => client.getWallet(address), [address], {
|
|
92
|
+
enabled: !!address
|
|
93
|
+
});
|
|
89
94
|
}
|
|
90
95
|
export {
|
|
91
96
|
HardKASProvider,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hardkas/react",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2-alpha",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"react-dom": ">=18"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@hardkas/client": "0.9.
|
|
20
|
+
"@hardkas/client": "0.9.2-alpha"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@testing-library/dom": "^10.4.1",
|