feat(web): Add mobile bridge support for P2P WebView integration

- Add mobile-bridge.ts utility for native app communication
- Add useMobileBridge.ts React hook
- Update AuthContext to detect and use native wallet
- Update PezkuwiContext to connect mobile wallet automatically
- Update WalletContext to sign transactions via native bridge

Mobile app can now seamlessly use web P2P features with native wallet.
This commit is contained in:
2026-01-15 10:00:11 +03:00
parent ba74fe4298
commit 298a2c57f1
5 changed files with 415 additions and 11 deletions
+38 -2
View File
@@ -1,6 +1,7 @@
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
import { supabase } from '@/lib/supabase';
import { User } from '@supabase/supabase-js';
import { isMobileApp, getNativeWalletAddress, getNativeAccountName } from '@/lib/mobile-bridge';
// Session timeout configuration
const SESSION_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
@@ -159,7 +160,31 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
}
}, []);
// Setup native mobile wallet if running in mobile app
const setupMobileWallet = useCallback(() => {
if (isMobileApp()) {
const nativeAddress = getNativeWalletAddress();
const nativeAccountName = getNativeAccountName();
if (nativeAddress) {
// Store native wallet address for admin checks and wallet operations
localStorage.setItem('selectedWallet', nativeAddress);
if (nativeAccountName) {
localStorage.setItem('selectedWalletName', nativeAccountName);
}
if (import.meta.env.DEV) {
console.log('[Mobile] Native wallet detected:', nativeAddress);
}
// Dispatch wallet change event
window.dispatchEvent(new Event('walletChanged'));
}
}
}, []);
useEffect(() => {
// Setup mobile wallet first
setupMobileWallet();
// Check active sessions and sets the user
supabase.auth.getSession().then(({ data: { session } }) => {
setUser(session?.user ?? null);
@@ -178,17 +203,28 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
setLoading(false);
});
// Listen for wallet changes (from PezkuwiContext)
// Listen for wallet changes (from PezkuwiContext or native bridge)
const handleWalletChange = () => {
checkAdminStatus();
};
window.addEventListener('walletChanged', handleWalletChange);
// Listen for native bridge ready event (mobile app)
const handleNativeReady = () => {
if (import.meta.env.DEV) {
console.log('[Mobile] Native bridge ready');
}
setupMobileWallet();
checkAdminStatus();
};
window.addEventListener('pezkuwi-native-ready', handleNativeReady);
return () => {
subscription.unsubscribe();
window.removeEventListener('walletChanged', handleWalletChange);
window.removeEventListener('pezkuwi-native-ready', handleNativeReady);
};
}, [checkAdminStatus]);
}, [checkAdminStatus, setupMobileWallet]);
const signIn = async (email: string, password: string, rememberMe: boolean = false) => {
try {