Skip to content

Commit

Permalink
chore: enable chains update from config
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Dec 1, 2023
1 parent a42e720 commit 9cc4f33
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
24 changes: 12 additions & 12 deletions packages/widget/src/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ export const AppProvider: React.FC<PropsWithChildren<WidgetConfigProps>> = ({
}) => {
return (
<QueryClientProvider client={queryClient}>
<StoreProvider config={config}>
<WidgetProvider config={config}>
<ThemeProvider>
<I18nProvider>
<WalletProvider>
<FormProvider>
<WidgetProvider config={config}>
<ThemeProvider>
<I18nProvider>
<WalletProvider>
<FormProvider>
<StoreProvider config={config}>
<AppRouter>{children}</AppRouter>
</FormProvider>
</WalletProvider>
</I18nProvider>
</ThemeProvider>
</WidgetProvider>
</StoreProvider>
</StoreProvider>
</FormProvider>
</WalletProvider>
</I18nProvider>
</ThemeProvider>
</WidgetProvider>
</QueryClientProvider>
);
};
Expand Down
49 changes: 10 additions & 39 deletions packages/widget/src/hooks/useChains.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
import { useQuery } from '@tanstack/react-query';
import { useFormContext } from 'react-hook-form';
import { FormKey, isItemAllowed, useWidgetConfig } from '../providers';
import { useChainOrderStore } from '../stores';
import { useMemo } from 'react';
import { isItemAllowed, useWidgetConfig } from '../providers';
import { useAvailableChains } from './useAvailableChains';

export const useChains = () => {
const { chains, keyPrefix } = useWidgetConfig();
const { getValues, setValue } = useFormContext();
const initializeChains = useChainOrderStore(
(state) => state.initializeChains,
);
const { chains } = useWidgetConfig();
const {
chains: availableChains,
isLoading: isLoadingAvailableChains,
getChainById,
} = useAvailableChains();

const { data: filteredChains, isLoading: isLoadingFilteredChains } = useQuery(
{
queryKey: ['filtered-chains', availableChains?.length, keyPrefix],
queryFn: () => {
if (!availableChains) {
return;
}
const filteredChains = availableChains.filter((chain) =>
isItemAllowed(chain.id, chains),
);
const chainOrder = initializeChains(
filteredChains.map((chain) => chain.id),
);
const [fromChainValue, toChainValue] = getValues([
FormKey.FromChain,
FormKey.ToChain,
]);
if (!fromChainValue) {
setValue(FormKey.FromChain, chainOrder[0]);
}
if (!toChainValue) {
setValue(FormKey.ToChain, chainOrder[0]);
}

return filteredChains;
},
enabled: Boolean(availableChains),
},
);
const filteredChains = useMemo(() => {
const filteredChains = availableChains?.filter((chain) =>
isItemAllowed(chain.id, chains),
);
return filteredChains;
}, [availableChains, chains]);

return {
chains: filteredChains,
getChainById,
isLoading: isLoadingAvailableChains || isLoadingFilteredChains,
isLoading: isLoadingAvailableChains,
};
};
29 changes: 28 additions & 1 deletion packages/widget/src/stores/chains/ChainOrderStore.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createContext, useContext, useRef } from 'react';
import { createContext, useContext, useEffect, useRef } from 'react';
import { useFormContext } from 'react-hook-form';
import type { StoreApi } from 'zustand';
import type { UseBoundStoreWithEqualityFn } from 'zustand/traditional';
import { useChains } from '../../hooks';
import { FormKey } from '../../providers';
import type { PersistStoreProviderProps } from '../types';
import { createChainOrderStore } from './createChainOrderStore';
import type { ChainOrderState } from './types';
Expand All @@ -18,9 +21,33 @@ export function ChainOrderStoreProvider({
...props
}: PersistStoreProviderProps) {
const storeRef = useRef<ChainOrderStore>();
const { chains: filteredChains } = useChains();
const { setValue, getValues } = useFormContext();

if (!storeRef.current) {
storeRef.current = createChainOrderStore(props);
}

useEffect(() => {
if (filteredChains) {
const chainOrder = storeRef.current
?.getState()
.initializeChains(filteredChains.map((chain) => chain.id));
if (chainOrder) {
const [fromChainValue, toChainValue] = getValues([
FormKey.FromChain,
FormKey.ToChain,
]);
if (!fromChainValue) {
setValue(FormKey.FromChain, chainOrder[0]);
}
if (!toChainValue) {
setValue(FormKey.ToChain, chainOrder[0]);
}
}
}
}, [filteredChains, getValues, setValue]);

return (
<ChainOrderStoreContext.Provider value={storeRef.current}>
{children}
Expand Down

0 comments on commit 9cc4f33

Please sign in to comment.