@cowprotocol/widget-react 3.0.5 → 3.1.0
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 +109 -59
- package/package.json +10 -5
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 k=require("@cowprotocol/widget-lib"),h=require("react/jsx-runtime"),s=require("react");function E(e){const{params:r,provider:t,listeners:n,onReady:c,enableSafeSdkBridge:o=!0}=e,[f,d]=s.useState(null),y=s.useRef(null),l=s.useRef(t),w=s.useRef(n),R=s.useRef(o),S=s.useRef(null),u=s.useRef(null),i=s.useCallback((a,m)=>{try{console.log(`[WIDGET] ${a}`),m()}catch(g){const p=`Error ${a.toLowerCase()}`,v=g instanceof Error?g:new Error("Unknown CowSwapWidget error",{cause:g});console.error(`[WIDGET] ${p}`,v),d({message:p,error:v})}},[]);return j({refs:{paramsRef:y,providerRef:l,listenersRef:w,enableSafeSdkBridgeRef:R,widgetHandlerRef:u},tryOrHandleError:i}),s.useEffect(()=>{const a=!!y.current&&W(y.current,r),m=R.current!==o;if(!S.current||JSON.stringify(y.current)===JSON.stringify(r)&&!a&&!m)return;const g=S.current,p=u.current;y.current=r,R.current=o,p===null?i("Creating a new widget",()=>{u.current=b({container:g,params:r,provider:l.current,listeners:n,onReady:c,enableSafeSdkBridge:o}),w.current=n}):m?i("Recreating the widget",()=>{p.destroy(),u.current=b({container:g,params:r,provider:l.current,listeners:n,onReady:c,enableSafeSdkBridge:o}),w.current=n}):i("Updating the widget",()=>p.updateParams(r))},[r,o,i]),s.useEffect(()=>{if(!u.current||l.current===t)return;l.current=t;const a=S.current;a&&i("Updating the provider",()=>{u.current?.destroy(),u.current=b({container:a,params:r,provider:l.current,listeners:n,onReady:c,enableSafeSdkBridge:o}),R.current=o})},[c,t,i]),s.useEffect(()=>{if(!u.current||w.current===n)return;const a=u.current;i("Updating the listeners",()=>a.updateListeners(n))},[n,i]),f?h.jsx(O,{error:f}):h.jsx("div",{ref:S,style:{width:"100%"}})}function j({refs:e,tryOrHandleError:r}){s.useEffect(()=>()=>{e.paramsRef.current=null,e.providerRef.current=void 0,e.listenersRef.current=void 0,e.enableSafeSdkBridgeRef.current=!0;const t=e.widgetHandlerRef.current;t&&(r("Destroy widget",()=>t.destroy()),e.widgetHandlerRef.current=null)},[])}function O({error:e}){return h.jsxs("div",{style:{color:"#ff3a3a"},children:[e.message,e.error.message&&h.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:e.error.message})]})}function W(e,r){const t=r.hooks??{},n=Object.keys(t),c=e.hooks??{},o=Object.keys(c);return n.some(f=>{const d=f;return t[d]!==c[d]})||o.some(f=>{const d=f;return t[d]!==c[d]})}function b({container:e,params:r,provider:t,listeners:n,onReady:c,enableSafeSdkBridge:o}){return k.createCowSwapWidget(e,{params:r,provider:t,listeners:n,onReady:c,enableSafeSdkBridge:o})}exports.CowSwapWidget=E;Object.keys(k).forEach(e=>{e!=="default"&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:()=>k[e]})});
|
package/index.js
CHANGED
|
@@ -1,82 +1,132 @@
|
|
|
1
|
-
import { createCowSwapWidget as
|
|
1
|
+
import { createCowSwapWidget as H } from "@cowprotocol/widget-lib";
|
|
2
2
|
export * from "@cowprotocol/widget-lib";
|
|
3
|
-
import {
|
|
4
|
-
import { useState as
|
|
5
|
-
function
|
|
3
|
+
import { jsx as v, jsxs as W } from "react/jsx-runtime";
|
|
4
|
+
import { useState as b, useRef as p, useCallback as D, useEffect as R } from "react";
|
|
5
|
+
function $(e) {
|
|
6
6
|
const {
|
|
7
|
-
params:
|
|
8
|
-
provider:
|
|
7
|
+
params: r,
|
|
8
|
+
provider: t,
|
|
9
9
|
listeners: n,
|
|
10
|
-
onReady:
|
|
11
|
-
|
|
10
|
+
onReady: s,
|
|
11
|
+
enableSafeSdkBridge: o = !0
|
|
12
|
+
} = e, [d, a] = b(null), m = p(null), f = p(t), w = p(n), y = p(o), k = p(null), c = p(null), i = D((u, S) => {
|
|
12
13
|
try {
|
|
13
|
-
console.log(`[WIDGET] ${
|
|
14
|
-
} catch (
|
|
15
|
-
const
|
|
16
|
-
cause:
|
|
14
|
+
console.log(`[WIDGET] ${u}`), S();
|
|
15
|
+
} catch (l) {
|
|
16
|
+
const g = `Error ${u.toLowerCase()}`, E = l instanceof Error ? l : new Error("Unknown CowSwapWidget error", {
|
|
17
|
+
cause: l
|
|
17
18
|
});
|
|
18
|
-
console.error(`[WIDGET] ${
|
|
19
|
-
message:
|
|
20
|
-
error:
|
|
19
|
+
console.error(`[WIDGET] ${g}`, E), a({
|
|
20
|
+
message: g,
|
|
21
|
+
error: E
|
|
21
22
|
});
|
|
22
23
|
}
|
|
23
24
|
}, []);
|
|
24
|
-
return
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
return x({
|
|
26
|
+
refs: {
|
|
27
|
+
paramsRef: m,
|
|
28
|
+
providerRef: f,
|
|
29
|
+
listenersRef: w,
|
|
30
|
+
enableSafeSdkBridgeRef: y,
|
|
31
|
+
widgetHandlerRef: c
|
|
32
|
+
},
|
|
33
|
+
tryOrHandleError: i
|
|
34
|
+
}), R(() => {
|
|
35
|
+
const u = !!m.current && O(m.current, r), S = y.current !== o;
|
|
36
|
+
if (!k.current || JSON.stringify(m.current) === JSON.stringify(r) && !u && !S)
|
|
31
37
|
return;
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
const l = k.current, g = c.current;
|
|
39
|
+
m.current = r, y.current = o, g === null ? i("Creating a new widget", () => {
|
|
40
|
+
c.current = h({
|
|
41
|
+
container: l,
|
|
42
|
+
params: r,
|
|
43
|
+
provider: f.current,
|
|
37
44
|
listeners: n,
|
|
38
|
-
onReady:
|
|
45
|
+
onReady: s,
|
|
46
|
+
enableSafeSdkBridge: o
|
|
39
47
|
}), w.current = n;
|
|
40
|
-
}) :
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
}) : S ? i("Recreating the widget", () => {
|
|
49
|
+
g.destroy(), c.current = h({
|
|
50
|
+
container: l,
|
|
51
|
+
params: r,
|
|
52
|
+
provider: f.current,
|
|
53
|
+
listeners: n,
|
|
54
|
+
onReady: s,
|
|
55
|
+
enableSafeSdkBridge: o
|
|
56
|
+
}), w.current = n;
|
|
57
|
+
}) : i("Updating the widget", () => g.updateParams(r));
|
|
58
|
+
}, [r, o, i]), R(() => {
|
|
59
|
+
if (!c.current || f.current === t)
|
|
43
60
|
return;
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
61
|
+
f.current = t;
|
|
62
|
+
const u = k.current;
|
|
63
|
+
u && i("Updating the provider", () => {
|
|
64
|
+
c.current?.destroy(), c.current = h({
|
|
65
|
+
container: u,
|
|
66
|
+
params: r,
|
|
67
|
+
provider: f.current,
|
|
50
68
|
listeners: n,
|
|
51
|
-
onReady:
|
|
52
|
-
|
|
69
|
+
onReady: s,
|
|
70
|
+
enableSafeSdkBridge: o
|
|
71
|
+
}), y.current = o;
|
|
53
72
|
});
|
|
54
|
-
}, [
|
|
55
|
-
if (!
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
}, [n,
|
|
73
|
+
}, [s, t, i]), R(() => {
|
|
74
|
+
if (!c.current || w.current === n) return;
|
|
75
|
+
const u = c.current;
|
|
76
|
+
i("Updating the listeners", () => u.updateListeners(n));
|
|
77
|
+
}, [n, i]), d ? /* @__PURE__ */ v(C, { error: d }) : /* @__PURE__ */ v("div", { ref: k, style: {
|
|
78
|
+
width: "100%"
|
|
79
|
+
} });
|
|
80
|
+
}
|
|
81
|
+
function x({
|
|
82
|
+
refs: e,
|
|
83
|
+
tryOrHandleError: r
|
|
84
|
+
}) {
|
|
85
|
+
R(() => () => {
|
|
86
|
+
e.paramsRef.current = null, e.providerRef.current = void 0, e.listenersRef.current = void 0, e.enableSafeSdkBridgeRef.current = !0;
|
|
87
|
+
const t = e.widgetHandlerRef.current;
|
|
88
|
+
t && (r("Destroy widget", () => t.destroy()), e.widgetHandlerRef.current = null);
|
|
89
|
+
}, []);
|
|
90
|
+
}
|
|
91
|
+
function C({
|
|
92
|
+
error: e
|
|
93
|
+
}) {
|
|
94
|
+
return /* @__PURE__ */ W("div", { style: {
|
|
59
95
|
color: "#ff3a3a"
|
|
60
96
|
}, children: [
|
|
61
|
-
|
|
62
|
-
|
|
97
|
+
e.message,
|
|
98
|
+
e.error.message && /* @__PURE__ */ v("pre", { style: {
|
|
63
99
|
whiteSpace: "pre-wrap",
|
|
64
100
|
fontSize: "0.75em"
|
|
65
|
-
}, children:
|
|
66
|
-
] })
|
|
67
|
-
width: "100%"
|
|
68
|
-
} });
|
|
101
|
+
}, children: e.error.message })
|
|
102
|
+
] });
|
|
69
103
|
}
|
|
70
|
-
function
|
|
71
|
-
const
|
|
72
|
-
return n.some((
|
|
73
|
-
const
|
|
74
|
-
return
|
|
75
|
-
}) ||
|
|
76
|
-
const
|
|
77
|
-
return
|
|
104
|
+
function O(e, r) {
|
|
105
|
+
const t = r.hooks ?? {}, n = Object.keys(t), s = e.hooks ?? {}, o = Object.keys(s);
|
|
106
|
+
return n.some((d) => {
|
|
107
|
+
const a = d;
|
|
108
|
+
return t[a] !== s[a];
|
|
109
|
+
}) || o.some((d) => {
|
|
110
|
+
const a = d;
|
|
111
|
+
return t[a] !== s[a];
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function h({
|
|
115
|
+
container: e,
|
|
116
|
+
params: r,
|
|
117
|
+
provider: t,
|
|
118
|
+
listeners: n,
|
|
119
|
+
onReady: s,
|
|
120
|
+
enableSafeSdkBridge: o
|
|
121
|
+
}) {
|
|
122
|
+
return H(e, {
|
|
123
|
+
params: r,
|
|
124
|
+
provider: t,
|
|
125
|
+
listeners: n,
|
|
126
|
+
onReady: s,
|
|
127
|
+
enableSafeSdkBridge: o
|
|
78
128
|
});
|
|
79
129
|
}
|
|
80
130
|
export {
|
|
81
|
-
|
|
131
|
+
$ as CowSwapWidget
|
|
82
132
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
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,12 +15,17 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@cowprotocol/events": "^4.
|
|
19
|
-
"@cowprotocol/types": "^4.
|
|
20
|
-
"@cowprotocol/widget-lib": "^4.0
|
|
18
|
+
"@cowprotocol/events": "^4.5.0",
|
|
19
|
+
"@cowprotocol/types": "^4.4.0",
|
|
20
|
+
"@cowprotocol/widget-lib": "^4.1.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@
|
|
23
|
+
"@testing-library/dom": "10.4.1",
|
|
24
|
+
"@testing-library/react": "16.3.0",
|
|
25
|
+
"@types/react": "19.1.3",
|
|
26
|
+
"@types/react-dom": "19.1.3",
|
|
27
|
+
"react": "19.1.2",
|
|
28
|
+
"react-dom": "19.1.2"
|
|
24
29
|
},
|
|
25
30
|
"peerDependencies": {
|
|
26
31
|
"react": "19.1.2"
|