@turnkey/react-wallet-kit
Version:
The easiest and most powerful way to integrate Turnkey's Embedded Wallets into your React applications.
94 lines (91 loc) • 4.91 kB
JavaScript
'use client';
import { toConsumableArray as _toConsumableArray, slicedToArray as _slicedToArray, asyncToGenerator as _asyncToGenerator, regenerator as _regenerator, objectSpread2 as _objectSpread2 } from '../_virtual/_rollupPluginBabelHelpers.mjs';
import { jsx } from 'react/jsx-runtime';
import { useState, useCallback, useEffect, createContext, useContext } from 'react';
import { buildWalletConnectAppEntries } from '@turnkey/core';
import { useTurnkey } from './client/Hook.mjs';
var initialState = {
walletConnectApps: [],
isLoadingApps: false
};
var WalletConnectContext = /*#__PURE__*/createContext(null);
/**
* @internal
* WalletConnectProvider provides context for WalletConnect wallet apps.
*
* Automatically fetches wallet apps on mount using the projectId from config.
* This is an internal provider and not exported
*/
function WalletConnectProvider(_ref) {
var _config$walletConfig, _config$walletConfig$, _config$walletConfig$2, _config$walletConfig2, _config$walletConfig3, _config$walletConfig4, _config$walletConfig$3, _config$walletConfig5, _config$walletConfig6, _config$walletConfig7;
var children = _ref.children;
var _useTurnkey = useTurnkey(),
config = _useTurnkey.config;
var projectId = config === null || config === void 0 ? void 0 : (_config$walletConfig = config.walletConfig) === null || _config$walletConfig === void 0 ? void 0 : (_config$walletConfig$ = _config$walletConfig.walletConnect) === null || _config$walletConfig$ === void 0 ? void 0 : _config$walletConfig$.projectId;
// WalletConnect will only work if the app supports ALL of the configured namespaces, so
// we build a list of these namespaces so that `buildWalletConnectAppEntries` can filter
// and only returns apps that support all of them
var namespaces = [].concat(_toConsumableArray((_config$walletConfig$2 = config === null || config === void 0 ? void 0 : (_config$walletConfig2 = config.walletConfig) === null || _config$walletConfig2 === void 0 ? void 0 : (_config$walletConfig3 = _config$walletConfig2.chains) === null || _config$walletConfig3 === void 0 ? void 0 : (_config$walletConfig4 = _config$walletConfig3.ethereum) === null || _config$walletConfig4 === void 0 ? void 0 : _config$walletConfig4.walletConnectNamespaces) !== null && _config$walletConfig$2 !== void 0 ? _config$walletConfig$2 : []), _toConsumableArray((_config$walletConfig$3 = config === null || config === void 0 ? void 0 : (_config$walletConfig5 = config.walletConfig) === null || _config$walletConfig5 === void 0 ? void 0 : (_config$walletConfig6 = _config$walletConfig5.chains) === null || _config$walletConfig6 === void 0 ? void 0 : (_config$walletConfig7 = _config$walletConfig6.solana) === null || _config$walletConfig7 === void 0 ? void 0 : _config$walletConfig7.walletConnectNamespaces) !== null && _config$walletConfig$3 !== void 0 ? _config$walletConfig$3 : []));
var _useState = useState(initialState),
_useState2 = _slicedToArray(_useState, 2),
state = _useState2[0],
setState = _useState2[1];
var fetchApps = useCallback(/*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(pid, ns) {
var entries;
return _regenerator().w(function (_context) {
while (1) switch (_context.p = _context.n) {
case 0:
setState(function (s) {
return _objectSpread2(_objectSpread2({}, s), {}, {
isLoadingApps: true
});
});
_context.p = 1;
_context.n = 2;
return buildWalletConnectAppEntries(pid, ns);
case 2:
entries = _context.v;
setState({
walletConnectApps: entries,
isLoadingApps: false
});
_context.n = 4;
break;
case 3:
_context.p = 3;
_context.v;
setState(function (s) {
return _objectSpread2(_objectSpread2({}, s), {}, {
isLoadingApps: false
});
});
case 4:
return _context.a(2);
}
}, _callee, null, [[1, 3]]);
}));
return function (_x, _x2) {
return _ref2.apply(this, arguments);
};
}(), []);
// we auto-fetch wallet apps on mount if projectId and namespaces are available
useEffect(function () {
if (projectId && namespaces.length > 0) {
fetchApps(projectId, namespaces);
}
}, [projectId, namespaces.join(","), fetchApps]);
return jsx(WalletConnectContext.Provider, {
value: state,
children: children
});
}
function useWalletConnect() {
var ctx = useContext(WalletConnectContext);
if (!ctx) {
throw new Error("useWalletConnect must be used within WalletConnectProvider.");
}
return ctx;
}
export { WalletConnectProvider, useWalletConnect };
//# sourceMappingURL=WalletConnectProvider.mjs.map