From bbfecdf202ca922ca81e20ef7606872bc85184ae Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Thu, 16 Jan 2025 16:30:29 -0800 Subject: [PATCH] Fix back navigation for Mt Pelerin quotes --- .../gui/providers/mtpelerinProvider.ts | 24 ++++++++++++++++++- src/plugins/gui/scenes/FiatPluginWebView.tsx | 17 +++++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/plugins/gui/providers/mtpelerinProvider.ts b/src/plugins/gui/providers/mtpelerinProvider.ts index 7b1eb8f7d44..650942454bf 100644 --- a/src/plugins/gui/providers/mtpelerinProvider.ts +++ b/src/plugins/gui/providers/mtpelerinProvider.ts @@ -507,6 +507,9 @@ export const mtpelerinProvider: FiatProviderFactory = { injecteJs(run) } + // This is the state of the user-journey through the quoting process + let userFlowStatus: 'preparing-quote' | 'submitted-quote' | 'navigating-back' = 'preparing-quote' + const onMessage: FiatPluginOpenWebViewParams['onMessage'] = (eventMessage: string, injectJs) => { const message = asMessage(JSON.parse(eventMessage)) try { @@ -655,6 +658,9 @@ export const mtpelerinProvider: FiatProviderFactory = { } sendResponse('onsenttransaction', tx.signedTx, injectJs) + + // The user has signed and sent the transaction + userFlowStatus = 'submitted-quote' } send().catch((e: unknown) => { if (!(e instanceof Error && e.message.includes(SendErrorBackPressed))) { @@ -739,7 +745,23 @@ export const mtpelerinProvider: FiatProviderFactory = { url, injectedJs, onMessage, - onClose: () => {} + onClose: () => { + // Hi-jack the back navigation + switch (userFlowStatus) { + case 'preparing-quote': + case 'navigating-back': + return true + case 'submitted-quote': { + // If the user has submitted a quote, then navigating back + // will skip all the way back to the plugin list scene. + showUi.exitScene() + showUi.exitScene() + userFlowStatus = 'navigating-back' + // Prevent default navigation + return false + } + } + } }) }, closeQuote: async (): Promise => {} diff --git a/src/plugins/gui/scenes/FiatPluginWebView.tsx b/src/plugins/gui/scenes/FiatPluginWebView.tsx index 441d97fe265..44b1b9d720a 100644 --- a/src/plugins/gui/scenes/FiatPluginWebView.tsx +++ b/src/plugins/gui/scenes/FiatPluginWebView.tsx @@ -1,3 +1,4 @@ +import { useNavigation } from '@react-navigation/native' import * as React from 'react' import { WebView, WebViewNavigation } from 'react-native-webview' @@ -8,7 +9,7 @@ import { BuyTabSceneProps } from '../../../types/routerTypes' export interface FiatPluginOpenWebViewParams { url: string injectedJs?: string - onClose?: () => void + onClose?: (() => boolean) | (() => void) onMessage?: (message: string, injectJs: (js: string) => void) => void onUrlChange?: (url: string) => void } @@ -18,6 +19,7 @@ interface Props extends BuyTabSceneProps<'guiPluginWebView'> {} export function FiatPluginWebViewComponent(props: Props): JSX.Element { const { route } = props const { injectedJs, onClose, onMessage, onUrlChange, url } = route.params + const navigation = useNavigation() const webViewRef = React.useRef(null) @@ -35,10 +37,15 @@ export function FiatPluginWebViewComponent(props: Props): JSX.Element { if (onMessage != null) onMessage(data, injectJs) }) - React.useEffect(() => () => { - // Cleanup code when scene unmounts - if (onClose != null) onClose() - }) + React.useEffect( + () => + navigation.addListener('beforeRemove', event => { + if (onClose != null) { + if (onClose() === false) event.preventDefault() + } + }), + [navigation, onClose] + ) return (