Skip to content

Commit 0e10c49

Browse files
authored
Merge pull request #137 from oasisprotocol/ml/on-top-up-destroyed-switch-chain
Add useBlockNavigatingAway
2 parents 3f92e08 + c043b94 commit 0e10c49

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/pages/CreateApp/PaymentStep.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type FC } from 'react'
1+
import { type FC, useEffect } from 'react'
22
import { CreateLayout } from './CreateLayout'
33
import { CreateFormHeader } from './CreateFormHeader'
44
import { CreateFormNavigation } from './CreateFormNavigation'
@@ -13,6 +13,7 @@ import { fromBaseUnits } from '../../utils/number-utils'
1313
import { useNetwork } from '../../hooks/useNetwork.ts'
1414
import { sapphire, sapphireTestnet } from 'viem/chains'
1515
import { useTicker } from '../../hooks/useTicker'
16+
import { useBlockNavigatingAway } from './useBlockNavigatingAway.ts'
1617

1718
type PaymentStepProps = {
1819
handleNext: () => void
@@ -60,6 +61,16 @@ export const PaymentStep: FC<PaymentStepProps> = ({
6061
? null
6162
: BigNumber(amountRequired).minus(sapphireBalance.value)
6263

64+
const { blockNavigatingAway, allowNavigatingAway } = useBlockNavigatingAway()
65+
66+
useEffect(() => {
67+
if (canNavigateAway) {
68+
allowNavigatingAway()
69+
} else {
70+
blockNavigatingAway()
71+
}
72+
}, [allowNavigatingAway, blockNavigatingAway, canNavigateAway])
73+
6374
return (
6475
<CreateLayout
6576
currentStep={4}

src/pages/Dashboard/MachinesDetails/MachineTopUp.tsx

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type FC, useMemo } from 'react'
1+
import { type FC, useEffect, useMemo } from 'react'
22
import { useNetwork } from '../../../hooks/useNetwork'
33
import { useNavigate, useParams } from 'react-router-dom'
44
import { useGetRuntimeRoflmarketProvidersAddressInstancesId } from '../../../nexus/generated/api'
@@ -9,17 +9,20 @@ import { trimLongString } from '../../../utils/trimLongString.ts'
99
import { BuildForm } from '../../../components/BuildForm'
1010
import { CreateFormNavigation } from '../../CreateApp/CreateFormNavigation.tsx'
1111
import { useMachineTopUp } from '../../../backend/api.ts'
12-
import { useAccount, useBalance } from 'wagmi'
12+
import { useAccount, useBalance, useChainId } from 'wagmi'
1313
import { sapphire, sapphireTestnet } from 'viem/chains'
1414
import { NumberUtils } from '../../../utils/number.utils.ts'
1515
import { BigNumber } from 'bignumber.js'
1616
import { TopUp } from '../../../components/top-up/TopUp'
1717
import { Spinner } from '../../../components/Spinner'
1818
import { defaultBuildConfig } from '../../CreateApp/templates.tsx'
19+
import { useBlockNavigatingAway } from '../../CreateApp/useBlockNavigatingAway.ts'
1920

2021
export const MachineTopUp: FC = () => {
2122
const navigate = useNavigate()
2223
const network = useNetwork()
24+
const currentChainId = useChainId()
25+
const canNavigateAway = currentChainId === sapphire.id || currentChainId === sapphireTestnet.id
2326
const { provider, id } = useParams()
2427
// TODO: Refactor, join with MachineDetails/index.tsx state into a provider
2528
const roflMachinesQuery = useGetRuntimeRoflmarketProvidersAddressInstancesId(
@@ -55,8 +58,20 @@ export const MachineTopUp: FC = () => {
5558
[provider],
5659
)
5760

61+
const { blockNavigatingAway, allowNavigatingAway } = useBlockNavigatingAway()
62+
63+
useEffect(() => {
64+
if (canNavigateAway) {
65+
allowNavigatingAway()
66+
} else {
67+
blockNavigatingAway()
68+
}
69+
}, [allowNavigatingAway, blockNavigatingAway, canNavigateAway])
70+
5871
const handleBack = () => {
59-
navigate(`/dashboard/machines/${machine!.provider}/instances/${machine!.id}`)
72+
if (canNavigateAway) {
73+
navigate(`/dashboard/machines/${machine!.provider}/instances/${machine!.id}`)
74+
}
6075
}
6176

6277
const handleTopUpSuccess = () => {
@@ -111,11 +126,19 @@ export const MachineTopUp: FC = () => {
111126
{isSapphireBalanceLoading && <Spinner />}
112127
<TopUp minAmount={minAmount} onTopUpSuccess={handleTopUpSuccess}>
113128
{({ isValid, onSubmit }) => (
114-
<CreateFormNavigation
115-
handleNext={onSubmit}
116-
handleBack={handleBack}
117-
disabled={!isValid}
118-
/>
129+
<>
130+
<CreateFormNavigation
131+
handleNext={onSubmit}
132+
handleBack={handleBack}
133+
disabled={!isValid}
134+
/>
135+
{!canNavigateAway && (
136+
<p className="text-xs text-error py-2">
137+
Before navigation away, manually switch the chain to ${sapphire.name}{' '}
138+
(chainId: ${sapphire.id})
139+
</p>
140+
)}
141+
</>
119142
)}
120143
</TopUp>
121144
</>
@@ -130,6 +153,12 @@ export const MachineTopUp: FC = () => {
130153
disabled={machineTopUp.isPending || noOffersWarning}
131154
isLoading={machineTopUp.isPending}
132155
/>
156+
{!canNavigateAway && (
157+
<p className="text-xs text-error py-2">
158+
Before navigation away, manually switch the chain to ${sapphire.name} (chainId: $
159+
{sapphire.id})
160+
</p>
161+
)}
133162
{machineTopUp.isError && (
134163
<p className="text-xs text-error break-all">{machineTopUp?.error.message}</p>
135164
)}

0 commit comments

Comments
 (0)