-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: enable chains update from config
- Loading branch information
Showing
3 changed files
with
50 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters