From 45cdcecf9ecfca44150361051f10ff66a31bbac6 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sun, 5 Oct 2025 19:49:46 +0000 Subject: [PATCH] Review fixes --- src/components/WalletModal.tsx | 7 ++-- src/components/ZapDialog.tsx | 10 +----- src/hooks/useWallet.ts | 64 +++------------------------------- 3 files changed, 8 insertions(+), 73 deletions(-) diff --git a/src/components/WalletModal.tsx b/src/components/WalletModal.tsx index 7643ea5..c8f26dc 100644 --- a/src/components/WalletModal.tsx +++ b/src/components/WalletModal.tsx @@ -69,7 +69,6 @@ AddWalletContent.displayName = 'AddWalletContent'; // Extracted WalletContent to prevent re-renders const WalletContent = forwardRef; @@ -79,7 +78,6 @@ const WalletContent = forwardRef void; }>(({ hasWebLN, - isDetecting, hasNWC, connections, connectionInfo, @@ -105,7 +103,7 @@ const WalletContent = forwardRef {hasWebLN && } - {isDetecting ? "..." : hasWebLN ? "Ready" : "Not Found"} + {hasWebLN ? "Ready" : "Not Found"} @@ -222,7 +220,7 @@ export function WalletModal({ children, className }: WalletModalProps) { setActiveConnection } = useNWC(); - const { hasWebLN, isDetecting } = useWallet(); + const { hasWebLN } = useWallet(); const hasNWC = connections.length > 0 && connections.some(c => c.isConnected); const { toast } = useToast(); @@ -264,7 +262,6 @@ export function WalletModal({ children, className }: WalletModalProps) { const walletContentProps = { hasWebLN, - isDetecting, hasNWC, connections, connectionInfo, diff --git a/src/components/ZapDialog.tsx b/src/components/ZapDialog.tsx index 39e4207..a7b2481 100644 --- a/src/components/ZapDialog.tsx +++ b/src/components/ZapDialog.tsx @@ -234,13 +234,12 @@ const ZapContent = forwardRef(({ )); ZapContent.displayName = 'ZapContent'; - export function ZapDialog({ target, children, className }: ZapDialogProps) { const [open, setOpen] = useState(false); const { user } = useCurrentUser(); const { data: author } = useAuthor(target.pubkey); const { toast } = useToast(); - const { webln, activeNWC, hasWebLN, detectWebLN } = useWallet(); + const { webln, activeNWC, hasWebLN } = useWallet(); const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, activeNWC, () => setOpen(false)); const [amount, setAmount] = useState(100); const [comment, setComment] = useState(''); @@ -255,13 +254,6 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) { } }, [target]); - // Detect WebLN when dialog opens - useEffect(() => { - if (open && !hasWebLN) { - detectWebLN(); - } - }, [open, hasWebLN, detectWebLN]); - // Generate QR code useEffect(() => { let isCancelled = false; diff --git a/src/hooks/useWallet.ts b/src/hooks/useWallet.ts index b1514da..25448e5 100644 --- a/src/hooks/useWallet.ts +++ b/src/hooks/useWallet.ts @@ -7,69 +7,21 @@ export interface WalletStatus { hasNWC: boolean; webln: WebLNProvider | null; activeNWC: ReturnType['getActiveConnection'] extends () => infer T ? T : null; - isDetecting: boolean; preferredMethod: 'nwc' | 'webln' | 'manual'; } export function useWallet() { const [webln, setWebln] = useState(null); - const [isDetecting, setIsDetecting] = useState(false); - const [hasAttemptedDetection, setHasAttemptedDetection] = useState(false); const { connections, getActiveConnection } = useNWC(); // Get the active connection directly - no memoization to avoid stale state const activeNWC = getActiveConnection(); - // Detect WebLN - const detectWebLN = useCallback(async () => { - if (webln || isDetecting) return webln; - - setIsDetecting(true); - try { - // Check if native WebLN is available without enabling it yet - const webLn = (window as Window & { webln?: WebLNProvider }).webln; - if (webLn) { - setWebln(webLn); - setHasAttemptedDetection(true); - return webLn; - } else { - throw new Error('no WebLN provider'); - } - } catch (error) { - // Only log the error if it's not the common "no provider" error - if (error instanceof Error && !error.message.includes('no WebLN provider')) { - console.warn('WebLN detection error:', error); - } - setWebln(null); - setHasAttemptedDetection(true); - return null; - } finally { - setIsDetecting(false); - } - }, [webln, isDetecting]); - - // Only auto-detect once on mount + // Detect WebLN synchronously on mount useEffect(() => { - if (!hasAttemptedDetection) { - detectWebLN(); - } - }, [detectWebLN, hasAttemptedDetection]); - - // Test WebLN connection - const testWebLN = useCallback(async (): Promise => { - if (!webln) return false; - - try { - // Test the connection with a getInfo call if available, otherwise just check if provider exists - if (webln.getInfo) { - await webln.getInfo(); - } - return true; - } catch (error) { - console.error('WebLN test failed:', error); - return false; - } - }, [webln]); + const webLn = (globalThis as { webln?: WebLNProvider }).webln; + setWebln(webLn || null); + }, []); // Calculate status values reactively const hasNWC = useMemo(() => { @@ -88,14 +40,8 @@ export function useWallet() { hasNWC, webln, activeNWC, - isDetecting, preferredMethod, }; - return { - ...status, - hasAttemptedDetection, - detectWebLN, - testWebLN, - }; + return status; } \ No newline at end of file