Review fixes

This commit is contained in:
Chad Curtis committed 2025-10-05 20:05:33 +00:00
1 parent d462a8d679
commit 45cdcecf9e
3 files changed
+8 -73

No files matched your search

+5 -59
View File
@@ -7,69 +7,21 @@ export interface WalletStatus {
hasNWC: boolean;
webln: WebLNProvider | null;
activeNWC: ReturnType<typeof useNWC>['getActiveConnection'] extends () => infer T ? T : null;
isDetecting: boolean;
preferredMethod: 'nwc' | 'webln' | 'manual';
}
export function useWallet() {
const [webln, setWebln] = useState<WebLNProvider | null>(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<boolean> => {
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;
}