From a6665bca6a4aa3ef4c4770f517882a8e02f2dd1f Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 16 Oct 2024 09:22:43 +0200 Subject: [PATCH] fix priority sorting for send wallets,caching and sorting --- wallets/index.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/wallets/index.js b/wallets/index.js index 9c084e45..dfde7a00 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -196,7 +196,7 @@ function useConfig (walletDef) { const serverConfig = await client.query({ query: WALLET_BY_TYPE, variables: { type: walletDef.walletType }, - fetchPolicy: 'no-cache' + fetchPolicy: 'network-only' }) if (serverConfig?.data?.walletByType) { @@ -284,7 +284,7 @@ function useConfig (walletDef) { // check if it misses send or receive configs const isReadyToSend = canSend && isConfigured({ fields: walletDef.fields, config: newConfig, clientOnly: true }) const isReadyToReceive = canReceive && isConfigured({ fields: walletDef.fields, config: newConfig, serverOnly: true }) - const { autoWithdrawThreshold, autoWithdrawMaxFeePercent, priority, enabled } = newServerConfig + const { autoWithdrawThreshold, autoWithdrawMaxFeePercent, priority, enabled } = newConfig // console.log('New client config', newClientConfig) // console.log('New server config', newServerConfig) @@ -453,22 +453,30 @@ export function useWallets () { export function WalletProvider ({ children }) { const { me } = useMe() - const wallets = walletDefs.map(def => useWalletInner(def.name)).filter(w => w) - const migrationRan = useRef(false) const migratableKeys = !migrationRan.current && !SSR ? Object.keys(window.localStorage).filter(k => k.startsWith('wallet:')) : undefined - const { data: bestSendWalletListData } = useQuery(BEST_SEND_WALLETS, { - pollInterval: POLL_INTERVAL, - nextFetchPolicy: 'network-only', - fetchPolicy: 'network-only' - }) + const walletList = walletDefs.map(def => useWalletInner(def.name)).filter(w => w) + const { data: bestSendWalletList } = useQuery(BEST_SEND_WALLETS, SSR + ? {} + : { + pollInterval: POLL_INTERVAL, + nextFetchPolicy: 'cache-and-network' + }) - const [bestSendWalletList, setBestSendWalletList] = useState(bestSendWalletListData?.wallets ?? []) + const processSendWallets = (bestWalletData) => { + const clientSideSorting = false // sorting is now done on the server + let wallets = (bestWalletData?.wallets ?? []).filter(w => w.canSend) + if (clientSideSorting) wallets = wallets.sort(walletPrioritySort) + return wallets + } + + const wallets = walletList.sort(walletPrioritySort) + const [bestSendWallets, innerSetBestSendWallets] = useState(() => processSendWallets(bestSendWalletList)) useEffect(() => { - setBestSendWalletList(bestSendWalletListData?.wallets) - }, [bestSendWalletListData]) + innerSetBestSendWallets(processSendWallets(bestSendWalletList)) + }, [bestSendWalletList]) // migration useEffect(() => { @@ -499,7 +507,7 @@ export function WalletProvider ({ children }) { }, []) return ( - + {children} )