From cce5d5c2ec7a55ca2e4f3f511ec0c5c2791bc03c Mon Sep 17 00:00:00 2001 From: tommasini <46944231+tommasini@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:16:20 +0100 Subject: [PATCH] chore: revert webview focused work (#11976) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** Fix disconnect in, in-app browser ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- app/components/Views/BrowserTab/index.js | 14 ----- app/util/browser/webViewFocus.test.ts | 74 ------------------------ app/util/browser/webViewFocus.ts | 32 ---------- 3 files changed, 120 deletions(-) delete mode 100644 app/util/browser/webViewFocus.test.ts delete mode 100644 app/util/browser/webViewFocus.ts diff --git a/app/components/Views/BrowserTab/index.js b/app/components/Views/BrowserTab/index.js index 501058acb1d..b602a19466c 100644 --- a/app/components/Views/BrowserTab/index.js +++ b/app/components/Views/BrowserTab/index.js @@ -108,8 +108,6 @@ import { useMetrics } from '../../../components/hooks/useMetrics'; import { trackDappViewedEvent } from '../../../util/metrics'; import trackErrorAsAnalytics from '../../../util/metrics/TrackError/trackErrorAsAnalytics'; import { selectPermissionControllerState } from '../../../selectors/snaps/permissionController'; -import { useIsFocused } from '@react-navigation/native'; -import handleWebViewFocus from '../../../util/browser/webViewFocus'; import { isTest } from '../../../util/test/utils.js'; import { EXTERNAL_LINK_TYPE } from '../../../constants/browser'; @@ -277,11 +275,9 @@ export const BrowserTab = (props) => { const [blockedUrl, setBlockedUrl] = useState(undefined); const [ipfsBannerVisible, setIpfsBannerVisible] = useState(false); const [isResolvedIpfsUrl, setIsResolvedIpfsUrl] = useState(false); - const previousChainIdRef = useRef('0x1'); const webviewRef = useRef(null); const blockListType = useRef(''); const allowList = useRef([]); - const isFocused = useIsFocused(); const url = useRef(''); const title = useRef(''); @@ -720,16 +716,6 @@ export const BrowserTab = (props) => { }; }, [goBack, isTabActive, props.navigation]); - useEffect(() => { - handleWebViewFocus({ - webviewRef, - isFocused, - chainId: props.chainId, - previousChainId: previousChainIdRef.current, - }); - previousChainIdRef.current = props.chainId; - }, [webviewRef, isFocused, props.chainId]); - /** * Inject home page scripts to get the favourites and set analytics key */ diff --git a/app/util/browser/webViewFocus.test.ts b/app/util/browser/webViewFocus.test.ts deleted file mode 100644 index c471fca34af..00000000000 --- a/app/util/browser/webViewFocus.test.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { Hex } from '@metamask/utils'; -import handleWebViewFocus from './webViewFocus'; - -interface WebViewRef { - injectJavaScript: (script: string) => void; - stopLoading: () => void; - reload: () => void; -} - -describe('handleWebViewFocus', () => { - let webviewRef: React.RefObject; - let stopLoading: jest.Mock; - let reload: jest.Mock; - - beforeEach(() => { - stopLoading = jest.fn(); - reload = jest.fn(); - webviewRef = { - current: { - injectJavaScript: jest.fn(), - stopLoading, - reload, - }, - }; - }); - - it('should reload the webview if chainId has changed', () => { - handleWebViewFocus({ - webviewRef, - isFocused: true, - chainId: '0x1' as Hex, - previousChainId: '0x2' as Hex, - }); - - expect(reload).toHaveBeenCalled(); - expect(stopLoading).not.toHaveBeenCalled(); - }); - - it('should not reload the webview if chainId has not changed', () => { - handleWebViewFocus({ - webviewRef, - isFocused: true, - chainId: '0x1' as Hex, - previousChainId: '0x1' as Hex, - }); - - expect(reload).not.toHaveBeenCalled(); - expect(stopLoading).not.toHaveBeenCalled(); - }); - - it('should do nothing if webviewRef.current is null', () => { - handleWebViewFocus({ - webviewRef: { current: null }, - isFocused: true, - chainId: '0x1' as Hex, - previousChainId: '0x2' as Hex, - }); - - expect(stopLoading).not.toHaveBeenCalled(); - expect(reload).not.toHaveBeenCalled(); - }); - - it('should stop loading if webview is not focused', () => { - handleWebViewFocus({ - webviewRef, - isFocused: false, - chainId: '0x1' as Hex, - previousChainId: '0x1' as Hex, - }); - - expect(stopLoading).toHaveBeenCalled(); - expect(reload).not.toHaveBeenCalled(); - }); -}); diff --git a/app/util/browser/webViewFocus.ts b/app/util/browser/webViewFocus.ts deleted file mode 100644 index bcf5430f08b..00000000000 --- a/app/util/browser/webViewFocus.ts +++ /dev/null @@ -1,32 +0,0 @@ -import React from 'react'; -import { Hex } from '@metamask/utils'; - -interface WebViewFocus { - webviewRef: React.RefObject<{ - injectJavaScript: (script: string) => void; - stopLoading: () => void; - reload: () => void; - }>; - isFocused: boolean; - chainId: Hex; - previousChainId?: Hex; -} - -const handleWebViewFocus = ({ - webviewRef, - isFocused, - chainId, - previousChainId, -}: WebViewFocus) => { - if (webviewRef.current) { - if (!isFocused) { - webviewRef.current.stopLoading(); - } - - if (previousChainId && previousChainId !== chainId) { - webviewRef.current.reload(); - } - } -}; - -export default handleWebViewFocus;