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 tab url type #10894

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion app/actions/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ export function closeAllTabs() {
* Creates a new tab
*
* @param {string} url - The website's url
* @param {string} linkType - optional link type
*/
export function createNewTab(url) {
export function createNewTab(url, linkType) {
return {
type: 'CREATE_NEW_TAB',
url,
linkType,
id: Date.now(),
};
}
Expand Down
2 changes: 2 additions & 0 deletions app/components/UI/CollectibleModal/CollectibleModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MetaMetricsEvents } from '../../../core/Analytics';
import { selectChainId } from '../../../selectors/networkController';
import { getDecimalChainId } from '../../../util/networks';
import { Nft } from '@metamask/assets-controllers';
import { EXTERNAL_LINK_TYPE } from '../../../constants/browser';

const CollectibleModal = () => {
const navigation = useNavigation();
Expand Down Expand Up @@ -92,6 +93,7 @@ const CollectibleModal = () => {
screen: Routes.BROWSER_VIEW,
params: {
newTabUrl: url,
linkType: EXTERNAL_LINK_TYPE,
timestamp: Date.now(),
},
});
Expand Down
14 changes: 8 additions & 6 deletions app/components/Views/Browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const Browser = (props) => {
const { trackEvent } = useMetrics();
const { toastRef } = useContext(ToastContext);
const browserUrl = props.route?.params?.url;
const linkType = props.route?.params?.linkType;
const prevSiteHostname = useRef(browserUrl);
const { accounts, ensByAccountAddress } = useAccounts();
const accountAvatarType = useSelector((state) =>
Expand Down Expand Up @@ -114,8 +115,8 @@ export const Browser = (props) => {
[navigation, route, colors],
);

const newTab = (url) => {
createNewTab(url || AppConstants.HOMEPAGE_URL);
const newTab = (url, linkType) => {
createNewTab(url || AppConstants.HOMEPAGE_URL, linkType);
};

const updateTabInfo = (url, tabID) =>
Expand Down Expand Up @@ -222,15 +223,15 @@ export const Browser = (props) => {
[tabs],
);

// Handle deeplinks.
// Handle links with associated timestamp.
useEffect(
() => {
const newTabUrl = route.params?.newTabUrl;
const deeplinkTimestamp = route.params?.timestamp;
const existingTabId = route.params?.existingTabId;
if (newTabUrl && deeplinkTimestamp) {
// Open url from deeplink.
newTab(newTabUrl);
// Open url from link.
newTab(newTabUrl, linkType);
} else if (existingTabId) {
const existingTab = tabs.find((tab) => tab.id === existingTabId);
if (existingTab) {
Expand Down Expand Up @@ -359,6 +360,7 @@ export const Browser = (props) => {
id={tab.id}
key={`tab_${tab.id}`}
initialUrl={tab.url || AppConstants.HOMEPAGE_URL}
linkType={tab.linkType}
updateTabInfo={updateTabInfo}
showTabs={showTabs}
newTab={newTab}
Expand All @@ -384,7 +386,7 @@ const mapStateToProps = (state) => ({
});

const mapDispatchToProps = (dispatch) => ({
createNewTab: (url) => dispatch(createNewTab(url)),
createNewTab: (url, linkType) => dispatch(createNewTab(url, linkType)),
closeAllTabs: () => dispatch(closeAllTabs()),
closeTab: (id) => dispatch(closeTab(id)),
setActiveTab: (id) => dispatch(setActiveTab(id)),
Expand Down
23 changes: 21 additions & 2 deletions app/components/Views/BrowserTab/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useState, useRef, useEffect, useCallback } from 'react';
import React, {
useState,
useRef,
useEffect,
useCallback,
useMemo,
} from 'react';
import {
Text,
StyleSheet,
Expand Down Expand Up @@ -105,6 +111,7 @@ import { selectPermissionControllerState } from '../../../selectors/snaps/permis
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';

const { HOMEPAGE_URL, NOTIFICATION_NAMES } = AppConstants;
const HOMEPAGE_HOST = new URL(HOMEPAGE_URL)?.hostname;
Expand Down Expand Up @@ -1504,6 +1511,11 @@ export const BrowserTab = (props) => {
</View>
);

const isExternalLink = useMemo(
() => props.linkType === EXTERNAL_LINK_TYPE,
[props.linkType],
);

/**
* Main render
*/
Expand Down Expand Up @@ -1533,7 +1545,10 @@ export const BrowserTab = (props) => {
renderError={() => (
<WebviewError error={error} returnHome={returnHome} />
)}
source={{ uri: initialUrl }}
source={{
uri: initialUrl,
...(isExternalLink ? { headers: { Cookie: '' } } : null),
}}
injectedJavaScriptBeforeContentLoaded={entryScriptWeb3}
style={styles.webview}
onLoadStart={onLoadStart}
Expand Down Expand Up @@ -1578,6 +1593,10 @@ BrowserTab.propTypes = {
* InitialUrl
*/
initialUrl: PropTypes.string,
/**
* linkType - type of link to open
*/
linkType: PropTypes.string,
/**
* Protocol string to append to URLs that have none
*/
Expand Down
1 change: 1 addition & 0 deletions app/constants/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const EXTERNAL_LINK_TYPE = 'external-link';
2 changes: 2 additions & 0 deletions app/core/DeeplinkManager/Handlers/handleBrowserUrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Routes from '../../../constants/navigation/Routes';
import { InteractionManager } from 'react-native';
import DeeplinkManager from '../DeeplinkManager';
import { EXTERNAL_LINK_TYPE } from '../../../constants/browser';

function handleBrowserUrl({
deeplinkManager,
Expand All @@ -19,6 +20,7 @@ function handleBrowserUrl({
screen: Routes.BROWSER.VIEW,
params: {
newTabUrl: url,
linkType: EXTERNAL_LINK_TYPE,
timestamp: Date.now(),
},
});
Expand Down
9 changes: 8 additions & 1 deletion app/reducers/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ const browserReducer = (state = initialState, action) => {
case 'CREATE_NEW_TAB':
return {
...state,
tabs: [...state.tabs, { url: action.url, id: action.id }],
tabs: [
...state.tabs,
{
url: action.url,
...(action.linkType && { linkType: action.linkType }),
id: action.id,
},
],
};
case 'CLOSE_TAB':
return {
Expand Down
Loading