)}
>
)
}
function WalletSendRecvSelector () {
const path = useWalletPathname()
const selected = useSendRecvParam()
// TODO(wallet-v2): if you click a nav link again, it will update the URL
// but not run the effect again to select the first protocol by default
return (
)
}
function WalletProtocolSelector () {
const walletPath = useWalletPathname()
const sendRecvParam = useSendRecvParam()
const path = `${walletPath}/${sendRecvParam}`
const protocols = useWalletProtocols()
const selected = useWalletProtocolParam()
const router = useRouter()
useEffect(() => {
if (!selected && protocols.length > 0) {
router.replace(`/${path}/${urlify(protocols[0].name)}`, null, { shallow: true })
}
}, [path])
if (protocols.length === 0) {
// TODO(wallet-v2): let user know how to request support if the wallet actually does support sending
return (
{sendRecvParam === 'send' ? 'sending' : 'receiving'} not supported
)
}
return (
)
}
function WalletProtocolForm () {
const sendRecvParam = useSendRecvParam()
const router = useRouter()
const protocol = useSelectedProtocol()
if (!protocol) return null
// I think it is okay to skip this hook if the protocol is not found
// because we will need to change the URL to get a different protocol
// so the amount of rendered hooks should stay the same during the lifecycle of this component
const wallet = useWallet()
const upsertWalletProtocol = useWalletProtocolUpsert(wallet, protocol)
const toaster = useToast()
const refetch = useWalletRefetch()
const { fields, initial, schema } = useProtocolForm(protocol)
// create a copy of values to avoid mutating the original
const onSubmit = useCallback(async ({ ...values }) => {
const lud16Domain = walletLud16Domain(wallet.name)
if (values.address && lud16Domain) {
values.address = `${values.address}@${lud16Domain}`
}
const upsert = await upsertWalletProtocol(values)
if (isWallet(wallet)) {
toaster.success('wallet saved')
refetch()
return
}
// we just created a new user wallet from a template
router.replace(`/wallets/${upsert.id}/${sendRecvParam}`, null, { shallow: true })
toaster.success('wallet attached', { persistOnNavigate: true })
}, [upsertWalletProtocol, toaster, wallet, router])
return (
<>
>
)
}
function WalletProtocolFormButtons () {
const protocol = useSelectedProtocol()
const removeWalletProtocol = useWalletProtocolRemove(protocol)
const refetch = useWalletRefetch()
const router = useRouter()
const wallet = useWallet()
const isLastProtocol = wallet.protocols.length === 1
const onDetach = useCallback(async () => {
await removeWalletProtocol()
if (isLastProtocol) {
router.replace('/wallets', null, { shallow: true })
return
}
refetch()
}, [removeWalletProtocol, refetch, isLastProtocol, router])
return (