useWallet.ts
1 import { useMemo } from 'react'; 2 import { useNWC } from '@/hooks/useNWCContext'; 3 import type { WebLNProvider } from '@webbtc/webln-types'; 4 5 export interface WalletStatus { 6 hasNWC: boolean; 7 webln: WebLNProvider | null; 8 activeNWC: ReturnType<typeof useNWC>['getActiveConnection'] extends () => infer T ? T : null; 9 preferredMethod: 'nwc' | 'webln' | 'manual'; 10 } 11 12 export function useWallet() { 13 const { connections, getActiveConnection } = useNWC(); 14 15 // Get the active connection directly - no memoization to avoid stale state 16 const activeNWC = getActiveConnection(); 17 18 // Access WebLN directly from browser global scope 19 const webln = (globalThis as { webln?: WebLNProvider }).webln || null; 20 21 // Calculate status values reactively 22 const hasNWC = useMemo(() => { 23 return connections.length > 0 && connections.some(c => c.isConnected); 24 }, [connections]); 25 26 // Determine preferred payment method 27 const preferredMethod: WalletStatus['preferredMethod'] = activeNWC 28 ? 'nwc' 29 : webln 30 ? 'webln' 31 : 'manual'; 32 33 const status: WalletStatus = { 34 hasNWC, 35 webln, 36 activeNWC, 37 preferredMethod, 38 }; 39 40 return status; 41 }