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