Skip to content

Commit

Permalink
fix: validate address from URL params and bookmark it
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Jan 28, 2025
1 parent 9b23304 commit 53b3294
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/widget/src/stores/form/FormUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ export const FormUpdater: React.FC<{
if (reactiveFormValues.toAddress) {
setSendToWallet(true)
}

setSelectedBookmark(toAddress)
if (toAddress) {
setSelectedBookmark(toAddress)
}

setUserAndDefaultValues(
accountForChainId(reactiveFormValues, account.chainId)
Expand Down
42 changes: 38 additions & 4 deletions packages/widget/src/stores/form/URLSearchParamsBuilder.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import { useAddressValidation } from '../../hooks/useAddressValidation.js'
import { useSendToWalletActions } from '../../stores/settings/useSendToWalletStore.js'
import { formatInputAmount } from '../../utils/format.js'
import { useBookmarkActions } from '../bookmarks/useBookmarkActions.js'
import type { DefaultValues, FormFieldNames } from '../form/types.js'
import { useFieldActions } from '../form/useFieldActions.js'
import { useFieldValues } from '../form/useFieldValues.js'
Expand Down Expand Up @@ -49,6 +51,8 @@ export const URLSearchParamsBuilder = () => {
const touchedFields = useTouchedFields()
const values = useFieldValues(...formValueKeys)
const { setSendToWallet } = useSendToWalletActions()
const { setSelectedBookmark, addRecentWallet } = useBookmarkActions()
const { validateAddress } = useAddressValidation()

// Using these methods as trying to use the touchedFields and values above
// often has a lag that can effect the widgets initialisation sequence
Expand All @@ -57,15 +61,45 @@ export const URLSearchParamsBuilder = () => {
useFieldActions()

useEffect(() => {
// get the initial values from the querysting
// get the initial values from the querystring
const formValues = getDefaultValuesFromQueryString()

if (formValues.toAddress) {
setSendToWallet(true)
/**
* When URL builder is enabled and user opens a page with toAddress parameter,
* validate the address and set it up as a bookmark. This allows direct linking
* to the widget with a pre-filled destination address that will be treated the
* same way as a manually entered and validated address.
*/
const initializeFromAddress = async () => {
if (formValues.toAddress) {
try {
const validationResult = await validateAddress({
value: formValues.toAddress,
})
if (validationResult.isValid) {
const bookmark = {
address: validationResult.address,
chainType: validationResult.chainType,
}
setSelectedBookmark(bookmark)
addRecentWallet(bookmark)
setSendToWallet(true)
}
} catch (_) {
// Address validation failed
}
}
}

initializeFromAddress()
setUserAndDefaultValues(formValues)
}, [setUserAndDefaultValues, setSendToWallet])
}, [
setUserAndDefaultValues,
setSendToWallet,
validateAddress,
setSelectedBookmark,
addRecentWallet,
])

// biome-ignore lint/correctness/useExhaustiveDependencies:
useEffect(() => {
Expand Down

0 comments on commit 53b3294

Please sign in to comment.