mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
modular cashu hooks, wallet toggle, qr code modals
This commit is contained in:
1 parent
ec465d9892
commit
22d3675f2b
12 files changed
+1537
-1051
No files matched your search
+363
-316
@@ -3,13 +3,13 @@ import { useCashu, type HistoryEntry } from '@/hooks/useCashu';
|
||||
import { useNpubCash } from '@/hooks/useNpubCash';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import { useLightningAddress } from '@/hooks/useLightningAddress';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger} from '@/components/ui/dropdown-menu';
|
||||
import { Loader2, Wallet, Plus, Send, Download, Bitcoin, RefreshCw, Copy, Check, ChevronDown, History, ArrowUpRight, ArrowDownLeft, Zap, X, Coins, ChevronUp, CreditCard } from 'lucide-react';
|
||||
import { Loader2, Wallet, Plus, Send, Download, Bitcoin, RefreshCw, Copy, Check, ChevronDown, History, ArrowUpRight, ArrowDownLeft, Zap, X, Coins, ChevronUp, QrCode } from 'lucide-react';
|
||||
import QRCode from 'qrcode';
|
||||
|
||||
export function CashuWallet() {
|
||||
@@ -34,7 +34,6 @@ export function CashuWallet() {
|
||||
addMintQuote,
|
||||
refreshBalances,
|
||||
refreshMints,
|
||||
checkAndRedeemPendingQuotes,
|
||||
clearError,
|
||||
getHistory,
|
||||
} = useCashu();
|
||||
@@ -52,6 +51,8 @@ export function CashuWallet() {
|
||||
} = useNpubCash();
|
||||
|
||||
const { getInvoice: getLightningInvoice, isLoading: isResolvingAddress } = useLightningAddress();
|
||||
|
||||
const { toast } = useToast();
|
||||
|
||||
// ============================================================================
|
||||
// STATE - Input Values
|
||||
@@ -70,8 +71,7 @@ export function CashuWallet() {
|
||||
// ============================================================================
|
||||
|
||||
const [showSats, setShowSats] = useState(true);
|
||||
const [receiveMode, setReceiveMode] = useState<'tokens' | 'lightning'>('tokens');
|
||||
const [sendMode, setSendMode] = useState<'tokens' | 'lightning'>('tokens');
|
||||
const [mode, setMode] = useState<'tokens' | 'lightning'>('tokens');
|
||||
const [activeMintUrl, setActiveMintUrl] = useState<string | null>(null);
|
||||
|
||||
// ============================================================================
|
||||
@@ -80,14 +80,6 @@ export function CashuWallet() {
|
||||
|
||||
const [sentToken, setSentToken] = useState<string | null>(null);
|
||||
const [pendingLightningAddress, setPendingLightningAddress] = useState<string | null>(null);
|
||||
const [pendingQuotes, setPendingQuotes] = useLocalStorage<Array<{
|
||||
quoteId: string;
|
||||
mintUrl: string;
|
||||
amount: number;
|
||||
invoice: string;
|
||||
timestamp: number;
|
||||
qrCodeUrl?: string;
|
||||
}>>('cashu-pending-quotes', []);
|
||||
|
||||
// ============================================================================
|
||||
// STATE - Copy Feedback
|
||||
@@ -111,12 +103,24 @@ export function CashuWallet() {
|
||||
denominations: { [key: number]: number };
|
||||
totalBalance: number;
|
||||
} | null>(null);
|
||||
|
||||
const [showLightningQR, setShowLightningQR] = useState(false);
|
||||
const [lightningQRCode, setLightningQRCode] = useState<string>('');
|
||||
|
||||
const [showInvoiceModal, setShowInvoiceModal] = useState(false);
|
||||
const [currentInvoice, setCurrentInvoice] = useState<{
|
||||
quoteId: string;
|
||||
mintUrl: string;
|
||||
amount: number;
|
||||
invoice: string;
|
||||
qrCodeUrl: string;
|
||||
} | null>(null);
|
||||
|
||||
// ============================================================================
|
||||
// COMPUTED VALUES
|
||||
// ============================================================================
|
||||
|
||||
const totalBalance = Object.values(balances).reduce((sum, balance) => sum + balance, 0);
|
||||
const totalBalance: number = (Object.values(balances) as number[]).reduce((sum: number, balance: number) => sum + balance, 0);
|
||||
|
||||
// ============================================================================
|
||||
// HELPER FUNCTIONS - Display
|
||||
@@ -158,6 +162,31 @@ export function CashuWallet() {
|
||||
}
|
||||
};
|
||||
|
||||
// Generate QR code for Lightning address
|
||||
const generateLightningQRCode = async () => {
|
||||
if (!lightningAddress) return;
|
||||
|
||||
try {
|
||||
const qrCodeUrl = await QRCode.toDataURL(lightningAddress, {
|
||||
width: 256,
|
||||
margin: 2,
|
||||
color: {
|
||||
dark: '#000000',
|
||||
light: '#FFFFFF',
|
||||
},
|
||||
});
|
||||
setLightningQRCode(qrCodeUrl);
|
||||
setShowLightningQR(true);
|
||||
} catch (err) {
|
||||
console.error('Failed to generate Lightning address QR code:', err);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "QR Code Failed",
|
||||
description: "Failed to generate QR code for Lightning address",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// HELPER FUNCTIONS - QR Code
|
||||
@@ -267,18 +296,24 @@ export function CashuWallet() {
|
||||
// ============================================================================
|
||||
|
||||
const handleReceive = async () => {
|
||||
if (receiveMode === 'tokens') {
|
||||
if (mode === 'tokens') {
|
||||
if (!tokenInput.trim()) return;
|
||||
try {
|
||||
await receive(tokenInput);
|
||||
setTokenInput('');
|
||||
const message = 'Token received successfully!';
|
||||
console.log('✅', message);
|
||||
alert(message);
|
||||
console.log('✅ Token received successfully!');
|
||||
toast({
|
||||
title: "Token Received",
|
||||
description: "Cashu token redeemed successfully!",
|
||||
});
|
||||
} catch (err) {
|
||||
const errorMessage = `Failed to receive token: ${err instanceof Error ? err.message : 'Unknown error'}`;
|
||||
console.error('❌', errorMessage, err);
|
||||
alert(errorMessage);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Receive Failed",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Lightning mode - create mint quote
|
||||
@@ -286,9 +321,12 @@ export function CashuWallet() {
|
||||
try {
|
||||
const amount = parseInt(quoteAmount);
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
const message = 'Please enter a valid amount';
|
||||
console.warn('⚠️', message);
|
||||
alert(message);
|
||||
console.warn('⚠️ Invalid amount');
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Invalid Amount",
|
||||
description: "Please enter a valid amount",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const quote = await createMintQuote(activeMintUrl, amount);
|
||||
@@ -296,26 +334,32 @@ export function CashuWallet() {
|
||||
// Generate QR code for the Lightning invoice
|
||||
const qrCodeUrl = await generateQRCodeForQuote(quote.request);
|
||||
|
||||
// Save the pending quote with QR code
|
||||
setPendingQuotes(prev => [...prev, {
|
||||
// Set the invoice data and show modal
|
||||
setCurrentInvoice({
|
||||
quoteId: quote.quote,
|
||||
mintUrl: activeMintUrl,
|
||||
amount,
|
||||
invoice: quote.request,
|
||||
timestamp: Date.now(),
|
||||
qrCodeUrl
|
||||
}]);
|
||||
});
|
||||
setShowInvoiceModal(true);
|
||||
|
||||
const message = `Mint quote created! Request: ${quote.request}`;
|
||||
console.log('✅', message);
|
||||
console.log('💰 Quote object:', quote);
|
||||
console.log('✅ Mint quote created:', quote);
|
||||
console.log('🔗 Lightning Invoice:', quote.request);
|
||||
alert('Lightning invoice created! After paying, click the refresh button (🔄) to claim your tokens.');
|
||||
console.log('💡 Coco will automatically redeem when payment is detected');
|
||||
toast({
|
||||
title: "Lightning Invoice Created",
|
||||
description: "Coco will automatically claim your tokens when payment is detected.",
|
||||
});
|
||||
setQuoteAmount('');
|
||||
} catch (err) {
|
||||
const errorMessage = `Failed to create mint quote: ${err instanceof Error ? err.message : 'Unknown error'}`;
|
||||
console.error('❌', errorMessage, err);
|
||||
alert(errorMessage);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Quote Creation Failed",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -325,14 +369,17 @@ export function CashuWallet() {
|
||||
// ============================================================================
|
||||
|
||||
const handleSend = async () => {
|
||||
if (sendMode === 'tokens') {
|
||||
if (mode === 'tokens') {
|
||||
if (!sendAmount || !activeMintUrl) return;
|
||||
try {
|
||||
const amount = parseInt(sendAmount);
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
const message = 'Please enter a valid amount';
|
||||
console.warn('⚠️', message);
|
||||
alert(message);
|
||||
console.warn('⚠️ Invalid amount');
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Invalid Amount",
|
||||
description: "Please enter a valid amount",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const encodedToken = await send(activeMintUrl, amount);
|
||||
@@ -343,7 +390,11 @@ export function CashuWallet() {
|
||||
} catch (err) {
|
||||
const errorMessage = `Failed to send tokens: ${err instanceof Error ? err.message : 'Unknown error'}`;
|
||||
console.error('❌', errorMessage, err);
|
||||
alert(errorMessage);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Send Failed",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Lightning mode
|
||||
@@ -357,7 +408,11 @@ export function CashuWallet() {
|
||||
try {
|
||||
const amount = parseInt(lightningAddressAmount);
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
alert('Please enter a valid amount');
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Invalid Amount",
|
||||
description: "Please enter a valid amount",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -374,19 +429,25 @@ export function CashuWallet() {
|
||||
await payMeltQuote(activeMintUrl, meltQuote.quote);
|
||||
|
||||
console.log('✅ Lightning payment sent successfully!');
|
||||
alert(`Successfully sent ${amount} sats to ${pendingLightningAddress}!`);
|
||||
toast({
|
||||
title: "Payment Sent",
|
||||
description: `Successfully sent ${amount} sats to ${pendingLightningAddress}!`,
|
||||
});
|
||||
|
||||
// Reset state
|
||||
setPendingLightningAddress(null);
|
||||
setLightningAddressInput('');
|
||||
setLightningAddressAmount('');
|
||||
|
||||
// Refresh balances
|
||||
await refreshBalances();
|
||||
// No need to manually refresh balances - events will handle it automatically
|
||||
} catch (err) {
|
||||
const errorMessage = `Failed to pay Lightning address: ${err instanceof Error ? err.message : 'Unknown error'}`;
|
||||
console.error('❌', errorMessage, err);
|
||||
alert(errorMessage);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Payment Failed",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Step 1: Check if input is a Lightning address or invoice
|
||||
@@ -408,15 +469,21 @@ export function CashuWallet() {
|
||||
await payMeltQuote(activeMintUrl, meltQuote.quote);
|
||||
|
||||
console.log('✅ Lightning invoice paid successfully!');
|
||||
alert('Lightning invoice paid successfully! Your tokens have been converted to Lightning payment.');
|
||||
toast({
|
||||
title: "Invoice Paid",
|
||||
description: "Lightning invoice paid successfully! Your tokens have been converted to Lightning payment.",
|
||||
});
|
||||
setLightningInvoice('');
|
||||
|
||||
// Refresh balances to show updated amounts
|
||||
await refreshBalances();
|
||||
// No need to manually refresh balances - events will handle it automatically
|
||||
} catch (err) {
|
||||
const errorMessage = `Failed to pay Lightning invoice: ${err instanceof Error ? err.message : 'Unknown error'}`;
|
||||
console.error('❌', errorMessage, err);
|
||||
alert(errorMessage);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Invoice Payment Failed",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -439,7 +506,11 @@ export function CashuWallet() {
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to copy to clipboard:', err);
|
||||
alert('Failed to copy to clipboard');
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Copy Failed",
|
||||
description: "Failed to copy to clipboard",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -459,17 +530,20 @@ export function CashuWallet() {
|
||||
await addMint(normalizedUrl);
|
||||
setMintUrl('');
|
||||
|
||||
// Explicitly refresh mints and balances
|
||||
await refreshMints();
|
||||
await refreshBalances();
|
||||
|
||||
const message = `Mint added successfully: ${normalizedUrl}`;
|
||||
console.log('✅', message);
|
||||
alert('Mint added successfully!');
|
||||
// No need to manually refresh - events will handle it automatically
|
||||
console.log('✅ Mint added successfully:', normalizedUrl);
|
||||
toast({
|
||||
title: "Mint Added",
|
||||
description: "Mint added successfully!",
|
||||
});
|
||||
} catch (err) {
|
||||
const errorMessage = `Failed to add mint: ${err instanceof Error ? err.message : 'Unknown error'}`;
|
||||
console.error('❌', errorMessage, err);
|
||||
alert(errorMessage);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Add Mint Failed",
|
||||
description: errorMessage,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -477,25 +551,6 @@ export function CashuWallet() {
|
||||
// EVENT HANDLERS - Quote Management
|
||||
// ============================================================================
|
||||
|
||||
const handleClaimQuote = async (quoteId: string, mintUrl: string) => {
|
||||
try {
|
||||
console.log('🎯 Attempting to claim quote:', quoteId, 'from mint:', mintUrl);
|
||||
await redeemMintQuote(mintUrl, quoteId);
|
||||
|
||||
// Remove from pending quotes
|
||||
setPendingQuotes(prev => prev.filter(q => q.quoteId !== quoteId));
|
||||
|
||||
// Refresh balances
|
||||
await refreshBalances();
|
||||
|
||||
console.log('✅ Quote claimed successfully!');
|
||||
alert('Tokens claimed successfully! Your balance has been updated.');
|
||||
} catch (err) {
|
||||
const errorMessage = `Failed to claim tokens: ${err instanceof Error ? err.message : 'Unknown error'}`;
|
||||
console.error('❌', errorMessage, err);
|
||||
alert(errorMessage);
|
||||
}
|
||||
};
|
||||
|
||||
// Claim npub.cash quotes
|
||||
const handleClaimNpubCashQuote = async (quote: typeof quotes[0]) => {
|
||||
@@ -522,17 +577,20 @@ export function CashuWallet() {
|
||||
console.log('✅ Quote added to coco-cashu successfully');
|
||||
console.log('💡 Coco will automatically redeem when mint processor runs');
|
||||
|
||||
// Trigger the mint processor to check and redeem the quote
|
||||
await checkAndRedeemPendingQuotes();
|
||||
|
||||
// Refresh quotes and balances
|
||||
// Refresh quotes (balances will be updated automatically via events)
|
||||
await fetchQuotes();
|
||||
await refreshBalances();
|
||||
|
||||
alert(`Successfully claimed ${quote.amount} sats!`);
|
||||
toast({
|
||||
title: "Quote Claimed",
|
||||
description: `Successfully claimed ${quote.amount} sats!`,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Failed to claim npub.cash quote:', err);
|
||||
alert(`Failed to claim: ${err instanceof Error ? err.message : 'Unknown error'}`);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Claim Failed",
|
||||
description: `Failed to claim: ${err instanceof Error ? err.message : 'Unknown error'}`,
|
||||
});
|
||||
}
|
||||
};
|
||||
const handleShowDenominations = async (mintUrl: string) => {
|
||||
@@ -544,7 +602,11 @@ export function CashuWallet() {
|
||||
console.log('💰 Raw proofs for mint:', proofs);
|
||||
|
||||
if (!proofs || proofs.length === 0) {
|
||||
alert('No tokens found for this mint');
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "No Tokens",
|
||||
description: "No tokens found for this mint",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -568,7 +630,11 @@ export function CashuWallet() {
|
||||
|
||||
} catch (err) {
|
||||
console.error('Failed to get denominations:', err);
|
||||
alert('Failed to get token denominations. Check console for details.');
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Failed to Get Denominations",
|
||||
description: "Failed to get token denominations. Check console for details.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -576,18 +642,39 @@ export function CashuWallet() {
|
||||
// EFFECTS
|
||||
// ============================================================================
|
||||
|
||||
// Auto-select active mint when mints or balances change
|
||||
// Show error toasts when errors occur
|
||||
useEffect(() => {
|
||||
if (error) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Wallet Error",
|
||||
description: error.message,
|
||||
});
|
||||
}
|
||||
}, [error, toast]);
|
||||
|
||||
useEffect(() => {
|
||||
if (npubCashError) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "npub.cash Error",
|
||||
description: npubCashError,
|
||||
action: npubCashError.includes('Authorization') ? (
|
||||
<Button variant="outline" size="sm" onClick={retryAuth}>
|
||||
Retry Auth
|
||||
</Button>
|
||||
) : undefined,
|
||||
});
|
||||
}
|
||||
}, [npubCashError, toast, retryAuth]);
|
||||
|
||||
// Auto-select active mint when mints change (balances will update automatically via events)
|
||||
useEffect(() => {
|
||||
if (mints.length > 0 && !activeMintUrl) {
|
||||
// Find mint with highest balance, or first mint if balances are equal
|
||||
const mintWithHighestBalance = mints.reduce((highest, current) => {
|
||||
const currentBalance = balances[current.url] || 0;
|
||||
const highestBalance = balances[highest.url] || 0;
|
||||
return currentBalance > highestBalance ? current : highest;
|
||||
});
|
||||
handleSetActiveMint(mintWithHighestBalance.url);
|
||||
// Select first mint initially - user can change via dropdown
|
||||
handleSetActiveMint(mints[0].url);
|
||||
}
|
||||
}, [mints, activeMintUrl, balances, handleSetActiveMint]);
|
||||
}, [mints, activeMintUrl, handleSetActiveMint]);
|
||||
|
||||
// Load transaction history when wallet is initialized
|
||||
useEffect(() => {
|
||||
@@ -596,27 +683,6 @@ export function CashuWallet() {
|
||||
}
|
||||
}, [isInitialized, historyEntries.length, loadHistory]);
|
||||
|
||||
// Generate QR codes for existing pending quotes that don't have them
|
||||
useEffect(() => {
|
||||
const generateMissingQRCodes = async () => {
|
||||
const quotesNeedingQR = pendingQuotes.filter(quote => !quote.qrCodeUrl);
|
||||
if (quotesNeedingQR.length === 0) return;
|
||||
|
||||
const updatedQuotes = await Promise.all(
|
||||
pendingQuotes.map(async (quote) => {
|
||||
if (!quote.qrCodeUrl) {
|
||||
const qrCodeUrl = await generateQRCodeForQuote(quote.invoice);
|
||||
return { ...quote, qrCodeUrl };
|
||||
}
|
||||
return quote;
|
||||
})
|
||||
);
|
||||
|
||||
setPendingQuotes(updatedQuotes);
|
||||
};
|
||||
|
||||
generateMissingQRCodes();
|
||||
}, []); // Only run once on mount
|
||||
|
||||
// ============================================================================
|
||||
// RENDER - Loading State
|
||||
@@ -680,18 +746,41 @@ export function CashuWallet() {
|
||||
size="icon"
|
||||
onClick={async () => {
|
||||
console.log('🔄 Manual refresh triggered');
|
||||
await checkAndRedeemPendingQuotes();
|
||||
await refreshBalances();
|
||||
await loadHistory();
|
||||
await refreshNpubCash();
|
||||
console.log('💰 Current balances after manual refresh:', balances);
|
||||
console.log('💰 Current balances (auto-updated via events):', balances);
|
||||
}}
|
||||
className="mt-2"
|
||||
title="Check for paid invoices and refresh balances and history"
|
||||
title="Refresh history and npub.cash data"
|
||||
>
|
||||
<RefreshCw className="h-5 w-5" />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Main Mode Toggle */}
|
||||
<div className="mt-4 flex items-center justify-center">
|
||||
<div className="flex gap-1 bg-muted rounded-lg p-1">
|
||||
<button
|
||||
onClick={() => setMode('tokens')}
|
||||
className={`px-4 py-2 rounded text-sm transition-colors flex items-center gap-2 ${
|
||||
mode === 'tokens' ? 'bg-background shadow-sm' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
>
|
||||
<Coins className="h-4 w-4" />
|
||||
Cashu
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setMode('lightning')}
|
||||
className={`px-4 py-2 rounded text-sm transition-colors flex items-center gap-2 ${
|
||||
mode === 'lightning' ? 'bg-background shadow-sm' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
>
|
||||
<Zap className="h-4 w-4" />
|
||||
Lightning
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Active Mint Dropdown and Balance */}
|
||||
<div className="mt-3 flex items-center justify-center gap-4">
|
||||
<DropdownMenu>
|
||||
@@ -753,82 +842,49 @@ export function CashuWallet() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* npub.cash Lightning Address */}
|
||||
{/* Lightning Address */}
|
||||
{lightningAddress && (
|
||||
<div className="mt-4 p-4 bg-muted/30 rounded-lg">
|
||||
<div className="flex items-center justify-center gap-2 mb-2">
|
||||
<CreditCard className="h-4 w-4 text-muted-foreground" />
|
||||
<span className="text-sm font-medium text-muted-foreground">Your Lightning Address</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
<code className="bg-background px-3 py-2 rounded text-sm font-mono border">
|
||||
{lightningAddress}
|
||||
</code>
|
||||
<div className="mt-3 flex items-center justify-center gap-2">
|
||||
<code className="bg-background px-3 py-2 rounded text-sm font-mono border">
|
||||
{lightningAddress}
|
||||
</code>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={copyLightningAddress}
|
||||
className="h-8"
|
||||
>
|
||||
{copiedLightningAddress ? (
|
||||
<Check className="h-3 w-3" />
|
||||
) : (
|
||||
<Copy className="h-3 w-3" />
|
||||
)}
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={generateLightningQRCode}
|
||||
className="h-8"
|
||||
>
|
||||
<QrCode className="h-3 w-3" />
|
||||
</Button>
|
||||
{unclaimedAmount > 0 && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={copyLightningAddress}
|
||||
className="h-8"
|
||||
onClick={refreshNpubCash}
|
||||
disabled={isNpubCashLoading}
|
||||
className="h-8 text-xs"
|
||||
>
|
||||
{copiedLightningAddress ? (
|
||||
<Check className="h-3 w-3" />
|
||||
) : (
|
||||
<Copy className="h-3 w-3" />
|
||||
)}
|
||||
<RefreshCw className={`h-3 w-3 mr-1 ${isNpubCashLoading ? 'animate-spin' : ''}`} />
|
||||
{unclaimedAmount} sats
|
||||
</Button>
|
||||
</div>
|
||||
{unclaimedAmount > 0 && (
|
||||
<div className="mt-2 flex items-center justify-center gap-2">
|
||||
<Badge variant="secondary" className="text-sm">
|
||||
{unclaimedAmount} sats unclaimed
|
||||
</Badge>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={refreshNpubCash}
|
||||
disabled={isNpubCashLoading}
|
||||
className="h-6 text-xs"
|
||||
>
|
||||
<RefreshCw className={`h-3 w-3 mr-1 ${isNpubCashLoading ? 'animate-spin' : ''}`} />
|
||||
Refresh
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* ========================================================================
|
||||
SECTION: Error Display
|
||||
======================================================================== */}
|
||||
{(error || npubCashError) && (
|
||||
<Alert variant="destructive" className="mb-6">
|
||||
<AlertDescription className="flex items-center justify-between">
|
||||
<div className="flex-1">
|
||||
{error || npubCashError}
|
||||
{npubCashError && npubCashError.includes('Authorization') && (
|
||||
<div className="text-xs mt-1 opacity-75">
|
||||
Try refreshing the page or check your Nostr extension
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{npubCashError && (
|
||||
<Button variant="outline" size="sm" onClick={retryAuth}>
|
||||
Retry Auth
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="outline" size="sm" onClick={() => {
|
||||
if (error) clearError();
|
||||
// npub.cash errors are handled by retryAuth
|
||||
}}>
|
||||
Dismiss
|
||||
</Button>
|
||||
</div>
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{/* ========================================================================
|
||||
SECTION: Receive & Send Cards
|
||||
@@ -838,39 +894,19 @@ export function CashuWallet() {
|
||||
{/* Receive Card */}
|
||||
<Card className="border-0 shadow-none bg-muted/30">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
<Download className="h-5 w-5 mr-2" />
|
||||
Receive
|
||||
</div>
|
||||
<div className="flex gap-1 bg-muted rounded-lg p-1">
|
||||
<button
|
||||
onClick={() => setReceiveMode('tokens')}
|
||||
className={`px-3 py-1 rounded text-sm transition-colors flex items-center gap-1 ${
|
||||
receiveMode === 'tokens' ? 'bg-background shadow-sm' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
>
|
||||
<Coins className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setReceiveMode('lightning')}
|
||||
className={`px-3 py-1 rounded text-sm transition-colors flex items-center gap-1 ${
|
||||
receiveMode === 'lightning' ? 'bg-background shadow-sm' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
>
|
||||
<Zap className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
<CardTitle className="flex items-center">
|
||||
<Download className="h-5 w-5 mr-2" />
|
||||
Receive
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
{receiveMode === 'tokens'
|
||||
{mode === 'tokens'
|
||||
? 'Enter Cashu token to redeem'
|
||||
: 'Deposit funds with Lightning'
|
||||
}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{receiveMode === 'tokens' ? (
|
||||
{mode === 'tokens' ? (
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
placeholder="cashuBo2Ft..."
|
||||
@@ -910,32 +946,12 @@ export function CashuWallet() {
|
||||
{/* Send Card */}
|
||||
<Card className="border-0 shadow-none bg-muted/30">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
<Send className="h-5 w-5 mr-2" />
|
||||
Send
|
||||
</div>
|
||||
<div className="flex gap-1 bg-muted rounded-lg p-1">
|
||||
<button
|
||||
onClick={() => setSendMode('tokens')}
|
||||
className={`px-3 py-1 rounded text-sm transition-colors flex items-center gap-1 ${
|
||||
sendMode === 'tokens' ? 'bg-background shadow-sm' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
>
|
||||
<Coins className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setSendMode('lightning')}
|
||||
className={`px-3 py-1 rounded text-sm transition-colors flex items-center gap-1 ${
|
||||
sendMode === 'lightning' ? 'bg-background shadow-sm' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
>
|
||||
<Zap className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
<CardTitle className="flex items-center">
|
||||
<Send className="h-5 w-5 mr-2" />
|
||||
Send
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
{sendMode === 'tokens'
|
||||
{mode === 'tokens'
|
||||
? 'Generate Cashu token to send'
|
||||
: pendingLightningAddress
|
||||
? 'Enter the amount to send'
|
||||
@@ -944,7 +960,7 @@ export function CashuWallet() {
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{sendMode === 'tokens' ? (
|
||||
{mode === 'tokens' ? (
|
||||
<>
|
||||
{!activeMintUrl && (
|
||||
<div className="text-sm text-muted-foreground bg-muted p-3 rounded">
|
||||
@@ -1102,91 +1118,6 @@ export function CashuWallet() {
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* ========================================================================
|
||||
SECTION: Pending Lightning Payments
|
||||
======================================================================== */}
|
||||
{pendingQuotes.length > 0 && (
|
||||
<Card className="mt-6">
|
||||
<CardHeader className="relative">
|
||||
<Button
|
||||
onClick={() => setPendingQuotes([])}
|
||||
className="absolute top-2 right-2"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
<CardTitle className="flex items-center pr-8">
|
||||
<Bitcoin className="h-5 w-5 mr-2" />
|
||||
Pending Lightning Payments
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Click "Check Payment" after paying the Lightning invoice
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{pendingQuotes.map((quote) => (
|
||||
<div key={quote.quoteId} className="border rounded-lg p-4 space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="font-medium">{quote.amount} sats</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
{new URL(quote.mintUrl).hostname}
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground mt-1">
|
||||
Created: {new Date(quote.timestamp).toLocaleTimeString()}
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
onClick={() => handleClaimQuote(quote.quoteId, quote.mintUrl)}
|
||||
size="sm"
|
||||
>
|
||||
Check Payment
|
||||
</Button>
|
||||
</div>
|
||||
{/* QR Code */}
|
||||
{quote.qrCodeUrl && (
|
||||
<div className="flex justify-center mb-3">
|
||||
<div className="bg-white dark:bg-gray-100 p-3 rounded-lg border shadow-sm">
|
||||
<img
|
||||
src={quote.qrCodeUrl}
|
||||
alt="Lightning Invoice QR Code"
|
||||
className="w-28 h-28 sm:w-32 sm:h-32 object-contain"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="relative">
|
||||
<div className="bg-muted p-4 rounded-lg border font-mono text-sm">
|
||||
{quote.invoice.length > 20 ? `${quote.invoice.substring(0, 20)}...` : quote.invoice}
|
||||
</div>
|
||||
<Button
|
||||
onClick={() => handleCopy(quote.invoice, 'invoice')}
|
||||
className="absolute top-2 right-2"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
>
|
||||
{copiedInvoice === quote.invoice ? (
|
||||
<>
|
||||
<Check className="h-4 w-4 mr-2" />
|
||||
Copied!
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Copy className="h-4 w-4 mr-2" />
|
||||
Copy Invoice
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* ========================================================================
|
||||
SECTION: npub.cash Received Payments
|
||||
@@ -1196,7 +1127,7 @@ export function CashuWallet() {
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center justify-between">
|
||||
<div className="flex items-center">
|
||||
<CreditCard className="h-5 w-5 mr-2" />
|
||||
<Zap className="h-5 w-5 mr-2" />
|
||||
Received Payments (npub.cash)
|
||||
</div>
|
||||
<Button
|
||||
@@ -1420,6 +1351,122 @@ export function CashuWallet() {
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* ========================================================================
|
||||
SECTION: Lightning Address QR Code Modal
|
||||
======================================================================== */}
|
||||
{showLightningQR && lightningQRCode && (
|
||||
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
|
||||
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
|
||||
<CardHeader className="relative">
|
||||
<Button
|
||||
onClick={() => setShowLightningQR(false)}
|
||||
className="absolute top-2 right-2"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
<CardTitle className="flex items-center pr-8">
|
||||
<QrCode className="h-5 w-5 mr-2" />
|
||||
Lightning Address QR Code
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Scan to send Lightning payments
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex justify-center">
|
||||
<div className="bg-white dark:bg-gray-100 p-4 rounded-lg border shadow-sm">
|
||||
<img
|
||||
src={lightningQRCode}
|
||||
alt="Lightning Address QR Code"
|
||||
className="w-64 h-64 object-contain"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="text-center">
|
||||
<div className="bg-muted p-3 rounded-lg border font-mono text-sm break-all">
|
||||
{lightningAddress}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="text-xs text-muted-foreground bg-muted/30 p-3 rounded text-center">
|
||||
💡 Share this QR code or address to receive Lightning payments
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* ========================================================================
|
||||
SECTION: Lightning Invoice Modal
|
||||
======================================================================== */}
|
||||
{showInvoiceModal && currentInvoice && (
|
||||
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
|
||||
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
|
||||
<CardHeader className="relative">
|
||||
<Button
|
||||
onClick={() => setShowInvoiceModal(false)}
|
||||
className="absolute top-2 right-2"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
<CardTitle className="flex items-center pr-8">
|
||||
<Zap className="h-5 w-5 mr-2" />
|
||||
Lightning Invoice
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Pay this invoice to receive {currentInvoice.amount} sats
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex justify-center">
|
||||
<div className="bg-white dark:bg-gray-100 p-4 rounded-lg border shadow-sm">
|
||||
<img
|
||||
src={currentInvoice.qrCodeUrl}
|
||||
alt="Lightning Invoice QR Code"
|
||||
className="w-64 h-64 object-contain"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="text-center">
|
||||
<div className="bg-muted p-3 rounded-lg border font-mono text-sm break-all">
|
||||
{currentInvoice.invoice}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2 justify-center">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => handleCopy(currentInvoice.invoice, 'invoice')}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
{copiedInvoice === currentInvoice.invoice ? (
|
||||
<>
|
||||
<Check className="h-4 w-4" />
|
||||
Copied!
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Copy className="h-4 w-4" />
|
||||
Copy Invoice
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="text-xs text-muted-foreground bg-muted/30 p-3 rounded text-center">
|
||||
💡 Coco will automatically claim your tokens when payment is detected
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user