@cowprotocol/widget-react 1.1.0 → 1.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/index.cjs +1 -1
- package/index.js +54 -43
- package/package.json +3 -3
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@cowprotocol/widget-lib"),h=require("react/jsx-runtime"),t=require("react");function E(u){const{params:n,provider:i,listeners:s}=u,[a,g]=t.useState(null),o=t.useRef(null),r=t.useRef(i),w=t.useRef(s),d=t.useRef(null),c=t.useRef(null),f=t.useCallback((e,y)=>{try{console.log(`[WIDGET] ${e}`),y()}catch(l){const m=`Error ${e.toLowerCase()}`,v=l instanceof Error?l:new Error("Unknown CowSwapWidget error",{cause:l});console.error(`[WIDGET] ${m}`,v),g({message:m,error:v})}},[]);return t.useEffect(()=>()=>{o.current=null,r.current=void 0,w.current=void 0;const e=c.current;e&&(f("💥 Destroy widget",()=>e.destroy()),c.current=null)},[]),t.useEffect(()=>{const e=!!o.current&&S(o.current,n);if(!d.current||JSON.stringify(o.current)===JSON.stringify(n)&&!e)return;const y=d.current,l=c.current;o.current=n,l===null?f("Creating a new widget",()=>{c.current=p.createCowSwapWidget(y,{params:n,provider:r.current,listeners:s}),w.current=s}):f("Updating the widget",()=>l.updateParams(n))},[n,f]),t.useEffect(()=>{if(!c.current||r.current===i)return;r.current=i;const e=d.current;e&&f("Updating the provider",()=>{c.current?.destroy(),c.current=p.createCowSwapWidget(e,{params:n,provider:r.current,listeners:s})})},[i,f]),t.useEffect(()=>{if(!c.current||w.current===s)return;const e=c.current;f("Updating the listeners",()=>e.updateListeners(s))},[s,f]),a?h.jsxs("div",{style:{color:"#ff3a3a"},children:[a.message,a.error.message&&h.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:a.error.message})]}):h.jsx("div",{ref:d,style:{width:"100%"}})}function S(u,n){const i=n.hooks??{},s=Object.keys(i),a=u.hooks??{},g=Object.keys(a);return s.some(o=>{const r=o;return i[r]!==a[r]})||g.some(o=>{const r=o;return i[r]!==a[r]})}exports.CowSwapWidget=E;Object.keys(p).forEach(u=>{u!=="default"&&!Object.prototype.hasOwnProperty.call(exports,u)&&Object.defineProperty(exports,u,{enumerable:!0,get:()=>p[u]})});
|
package/index.js
CHANGED
|
@@ -1,68 +1,79 @@
|
|
|
1
|
-
import { createCowSwapWidget as
|
|
1
|
+
import { createCowSwapWidget as k } from "@cowprotocol/widget-lib";
|
|
2
2
|
export * from "@cowprotocol/widget-lib";
|
|
3
|
-
import { jsxs as
|
|
4
|
-
import { useState as
|
|
5
|
-
function
|
|
3
|
+
import { jsxs as E, jsx as v } from "react/jsx-runtime";
|
|
4
|
+
import { useState as S, useRef as l, useCallback as x, useEffect as d } from "react";
|
|
5
|
+
function O(p) {
|
|
6
6
|
const {
|
|
7
|
-
params:
|
|
8
|
-
provider:
|
|
7
|
+
params: t,
|
|
8
|
+
provider: c,
|
|
9
9
|
listeners: n
|
|
10
|
-
} =
|
|
10
|
+
} = p, [i, g] = S(null), o = l(null), e = l(c), m = l(n), f = l(null), s = l(null), u = x((r, w) => {
|
|
11
11
|
try {
|
|
12
|
-
console.log(`[WIDGET] ${r}`),
|
|
13
|
-
} catch (
|
|
14
|
-
const
|
|
15
|
-
cause:
|
|
12
|
+
console.log(`[WIDGET] ${r}`), w();
|
|
13
|
+
} catch (a) {
|
|
14
|
+
const y = `Error ${r.toLowerCase()}`, h = a instanceof Error ? a : new Error("Unknown CowSwapWidget error", {
|
|
15
|
+
cause: a
|
|
16
16
|
});
|
|
17
|
-
console.error(`[WIDGET] ${
|
|
18
|
-
message:
|
|
19
|
-
error:
|
|
17
|
+
console.error(`[WIDGET] ${y}`, h), g({
|
|
18
|
+
message: y,
|
|
19
|
+
error: h
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
22
|
}, []);
|
|
23
23
|
return d(() => () => {
|
|
24
|
-
|
|
25
|
-
const r =
|
|
26
|
-
r && (
|
|
24
|
+
o.current = null, e.current = void 0, m.current = void 0;
|
|
25
|
+
const r = s.current;
|
|
26
|
+
r && (u("💥 Destroy widget", () => r.destroy()), s.current = null);
|
|
27
27
|
}, []), d(() => {
|
|
28
|
-
|
|
28
|
+
const r = !!o.current && C(o.current, t);
|
|
29
|
+
if (!f.current || JSON.stringify(o.current) === JSON.stringify(t) && !r)
|
|
29
30
|
return;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
params:
|
|
34
|
-
provider:
|
|
31
|
+
const w = f.current, a = s.current;
|
|
32
|
+
o.current = t, a === null ? u("Creating a new widget", () => {
|
|
33
|
+
s.current = k(w, {
|
|
34
|
+
params: t,
|
|
35
|
+
provider: e.current,
|
|
35
36
|
listeners: n
|
|
36
|
-
}),
|
|
37
|
-
}) :
|
|
38
|
-
}, [
|
|
39
|
-
if (!
|
|
37
|
+
}), m.current = n;
|
|
38
|
+
}) : u("Updating the widget", () => a.updateParams(t));
|
|
39
|
+
}, [t, u]), d(() => {
|
|
40
|
+
if (!s.current || e.current === c)
|
|
40
41
|
return;
|
|
41
|
-
|
|
42
|
-
const r =
|
|
43
|
-
r &&
|
|
44
|
-
|
|
45
|
-
params:
|
|
46
|
-
provider:
|
|
42
|
+
e.current = c;
|
|
43
|
+
const r = f.current;
|
|
44
|
+
r && u("Updating the provider", () => {
|
|
45
|
+
s.current?.destroy(), s.current = k(r, {
|
|
46
|
+
params: t,
|
|
47
|
+
provider: e.current,
|
|
47
48
|
listeners: n
|
|
48
49
|
});
|
|
49
50
|
});
|
|
50
|
-
}, [
|
|
51
|
-
if (!
|
|
52
|
-
const r =
|
|
53
|
-
|
|
54
|
-
}, [n,
|
|
51
|
+
}, [c, u]), d(() => {
|
|
52
|
+
if (!s.current || m.current === n) return;
|
|
53
|
+
const r = s.current;
|
|
54
|
+
u("Updating the listeners", () => r.updateListeners(n));
|
|
55
|
+
}, [n, u]), i ? /* @__PURE__ */ E("div", { style: {
|
|
55
56
|
color: "#ff3a3a"
|
|
56
57
|
}, children: [
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
i.message,
|
|
59
|
+
i.error.message && /* @__PURE__ */ v("pre", { style: {
|
|
59
60
|
whiteSpace: "pre-wrap",
|
|
60
61
|
fontSize: "0.75em"
|
|
61
|
-
}, children:
|
|
62
|
-
] }) : /* @__PURE__ */
|
|
62
|
+
}, children: i.error.message })
|
|
63
|
+
] }) : /* @__PURE__ */ v("div", { ref: f, style: {
|
|
63
64
|
width: "100%"
|
|
64
65
|
} });
|
|
65
66
|
}
|
|
67
|
+
function C(p, t) {
|
|
68
|
+
const c = t.hooks ?? {}, n = Object.keys(c), i = p.hooks ?? {}, g = Object.keys(i);
|
|
69
|
+
return n.some((o) => {
|
|
70
|
+
const e = o;
|
|
71
|
+
return c[e] !== i[e];
|
|
72
|
+
}) || g.some((o) => {
|
|
73
|
+
const e = o;
|
|
74
|
+
return c[e] !== i[e];
|
|
75
|
+
});
|
|
76
|
+
}
|
|
66
77
|
export {
|
|
67
|
-
|
|
78
|
+
O as CowSwapWidget
|
|
68
79
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@cowprotocol/events": "^4.0
|
|
18
|
+
"@cowprotocol/events": "^4.1.0",
|
|
19
19
|
"@cowprotocol/types": "^4.1.0",
|
|
20
|
-
"@cowprotocol/widget-lib": "^2.
|
|
20
|
+
"@cowprotocol/widget-lib": "^2.1.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/react": "19.1.3"
|