Skip to content

Commit cc29bd6

Browse files
committed
Refactor initial values for forms with new field.populate callback
1 parent 73c612a commit cc29bd6

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

wallets/client/components/form/hooks.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { isTemplate, isWallet, protocolClientSchema, protocolFields, protocolFor
22
import { createContext, useContext, useEffect, useMemo, useCallback, useState } from 'react'
33
import { useWalletProtocolUpsert } from '@/wallets/client/hooks'
44
import { MultiStepForm, useFormState, useStep } from '@/components/multi-step-form'
5-
import { parseNwcUrl } from '@/wallets/lib/validate'
65

76
export const Step = {
87
SEND: 'send',
@@ -83,9 +82,9 @@ function useProtocolFormState (protocol) {
8382
}
8483

8584
export function useProtocolForm (protocol) {
85+
const [globalFormState] = useFormState()
8686
const [formState, setFormState] = useProtocolFormState(protocol)
8787
const [complementaryFormState] = useProtocolFormState({ name: protocol.name, send: !protocol.send })
88-
const [nwcSendFormState] = useProtocolFormState({ name: 'NWC', send: true })
8988
const wallet = useWallet()
9089
const lud16Domain = walletLud16Domain(wallet.name)
9190
const fields = protocolFields(protocol)
@@ -98,16 +97,13 @@ export function useProtocolForm (protocol) {
9897
value = complementaryFormState?.config?.[field.name]
9998
}
10099

101-
if (protocol.name === 'LN_ADDR' && field.name === 'address' && lud16Domain) {
102-
// automatically set lightning addresses from NWC urls if lud16 parameter is present
103-
if (nwcSendFormState?.config?.url) {
104-
const { lud16 } = parseNwcUrl(nwcSendFormState.config.url)
105-
if (lud16?.split('@')[1] === lud16Domain) value = lud16
106-
}
100+
if (!value && field.populate && globalFormState) {
101+
value = field.populate(wallet, globalFormState)
102+
}
103+
104+
if (lud16Domain && value) {
107105
// remove domain part since we will append it automatically on submit if lud16Domain is set
108-
if (lud16Domain && value) {
109-
value = value.split('@')[0]
110-
}
106+
value = value.split('@')[0]
111107
}
112108

113109
return {

wallets/client/components/form/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function WalletProtocolFormField ({ type, ...props }) {
182182
const [protocol] = useProtocol()
183183
const formik = useFormikContext()
184184

185-
function transform ({ validate, encrypt, editable, help, share, ...props }) {
185+
function transform ({ validate, encrypt, editable, help, share, populate, ...props }) {
186186
const [upperHint, bottomHint] = Array.isArray(props.hint) ? props.hint : [null, props.hint]
187187

188188
const parseHelpText = text => Array.isArray(text) ? text.join('\n\n') : text

wallets/lib/protocols/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ import clinkSuite from './clink'
3636
* @property {string} [hint] - hint text shown below field
3737
* @property {boolean} [share] - whether field can be used to prepopulate field of complementary send/receive protocol
3838
* @property {boolean} [editable] - whether the field is editable after it was saved
39+
* @property {ProtocolFieldPopulate} [populate] - function to populate the field using values from other protocol forms
40+
*/
41+
42+
/**
43+
* @callback ProtocolFieldPopulate
44+
* @param {Object} wallet - the wallet we are configuring
45+
* @param {Object} formState - the current form state across all protocols
46+
* @returns {string|null} - the value to populate the field with, or null if no value is available
3947
*/
4048

4149
/** @type {Protocol[]} */

wallets/lib/protocols/lnAddr.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { externalLightningAddressValidator } from '@/wallets/lib/validate'
1+
import { externalLightningAddressValidator, parseNwcUrl } from '@/wallets/lib/validate'
2+
import { protocolFormId, walletLud16Domain } from '../util'
23

34
// Lightning Address (LUD-16)
45
// https://github.com/lnurl/luds/blob/luds/16.md
@@ -13,8 +14,29 @@ export default {
1314
label: 'address',
1415
type: 'text',
1516
required: true,
16-
validate: externalLightningAddressValidator
17+
validate: externalLightningAddressValidator,
18+
populate: addressPopulate
1719
}
1820
],
1921
relationName: 'walletRecvLightningAddress'
2022
}
23+
24+
function addressPopulate (wallet, formState) {
25+
const nwcFormId = protocolFormId({ name: 'NWC', send: true })
26+
const nwcFormState = formState[nwcFormId]
27+
if (!nwcFormState?.config?.url) {
28+
return null
29+
}
30+
31+
const { lud16: nwcLud16 } = parseNwcUrl(nwcFormState.config.url)
32+
if (!nwcLud16) {
33+
return null
34+
}
35+
36+
const nwcLud16Domain = nwcLud16.split('@')[1]
37+
if (nwcLud16Domain !== walletLud16Domain(wallet.name)) {
38+
return null
39+
}
40+
41+
return nwcLud16
42+
}

0 commit comments

Comments
 (0)