Skip to content

Commit

Permalink
Only hold the active tab ref in BrowserContext (#5579)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Baroni <[email protected]>
  • Loading branch information
christianbaroni and christianbaroni authored Apr 2, 2024
1 parent 407aed2 commit 120b452
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 25 deletions.
18 changes: 2 additions & 16 deletions src/components/DappBrowser/BrowserContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import React, { createContext, useCallback, useContext, useLayoutEffect, useRef, useState } from 'react';
import React, { createContext, useCallback, useContext, useRef, useState } from 'react';
import { TextInput } from 'react-native';
import isEqual from 'react-fast-compare';
import { MMKV, useMMKVObject } from 'react-native-mmkv';
Expand Down Expand Up @@ -55,7 +55,6 @@ interface BrowserContextType {
tabViewVisible: SharedValue<boolean> | undefined;
toggleTabViewWorklet: (activeIndex?: number) => void;
updateActiveTabState: (newState: Partial<TabState>, tabId?: string) => void;
webViewRefs: React.MutableRefObject<(WebView | null)[]>;
}

export interface TabState {
Expand Down Expand Up @@ -121,7 +120,6 @@ const DEFAULT_BROWSER_CONTEXT: BrowserContextType = {
updateActiveTabState: () => {
return;
},
webViewRefs: { current: [] },
};

const BrowserContext = createContext<BrowserContextType>(DEFAULT_BROWSER_CONTEXT);
Expand Down Expand Up @@ -155,7 +153,6 @@ export const BrowserContextProvider = ({ children }: { children: React.ReactNode

const searchInputRef = useRef<TextInput>(null);
const scrollViewRef = useAnimatedRef<Animated.ScrollView>();
const webViewRefs = useRef<WebView[]>([]);
const activeTabRef = useRef<WebView | null>(null);

const loadProgress = useSharedValue(0);
Expand Down Expand Up @@ -222,7 +219,6 @@ export const BrowserContextProvider = ({ children }: { children: React.ReactNode
setActiveTabIndex(0);
animatedActiveTabIndex.value = 0;
setTabStates(EMPTY_TAB_STATE);
webViewRefs.current = [];
newTab();
return;
} else if (isLastTab && tabIndex > 0) {
Expand All @@ -238,9 +234,8 @@ export const BrowserContextProvider = ({ children }: { children: React.ReactNode
setTabStates(updatedTabs);
setActiveTabIndex(newActiveTabIndex);
animatedActiveTabIndex.value = newActiveTabIndex;
webViewRefs.current.splice(tabIndex, 1);
},
[activeTabIndex, animatedActiveTabIndex, newTab, setTabStates, tabStates, webViewRefs]
[activeTabIndex, animatedActiveTabIndex, newTab, setTabStates, tabStates]
);

const goBack = useCallback(() => {
Expand All @@ -261,14 +256,6 @@ export const BrowserContextProvider = ({ children }: { children: React.ReactNode
}
}, [activeTabRef]);

// useLayoutEffect seems to more reliably assign the ref correctly
useLayoutEffect(() => {
if (activeTabRef.current !== webViewRefs.current?.[activeTabIndex]) {
activeTabRef.current = webViewRefs.current?.[activeTabIndex] || null;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [activeTabIndex, webViewRefs]);

return (
<BrowserContext.Provider
value={{
Expand All @@ -291,7 +278,6 @@ export const BrowserContextProvider = ({ children }: { children: React.ReactNode
tabViewVisible,
toggleTabViewWorklet,
updateActiveTabState,
webViewRefs,
}}
>
{children}
Expand Down
10 changes: 1 addition & 9 deletions src/components/DappBrowser/BrowserTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export const BrowserTab = React.memo(function BrowserTab({ tabId, tabIndex, inje
tabViewVisible,
toggleTabViewWorklet,
updateActiveTabState,
webViewRefs,
} = useBrowserContext();
const { isDarkMode } = useColorMode();
const { width: deviceWidth } = useDimensions();
Expand Down Expand Up @@ -411,17 +410,10 @@ export const BrowserTab = React.memo(function BrowserTab({ tabId, tabIndex, inje
// useLayoutEffect seems to more reliably assign the ref correctly
useLayoutEffect(() => {
if (webViewRef.current !== null && isActiveTab) {
webViewRefs.current[tabIndex] = webViewRef.current;
activeTabRef.current = webViewRef.current;
}

const currentWebviewRef = webViewRefs.current;

return () => {
currentWebviewRef[tabIndex] = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isActiveTab, isOnHomepage, tabId, tabIndex, webViewRefs]);
}, [isActiveTab, isOnHomepage, tabId]);

const saveScreenshotToFileSystem = useCallback(
async (tempUri: string, tabId: string, timestamp: number, url: string) => {
Expand Down

0 comments on commit 120b452

Please sign in to comment.