@accesly/react 1.1.1 → 1.1.3
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/dist/.tsbuildinfo +1 -1
- package/dist/index.cjs +38 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +39 -23
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CognitoAuthClient, defaultSessionStorage, InMemoryDeviceStore, TokenManager, AccesslyApiClient, AccesslyEndpoints, getRandomBytes, unlockPasskey, hkdfSha256, zeroize, decryptAesGcm, sha256, registerPasskey, normalizeSecp256r1Pubkey, createWallet, generateRecoverySalt, deriveRecoveryKey, encryptAesGcm, emailHashBytes, computeSmartAccountAddress, signTransaction, generateX25519Keypair, unwrapSessionFragment2, reconstructFromPlainAndEncrypted, signSorobanAuthEntry, reconstructKey, AccesslyApiError } from '@accesly/core';
|
|
2
|
-
import { createContext, useMemo, useState, useRef, useEffect, useContext
|
|
2
|
+
import { createContext, useMemo, useState, useRef, useCallback, useEffect, useContext } from 'react';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
|
|
5
5
|
// src/provider.tsx
|
|
@@ -86,14 +86,14 @@ function AcceslyProvider(props) {
|
|
|
86
86
|
() => initialUsername(instances.sessionStorage)
|
|
87
87
|
);
|
|
88
88
|
const mountedRef = useRef(true);
|
|
89
|
-
const refreshStatus = async () => {
|
|
89
|
+
const refreshStatus = useCallback(async () => {
|
|
90
90
|
const next = await instances.tokenManager.getStatus();
|
|
91
91
|
const tokens = await Promise.resolve(instances.sessionStorage.load());
|
|
92
92
|
if (mountedRef.current) {
|
|
93
93
|
setStatus(next);
|
|
94
94
|
setUsername(tokens?.username ?? null);
|
|
95
95
|
}
|
|
96
|
-
};
|
|
96
|
+
}, [instances]);
|
|
97
97
|
useEffect(() => {
|
|
98
98
|
mountedRef.current = true;
|
|
99
99
|
void refreshStatus();
|
|
@@ -1113,6 +1113,11 @@ function useWalletStatus() {
|
|
|
1113
1113
|
return { status, walletAddress, onChain, isStale, refresh };
|
|
1114
1114
|
}
|
|
1115
1115
|
var POLL_INTERVAL_MS = 8e3;
|
|
1116
|
+
function useStableRef(value) {
|
|
1117
|
+
const ref = useRef(value);
|
|
1118
|
+
ref.current = value;
|
|
1119
|
+
return ref;
|
|
1120
|
+
}
|
|
1116
1121
|
function useBalance(walletAddress) {
|
|
1117
1122
|
const { wallet, _internal } = useAccesly();
|
|
1118
1123
|
const username = _internal.username;
|
|
@@ -1124,6 +1129,7 @@ function useBalance(walletAddress) {
|
|
|
1124
1129
|
const [isLoading, setIsLoading] = useState(true);
|
|
1125
1130
|
const [error, setError] = useState(null);
|
|
1126
1131
|
const fetchInFlight = useRef(false);
|
|
1132
|
+
const walletRef = useStableRef(wallet);
|
|
1127
1133
|
useEffect(() => {
|
|
1128
1134
|
if (walletAddress) {
|
|
1129
1135
|
setResolvedAddress(walletAddress);
|
|
@@ -1136,7 +1142,7 @@ function useBalance(walletAddress) {
|
|
|
1136
1142
|
let cancelled = false;
|
|
1137
1143
|
void (async () => {
|
|
1138
1144
|
try {
|
|
1139
|
-
const stored = await
|
|
1145
|
+
const stored = await walletRef.current.getStoredCredential(username);
|
|
1140
1146
|
if (cancelled) return;
|
|
1141
1147
|
setResolvedAddress(stored?.walletAddress ?? null);
|
|
1142
1148
|
} catch {
|
|
@@ -1146,13 +1152,14 @@ function useBalance(walletAddress) {
|
|
|
1146
1152
|
return () => {
|
|
1147
1153
|
cancelled = true;
|
|
1148
1154
|
};
|
|
1149
|
-
}, [walletAddress, username,
|
|
1155
|
+
}, [walletAddress, username, walletRef]);
|
|
1156
|
+
const endpointsRef = useStableRef(_internal.endpoints);
|
|
1150
1157
|
const doFetch = useCallback(async () => {
|
|
1151
1158
|
if (!resolvedAddress) return;
|
|
1152
1159
|
if (fetchInFlight.current) return;
|
|
1153
1160
|
fetchInFlight.current = true;
|
|
1154
1161
|
try {
|
|
1155
|
-
const res = await
|
|
1162
|
+
const res = await endpointsRef.current.walletBalance(resolvedAddress);
|
|
1156
1163
|
setStroops(res.xlm.stroops);
|
|
1157
1164
|
setXlm(res.xlm.xlm);
|
|
1158
1165
|
setError(null);
|
|
@@ -1162,19 +1169,20 @@ function useBalance(walletAddress) {
|
|
|
1162
1169
|
setIsLoading(false);
|
|
1163
1170
|
fetchInFlight.current = false;
|
|
1164
1171
|
}
|
|
1165
|
-
}, [resolvedAddress,
|
|
1172
|
+
}, [resolvedAddress, endpointsRef]);
|
|
1173
|
+
const doFetchRef = useStableRef(doFetch);
|
|
1166
1174
|
useEffect(() => {
|
|
1167
1175
|
if (!resolvedAddress) {
|
|
1168
1176
|
setIsLoading(false);
|
|
1169
1177
|
return void 0;
|
|
1170
1178
|
}
|
|
1171
|
-
void
|
|
1179
|
+
void doFetchRef.current();
|
|
1172
1180
|
let interval = null;
|
|
1173
1181
|
const startInterval = () => {
|
|
1174
1182
|
if (interval) return;
|
|
1175
1183
|
interval = setInterval(() => {
|
|
1176
1184
|
if (typeof document !== "undefined" && document.hidden) return;
|
|
1177
|
-
void
|
|
1185
|
+
void doFetchRef.current();
|
|
1178
1186
|
}, POLL_INTERVAL_MS);
|
|
1179
1187
|
};
|
|
1180
1188
|
const stopInterval = () => {
|
|
@@ -1189,7 +1197,7 @@ function useBalance(walletAddress) {
|
|
|
1189
1197
|
if (document.hidden) {
|
|
1190
1198
|
stopInterval();
|
|
1191
1199
|
} else {
|
|
1192
|
-
void
|
|
1200
|
+
void doFetchRef.current();
|
|
1193
1201
|
startInterval();
|
|
1194
1202
|
}
|
|
1195
1203
|
};
|
|
@@ -1202,14 +1210,19 @@ function useBalance(walletAddress) {
|
|
|
1202
1210
|
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
1203
1211
|
}
|
|
1204
1212
|
};
|
|
1205
|
-
}, [resolvedAddress,
|
|
1213
|
+
}, [resolvedAddress, doFetchRef]);
|
|
1206
1214
|
const refresh = useCallback(async () => {
|
|
1207
|
-
await
|
|
1208
|
-
}, [
|
|
1215
|
+
await doFetchRef.current();
|
|
1216
|
+
}, [doFetchRef]);
|
|
1209
1217
|
return { stroops, xlm, isLoading, error, refresh };
|
|
1210
1218
|
}
|
|
1211
1219
|
var POLL_INTERVAL_MS2 = 2e4;
|
|
1212
1220
|
var DEFAULT_LIMIT = 20;
|
|
1221
|
+
function useStableRef2(value) {
|
|
1222
|
+
const ref = useRef(value);
|
|
1223
|
+
ref.current = value;
|
|
1224
|
+
return ref;
|
|
1225
|
+
}
|
|
1213
1226
|
function useWalletActivity(walletAddress, opts = {}) {
|
|
1214
1227
|
const { wallet, _internal } = useAccesly();
|
|
1215
1228
|
const username = _internal.username;
|
|
@@ -1221,6 +1234,7 @@ function useWalletActivity(walletAddress, opts = {}) {
|
|
|
1221
1234
|
const [isLoading, setIsLoading] = useState(true);
|
|
1222
1235
|
const [error, setError] = useState(null);
|
|
1223
1236
|
const fetchInFlight = useRef(false);
|
|
1237
|
+
const walletRef = useStableRef2(wallet);
|
|
1224
1238
|
useEffect(() => {
|
|
1225
1239
|
if (walletAddress) {
|
|
1226
1240
|
setResolvedAddress(walletAddress);
|
|
@@ -1233,7 +1247,7 @@ function useWalletActivity(walletAddress, opts = {}) {
|
|
|
1233
1247
|
let cancelled = false;
|
|
1234
1248
|
void (async () => {
|
|
1235
1249
|
try {
|
|
1236
|
-
const stored = await
|
|
1250
|
+
const stored = await walletRef.current.getStoredCredential(username);
|
|
1237
1251
|
if (cancelled) return;
|
|
1238
1252
|
setResolvedAddress(stored?.walletAddress ?? null);
|
|
1239
1253
|
} catch {
|
|
@@ -1243,13 +1257,14 @@ function useWalletActivity(walletAddress, opts = {}) {
|
|
|
1243
1257
|
return () => {
|
|
1244
1258
|
cancelled = true;
|
|
1245
1259
|
};
|
|
1246
|
-
}, [walletAddress, username,
|
|
1260
|
+
}, [walletAddress, username, walletRef]);
|
|
1261
|
+
const endpointsRef = useStableRef2(_internal.endpoints);
|
|
1247
1262
|
const doFetch = useCallback(async () => {
|
|
1248
1263
|
if (!resolvedAddress) return;
|
|
1249
1264
|
if (fetchInFlight.current) return;
|
|
1250
1265
|
fetchInFlight.current = true;
|
|
1251
1266
|
try {
|
|
1252
|
-
const res = await
|
|
1267
|
+
const res = await endpointsRef.current.walletActivity(resolvedAddress, limit);
|
|
1253
1268
|
setEvents(res.events);
|
|
1254
1269
|
setError(null);
|
|
1255
1270
|
} catch (err) {
|
|
@@ -1258,19 +1273,20 @@ function useWalletActivity(walletAddress, opts = {}) {
|
|
|
1258
1273
|
setIsLoading(false);
|
|
1259
1274
|
fetchInFlight.current = false;
|
|
1260
1275
|
}
|
|
1261
|
-
}, [resolvedAddress, limit,
|
|
1276
|
+
}, [resolvedAddress, limit, endpointsRef]);
|
|
1277
|
+
const doFetchRef = useStableRef2(doFetch);
|
|
1262
1278
|
useEffect(() => {
|
|
1263
1279
|
if (!resolvedAddress) {
|
|
1264
1280
|
setIsLoading(false);
|
|
1265
1281
|
return void 0;
|
|
1266
1282
|
}
|
|
1267
|
-
void
|
|
1283
|
+
void doFetchRef.current();
|
|
1268
1284
|
let interval = null;
|
|
1269
1285
|
const start = () => {
|
|
1270
1286
|
if (interval) return;
|
|
1271
1287
|
interval = setInterval(() => {
|
|
1272
1288
|
if (typeof document !== "undefined" && document.hidden) return;
|
|
1273
|
-
void
|
|
1289
|
+
void doFetchRef.current();
|
|
1274
1290
|
}, POLL_INTERVAL_MS2);
|
|
1275
1291
|
};
|
|
1276
1292
|
const stop = () => {
|
|
@@ -1284,7 +1300,7 @@ function useWalletActivity(walletAddress, opts = {}) {
|
|
|
1284
1300
|
if (typeof document === "undefined") return;
|
|
1285
1301
|
if (document.hidden) stop();
|
|
1286
1302
|
else {
|
|
1287
|
-
void
|
|
1303
|
+
void doFetchRef.current();
|
|
1288
1304
|
start();
|
|
1289
1305
|
}
|
|
1290
1306
|
};
|
|
@@ -1297,10 +1313,10 @@ function useWalletActivity(walletAddress, opts = {}) {
|
|
|
1297
1313
|
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
1298
1314
|
}
|
|
1299
1315
|
};
|
|
1300
|
-
}, [resolvedAddress,
|
|
1316
|
+
}, [resolvedAddress, doFetchRef]);
|
|
1301
1317
|
const refresh = useCallback(async () => {
|
|
1302
|
-
await
|
|
1303
|
-
}, [
|
|
1318
|
+
await doFetchRef.current();
|
|
1319
|
+
}, [doFetchRef]);
|
|
1304
1320
|
return { events, isLoading, error, refresh };
|
|
1305
1321
|
}
|
|
1306
1322
|
|