Merge branch 'weblnQuiet' into 'main'

Make webln check less intrusive

See merge request soapbox-pub/mkstack!24
This commit is contained in:
Alex Gleason committed 2025-10-05 23:59:37 +00:00
commit c69e440c04
6 files changed
+49 -121

No files matched your search

+12 -34
View File
@@ -63,7 +63,6 @@
"tailwind-merge": "^2.5.2", "tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7", "tailwindcss-animate": "^1.0.7",
"vaul": "^0.9.3", "vaul": "^0.9.3",
"webln": "^0.3.2",
"zod": "^3.25.71" "zod": "^3.25.71"
}, },
"devDependencies": { "devDependencies": {
@@ -78,6 +77,7 @@
"@types/react": "^18.3.1", "@types/react": "^18.3.1",
"@types/react-dom": "^18.3.1", "@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.5.0", "@vitejs/plugin-react-swc": "^3.5.0",
"@webbtc/webln-types": "^3.0.0",
"autoprefixer": "^10.4.20", "autoprefixer": "^10.4.20",
"eslint": "^9.9.0", "eslint": "^9.9.0",
"eslint-plugin-react-hooks": "^5.1.0-rc.0", "eslint-plugin-react-hooks": "^5.1.0-rc.0",
@@ -3416,15 +3416,6 @@
"license": "MIT", "license": "MIT",
"peer": true "peer": true
}, },
"node_modules/@types/chrome": {
"version": "0.0.74",
"resolved": "https://registry.npmjs.org/@types/chrome/-/chrome-0.0.74.tgz",
"integrity": "sha512-hzosS5CkQcIKCgxcsV2AzbJ36KNxG/Db2YEN/erEu7Boprg+KpMDLBQqKFmSo+JkQMGqRcicUyqCowJpuT+C6A==",
"license": "MIT",
"dependencies": {
"@types/filesystem": "*"
}
},
"node_modules/@types/d3-array": { "node_modules/@types/d3-array": {
"version": "3.2.1", "version": "3.2.1",
"resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.2.1.tgz", "resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.2.1.tgz",
@@ -3494,21 +3485,6 @@
"integrity": "sha512-w28IoSUCJpidD/TGviZwwMJckNESJZXFu7NBZ5YJ4mEUnNraUn9Pm8HSZm/jDF1pDWYKspWE7oVphigUPRakIQ==", "integrity": "sha512-w28IoSUCJpidD/TGviZwwMJckNESJZXFu7NBZ5YJ4mEUnNraUn9Pm8HSZm/jDF1pDWYKspWE7oVphigUPRakIQ==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/filesystem": {
"version": "0.0.36",
"resolved": "https://registry.npmjs.org/@types/filesystem/-/filesystem-0.0.36.tgz",
"integrity": "sha512-vPDXOZuannb9FZdxgHnqSwAG/jvdGM8Wq+6N4D/d80z+D4HWH+bItqsZaVRQykAn6WEVeEkLm2oQigyHtgb0RA==",
"license": "MIT",
"dependencies": {
"@types/filewriter": "*"
}
},
"node_modules/@types/filewriter": {
"version": "0.0.33",
"resolved": "https://registry.npmjs.org/@types/filewriter/-/filewriter-0.0.33.tgz",
"integrity": "sha512-xFU8ZXTw4gd358lb2jw25nxY9QAgqn2+bKKjKOYfNCzN4DKCFetK7sPtrlpg66Ywe3vWY9FNxprZawAh9wfJ3g==",
"license": "MIT"
},
"node_modules/@types/json-schema": { "node_modules/@types/json-schema": {
"version": "7.0.15", "version": "7.0.15",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz",
@@ -3929,6 +3905,17 @@
"url": "https://opencollective.com/vitest" "url": "https://opencollective.com/vitest"
} }
}, },
"node_modules/@webbtc/webln-types": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@webbtc/webln-types/-/webln-types-3.0.0.tgz",
"integrity": "sha512-aXfTHLKz5lysd+6xTeWl+qHNh/p3qVYbeLo+yDN5cUDmhie2ZoGvkppfWxzbGkcFBzb6dJyQ2/i2cbmDHas+zQ==",
"dev": true,
"license": "MIT",
"funding": {
"type": "lightning",
"url": "lightning:[email protected]"
}
},
"node_modules/acorn": { "node_modules/acorn": {
"version": "8.15.0", "version": "8.15.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz",
@@ -8011,15 +7998,6 @@
"node": ">=12" "node": ">=12"
} }
}, },
"node_modules/webln": {
"version": "0.3.2",
"resolved": "https://registry.npmjs.org/webln/-/webln-0.3.2.tgz",
"integrity": "sha512-YYT83aOCLup2AmqvJdKtdeBTaZpjC6/JDMe8o6x1kbTYWwiwrtWHyO//PAsPixF3jwFsAkj5DmiceB6w/QSe7Q==",
"license": "MIT",
"dependencies": {
"@types/chrome": "^0.0.74"
}
},
"node_modules/webpack-virtual-modules": { "node_modules/webpack-virtual-modules": {
"version": "0.6.2", "version": "0.6.2",
"resolved": "https://registry.npmjs.org/webpack-virtual-modules/-/webpack-virtual-modules-0.6.2.tgz", "resolved": "https://registry.npmjs.org/webpack-virtual-modules/-/webpack-virtual-modules-0.6.2.tgz",
+1 -1
View File
@@ -65,7 +65,6 @@
"tailwind-merge": "^2.5.2", "tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7", "tailwindcss-animate": "^1.0.7",
"vaul": "^0.9.3", "vaul": "^0.9.3",
"webln": "^0.3.2",
"zod": "^3.25.71" "zod": "^3.25.71"
}, },
"devDependencies": { "devDependencies": {
@@ -80,6 +79,7 @@
"@types/react": "^18.3.1", "@types/react": "^18.3.1",
"@types/react-dom": "^18.3.1", "@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.5.0", "@vitejs/plugin-react-swc": "^3.5.0",
"@webbtc/webln-types": "^3.0.0",
"autoprefixer": "^10.4.20", "autoprefixer": "^10.4.20",
"eslint": "^9.9.0", "eslint": "^9.9.0",
"eslint-plugin-react-hooks": "^5.1.0-rc.0", "eslint-plugin-react-hooks": "^5.1.0-rc.0",
+9 -11
View File
@@ -29,6 +29,7 @@ import { useWallet } from '@/hooks/useWallet';
import { useToast } from '@/hooks/useToast'; import { useToast } from '@/hooks/useToast';
import { useIsMobile } from '@/hooks/useIsMobile'; import { useIsMobile } from '@/hooks/useIsMobile';
import type { NWCConnection, NWCInfo } from '@/hooks/useNWC'; import type { NWCConnection, NWCInfo } from '@/hooks/useNWC';
import type { WebLNProvider } from "@webbtc/webln-types";
interface WalletModalProps { interface WalletModalProps {
children?: React.ReactNode; children?: React.ReactNode;
@@ -68,8 +69,7 @@ AddWalletContent.displayName = 'AddWalletContent';
// Extracted WalletContent to prevent re-renders // Extracted WalletContent to prevent re-renders
const WalletContent = forwardRef<HTMLDivElement, { const WalletContent = forwardRef<HTMLDivElement, {
hasWebLN: boolean; webln: WebLNProvider | null;
isDetecting: boolean;
hasNWC: boolean; hasNWC: boolean;
connections: NWCConnection[]; connections: NWCConnection[];
connectionInfo: Record<string, NWCInfo>; connectionInfo: Record<string, NWCInfo>;
@@ -78,8 +78,7 @@ const WalletContent = forwardRef<HTMLDivElement, {
handleRemoveConnection: (cs: string) => void; handleRemoveConnection: (cs: string) => void;
setAddDialogOpen: (open: boolean) => void; setAddDialogOpen: (open: boolean) => void;
}>(({ }>(({
hasWebLN, webln,
isDetecting,
hasNWC, hasNWC,
connections, connections,
connectionInfo, connectionInfo,
@@ -103,9 +102,9 @@ const WalletContent = forwardRef<HTMLDivElement, {
</div> </div>
</div> </div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
{hasWebLN && <CheckCircle className="h-4 w-4 text-green-600" />} {webln && <CheckCircle className="h-4 w-4 text-green-600" />}
<Badge variant={hasWebLN ? "default" : "secondary"} className="text-xs"> <Badge variant={webln ? "default" : "secondary"} className="text-xs">
{isDetecting ? "..." : hasWebLN ? "Ready" : "Not Found"} {webln ? "Ready" : "Not Found"}
</Badge> </Badge>
</div> </div>
</div> </div>
@@ -191,7 +190,7 @@ const WalletContent = forwardRef<HTMLDivElement, {
)} )}
</div> </div>
{/* Help */} {/* Help */}
{!hasWebLN && connections.length === 0 && ( {!webln && connections.length === 0 && (
<> <>
<Separator /> <Separator />
<div className="text-center py-4 space-y-2"> <div className="text-center py-4 space-y-2">
@@ -222,7 +221,7 @@ export function WalletModal({ children, className }: WalletModalProps) {
setActiveConnection setActiveConnection
} = useNWC(); } = useNWC();
const { hasWebLN, isDetecting } = useWallet(); const { webln } = useWallet();
const hasNWC = connections.length > 0 && connections.some(c => c.isConnected); const hasNWC = connections.length > 0 && connections.some(c => c.isConnected);
const { toast } = useToast(); const { toast } = useToast();
@@ -263,8 +262,7 @@ export function WalletModal({ children, className }: WalletModalProps) {
}; };
const walletContentProps = { const walletContentProps = {
hasWebLN, webln,
isDetecting,
hasNWC, hasNWC,
connections, connections,
connectionInfo, connectionInfo,
+6 -13
View File
@@ -33,6 +33,7 @@ import { useWallet } from '@/hooks/useWallet';
import { useIsMobile } from '@/hooks/useIsMobile'; import { useIsMobile } from '@/hooks/useIsMobile';
import type { Event } from 'nostr-tools'; import type { Event } from 'nostr-tools';
import QRCode from 'qrcode'; import QRCode from 'qrcode';
import type { WebLNProvider } from "@webbtc/webln-types";
interface ZapDialogProps { interface ZapDialogProps {
target: Event; target: Event;
@@ -55,7 +56,7 @@ interface ZapContentProps {
isZapping: boolean; isZapping: boolean;
qrCodeUrl: string; qrCodeUrl: string;
copied: boolean; copied: boolean;
hasWebLN: boolean; webln: WebLNProvider | null;
handleZap: () => void; handleZap: () => void;
handleCopy: () => void; handleCopy: () => void;
openInWallet: () => void; openInWallet: () => void;
@@ -73,7 +74,7 @@ const ZapContent = forwardRef<HTMLDivElement, ZapContentProps>(({
isZapping, isZapping,
qrCodeUrl, qrCodeUrl,
copied, copied,
hasWebLN, webln,
handleZap, handleZap,
handleCopy, handleCopy,
openInWallet, openInWallet,
@@ -138,7 +139,7 @@ const ZapContent = forwardRef<HTMLDivElement, ZapContentProps>(({
{/* Payment buttons */} {/* Payment buttons */}
<div className="space-y-3 mt-4"> <div className="space-y-3 mt-4">
{hasWebLN && ( {webln && (
<Button <Button
onClick={() => { onClick={() => {
const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount; const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount;
@@ -234,13 +235,12 @@ const ZapContent = forwardRef<HTMLDivElement, ZapContentProps>(({
)); ));
ZapContent.displayName = 'ZapContent'; ZapContent.displayName = 'ZapContent';
export function ZapDialog({ target, children, className }: ZapDialogProps) { export function ZapDialog({ target, children, className }: ZapDialogProps) {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const { user } = useCurrentUser(); const { user } = useCurrentUser();
const { data: author } = useAuthor(target.pubkey); const { data: author } = useAuthor(target.pubkey);
const { toast } = useToast(); const { toast } = useToast();
const { webln, activeNWC, hasWebLN, detectWebLN } = useWallet(); const { webln, activeNWC } = useWallet();
const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, activeNWC, () => setOpen(false)); const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, activeNWC, () => setOpen(false));
const [amount, setAmount] = useState<number | string>(100); const [amount, setAmount] = useState<number | string>(100);
const [comment, setComment] = useState<string>(''); const [comment, setComment] = useState<string>('');
@@ -255,13 +255,6 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) {
} }
}, [target]); }, [target]);
// Detect WebLN when dialog opens
useEffect(() => {
if (open && !hasWebLN) {
detectWebLN();
}
}, [open, hasWebLN, detectWebLN]);
// Generate QR code // Generate QR code
useEffect(() => { useEffect(() => {
let isCancelled = false; let isCancelled = false;
@@ -345,7 +338,7 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) {
isZapping, isZapping,
qrCodeUrl, qrCodeUrl,
copied, copied,
hasWebLN, webln,
handleZap, handleZap,
handleCopy, handleCopy,
openInWallet, openInWallet,
+5 -58
View File
@@ -1,68 +1,22 @@
import { useState, useEffect, useCallback, useMemo } from 'react'; import { useMemo } from 'react';
import { useNWC } from '@/hooks/useNWCContext'; import { useNWC } from '@/hooks/useNWCContext';
import type { WebLNProvider } from 'webln'; import type { WebLNProvider } from '@webbtc/webln-types';
import { requestProvider } from 'webln';
export interface WalletStatus { export interface WalletStatus {
hasWebLN: boolean;
hasNWC: boolean; hasNWC: boolean;
webln: WebLNProvider | null; webln: WebLNProvider | null;
activeNWC: ReturnType<typeof useNWC>['getActiveConnection'] extends () => infer T ? T : null; activeNWC: ReturnType<typeof useNWC>['getActiveConnection'] extends () => infer T ? T : null;
isDetecting: boolean;
preferredMethod: 'nwc' | 'webln' | 'manual'; preferredMethod: 'nwc' | 'webln' | 'manual';
} }
export function useWallet() { export function useWallet() {
const [webln, setWebln] = useState<WebLNProvider | null>(null);
const [isDetecting, setIsDetecting] = useState(false);
const [hasAttemptedDetection, setHasAttemptedDetection] = useState(false);
const { connections, getActiveConnection } = useNWC(); const { connections, getActiveConnection } = useNWC();
// Get the active connection directly - no memoization to avoid stale state // Get the active connection directly - no memoization to avoid stale state
const activeNWC = getActiveConnection(); const activeNWC = getActiveConnection();
// Detect WebLN // Access WebLN directly from browser global scope
const detectWebLN = useCallback(async () => { const webln = (globalThis as { webln?: WebLNProvider }).webln || null;
if (webln || isDetecting) return webln;
setIsDetecting(true);
try {
const provider = await requestProvider();
setWebln(provider);
setHasAttemptedDetection(true);
return 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
useEffect(() => {
if (!hasAttemptedDetection) {
detectWebLN();
}
}, [detectWebLN, hasAttemptedDetection]);
// Test WebLN connection
const testWebLN = useCallback(async (): Promise<boolean> => {
if (!webln) return false;
try {
await webln.enable();
return true;
} catch (error) {
console.error('WebLN test failed:', error);
return false;
}
}, [webln]);
// Calculate status values reactively // Calculate status values reactively
const hasNWC = useMemo(() => { const hasNWC = useMemo(() => {
@@ -77,18 +31,11 @@ export function useWallet() {
: 'manual'; : 'manual';
const status: WalletStatus = { const status: WalletStatus = {
hasWebLN: !!webln,
hasNWC, hasNWC,
webln, webln,
activeNWC, activeNWC,
isDetecting,
preferredMethod, preferredMethod,
}; };
return { return status;
...status,
hasAttemptedDetection,
detectWebLN,
testWebLN,
};
} }
+16 -4
View File
@@ -7,7 +7,7 @@ import { useNWC } from '@/hooks/useNWCContext';
import type { NWCConnection } from '@/hooks/useNWC'; import type { NWCConnection } from '@/hooks/useNWC';
import { nip57 } from 'nostr-tools'; import { nip57 } from 'nostr-tools';
import type { Event } from 'nostr-tools'; import type { Event } from 'nostr-tools';
import type { WebLNProvider } from 'webln'; import type { WebLNProvider } from '@webbtc/webln-types';
import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useNostr } from '@nostrify/react'; import { useNostr } from '@nostrify/react';
import type { NostrEvent } from '@nostrify/nostrify'; import type { NostrEvent } from '@nostrify/nostrify';
@@ -263,10 +263,22 @@ export function useZaps(
}); });
} }
} }
if (webln) { // Try WebLN next if (webln) { // Try WebLN next
try { try {
await webln.sendPayment(newInvoice); // For native WebLN, we may need to enable it first
let webLnProvider = webln;
if (webln.enable && typeof webln.enable === 'function') {
const enabledProvider = await webln.enable();
// Some implementations return the provider, others return void
// Cast to WebLNProvider to handle both cases
const provider = enabledProvider as WebLNProvider | undefined;
if (provider) {
webLnProvider = provider;
}
}
await webLnProvider.sendPayment(newInvoice);
// Clear states immediately on success // Clear states immediately on success
setIsZapping(false); setIsZapping(false);
@@ -283,7 +295,7 @@ export function useZaps(
// Close dialog last to ensure clean state // Close dialog last to ensure clean state
onZapSuccess?.(); onZapSuccess?.();
} catch (weblnError) { } catch (weblnError) {
console.error('webln payment failed, falling back:', weblnError); console.error('WebLN payment failed, falling back:', weblnError);
// Show specific WebLN error to user for debugging // Show specific WebLN error to user for debugging
const errorMessage = weblnError instanceof Error ? weblnError.message : 'Unknown WebLN error'; const errorMessage = weblnError instanceof Error ? weblnError.message : 'Unknown WebLN error';