mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { useNWC } from '@/hooks/useNWCContext';
|
|
import { useCashu } from '@/hooks/cashu/useCashu';
|
|
import type { WebLNProvider } from '@webbtc/webln-types';
|
|
|
|
export interface WalletStatus {
|
|
hasNWC: boolean;
|
|
webln: WebLNProvider | null;
|
|
activeNWC: ReturnType<typeof useNWC>['getActiveConnection'] extends () => infer T ? T : null;
|
|
preferredMethod: 'cashu' | 'nwc' | 'webln' | 'manual';
|
|
// Cashu integration
|
|
hasCashu: boolean;
|
|
cashuBalance: number;
|
|
isCashuInitialized: boolean;
|
|
}
|
|
|
|
export function useWallet() {
|
|
const { connections, getActiveConnection } = useNWC();
|
|
const { balances, mints, isInitialized: isCashuInitialized } = useCashu();
|
|
|
|
// Get the active connection directly - no memoization to avoid stale state
|
|
const activeNWC = getActiveConnection();
|
|
|
|
// Access WebLN directly from browser global scope
|
|
const webln = (globalThis as { webln?: WebLNProvider }).webln || null;
|
|
|
|
// Calculate Cashu status
|
|
const hasCashu = useMemo(() => {
|
|
return isCashuInitialized && mints.some(mint => (balances[mint.url] || 0) > 0);
|
|
}, [isCashuInitialized, mints, balances]);
|
|
|
|
const cashuBalance = useMemo(() => {
|
|
return Object.values(balances).reduce((sum, balance) => sum + balance, 0);
|
|
}, [balances]);
|
|
|
|
// Calculate Lightning status values reactively
|
|
const hasNWC = useMemo(() => {
|
|
return connections.length > 0 && connections.some(c => c.isConnected);
|
|
}, [connections]);
|
|
|
|
// Determine preferred payment method - Cashu first, then Lightning fallbacks
|
|
const preferredMethod: WalletStatus['preferredMethod'] = hasCashu
|
|
? 'cashu'
|
|
: activeNWC
|
|
? 'nwc'
|
|
: webln
|
|
? 'webln'
|
|
: 'manual';
|
|
|
|
const status: WalletStatus = {
|
|
hasNWC,
|
|
webln,
|
|
activeNWC,
|
|
preferredMethod,
|
|
hasCashu,
|
|
cashuBalance,
|
|
isCashuInitialized,
|
|
};
|
|
|
|
return status;
|
|
} |