* Use SSR for wallet forms * Fix back/forward navigation with useData hook * Fix protocol fallback not working with shallow routing * Fix wallet refetch * Replace useEffect for default selection with smart link * Remove unused useWalletQuery * Move server2client wallet transform into single function * Add comment about graphql-tag fragment warning * Check if wallet not found * Handle wallet is sometimes null on back or forward navigation
62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
import { useMe } from '@/components/me'
|
|
import { useWallets } from '@/wallets/client/context'
|
|
import protocols from '@/wallets/client/protocols'
|
|
import { isWallet } from '@/wallets/lib/util'
|
|
import { useMemo } from 'react'
|
|
|
|
export function useSendProtocols () {
|
|
const wallets = useWallets()
|
|
return useMemo(
|
|
() => wallets
|
|
.filter(w => w.send)
|
|
.reduce((acc, wallet) => {
|
|
return [
|
|
...acc,
|
|
...wallet.protocols
|
|
.filter(p => p.send && p.enabled)
|
|
.map(walletProtocol => {
|
|
const { sendPayment } = protocols.find(p => p.name === walletProtocol.name)
|
|
return {
|
|
...walletProtocol,
|
|
sendPayment
|
|
}
|
|
})
|
|
]
|
|
}, [])
|
|
, [wallets])
|
|
}
|
|
|
|
export function useHasSendWallet () {
|
|
const protocols = useSendProtocols()
|
|
return useMemo(() => protocols.length > 0, [protocols])
|
|
}
|
|
|
|
export function useWalletSupport (wallet) {
|
|
const template = isWallet(wallet) ? wallet.template : wallet
|
|
return useMemo(() => ({ receive: template.receive === WalletStatus.OK, send: template.send === WalletStatus.OK }), [template])
|
|
}
|
|
|
|
export const WalletStatus = {
|
|
OK: 'OK',
|
|
ERROR: 'ERROR',
|
|
WARNING: 'WARNING',
|
|
DISABLED: 'DISABLED'
|
|
}
|
|
|
|
export function useWalletStatus (wallet) {
|
|
if (!isWallet(wallet)) return WalletStatus.DISABLED
|
|
|
|
return useMemo(() => ({ send: wallet.send, receive: wallet.receive }), [wallet])
|
|
}
|
|
|
|
export function useWalletsUpdatedAt () {
|
|
const { me } = useMe()
|
|
return me?.privates?.walletsUpdatedAt
|
|
}
|
|
|
|
export function useProtocolTemplates (wallet) {
|
|
return useMemo(() => {
|
|
return isWallet(wallet) ? wallet.template.protocols : wallet.protocols
|
|
}, [wallet])
|
|
}
|