From c7fc7fc2115252a29229365e548cc0b94da6a738 Mon Sep 17 00:00:00 2001 From: tommasini Date: Fri, 20 Dec 2024 16:31:42 +0000 Subject: [PATCH 1/2] active url to save with useState --- .../UI/BrowserUrlBar/BrowserUrlBar.tsx | 7 +-- .../UI/BrowserUrlBar/BrowserUrlBar.types.ts | 2 +- .../Views/BrowserTab/BrowserTab.tsx | 57 ++++++++++--------- .../BrowserTab/components/Options/index.tsx | 16 +++--- .../components/PhishingModal/index.tsx | 6 +- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/app/components/UI/BrowserUrlBar/BrowserUrlBar.tsx b/app/components/UI/BrowserUrlBar/BrowserUrlBar.tsx index 68215166dd1..de6e42bc272 100644 --- a/app/components/UI/BrowserUrlBar/BrowserUrlBar.tsx +++ b/app/components/UI/BrowserUrlBar/BrowserUrlBar.tsx @@ -19,7 +19,6 @@ import Icon, { import { BrowserUrlBarProps } from './BrowserUrlBar.types'; import stylesheet from './BrowserUrlBar.styles'; import { BrowserViewSelectorsIDs } from '../../../../e2e/selectors/Browser/BrowserView.selectors'; -import Url from 'url-parse'; import { regex } from '../../../../app/util/regex'; import { strings } from '../../../../locales/i18n'; import { BrowserURLBarSelectorsIDs } from '../../../../e2e/selectors/Browser/BrowserURLBar.selectors'; @@ -44,7 +43,7 @@ const BrowserUrlBar = forwardRef( onBlur, onChangeText, connectedAccounts, - activeUrlRef, + activeUrl, }, ref, ) => { @@ -147,9 +146,7 @@ const BrowserUrlBar = forwardRef( hostInfo: { metadata: { // TODO: This is not an origin, it's a hostname - origin: - activeUrlRef.current && - new URLParse(activeUrlRef.current).hostname, + origin: activeUrl && new URLParse(activeUrl).hostname, }, }, }, diff --git a/app/components/UI/BrowserUrlBar/BrowserUrlBar.types.ts b/app/components/UI/BrowserUrlBar/BrowserUrlBar.types.ts index 3ea084b2ef7..091c14b98b4 100644 --- a/app/components/UI/BrowserUrlBar/BrowserUrlBar.types.ts +++ b/app/components/UI/BrowserUrlBar/BrowserUrlBar.types.ts @@ -9,5 +9,5 @@ export type BrowserUrlBarProps = { onBlur: () => void; onChangeText: (text: string) => void; connectedAccounts: string[]; - activeUrlRef: React.RefObject; + activeUrl: string; }; diff --git a/app/components/Views/BrowserTab/BrowserTab.tsx b/app/components/Views/BrowserTab/BrowserTab.tsx index 1a0b7587b29..6be5ce13926 100644 --- a/app/components/Views/BrowserTab/BrowserTab.tsx +++ b/app/components/Views/BrowserTab/BrowserTab.tsx @@ -138,7 +138,8 @@ export const BrowserTab: React.FC = (props) => { const urlBarResultsRef = useRef(null); const [isSecureConnection, setIsSecureConnection] = useState(false); - const activeUrl = useRef(''); + // const activeUrl = useRef(''); + const [activeUrl, setActiveUrl] = useState(''); const title = useRef(''); const icon = useRef(); const backgroundBridges = useRef< @@ -154,7 +155,7 @@ export const BrowserTab: React.FC = (props) => { const wizardScrollAdjusted = useRef(false); const permittedAccountsList = useSelector((state: RootState) => { const permissionsControllerState = selectPermissionControllerState(state); - const hostname = new URLParse(activeUrl.current).hostname; + const hostname = new URLParse(activeUrl).hostname; const permittedAcc = getPermittedAccountsByHostname( permissionsControllerState, hostname, @@ -162,7 +163,7 @@ export const BrowserTab: React.FC = (props) => { return permittedAcc; }, isEqual); - const favicon = useFavicon(activeUrl.current); + const favicon = useFavicon(activeUrl); const { trackEvent, isEnabled, getMetaMetricsId, createEventBuilder } = useMetrics(); /** @@ -177,17 +178,20 @@ export const BrowserTab: React.FC = (props) => { /** * Checks if a given url or the current url is the homepage */ - const isHomepage = useCallback((checkUrl = null) => { - const currentPage = checkUrl || activeUrl.current; - const prefixedUrl = prefixUrlWithProtocol(currentPage); - const { host: currentHost } = getUrlObj(prefixedUrl); - return ( - currentHost === HOMEPAGE_HOST || currentHost === OLD_HOMEPAGE_URL_HOST - ); - }, []); + const isHomepage = useCallback( + (checkUrl = null) => { + const currentPage = checkUrl || activeUrl; + const prefixedUrl = prefixUrlWithProtocol(currentPage); + const { host: currentHost } = getUrlObj(prefixedUrl); + return ( + currentHost === HOMEPAGE_HOST || currentHost === OLD_HOMEPAGE_URL_HOST + ); + }, + [activeUrl], + ); const notifyAllConnections = useCallback((payload) => { - const fullHostname = new URLParse(activeUrl.current).hostname; + const fullHostname = new URLParse(activeUrl).hostname; // TODO:permissions move permissioning logic elsewhere backgroundBridges.current.forEach((bridge) => { @@ -480,8 +484,8 @@ export const BrowserTab: React.FC = (props) => { const { current } = webviewRef; current && current.reload(); - triggerDappViewedEvent(activeUrl.current); - }, []); + triggerDappViewedEvent(activeUrl); + }, [activeUrl]); /** * Handle when the drawer (app menu) is opened @@ -592,7 +596,7 @@ export const BrowserTab: React.FC = (props) => { title: string; icon: ImageSourcePropType; }) => { - activeUrl.current = siteInfo.url; + setActiveUrl(siteInfo.url); title.current = siteInfo.title; if (siteInfo.icon) icon.current = siteInfo.icon; }; @@ -712,7 +716,7 @@ export const BrowserTab: React.FC = (props) => { webStates.current[url] = { ...webStates.current[url], ended: true }; const { started, ended } = webStates.current[url]; const incomingOrigin = new URLParse(url).origin; - const activeOrigin = new URLParse(activeUrl.current).origin; + const activeOrigin = new URLParse(activeUrl).origin; if ((started && ended) || incomingOrigin === activeOrigin) { delete webStates.current[url]; // Update navigation bar address with title of loaded url. @@ -760,10 +764,7 @@ export const BrowserTab: React.FC = (props) => { } } catch (e: unknown) { const onMessageError = e as Error; - Logger.error( - onMessageError, - `Browser::onMessage on ${activeUrl.current}`, - ); + Logger.error(onMessageError, `Browser::onMessage on ${activeUrl}`); } }; @@ -772,7 +773,7 @@ export const BrowserTab: React.FC = (props) => { */ const goToHomepage = async () => { toggleOptionsIfNeeded(); - if (activeUrl.current === HOMEPAGE_URL) return reload(); + if (activeUrl === HOMEPAGE_URL) return reload(); await go(HOMEPAGE_URL); trackEvent(createEventBuilder(MetaMetricsEvents.DAPP_HOME).build()); }; @@ -801,7 +802,7 @@ export const BrowserTab: React.FC = (props) => { getProviderState, navigation, // Website info - url: activeUrl, + url: { current: activeUrl }, title, icon, // Bookmarks @@ -849,7 +850,7 @@ export const BrowserTab: React.FC = (props) => { // Cancel loading the page if we detect its a phishing page if (!isAllowedOrigin(urlOrigin)) { - handleNotAllowedUrl(activeUrl.current); // should this be activeUrl.current instead of url? + handleNotAllowedUrl(activeUrl); // should this be activeUrl instead of url? return false; } @@ -996,9 +997,9 @@ export const BrowserTab: React.FC = (props) => { ); const checkTabPermissions = useCallback(() => { - if (!activeUrl.current) return; + if (!activeUrl) return; - const hostname = new URLParse(activeUrl.current).hostname; + const hostname = new URLParse(activeUrl).hostname; const permissionsControllerState = Engine.context.PermissionController.state; const permittedAccounts = getPermittedAccountsByHostname( @@ -1042,7 +1043,7 @@ export const BrowserTab: React.FC = (props) => { Logger.error(checkTabPermissionsError, 'Error in checkTabPermissions'); } } - }, [props.chainId, navigation]); + }, [props.chainId, navigation, activeUrl]); useEffect(() => { if ( @@ -1080,7 +1081,7 @@ export const BrowserTab: React.FC = (props) => { const onCancel = () => { // Reset the url bar to the current url - urlBarRef.current?.setNativeProps({ text: activeUrl.current }); + urlBarRef.current?.setNativeProps({ text: activeUrl }); }; const onFocusUrlBar = () => { @@ -1115,7 +1116,7 @@ export const BrowserTab: React.FC = (props) => { onBlur={onBlurUrlBar} onChangeText={onChangeUrlBar} connectedAccounts={permittedAccountsList} - activeUrlRef={activeUrl} + activeUrl={activeUrl} /> {renderProgressBar()} diff --git a/app/components/Views/BrowserTab/components/Options/index.tsx b/app/components/Views/BrowserTab/components/Options/index.tsx index f68bb2611a2..0dfc51b0c58 100644 --- a/app/components/Views/BrowserTab/components/Options/index.tsx +++ b/app/components/Views/BrowserTab/components/Options/index.tsx @@ -41,7 +41,7 @@ interface OptionsProps { toggleOptions: () => void; onNewTabPress: () => void; toggleOptionsIfNeeded: () => void; - activeUrl: MutableRefObject; + activeUrl: string; isHomepage: () => boolean; getMaskedUrl: (urlToMask: string, sessionENSNames: SessionENSNames) => string; onSubmitEditing: (url: string) => void; @@ -83,9 +83,9 @@ const Options = ({ */ const openInBrowser = () => { toggleOptionsIfNeeded(); - Linking.openURL(activeUrl.current).catch((openInBrowserError) => + Linking.openURL(activeUrl).catch((openInBrowserError) => Logger.log( - `Error while trying to open external link: ${activeUrl.current}`, + `Error while trying to open external link: ${activeUrl}`, openInBrowserError, ), ); @@ -98,7 +98,7 @@ const Options = ({ */ const goToFavorites = async () => { toggleOptionsIfNeeded(); - if (activeUrl.current === OLD_HOMEPAGE_URL_HOST) return reload(); + if (activeUrl === OLD_HOMEPAGE_URL_HOST) return reload(); await onSubmitEditing(OLD_HOMEPAGE_URL_HOST); trackEvent( createEventBuilder(MetaMetricsEvents.DAPP_GO_TO_FAVORITES).build(), @@ -127,7 +127,7 @@ const Options = ({ screen: 'AddBookmark', params: { title: title.current || '', - url: getMaskedUrl(activeUrl.current, sessionENSNames), + url: getMaskedUrl(activeUrl, sessionENSNames), onAddBookmark: async ({ name, url: urlToAdd, @@ -197,7 +197,9 @@ const Options = ({ }; const isBookmark = () => { - const maskedUrl = getMaskedUrl(activeUrl.current, sessionENSNames); + const maskedUrl = getMaskedUrl(activeUrl, sessionENSNames); + console.log('ENTER maskedUrl', maskedUrl); + return bookmarks.some( ({ url: bookmark }: { url: string }) => bookmark === maskedUrl, ); @@ -218,7 +220,7 @@ const Options = ({ const share = () => { toggleOptionsIfNeeded(); Share.open({ - url: activeUrl.current, + url: activeUrl, }).catch((err) => { Logger.log('Error while trying to share address', err); }); diff --git a/app/components/Views/BrowserTab/components/PhishingModal/index.tsx b/app/components/Views/BrowserTab/components/PhishingModal/index.tsx index b387992bad8..42dd1fd296a 100644 --- a/app/components/Views/BrowserTab/components/PhishingModal/index.tsx +++ b/app/components/Views/BrowserTab/components/PhishingModal/index.tsx @@ -20,7 +20,7 @@ interface PhishingModalProps { go: (url: string) => void; urlBarRef: React.MutableRefObject; addToWhitelist: (hostname: string) => void; - activeUrl: React.MutableRefObject; + activeUrl: string; blockListType: React.MutableRefObject; onSubmitEditing: (url: string) => void; } @@ -59,7 +59,7 @@ const PhishingModal = ({ addToWhitelist(urlOrigin); setShowPhishingModal(false); - blockedUrl !== activeUrl.current && + blockedUrl !== activeUrl && setTimeout(() => { onSubmitEditing(blockedUrl); setBlockedUrl(undefined); @@ -88,7 +88,7 @@ const PhishingModal = ({ * Go back from phishing website alert */ const goBackToSafety = () => { - urlBarRef.current?.setNativeProps({ text: activeUrl.current }); + urlBarRef.current?.setNativeProps({ text: activeUrl }); setTimeout(() => { setShowPhishingModal(false); From ea27465de2bf85b01ff821aacd54bfbf7543200e Mon Sep 17 00:00:00 2001 From: tommasini Date: Fri, 20 Dec 2024 17:05:22 +0000 Subject: [PATCH 2/2] update notifyAllConnection dependency --- .../Views/BrowserTab/BrowserTab.tsx | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/app/components/Views/BrowserTab/BrowserTab.tsx b/app/components/Views/BrowserTab/BrowserTab.tsx index 6be5ce13926..706a9e6384c 100644 --- a/app/components/Views/BrowserTab/BrowserTab.tsx +++ b/app/components/Views/BrowserTab/BrowserTab.tsx @@ -190,16 +190,19 @@ export const BrowserTab: React.FC = (props) => { [activeUrl], ); - const notifyAllConnections = useCallback((payload) => { - const fullHostname = new URLParse(activeUrl).hostname; - - // TODO:permissions move permissioning logic elsewhere - backgroundBridges.current.forEach((bridge) => { - if (bridge.hostname === fullHostname) { - bridge.sendNotification(payload); - } - }); - }, []); + const notifyAllConnections = useCallback( + (payload) => { + const fullHostname = new URLParse(activeUrl).hostname; + + // TODO:permissions move permissioning logic elsewhere + backgroundBridges.current.forEach((bridge) => { + if (bridge.hostname === fullHostname) { + bridge.sendNotification(payload); + } + }); + }, + [activeUrl], + ); /** * Dismiss the text selection on the current website @@ -250,6 +253,8 @@ export const BrowserTab: React.FC = (props) => { toggleOptionsIfNeeded(); const { current } = webviewRef; current && current.goBack(); + + // we need to update the activeUrl state to the previous url }, [backEnabled, toggleOptionsIfNeeded]); /**