-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(ConnectWalletForm): preserve key in errors #641
base: main
Are you sure you want to change the base?
Conversation
Extension builds preview
|
const [autoKeyShareFailed, setAutoKeyShareFailed] = React.useState( | ||
isAutoKeyAddFailed(state), | ||
); | ||
|
||
const resetState = React.useCallback(async () => { | ||
await clearConnectState(); | ||
setErrors((_) => ({ ..._, keyPair: '', connect: '' })); | ||
setErrors((_) => ({ ..._, keyPair: null, connect: null })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest renaming the parameter to prev
or something else that suggests that this is the previous state. Clearer than _
.
setErrors((_) => ({ ..._, keyPair: null, connect: null })); | |
setErrors((prev) => ({ ...prev, keyPair: null, connect: null })); |
setAutoKeyShareFailed(false); | ||
}, [clearConnectState]); | ||
|
||
const toErrorInfo = React.useCallback( | ||
(err?: string | ErrorWithKeyLike | null): ErrorInfo | null => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have the error as string as well? Can't we pass the whole error directly? For example here:
web-monetization-extension/src/popup/components/ConnectWalletForm.tsx
Lines 114 to 124 in a012ce4
try { | |
setIsValidating((_) => ({ ..._, walletAddressUrl: true })); | |
const url = new URL(toWalletAddressUrl(walletAddressUrl)); | |
const walletAddress = await getWalletInfo(url.toString()); | |
setWalletAddressInfo(walletAddress); | |
} catch (error) { | |
setErrors((_) => ({ | |
..._, | |
walletAddressUrl: toErrorInfo(error.message), | |
})); | |
return false; |
keyPair: state?.status === 'error:key' ? toErrorInfo(state.error) : null, | ||
connect: state?.status === 'error' ? toErrorInfo(state.error) : null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my information, state
represents the transient state?
const handleSubmit = async (ev?: React.FormEvent<HTMLFormElement>) => { | ||
ev?.preventDefault(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the event optional now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because now we manually trigger the submit (without the form also), like on "Accept consent" or "retry".
walletAddressUrl: toErrorInfo(errWalletAddressUrl), | ||
amount: toErrorInfo(errAmount), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are mixing errors as exceptions and errors as value with the validateWalletAddressUrl
and validateAmount
. Not a big fan, but I understand why this approach.
It might be worth looking into neverthrow later on.
Earlier, we stored only localized message in
errors[foo]
. Now we preserve thekey
and original error under{info}
; and add the localized message under{message}
.This will help with error handling based on key, and paves way to support retry add-key on retry-able errors (such as timed out waiting for login, wrong account was logged in, key-add consent accidentally declined etc.)
Part of #613