@lifi/widget
Version:
LI.FI Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.
56 lines (55 loc) • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useChainOrderStoreContext = exports.useChainOrderStore = exports.ChainOrderStoreProvider = exports.ChainOrderStoreContext = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_hook_form_1 = require("react-hook-form");
const hooks_1 = require("../../hooks");
const providers_1 = require("../../providers");
const createChainOrderStore_1 = require("./createChainOrderStore");
exports.ChainOrderStoreContext = (0, react_1.createContext)(null);
function ChainOrderStoreProvider({ children, ...props }) {
const storeRef = (0, react_1.useRef)();
const { chains: filteredChains } = (0, hooks_1.useChains)();
const { setValue, getValues } = (0, react_hook_form_1.useFormContext)();
if (!storeRef.current) {
storeRef.current = (0, createChainOrderStore_1.createChainOrderStore)(props);
}
(0, react_1.useEffect)(() => {
if (filteredChains) {
const chainOrder = storeRef.current
?.getState()
.initializeChains(filteredChains.map((chain) => chain.id));
if (chainOrder) {
const [fromChainValue, toChainValue] = getValues([
providers_1.FormKey.FromChain,
providers_1.FormKey.ToChain,
]);
if (!fromChainValue) {
setValue(providers_1.FormKey.FromChain, chainOrder[0]);
}
if (!toChainValue) {
setValue(providers_1.FormKey.ToChain, chainOrder[0]);
}
}
}
}, [filteredChains, getValues, setValue]);
return ((0, jsx_runtime_1.jsx)(exports.ChainOrderStoreContext.Provider, { value: storeRef.current, children: children }));
}
exports.ChainOrderStoreProvider = ChainOrderStoreProvider;
function useChainOrderStore(selector, equalityFn) {
const useStore = (0, react_1.useContext)(exports.ChainOrderStoreContext);
if (!useStore) {
throw new Error(`You forgot to wrap your component in <${ChainOrderStoreProvider.name}>.`);
}
return useStore(selector, equalityFn);
}
exports.useChainOrderStore = useChainOrderStore;
function useChainOrderStoreContext() {
const useStore = (0, react_1.useContext)(exports.ChainOrderStoreContext);
if (!useStore) {
throw new Error(`You forgot to wrap your component in <${ChainOrderStoreProvider.name}>.`);
}
return useStore;
}
exports.useChainOrderStoreContext = useChainOrderStoreContext;