Skip to content
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

fix: loading state on Send page #300

Merged
3 commits merged into from
Jun 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions src/components/Send.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,8 @@ export default function Send() {

const isCoinjoinInProgress = useMemo(() => serviceInfo && serviceInfo.coinjoinInProgress, [serviceInfo])
const isMakerRunning = useMemo(() => serviceInfo && serviceInfo.makerRunning, [serviceInfo])
const waitForTakerToFinish = useMemo(() => !!isCoinjoinInProgress, [isCoinjoinInProgress])
const isOperationDisabled = useMemo(
() => isCoinjoinInProgress || isMakerRunning,
[isCoinjoinInProgress, isMakerRunning]
)

const [alert, setAlert] = useState(null)
const [isLoading, setIsLoading] = useState(true)
const [isSending, setIsSending] = useState(false)
const [isCoinjoin, setIsCoinjoin] = useState(IS_COINJOIN_DEFAULT_VAL)
const [minNumCollaborators, setMinNumCollaborators] = useState(MINIMUM_MAKERS_DEFAULT_VAL)
Expand All @@ -208,6 +202,16 @@ export default function Send() {
const [paymentSuccessfulInfoAlert, setPaymentSuccessfulInfoAlert] = useState(null)
const [takerStartedInfoAlert, setTakerStartedInfoAlert] = useState(null)

const isOperationDisabled = useMemo(
() => isCoinjoinInProgress || isMakerRunning || waitForUtxosToBeSpent.length > 0,
[isCoinjoinInProgress, isMakerRunning, waitForUtxosToBeSpent]
)
const [isInitializing, setIsInitializing] = useState(!isOperationDisabled)
const isLoading = useMemo(
() => isInitializing || waitForUtxosToBeSpent.length > 0,
[isInitializing, waitForUtxosToBeSpent]
)

useEffect(() => {
setTakerStartedInfoAlert((current) => (isCoinjoinInProgress ? current : null))
}, [isCoinjoinInProgress])
Expand Down Expand Up @@ -273,7 +277,6 @@ export default function Send() {
useEffect(() => {
if (waitForUtxosToBeSpent.length === 0) return

setIsLoading(true)
const abortCtrl = new AbortController()

// Delaying the poll requests gives the wallet some time to synchronize
Expand All @@ -289,15 +292,13 @@ export default function Send() {
const outputs = data.data.utxos.utxos.map((it) => it.utxo)
const utxosStillPresent = waitForUtxosToBeSpent.filter((it) => outputs.includes(it))
setWaitForUtxosToBeSpent([...utxosStillPresent])
setIsLoading(utxosStillPresent.length > 0)
})
.catch((err) => {
if (abortCtrl.signal.aborted) return

// Stop waiting for wallet synchronization on errors, but inform
// the user that loading the wallet info failed
setWaitForUtxosToBeSpent([])
setIsLoading(false)

const message = err.message || t('send.error_loading_wallet_failed')
setAlert({ variant: 'danger', message })
Expand All @@ -311,13 +312,15 @@ export default function Send() {
}, [waitForUtxosToBeSpent, reloadCurrentWalletInfo, t])

useEffect(() => {
if (waitForTakerToFinish) return
if (waitForUtxosToBeSpent.length > 0) return
if (isOperationDisabled) {
setIsInitializing(false)
return
}

const abortCtrl = new AbortController()

setAlert(null)
setIsLoading(true)
setIsInitializing(true)

// reloading service info is important, is it must be known as soon as possible
// if the operation is even allowed, i.e. if no other service is running
Expand Down Expand Up @@ -346,19 +349,11 @@ export default function Send() {
})

Promise.all([loadingServiceInfo, loadingWalletInfoAndUtxos, loadingMinimumMakerConfig]).finally(
() => !abortCtrl.signal.aborted && setIsLoading(false)
() => !abortCtrl.signal.aborted && setIsInitializing(false)
)

return () => abortCtrl.abort()
}, [
waitForTakerToFinish,
waitForUtxosToBeSpent,
wallet,
reloadCurrentWalletInfo,
reloadServiceInfo,
loadConfigValue,
t,
])
}, [isOperationDisabled, wallet, reloadCurrentWalletInfo, reloadServiceInfo, loadConfigValue, t])

const sendPayment = async (account, destination, amount_sats) => {
setAlert(null)
Expand Down