import { FC, useEffect } from 'react';
import { Alert } from 'react-native';
import { useMyDynamicClient } from './useMyDynamicClient';
export const PhantomEventHandler: FC = () => {
const { client } = useMyDynamicClient();
useEffect(() => {
const handleSignMessage = ({ signature, errorCode, errorMessage }) => {
if (errorCode) {
Alert.alert('Sign Message Failed', errorMessage || errorCode);
} else {
Alert.alert('Message Signed', `Signature: ${signature?.slice(0, 20)}...`);
}
};
const handleSignTransaction = ({ transaction, errorCode, errorMessage }) => {
if (errorCode) {
console.error('Transaction signing failed:', errorMessage);
return;
}
console.log('Signed transaction:', transaction);
};
const handleSignAndSendTransaction = ({ signature, errorCode, errorMessage }) => {
if (errorCode) {
console.error('Transaction failed:', errorMessage);
return;
}
console.log('Transaction sent, signature:', signature);
};
const handleSignAllTransactions = ({ transactions, errorCode, errorMessage }) => {
if (errorCode) {
console.error('Batch signing failed:', errorMessage);
return;
}
console.log('Signed transactions:', transactions);
};
// Subscribe to events
client.wallets.phantom.on('signMessageResult', handleSignMessage);
client.wallets.phantom.on('signTransactionResult', handleSignTransaction);
client.wallets.phantom.on('signAndSendTransactionResult', handleSignAndSendTransaction);
client.wallets.phantom.on('signAllTransactionsResult', handleSignAllTransactions);
// Cleanup on unmount
return () => {
client.wallets.phantom.off('signMessageResult', handleSignMessage);
client.wallets.phantom.off('signTransactionResult', handleSignTransaction);
client.wallets.phantom.off('signAndSendTransactionResult', handleSignAndSendTransaction);
client.wallets.phantom.off('signAllTransactionsResult', handleSignAllTransactions);
};
}, [client]);
return null;
};