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: Browser dialog prompts in app #10540

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
841f1b4
made a change to the Browser Tab component that checks if the webview…
Daniel-Cross Aug 2, 2024
dc69d9d
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 2, 2024
a028cda
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 2, 2024
4457e98
loading will now stop when tab is not focused
Daniel-Cross Aug 2, 2024
1f004f9
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 12, 2024
998e8f4
added a download resume when tab is back in focus
Daniel-Cross Aug 12, 2024
1618c28
removed the paused state as it was unused
Daniel-Cross Aug 12, 2024
e024750
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 12, 2024
6f29cae
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 12, 2024
d999574
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 12, 2024
d604a37
created a custom function for the handle web view focus instead of ha…
Daniel-Cross Aug 13, 2024
c94b392
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 13, 2024
a455c56
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 13, 2024
815e0ae
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 13, 2024
4c8ad87
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 14, 2024
6bc26b3
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 15, 2024
a43f1e2
yarn deduplicate
Daniel-Cross Aug 15, 2024
9ffd39d
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 15, 2024
c1cb958
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 15, 2024
f21bd23
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 16, 2024
38002d4
updated function to check for change in chainId, and if so, refresh t…
Daniel-Cross Aug 21, 2024
768a013
updated tests
Daniel-Cross Aug 21, 2024
e642170
resolved conflicts
Daniel-Cross Aug 21, 2024
7851075
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 21, 2024
ab2e4fe
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 22, 2024
8cad7d6
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 22, 2024
93aff6b
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 26, 2024
9825a1e
made a change to the useEffect to remove the ignore linting
Daniel-Cross Aug 26, 2024
ac88618
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 26, 2024
a4d0258
removed console log
Daniel-Cross Aug 26, 2024
2efa844
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 27, 2024
ad42703
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 27, 2024
276b154
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 27, 2024
1dfa9dd
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 27, 2024
2f3a042
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 27, 2024
f4a01ab
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 28, 2024
7985d0a
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 28, 2024
9b3d3a0
changed tests to check for hex
Daniel-Cross Aug 28, 2024
8c59f7f
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 28, 2024
3a4d0f4
Merge branch 'main' into 1247-bug-browser-dialog-prompts-can-overlay-…
Daniel-Cross Aug 29, 2024
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
14 changes: 14 additions & 0 deletions app/components/Views/BrowserTab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ 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';

const { HOMEPAGE_URL, NOTIFICATION_NAMES } = AppConstants;
Expand Down Expand Up @@ -267,9 +269,11 @@ 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('');
Expand Down Expand Up @@ -706,6 +710,16 @@ 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
*/
Expand Down
74 changes: 74 additions & 0 deletions app/util/browser/webViewFocus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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<WebViewRef>;
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();
});
});
32 changes: 32 additions & 0 deletions app/util/browser/webViewFocus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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;
Loading