Remove unnecessary code

This commit is contained in:
AdityaSalunkhe21 2025-04-11 19:17:49 +05:30
parent feb86bd5f8
commit 1aabebf2c0
5 changed files with 10 additions and 42 deletions

View File

@ -40,7 +40,7 @@ import { WalletEmbed } from "./screens/WalletEmbed";
import { AutoSignIn } from "./screens/AutoSignIn";
import { checkSufficientFunds, getPathKey, sendMessage } from "./utils/misc";
import useAccountsData from "./hooks/useAccountsData";
import { useSignRequestHandler } from "./hooks/useSignRequestHandler";
import { useWebViewHandler } from "./hooks/useWebViewHandler";
const Stack = createStackNavigator<StackParamsList>();
@ -280,7 +280,7 @@ const App = (): React.JSX.Element => {
const showWalletConnect = useMemo(() => accounts.length > 0, [accounts]);
useSignRequestHandler();
useWebViewHandler();
return (
<Surface style={styles.appSurface}>

5
src/global.d.ts vendored
View File

@ -1,7 +1,7 @@
// Extends the Window interface for Android WebView communication
declare global {
interface Window {
// Android bridge callbacks for signature-related events
// Android bridge callbacks for signature and accounts related events
Android?: {
// Called when signature is successfully generated
onSignatureComplete?: (signature: string) => void;
@ -12,9 +12,6 @@ declare global {
// Called when signature process is cancelled
onSignatureCancelled?: () => void;
// Called when JS bridge is initialized
onJsBridgeReady?: () => void;
// Called when accounts are ready for use
onAccountsReady?: () => void;
};

View File

@ -57,6 +57,7 @@ const useGetOrCreateAccounts = () => {
const isAndroidWebView = !!(window.Android);
// TODO: Call method to auto create accounts from android app
if (isAndroidWebView) {
autoCreateAccounts();
}

View File

@ -1,4 +1,4 @@
import { useEffect, useRef, useCallback } from 'react';
import { useEffect, useCallback } from 'react';
import { useNavigation } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
@ -7,7 +7,7 @@ import { useNetworks } from '../context/NetworksContext';
import { StackParamsList } from '../types';
import useGetOrCreateAccounts from './useGetOrCreateAccounts';
export const useSignRequestHandler = () => {
export const useWebViewHandler = () => {
// Navigation and context hooks
const navigation = useNavigation<NativeStackNavigationProp<StackParamsList>>();
const { selectedNetwork } = useNetworks();
@ -16,19 +16,8 @@ export const useSignRequestHandler = () => {
// Initialize accounts
useGetOrCreateAccounts();
// Use a ref for pending messages instead of state to avoid re-renders
const pendingMessageRef = useRef<string | null>(null);
// Core navigation handler
const navigateToSignRequest = useCallback((message: string) => {
// Data readiness check
const isDataReady = !!(selectedNetwork && accounts && accounts.length > 0);
if (!isDataReady) {
pendingMessageRef.current = message;
return;
}
try {
// Validation checks
if (!selectedNetwork?.namespace || !selectedNetwork?.chainId) {
@ -81,29 +70,12 @@ export const useSignRequestHandler = () => {
}
}, [selectedNetwork, accounts, currentIndex, navigation]);
// Setup Android bridge and handle pending messages
useEffect(() => {
// Setup global function to receive sign requests from Android
// Assign the function to the window object
window.receiveSignRequestFromAndroid = navigateToSignRequest;
// Process any pending messages whenever dependencies change
if (pendingMessageRef.current && selectedNetwork && accounts?.length > 0) {
const message = pendingMessageRef.current;
pendingMessageRef.current = null;
navigateToSignRequest(message);
}
// Notify Android that JS bridge is ready
if (window.Android) {
const timeoutId = setTimeout(() => window.Android?.onJsBridgeReady?.(), 100);
return () => {
clearTimeout(timeoutId);
window.receiveSignRequestFromAndroid = undefined;
};
}
return () => {
window.receiveSignRequestFromAndroid = undefined;
};
}, [navigateToSignRequest, selectedNetwork, accounts]);
}, [navigateToSignRequest]); // Only the function reference as dependency
};

View File

@ -10,10 +10,8 @@ export type StackParamsList = {
selectedNamespace: string;
selectedChainId: string;
accountInfo: Account;
prefillMessage?: string;
};
SignRequest: {
path?: string;
namespace: string;
chainId?: string;
address: string;