mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-04-22 15:57:59 +00:00
Fix all shadow deprecation warnings across entire mobile app
- Replaced shadowColor/shadowOffset/shadowOpacity/shadowRadius with boxShadow - Fixed 28 files (21 screens + 7 components) - Preserved elevation for Android compatibility - All React Native Web deprecation warnings resolved Files fixed: - All screen components - All reusable components - Navigation components - Modal components
This commit is contained in:
@@ -0,0 +1,249 @@
|
||||
import React, { createContext, useContext, useEffect, useState, ReactNode } from 'react';
|
||||
import { ApiPromise, WsProvider } from '@pezkuwi/api';
|
||||
import { web3Accounts, web3Enable } from '@pezkuwi/extension-dapp';
|
||||
import type { InjectedAccountWithMeta } from '@pezkuwi/extension-inject/types';
|
||||
import { DEFAULT_ENDPOINT } from '../../../shared/blockchain/pezkuwi';
|
||||
|
||||
interface PezkuwiContextType {
|
||||
api: ApiPromise | null;
|
||||
isApiReady: boolean;
|
||||
isConnected: boolean;
|
||||
accounts: InjectedAccountWithMeta[];
|
||||
selectedAccount: InjectedAccountWithMeta | null;
|
||||
setSelectedAccount: (account: InjectedAccountWithMeta | null) => void;
|
||||
connectWallet: () => Promise<void>;
|
||||
disconnectWallet: () => void;
|
||||
error: string | null;
|
||||
sudoKey: string | null;
|
||||
}
|
||||
|
||||
const PezkuwiContext = createContext<PezkuwiContextType | undefined>(undefined);
|
||||
|
||||
interface PezkuwiProviderProps {
|
||||
children: ReactNode;
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
export const PezkuwiProvider: React.FC<PezkuwiProviderProps> = ({
|
||||
children,
|
||||
endpoint = DEFAULT_ENDPOINT // Beta testnet RPC from shared config
|
||||
}) => {
|
||||
const [api, setApi] = useState<ApiPromise | null>(null);
|
||||
const [isApiReady, setIsApiReady] = useState(false);
|
||||
const [accounts, setAccounts] = useState<InjectedAccountWithMeta[]>([]);
|
||||
const [selectedAccount, setSelectedAccount] = useState<InjectedAccountWithMeta | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [sudoKey, setSudoKey] = useState<string | null>(null);
|
||||
|
||||
// Wrapper to trigger events when wallet changes
|
||||
const handleSetSelectedAccount = (account: InjectedAccountWithMeta | null) => {
|
||||
setSelectedAccount(account);
|
||||
if (account) {
|
||||
localStorage.setItem('selectedWallet', account.address);
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.log('💾 Wallet saved:', account.address);
|
||||
}
|
||||
window.dispatchEvent(new Event('walletChanged'));
|
||||
} else {
|
||||
localStorage.removeItem('selectedWallet');
|
||||
window.dispatchEvent(new Event('walletChanged'));
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize Pezkuwi API with fallback endpoints
|
||||
useEffect(() => {
|
||||
const FALLBACK_ENDPOINTS = [
|
||||
endpoint,
|
||||
import.meta.env.VITE_WS_ENDPOINT_FALLBACK_1,
|
||||
import.meta.env.VITE_WS_ENDPOINT_FALLBACK_2,
|
||||
].filter(Boolean);
|
||||
|
||||
const initApi = async () => {
|
||||
let lastError: unknown = null;
|
||||
|
||||
for (const currentEndpoint of FALLBACK_ENDPOINTS) {
|
||||
try {
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.log('🔗 Connecting to Pezkuwi node:', currentEndpoint);
|
||||
}
|
||||
|
||||
const provider = new WsProvider(currentEndpoint);
|
||||
const apiInstance = await ApiPromise.create({ provider });
|
||||
|
||||
await apiInstance.isReady;
|
||||
|
||||
setApi(apiInstance);
|
||||
setIsApiReady(true);
|
||||
setError(null);
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.log('✅ Connected to Pezkuwi node');
|
||||
|
||||
// Get chain info
|
||||
const [chain, nodeName, nodeVersion] = await Promise.all([
|
||||
apiInstance.rpc.system.chain(),
|
||||
apiInstance.rpc.system.name(),
|
||||
apiInstance.rpc.system.version(),
|
||||
]);
|
||||
|
||||
if (import.meta.env.DEV) console.log(`📡 Chain: ${chain}`);
|
||||
if (import.meta.env.DEV) console.log(`🖥️ Node: ${nodeName} v${nodeVersion}`);
|
||||
}
|
||||
|
||||
// Fetch sudo key from blockchain
|
||||
try {
|
||||
const sudoAccount = await apiInstance.query.sudo.key();
|
||||
const sudoAddress = sudoAccount.toString();
|
||||
setSudoKey(sudoAddress);
|
||||
if (import.meta.env.DEV) console.log(`🔑 Sudo key: ${sudoAddress}`);
|
||||
} catch (err) {
|
||||
if (import.meta.env.DEV) console.warn('⚠️ Failed to fetch sudo key (sudo pallet may not be available):', err);
|
||||
}
|
||||
|
||||
return;
|
||||
} catch (err) {
|
||||
lastError = err;
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.warn(`⚠️ Failed to connect to ${currentEndpoint}, trying next...`);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.error('❌ Failed to connect to all endpoints:', lastError);
|
||||
}
|
||||
setError('Failed to connect to blockchain network. Please try again later.');
|
||||
setIsApiReady(false);
|
||||
};
|
||||
|
||||
initApi();
|
||||
|
||||
return () => {
|
||||
if (api) {
|
||||
api.disconnect();
|
||||
}
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [endpoint]);
|
||||
|
||||
// Auto-restore wallet on page load
|
||||
useEffect(() => {
|
||||
const restoreWallet = async () => {
|
||||
const savedAddress = localStorage.getItem('selectedWallet');
|
||||
if (!savedAddress) return;
|
||||
|
||||
try {
|
||||
// Enable extension
|
||||
const extensions = await web3Enable('PezkuwiChain');
|
||||
if (extensions.length === 0) return;
|
||||
|
||||
// Get accounts
|
||||
const allAccounts = await web3Accounts();
|
||||
if (allAccounts.length === 0) return;
|
||||
|
||||
// Find saved account
|
||||
const savedAccount = allAccounts.find(acc => acc.address === savedAddress);
|
||||
if (savedAccount) {
|
||||
setAccounts(allAccounts);
|
||||
handleSetSelectedAccount(savedAccount);
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.log('✅ Wallet restored:', savedAddress.slice(0, 8) + '...');
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.error('Failed to restore wallet:', err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
restoreWallet();
|
||||
}, []);
|
||||
|
||||
// Connect wallet (Pezkuwi.js extension)
|
||||
const connectWallet = async () => {
|
||||
try {
|
||||
setError(null);
|
||||
|
||||
// Enable extension
|
||||
const extensions = await web3Enable('PezkuwiChain');
|
||||
|
||||
if (extensions.length === 0) {
|
||||
setError('Please install Pezkuwi.js extension');
|
||||
window.open('https://js.pezkuwichain.io/extension/', '_blank');
|
||||
return;
|
||||
}
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.log('✅ Pezkuwi.js extension enabled');
|
||||
}
|
||||
|
||||
// Get accounts
|
||||
const allAccounts = await web3Accounts();
|
||||
|
||||
if (allAccounts.length === 0) {
|
||||
setError('No accounts found. Please create an account in Pezkuwi.js extension');
|
||||
return;
|
||||
}
|
||||
|
||||
setAccounts(allAccounts);
|
||||
|
||||
// Try to restore previously selected account, otherwise use first
|
||||
const savedAddress = localStorage.getItem('selectedWallet');
|
||||
const accountToSelect = savedAddress
|
||||
? allAccounts.find(acc => acc.address === savedAddress) || allAccounts[0]
|
||||
: allAccounts[0];
|
||||
|
||||
// Use wrapper to trigger events
|
||||
handleSetSelectedAccount(accountToSelect);
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.log(`✅ Found ${allAccounts.length} account(s)`);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.error('❌ Wallet connection failed:', err);
|
||||
}
|
||||
setError('Failed to connect wallet');
|
||||
}
|
||||
};
|
||||
|
||||
// Disconnect wallet
|
||||
const disconnectWallet = () => {
|
||||
setAccounts([]);
|
||||
handleSetSelectedAccount(null);
|
||||
if (import.meta.env.DEV) {
|
||||
if (import.meta.env.DEV) console.log('🔌 Wallet disconnected');
|
||||
}
|
||||
};
|
||||
|
||||
const value: PezkuwiContextType = {
|
||||
api,
|
||||
isApiReady,
|
||||
isConnected: isApiReady, // Alias for backward compatibility
|
||||
accounts,
|
||||
selectedAccount,
|
||||
setSelectedAccount: handleSetSelectedAccount,
|
||||
connectWallet,
|
||||
disconnectWallet,
|
||||
error,
|
||||
sudoKey,
|
||||
};
|
||||
|
||||
return (
|
||||
<PezkuwiContext.Provider value={value}>
|
||||
{children}
|
||||
</PezkuwiContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
// Hook to use Pezkuwi context
|
||||
export const usePezkuwi = (): PezkuwiContextType => {
|
||||
const context = useContext(PezkuwiContext);
|
||||
if (!context) {
|
||||
throw new Error('usePezkuwi must be used within PezkuwiProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user