@gardenfi/react-hooks 2.5.3-beta.3 → 2.5.3-beta.5

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/index3.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@gardenfi/orderbook"),s=require("react"),p=require("./index6.cjs"),R=(t,c)=>{const[m,d]=s.useState([]),o=s.useRef(null),a=s.useRef(!1),E=5e3,O=s.useCallback(async()=>{if(t)try{const e=c.getItem(p.PENDING_ORDERS_STORE),r=e?JSON.parse(e):[];if(r.length===0){d([]);return}const u=await Promise.all(r.map(n=>t.getOrder(n))),g=[],l=[];u.forEach((n,h)=>{if(!n.ok){l.push(r[h]);return}const i=n.val;i.status===f.OrderStatus.Redeemed||i.status===f.OrderStatus.Refunded||i.status===f.OrderStatus.Expired||(g.push(i),l.push(r[h]))});try{c.setItem(p.PENDING_ORDERS_STORE,JSON.stringify(l))}catch(n){console.error("Error persisting remaining pending order ids",n)}d(g)}catch(e){console.error("Error fetching pending orders:",e)}},[t,c]);return s.useEffect(()=>{if(t)if(t.redeemServiceEnabled){let e=!1;const r=()=>{e||(o.current=setTimeout(u,E))},u=async()=>{if(!e){if(a.current){r();return}a.current=!0;try{await O()}finally{a.current=!1}r()}};return u(),()=>{e=!0,o.current&&(clearTimeout(o.current),o.current=null)}}else{const e=r=>d(r);return t.on("onPendingOrdersChanged",e),()=>{t.off("onPendingOrdersChanged",e)}}},[t,O]),{pendingOrders:m}};exports.useOrderbook=R;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("@gardenfi/orderbook"),o=require("react"),R=require("./index6.cjs"),k=(r,u)=>{const[T,m]=o.useState([]),a=o.useRef(null),p=o.useRef(!1),E=o.useRef(!1),I=5e3,f=o.useCallback(()=>{var s,i,l,c,t,n,d,y,P,b;if(!r)return[];const e=new Set;try{[(i=(s=r.htlcs)==null?void 0:s.evm)==null?void 0:i.htlcActorAddress,(c=(l=r.htlcs)==null?void 0:l.sui)==null?void 0:c.htlcActorAddress,(n=(t=r.htlcs)==null?void 0:t.solana)==null?void 0:n.htlcActorAddress,(y=(d=r.htlcs)==null?void 0:d.starknet)==null?void 0:y.htlcActorAddress,(b=(P=r.htlcs)==null?void 0:P.bitcoin)==null?void 0:b.htlcActorAddress].filter(O=>!!O&&O.length>0).forEach(O=>e.add(O.toLowerCase()))}catch(N){console.error("Error getting HTLC addresses:",N)}return Array.from(e)},[r]),S=o.useCallback(async()=>{if(r)try{const e=f();if(e.length===0){console.log("No HTLC addresses found for initial pending orders fetch");return}const s=e.map(async t=>{try{const n=await r.getOrders({address:t,status:g.OrderLifecycle.pending,per_page:500});return n.ok?n.val.data:(console.error(`Failed to fetch orders for address ${t}:`,n.error),[])}catch(n){return console.error(`Error fetching orders for address ${t}:`,n),[]}}),c=(await Promise.all(s)).flat().map(t=>t.order_id);if(c.length>0)try{u.setItem(R.PENDING_ORDERS_STORE,JSON.stringify(c))}catch(t){console.error("Error saving initial pending order IDs to localStorage",t)}}catch(e){console.error("Error fetching initial pending orders:",e)}},[r,u,f]),A=o.useCallback(async()=>{if(r)try{const e=u.getItem(R.PENDING_ORDERS_STORE),s=e?JSON.parse(e):[];if(s.length===0){m([]);return}const i=await Promise.all(s.map(t=>r.getOrder(t))),l=[],c=[];i.forEach((t,n)=>{if(!t.ok){c.push(s[n]);return}const d=t.val;d.status===g.OrderStatus.Redeemed||d.status===g.OrderStatus.Refunded||d.status===g.OrderStatus.Expired||(l.push(d),c.push(s[n]))});try{u.setItem(R.PENDING_ORDERS_STORE,JSON.stringify(c))}catch(t){console.error("Error persisting remaining pending order ids",t)}m(l)}catch(e){console.error("Error fetching pending orders:",e)}},[r,u]),h=o.useMemo(()=>{try{return f().join(",")}catch(e){return console.error("Error computing HTLC addresses:",e),""}},[f]),C=o.useRef("");return o.useEffect(()=>{if(!r||!r.redeemServiceEnabled||!h)return;C.current!==h&&(E.current=!1,C.current=h),E.current||(S(),E.current=!0)},[r,h,S]),o.useEffect(()=>{if(r)if(r.redeemServiceEnabled){let e=!1;const s=()=>{e||(a.current=setTimeout(i,I))},i=async()=>{if(!e){if(p.current){s();return}p.current=!0;try{await A()}finally{p.current=!1}s()}};return i(),()=>{e=!0,a.current&&(clearTimeout(a.current),a.current=null)}}else{const e=s=>m(s);return r.on("onPendingOrdersChanged",e),()=>{r.off("onPendingOrdersChanged",e)}}},[r,A]),{pendingOrders:T}};exports.useOrderbook=k;
package/dist/index3.js CHANGED
@@ -1,66 +1,126 @@
1
- import { OrderStatus as l } from "@gardenfi/orderbook";
2
- import { useState as R, useRef as h, useCallback as y, useEffect as C } from "react";
3
- import { PENDING_ORDERS_STORE as g } from "./index6.js";
4
- const T = (t, u) => {
5
- const [O, c] = R([]), s = h(null), d = h(!1), E = 5e3, a = y(async () => {
6
- if (t)
1
+ import { OrderLifecycle as b, OrderStatus as A } from "@gardenfi/orderbook";
2
+ import { useState as k, useRef as m, useCallback as E, useMemo as D, useEffect as L } from "react";
3
+ import { PENDING_ORDERS_STORE as C } from "./index6.js";
4
+ const x = (e, l) => {
5
+ const [N, p] = k([]), a = m(null), g = m(!1), y = m(!1), w = 5e3, u = E(() => {
6
+ var s, c, d, o, t, n, i, O, I, T;
7
+ if (!e) return [];
8
+ const r = /* @__PURE__ */ new Set();
9
+ try {
10
+ [
11
+ (c = (s = e.htlcs) == null ? void 0 : s.evm) == null ? void 0 : c.htlcActorAddress,
12
+ (o = (d = e.htlcs) == null ? void 0 : d.sui) == null ? void 0 : o.htlcActorAddress,
13
+ (n = (t = e.htlcs) == null ? void 0 : t.solana) == null ? void 0 : n.htlcActorAddress,
14
+ (O = (i = e.htlcs) == null ? void 0 : i.starknet) == null ? void 0 : O.htlcActorAddress,
15
+ (T = (I = e.htlcs) == null ? void 0 : I.bitcoin) == null ? void 0 : T.htlcActorAddress
16
+ ].filter((h) => !!h && h.length > 0).forEach((h) => r.add(h.toLowerCase()));
17
+ } catch (v) {
18
+ console.error("Error getting HTLC addresses:", v);
19
+ }
20
+ return Array.from(r);
21
+ }, [e]), S = E(async () => {
22
+ if (e)
7
23
  try {
8
- const e = u.getItem(g), r = e ? JSON.parse(e) : [];
24
+ const r = u();
9
25
  if (r.length === 0) {
10
- c([]);
26
+ console.log("No HTLC addresses found for initial pending orders fetch");
11
27
  return;
12
28
  }
13
- const o = await Promise.all(r.map((n) => t.getOrder(n))), m = [], f = [];
14
- o.forEach((n, p) => {
15
- if (!n.ok) {
16
- f.push(r[p]);
29
+ const s = r.map(async (t) => {
30
+ try {
31
+ const n = await e.getOrders({
32
+ address: t,
33
+ status: b.pending,
34
+ per_page: 500
35
+ });
36
+ return n.ok ? n.val.data : (console.error(
37
+ `Failed to fetch orders for address ${t}:`,
38
+ n.error
39
+ ), []);
40
+ } catch (n) {
41
+ return console.error(`Error fetching orders for address ${t}:`, n), [];
42
+ }
43
+ }), o = (await Promise.all(s)).flat().map((t) => t.order_id);
44
+ if (o.length > 0)
45
+ try {
46
+ l.setItem(C, JSON.stringify(o));
47
+ } catch (t) {
48
+ console.error(
49
+ "Error saving initial pending order IDs to localStorage",
50
+ t
51
+ );
52
+ }
53
+ } catch (r) {
54
+ console.error("Error fetching initial pending orders:", r);
55
+ }
56
+ }, [e, l, u]), P = E(async () => {
57
+ if (e)
58
+ try {
59
+ const r = l.getItem(C), s = r ? JSON.parse(r) : [];
60
+ if (s.length === 0) {
61
+ p([]);
62
+ return;
63
+ }
64
+ const c = await Promise.all(s.map((t) => e.getOrder(t))), d = [], o = [];
65
+ c.forEach((t, n) => {
66
+ if (!t.ok) {
67
+ o.push(s[n]);
17
68
  return;
18
69
  }
19
- const i = n.val;
20
- i.status === l.Redeemed || i.status === l.Refunded || i.status === l.Expired || (m.push(i), f.push(r[p]));
70
+ const i = t.val;
71
+ i.status === A.Redeemed || i.status === A.Refunded || i.status === A.Expired || (d.push(i), o.push(s[n]));
21
72
  });
22
73
  try {
23
- u.setItem(g, JSON.stringify(f));
24
- } catch (n) {
25
- console.error("Error persisting remaining pending order ids", n);
74
+ l.setItem(C, JSON.stringify(o));
75
+ } catch (t) {
76
+ console.error("Error persisting remaining pending order ids", t);
26
77
  }
27
- c(m);
28
- } catch (e) {
29
- console.error("Error fetching pending orders:", e);
78
+ p(d);
79
+ } catch (r) {
80
+ console.error("Error fetching pending orders:", r);
30
81
  }
31
- }, [t, u]);
32
- return C(() => {
33
- if (t)
34
- if (t.redeemServiceEnabled) {
35
- let e = !1;
36
- const r = () => {
37
- e || (s.current = setTimeout(o, E));
38
- }, o = async () => {
39
- if (!e) {
40
- if (d.current) {
41
- r();
82
+ }, [e, l]), f = D(() => {
83
+ try {
84
+ return u().join(",");
85
+ } catch (r) {
86
+ return console.error("Error computing HTLC addresses:", r), "";
87
+ }
88
+ }, [u]), R = m("");
89
+ return L(() => {
90
+ if (!e || !e.redeemServiceEnabled || !f) return;
91
+ R.current !== f && (y.current = !1, R.current = f), y.current || (S(), y.current = !0);
92
+ }, [e, f, S]), L(() => {
93
+ if (e)
94
+ if (e.redeemServiceEnabled) {
95
+ let r = !1;
96
+ const s = () => {
97
+ r || (a.current = setTimeout(c, w));
98
+ }, c = async () => {
99
+ if (!r) {
100
+ if (g.current) {
101
+ s();
42
102
  return;
43
103
  }
44
- d.current = !0;
104
+ g.current = !0;
45
105
  try {
46
- await a();
106
+ await P();
47
107
  } finally {
48
- d.current = !1;
108
+ g.current = !1;
49
109
  }
50
- r();
110
+ s();
51
111
  }
52
112
  };
53
- return o(), () => {
54
- e = !0, s.current && (clearTimeout(s.current), s.current = null);
113
+ return c(), () => {
114
+ r = !0, a.current && (clearTimeout(a.current), a.current = null);
55
115
  };
56
116
  } else {
57
- const e = (r) => c(r);
58
- return t.on("onPendingOrdersChanged", e), () => {
59
- t.off("onPendingOrdersChanged", e);
117
+ const r = (s) => p(s);
118
+ return e.on("onPendingOrdersChanged", r), () => {
119
+ e.off("onPendingOrdersChanged", r);
60
120
  };
61
121
  }
62
- }, [t, a]), { pendingOrders: O };
122
+ }, [e, P]), { pendingOrders: N };
63
123
  };
64
124
  export {
65
- T as useOrderbook
125
+ x as useOrderbook
66
126
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/react-hooks",
3
- "version": "2.5.3-beta.3",
3
+ "version": "2.5.3-beta.5",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -27,7 +27,7 @@
27
27
  "registry": "https://registry.npmjs.org/"
28
28
  },
29
29
  "dependencies": {
30
- "@gardenfi/core": "2.5.3-beta.3",
30
+ "@gardenfi/core": "2.5.3-beta.5",
31
31
  "@gardenfi/orderbook": "2.5.3-beta.4",
32
32
  "@gardenfi/utils": "2.5.3-beta.3",
33
33
  "react": "^18.3.1",