@cowprotocol/widget-react 1.0.9 → 1.0.11
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/CHANGELOG.md +27 -0
- package/package.json +30 -8
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -0
- package/src/index.js.map +1 -0
- package/src/lib/CowSwapWidget.d.ts +3 -0
- package/src/lib/CowSwapWidget.js +101 -0
- package/src/lib/CowSwapWidget.js.map +1 -0
- package/jest.config.ts +0 -11
- package/project.json +0 -42
- package/src/index.ts +0 -2
- package/src/lib/CowSwapWidget.spec.tsx +0 -7
- package/src/lib/CowSwapWidget.tsx +0 -125
- package/tsconfig.json +0 -21
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.0.11](https://github.com/cowprotocol/cowswap/compare/widget-react-v1.0.10...widget-react-v1.0.11) (2026-03-18)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### 🐛 Bug Fixes
|
|
7
|
+
|
|
8
|
+
* update libs publising and bundling ([#7173](https://github.com/cowprotocol/cowswap/issues/7173)) ([e3bc5f1](https://github.com/cowprotocol/cowswap/commit/e3bc5f179124c11590a992789db43d912f8ee4a6))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @cowprotocol/events bumped to 4.0.1
|
|
16
|
+
* @cowprotocol/types bumped to 4.0.1
|
|
17
|
+
* @cowprotocol/widget-lib bumped to 2.0.1
|
|
18
|
+
|
|
19
|
+
## [1.0.10](https://github.com/cowprotocol/cowswap/compare/widget-react-v1.0.9...widget-react-v1.0.10) (2026-03-11)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Dependencies
|
|
23
|
+
|
|
24
|
+
* The following workspace dependencies were updated
|
|
25
|
+
* dependencies
|
|
26
|
+
* @cowprotocol/events bumped to 4.0.0
|
|
27
|
+
* @cowprotocol/types bumped to 4.0.0
|
|
28
|
+
* @cowprotocol/widget-lib bumped to 2.0.0
|
|
29
|
+
|
|
3
30
|
## [1.0.9](https://github.com/cowprotocol/cowswap/compare/widget-react-v1.0.8...widget-react-v1.0.9) (2026-03-10)
|
|
4
31
|
|
|
5
32
|
|
package/package.json
CHANGED
|
@@ -1,23 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/cowprotocol/cowswap.git",
|
|
8
|
+
"directory": "libs/widget-react"
|
|
9
|
+
},
|
|
5
10
|
"type": "commonjs",
|
|
6
|
-
"main": "./src/index.
|
|
11
|
+
"main": "./src/index.js",
|
|
7
12
|
"types": "./src/index.d.ts",
|
|
8
13
|
"license": "ISC",
|
|
9
14
|
"publishConfig": {
|
|
10
15
|
"access": "public"
|
|
11
16
|
},
|
|
12
17
|
"dependencies": {
|
|
13
|
-
"@cowprotocol/
|
|
14
|
-
"@cowprotocol/
|
|
15
|
-
"@cowprotocol/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"@types/react": "19.1.3"
|
|
18
|
+
"@cowprotocol/cow-sdk": "8.0.0",
|
|
19
|
+
"@cowprotocol/events": "^4.0.1",
|
|
20
|
+
"@cowprotocol/sdk-bridging": "3.0.0",
|
|
21
|
+
"@cowprotocol/types": "^4.0.1",
|
|
22
|
+
"@cowprotocol/widget-lib": "^2.0.1",
|
|
23
|
+
"@types/react": "19.1.3",
|
|
24
|
+
"@uniswap/sdk-core": "3.2.6",
|
|
25
|
+
"@web3-react/types": "8.2.3",
|
|
26
|
+
"eventemitter3": "4.0.7",
|
|
27
|
+
"@cowprotocol/iframe-transport": "2.0.9",
|
|
28
|
+
"tslib": "1.14.1"
|
|
19
29
|
},
|
|
20
30
|
"peerDependencies": {
|
|
21
31
|
"react": "19.1.2"
|
|
32
|
+
},
|
|
33
|
+
"resolutions": {
|
|
34
|
+
"@safe-global/safe-apps-sdk": "^9.1.0",
|
|
35
|
+
"@types/react": "19.1.3",
|
|
36
|
+
"@types/react-dom": "19.1.3",
|
|
37
|
+
"stylus": "npm:empty-npm-package@1.0.0",
|
|
38
|
+
"@babel/traverse": "^7.23.2",
|
|
39
|
+
"@walletconnect/ethereum-provider": "2.18.0",
|
|
40
|
+
"@walletconnect/sign-client": "2.18.0",
|
|
41
|
+
"@walletconnect/universal-provider": "2.18.0",
|
|
42
|
+
"@walletconnect/utils": "2.18.0",
|
|
43
|
+
"@walletconnect/types": "2.18.0"
|
|
22
44
|
}
|
|
23
45
|
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/widget-react/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,yBAAyB,CAAA"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { createCowSwapWidget, } from '@cowprotocol/widget-lib';
|
|
4
|
+
export function CowSwapWidget(props) {
|
|
5
|
+
const { params, provider, listeners } = props;
|
|
6
|
+
const [error, setError] = useState(null);
|
|
7
|
+
const paramsRef = useRef(null);
|
|
8
|
+
const providerRef = useRef(provider);
|
|
9
|
+
const listenersRef = useRef(listeners);
|
|
10
|
+
const containerRef = useRef(null);
|
|
11
|
+
const widgetHandlerRef = useRef(null);
|
|
12
|
+
// Error handling
|
|
13
|
+
const tryOrHandleError = useCallback((action, actionThatMightFail) => {
|
|
14
|
+
try {
|
|
15
|
+
console.log(`[WIDGET] ${action}`);
|
|
16
|
+
actionThatMightFail();
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
const errorMessage = `Error ${action.toLowerCase()}`;
|
|
20
|
+
console.error(`[WIDGET] ${errorMessage}`, error);
|
|
21
|
+
setError({ message: errorMessage, error });
|
|
22
|
+
}
|
|
23
|
+
}, []);
|
|
24
|
+
// Cleanup widget on mount
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
return () => {
|
|
27
|
+
// Cleanup references
|
|
28
|
+
paramsRef.current = null;
|
|
29
|
+
providerRef.current = undefined;
|
|
30
|
+
listenersRef.current = undefined;
|
|
31
|
+
// Destroy widget
|
|
32
|
+
const handler = widgetHandlerRef.current;
|
|
33
|
+
if (handler) {
|
|
34
|
+
tryOrHandleError('💥 Destroy widget', () => handler.destroy());
|
|
35
|
+
widgetHandlerRef.current = null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
39
|
+
}, []);
|
|
40
|
+
// Create/Update the widget if the parameters change
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!containerRef.current || JSON.stringify(paramsRef.current) === JSON.stringify(params)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const container = containerRef.current;
|
|
46
|
+
const handler = widgetHandlerRef.current;
|
|
47
|
+
paramsRef.current = params;
|
|
48
|
+
if (handler === null) {
|
|
49
|
+
tryOrHandleError('Creating a new widget', () => {
|
|
50
|
+
widgetHandlerRef.current = createCowSwapWidget(container, { params, provider: providerRef.current, listeners });
|
|
51
|
+
listenersRef.current = listeners;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
tryOrHandleError('Updating the widget', () => handler.updateParams(params));
|
|
56
|
+
}
|
|
57
|
+
// Trigger only on params changes
|
|
58
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
59
|
+
}, [params, tryOrHandleError]);
|
|
60
|
+
// Update widget provider (if it changes)
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (!widgetHandlerRef.current || providerRef.current === provider) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// Update provider
|
|
66
|
+
providerRef.current = provider;
|
|
67
|
+
// TODO: Fix this https://github.com/cowprotocol/cowswap/issues/3810#issue-2127257473 (in meantime forcing full refresh as before)
|
|
68
|
+
// const handler = widgetHandlerRef.current
|
|
69
|
+
// tryOrHandleError('Updating the provider', () => {
|
|
70
|
+
// handler.updateProvider(provider)
|
|
71
|
+
// if (paramsRef.current) {
|
|
72
|
+
// handler.updateWidget(paramsRef.current)
|
|
73
|
+
// }
|
|
74
|
+
// })
|
|
75
|
+
const container = containerRef.current;
|
|
76
|
+
if (container) {
|
|
77
|
+
tryOrHandleError('Updating the provider', () => {
|
|
78
|
+
// Destroy the old widget (if it exists)
|
|
79
|
+
widgetHandlerRef.current?.destroy();
|
|
80
|
+
// Re-create the widget
|
|
81
|
+
widgetHandlerRef.current = createCowSwapWidget(container, { params, provider: providerRef.current, listeners });
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// Trigger only on provider changes
|
|
85
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
86
|
+
}, [provider, tryOrHandleError]);
|
|
87
|
+
// Update widget listeners (if they change)
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
if (!widgetHandlerRef.current || listenersRef.current === listeners)
|
|
90
|
+
return;
|
|
91
|
+
const handler = widgetHandlerRef.current;
|
|
92
|
+
tryOrHandleError('Updating the listeners', () => handler.updateListeners(listeners));
|
|
93
|
+
}, [listeners, tryOrHandleError]);
|
|
94
|
+
// Handle errors
|
|
95
|
+
if (error) {
|
|
96
|
+
return (_jsxs("div", { style: { color: '#ff3a3a' }, children: [error.message, error.error.message && _jsx("pre", { style: { whiteSpace: 'pre-wrap', fontSize: '0.75em' }, children: error.error.message })] }));
|
|
97
|
+
}
|
|
98
|
+
// Render widget container
|
|
99
|
+
return _jsx("div", { ref: containerRef, style: { width: '100%' } });
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=CowSwapWidget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CowSwapWidget.js","sourceRoot":"","sources":["../../../../../libs/widget-react/src/lib/CowSwapWidget.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAa,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAI3E,OAAO,EAKL,mBAAmB,GACpB,MAAM,yBAAyB,CAAA;AAEhC,MAAM,UAAU,aAAa,CAAC,KAAyB;IACrD,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,KAAK,CAAA;IAC7C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAA2C,IAAI,CAAC,CAAA;IAClF,MAAM,SAAS,GAAG,MAAM,CAA6B,IAAI,CAAC,CAAA;IAC1D,MAAM,WAAW,GAAG,MAAM,CAA+B,QAAQ,CAAC,CAAA;IAClE,MAAM,YAAY,GAAG,MAAM,CAAsC,SAAS,CAAC,CAAA;IAC3E,MAAM,YAAY,GAAG,MAAM,CAAwB,IAAI,CAAC,CAAA;IACxD,MAAM,gBAAgB,GAAG,MAAM,CAA8B,IAAI,CAAC,CAAA;IAElE,iBAAiB;IACjB,MAAM,gBAAgB,GAAG,WAAW,CAAC,CAAC,MAAc,EAAE,mBAA4B,EAAE,EAAE;QACpF,IAAI,CAAC;YACH,OAAO,CAAC,GAAG,CAAC,YAAY,MAAM,EAAE,CAAC,CAAA;YACjC,mBAAmB,EAAE,CAAA;QACvB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,YAAY,GAAG,SAAS,MAAM,CAAC,WAAW,EAAE,EAAE,CAAA;YACpD,OAAO,CA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IAAI,YAAY,CAAC,OAAO,KAAK,SAAS;YAAE,OAAM;QAE3E,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAA;QACxC,gBAAgB,CAAC,wBAAwB,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC,CAAA;IACtF,CAAC,EAAE,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC,CAAA;IAEjC,gBAAgB;IAChB,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,CACL,eAAK,KAAK,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,aAC7B,KAAK,CAAC,OAAO,EACb,KAAK,CAAC,KAAK,CAAC,OAAO,IAAI,cAAK,KAAK,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAG,KAAK,CAAC,KAAK,CAAC,OAAO,GAAO,IAC3G,CACP,CAAA;IACH,CAAC;IAED,0BAA0B;IAC1B,OAAO,cAAK,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAQ,CAAA;AACjE,CAAC"}
|
package/jest.config.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
displayName: 'widget-react',
|
|
3
|
-
preset: '../../jest.preset.js',
|
|
4
|
-
transform: {
|
|
5
|
-
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
|
|
6
|
-
'^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
|
|
7
|
-
},
|
|
8
|
-
setupFilesAfterEnv: ['../../jest.setup.ts'],
|
|
9
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
10
|
-
coverageDirectory: '../../../coverage/libs/widget-react',
|
|
11
|
-
}
|
package/project.json
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "widget-react",
|
|
3
|
-
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "libs/widget-react/src",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"tags": [],
|
|
7
|
-
"targets": {
|
|
8
|
-
"lint": {
|
|
9
|
-
"executor": "@nx/eslint:lint",
|
|
10
|
-
"outputs": ["{options.outputFile}"],
|
|
11
|
-
"options": {
|
|
12
|
-
"lintFilePatterns": ["libs/widget-react/**/*.{ts,tsx,js,jsx}"]
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
"build": {
|
|
16
|
-
"executor": "@nx/js:tsc",
|
|
17
|
-
"outputs": ["{options.outputPath}"],
|
|
18
|
-
"options": {
|
|
19
|
-
"outputPath": "dist/libs/widget-react",
|
|
20
|
-
"main": "libs/widget-react/src/index.ts",
|
|
21
|
-
"tsConfig": "libs/widget-react/tsconfig.lib.json",
|
|
22
|
-
"assets": ["libs/widget-react/*.md"],
|
|
23
|
-
"updateBuildableProjectDepsInPackageJson": true,
|
|
24
|
-
"buildableProjectDepsInPackageJsonType": "dependencies"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"test": {
|
|
28
|
-
"executor": "@nx/jest:jest",
|
|
29
|
-
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
30
|
-
"options": {
|
|
31
|
-
"jestConfig": "libs/widget-react/jest.config.ts",
|
|
32
|
-
"passWithNoTests": true
|
|
33
|
-
},
|
|
34
|
-
"configurations": {
|
|
35
|
-
"ci": {
|
|
36
|
-
"ci": true,
|
|
37
|
-
"codeCoverage": true
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { ReactNode, useCallback, useEffect, useRef, useState } from 'react'
|
|
2
|
-
|
|
3
|
-
import type { CowWidgetEventListeners } from '@cowprotocol/events'
|
|
4
|
-
import type { Command } from '@cowprotocol/types'
|
|
5
|
-
import {
|
|
6
|
-
CowSwapWidgetHandler,
|
|
7
|
-
CowSwapWidgetParams,
|
|
8
|
-
CowSwapWidgetProps,
|
|
9
|
-
EthereumProvider,
|
|
10
|
-
createCowSwapWidget,
|
|
11
|
-
} from '@cowprotocol/widget-lib'
|
|
12
|
-
|
|
13
|
-
export function CowSwapWidget(props: CowSwapWidgetProps): ReactNode {
|
|
14
|
-
const { params, provider, listeners } = props
|
|
15
|
-
const [error, setError] = useState<{ error: Error; message: string } | null>(null)
|
|
16
|
-
const paramsRef = useRef<CowSwapWidgetParams | null>(null)
|
|
17
|
-
const providerRef = useRef<EthereumProvider | undefined>(provider)
|
|
18
|
-
const listenersRef = useRef<CowWidgetEventListeners | undefined>(listeners)
|
|
19
|
-
const containerRef = useRef<HTMLDivElement | null>(null)
|
|
20
|
-
const widgetHandlerRef = useRef<CowSwapWidgetHandler | null>(null)
|
|
21
|
-
|
|
22
|
-
// Error handling
|
|
23
|
-
const tryOrHandleError = useCallback((action: string, actionThatMightFail: Command) => {
|
|
24
|
-
try {
|
|
25
|
-
console.log(`[WIDGET] ${action}`)
|
|
26
|
-
actionThatMightFail()
|
|
27
|
-
} catch (error) {
|
|
28
|
-
const errorMessage = `Error ${action.toLowerCase()}`
|
|
29
|
-
console.error(`[WIDGET] ${errorMessage}`, error)
|
|
30
|
-
setError({ message: errorMessage, error })
|
|
31
|
-
}
|
|
32
|
-
}, [])
|
|
33
|
-
|
|
34
|
-
// Cleanup widget on mount
|
|
35
|
-
useEffect(() => {
|
|
36
|
-
return () => {
|
|
37
|
-
// Cleanup references
|
|
38
|
-
paramsRef.current = null
|
|
39
|
-
providerRef.current = undefined
|
|
40
|
-
listenersRef.current = undefined
|
|
41
|
-
|
|
42
|
-
// Destroy widget
|
|
43
|
-
const handler = widgetHandlerRef.current
|
|
44
|
-
if (handler) {
|
|
45
|
-
tryOrHandleError('💥 Destroy widget', () => handler.destroy())
|
|
46
|
-
widgetHandlerRef.current = null
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
50
|
-
}, [])
|
|
51
|
-
|
|
52
|
-
// Create/Update the widget if the parameters change
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
if (!containerRef.current || JSON.stringify(paramsRef.current) === JSON.stringify(params)) {
|
|
55
|
-
return
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const container = containerRef.current
|
|
59
|
-
const handler = widgetHandlerRef.current
|
|
60
|
-
paramsRef.current = params
|
|
61
|
-
|
|
62
|
-
if (handler === null) {
|
|
63
|
-
tryOrHandleError('Creating a new widget', () => {
|
|
64
|
-
widgetHandlerRef.current = createCowSwapWidget(container, { params, provider: providerRef.current, listeners })
|
|
65
|
-
listenersRef.current = listeners
|
|
66
|
-
})
|
|
67
|
-
} else {
|
|
68
|
-
tryOrHandleError('Updating the widget', () => handler.updateParams(params))
|
|
69
|
-
}
|
|
70
|
-
// Trigger only on params changes
|
|
71
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
72
|
-
}, [params, tryOrHandleError])
|
|
73
|
-
|
|
74
|
-
// Update widget provider (if it changes)
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
if (!widgetHandlerRef.current || providerRef.current === provider) {
|
|
77
|
-
return
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Update provider
|
|
81
|
-
providerRef.current = provider
|
|
82
|
-
|
|
83
|
-
// TODO: Fix this https://github.com/cowprotocol/cowswap/issues/3810#issue-2127257473 (in meantime forcing full refresh as before)
|
|
84
|
-
// const handler = widgetHandlerRef.current
|
|
85
|
-
// tryOrHandleError('Updating the provider', () => {
|
|
86
|
-
// handler.updateProvider(provider)
|
|
87
|
-
// if (paramsRef.current) {
|
|
88
|
-
// handler.updateWidget(paramsRef.current)
|
|
89
|
-
// }
|
|
90
|
-
// })
|
|
91
|
-
const container = containerRef.current
|
|
92
|
-
if (container) {
|
|
93
|
-
tryOrHandleError('Updating the provider', () => {
|
|
94
|
-
// Destroy the old widget (if it exists)
|
|
95
|
-
widgetHandlerRef.current?.destroy()
|
|
96
|
-
|
|
97
|
-
// Re-create the widget
|
|
98
|
-
widgetHandlerRef.current = createCowSwapWidget(container, { params, provider: providerRef.current, listeners })
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
// Trigger only on provider changes
|
|
102
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
103
|
-
}, [provider, tryOrHandleError])
|
|
104
|
-
|
|
105
|
-
// Update widget listeners (if they change)
|
|
106
|
-
useEffect(() => {
|
|
107
|
-
if (!widgetHandlerRef.current || listenersRef.current === listeners) return
|
|
108
|
-
|
|
109
|
-
const handler = widgetHandlerRef.current
|
|
110
|
-
tryOrHandleError('Updating the listeners', () => handler.updateListeners(listeners))
|
|
111
|
-
}, [listeners, tryOrHandleError])
|
|
112
|
-
|
|
113
|
-
// Handle errors
|
|
114
|
-
if (error) {
|
|
115
|
-
return (
|
|
116
|
-
<div style={{ color: '#ff3a3a' }}>
|
|
117
|
-
{error.message}
|
|
118
|
-
{error.error.message && <pre style={{ whiteSpace: 'pre-wrap', fontSize: '0.75em' }}>{error.error.message}</pre>}
|
|
119
|
-
</div>
|
|
120
|
-
)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Render widget container
|
|
124
|
-
return <div ref={containerRef} style={{ width: '100%' }}></div>
|
|
125
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"jsx": "react-jsx",
|
|
5
|
-
"allowJs": false,
|
|
6
|
-
"esModuleInterop": false,
|
|
7
|
-
"allowSyntheticDefaultImports": true,
|
|
8
|
-
"strict": true,
|
|
9
|
-
"types": ["vite/client"]
|
|
10
|
-
},
|
|
11
|
-
"files": [],
|
|
12
|
-
"include": [],
|
|
13
|
-
"references": [
|
|
14
|
-
{
|
|
15
|
-
"path": "./tsconfig.lib.json"
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
"path": "./tsconfig.spec.json"
|
|
19
|
-
}
|
|
20
|
-
]
|
|
21
|
-
}
|
package/tsconfig.lib.json
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../dist/out-tsc",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"types": ["node"]
|
|
7
|
-
},
|
|
8
|
-
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
|
9
|
-
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts", "src/**/*.spec.tsx", "src/**/*.test.tsx"]
|
|
10
|
-
}
|
package/tsconfig.spec.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../../dist/out-tsc",
|
|
5
|
-
"module": "commonjs",
|
|
6
|
-
"types": ["jest", "node"]
|
|
7
|
-
},
|
|
8
|
-
"include": [
|
|
9
|
-
"jest.config.ts",
|
|
10
|
-
"src/**/*.test.ts",
|
|
11
|
-
"src/**/*.spec.ts",
|
|
12
|
-
"src/**/*.test.tsx",
|
|
13
|
-
"src/**/*.spec.tsx",
|
|
14
|
-
"src/**/*.test.js",
|
|
15
|
-
"src/**/*.spec.js",
|
|
16
|
-
"src/**/*.test.jsx",
|
|
17
|
-
"src/**/*.spec.jsx",
|
|
18
|
-
"src/**/*.d.ts"
|
|
19
|
-
]
|
|
20
|
-
}
|