mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
modals and mint removal
This commit is contained in:
1 parent
22d3675f2b
commit
a8faa03b49
4 files changed
+368
-165
No files matched your search
+198
-157
@@ -1,7 +1,6 @@
|
|||||||
import { useState, useEffect, useCallback } from 'react';
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
import { useCashu, type HistoryEntry } from '@/hooks/useCashu';
|
import { useCashu, type HistoryEntry } from '@/hooks/useCashu';
|
||||||
import { useNpubCash } from '@/hooks/useNpubCash';
|
import { useNpubCash } from '@/hooks/useNpubCash';
|
||||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
|
||||||
import { useLightningAddress } from '@/hooks/useLightningAddress';
|
import { useLightningAddress } from '@/hooks/useLightningAddress';
|
||||||
import { useToast } from '@/hooks/useToast';
|
import { useToast } from '@/hooks/useToast';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
@@ -9,7 +8,8 @@ import { Input } from '@/components/ui/input';
|
|||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
import { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger} from '@/components/ui/dropdown-menu';
|
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, QrCode } from 'lucide-react';
|
import { QRModal } from '@/components/ui/qr-modal';
|
||||||
|
import { Loader2, Wallet, Plus, Send, Download, Bitcoin, RefreshCw, Copy, Check, ChevronDown, History, ArrowUpRight, ArrowDownLeft, Zap, X, Coins, ChevronUp, QrCode, AlertTriangle } from 'lucide-react';
|
||||||
import QRCode from 'qrcode';
|
import QRCode from 'qrcode';
|
||||||
|
|
||||||
export function CashuWallet() {
|
export function CashuWallet() {
|
||||||
@@ -27,6 +27,7 @@ export function CashuWallet() {
|
|||||||
send,
|
send,
|
||||||
getProofs,
|
getProofs,
|
||||||
addMint,
|
addMint,
|
||||||
|
removeMint,
|
||||||
createMintQuote,
|
createMintQuote,
|
||||||
createMeltQuote,
|
createMeltQuote,
|
||||||
payMeltQuote,
|
payMeltQuote,
|
||||||
@@ -115,6 +116,12 @@ export function CashuWallet() {
|
|||||||
invoice: string;
|
invoice: string;
|
||||||
qrCodeUrl: string;
|
qrCodeUrl: string;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
|
|
||||||
|
const [showRemoveMintModal, setShowRemoveMintModal] = useState(false);
|
||||||
|
const [mintToRemove, setMintToRemove] = useState<{
|
||||||
|
url: string;
|
||||||
|
name?: string;
|
||||||
|
} | null>(null);
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// COMPUTED VALUES
|
// COMPUTED VALUES
|
||||||
@@ -162,39 +169,13 @@ 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
|
// HELPER FUNCTIONS - QR Code Generation
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
const generateQRCodeForQuote = async (invoice: string): Promise<string> => {
|
const generateQRCode = async (text: string): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
return await QRCode.toDataURL(invoice, {
|
return await QRCode.toDataURL(text, {
|
||||||
width: 256,
|
width: 256,
|
||||||
margin: 2,
|
margin: 2,
|
||||||
color: {
|
color: {
|
||||||
@@ -204,10 +185,28 @@ export function CashuWallet() {
|
|||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to generate QR code:', err);
|
console.error('Failed to generate QR code:', err);
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: "QR Code Failed",
|
||||||
|
description: "Failed to generate QR code",
|
||||||
|
});
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Generate QR code for Lightning address
|
||||||
|
const generateLightningQRCode = async () => {
|
||||||
|
if (!lightningAddress) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const qrCodeUrl = await generateQRCode(lightningAddress);
|
||||||
|
setLightningQRCode(qrCodeUrl);
|
||||||
|
setShowLightningQR(true);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to generate Lightning address QR code:', err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// HELPER FUNCTIONS - Mint Management
|
// HELPER FUNCTIONS - Mint Management
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -332,7 +331,7 @@ export function CashuWallet() {
|
|||||||
const quote = await createMintQuote(activeMintUrl, amount);
|
const quote = await createMintQuote(activeMintUrl, amount);
|
||||||
|
|
||||||
// Generate QR code for the Lightning invoice
|
// Generate QR code for the Lightning invoice
|
||||||
const qrCodeUrl = await generateQRCodeForQuote(quote.request);
|
const qrCodeUrl = await generateQRCode(quote.request);
|
||||||
|
|
||||||
// Set the invoice data and show modal
|
// Set the invoice data and show modal
|
||||||
setCurrentInvoice({
|
setCurrentInvoice({
|
||||||
@@ -514,6 +513,47 @@ export function CashuWallet() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleRemoveMintClick = useCallback((mint: { url: string; name?: string }) => {
|
||||||
|
setMintToRemove(mint);
|
||||||
|
setShowRemoveMintModal(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleConfirmRemoveMint = useCallback(async () => {
|
||||||
|
if (!mintToRemove) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await removeMint(mintToRemove.url);
|
||||||
|
|
||||||
|
// Manually refresh mints since there's no mint:removed event
|
||||||
|
await refreshMints();
|
||||||
|
|
||||||
|
// If the removed mint was the active mint, select the first available mint
|
||||||
|
if (activeMintUrl === mintToRemove.url) {
|
||||||
|
const remainingMints = mints.filter(m => m.url !== mintToRemove.url);
|
||||||
|
if (remainingMints.length > 0) {
|
||||||
|
setActiveMintUrl(remainingMints[0].url);
|
||||||
|
} else {
|
||||||
|
setActiveMintUrl(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: "Mint Removed",
|
||||||
|
description: `${mintToRemove.name || new URL(mintToRemove.url).hostname} has been removed from your wallet`,
|
||||||
|
});
|
||||||
|
|
||||||
|
setShowRemoveMintModal(false);
|
||||||
|
setMintToRemove(null);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to remove mint:', err);
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: "Remove Failed",
|
||||||
|
description: `Failed to remove mint: ${err instanceof Error ? err.message : 'Unknown error'}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [mintToRemove, removeMint, refreshMints, activeMintUrl, mints, toast]);
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// EVENT HANDLERS - Mint Management
|
// EVENT HANDLERS - Mint Management
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -685,48 +725,57 @@ export function CashuWallet() {
|
|||||||
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// RENDER - Loading State
|
// RENDER - Loading Modal
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
if (isLoading) {
|
const LoadingModal = () => {
|
||||||
|
if (!isLoading) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center justify-center min-h-screen">
|
<div className="fixed inset-0 bg-background/80 backdrop-blur-sm flex items-center justify-center z-[200]">
|
||||||
<Card className="border-0 shadow-none bg-muted/30">
|
<Card className="border-0 shadow-lg bg-background">
|
||||||
<CardContent className="flex items-center justify-center p-8">
|
<CardContent className="flex items-center justify-center p-8">
|
||||||
<Loader2 className="h-8 w-8 animate-spin mr-2" />
|
<Loader2 className="h-8 w-8 animate-spin mr-3" />
|
||||||
<span>Initializing...</span>
|
<span className="text-lg font-medium">Loading</span>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// RENDER - Uninitialized State
|
// RENDER - Uninitialized State Modal
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
if (!isInitialized) {
|
const UninitializedModal = () => {
|
||||||
|
if (isInitialized) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="w-full max-w-2xl mx-auto">
|
<div className="fixed inset-0 bg-background/80 backdrop-blur-sm flex items-center justify-center z-[200]">
|
||||||
<CardHeader>
|
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
|
||||||
<CardTitle className="flex items-center">
|
<CardHeader>
|
||||||
<Wallet className="h-5 w-5 mr-2" />
|
<CardTitle className="flex items-center">
|
||||||
Cashu Wallet
|
<Wallet className="h-5 w-5 mr-2" />
|
||||||
</CardTitle>
|
Cashu Wallet
|
||||||
<CardDescription>
|
</CardTitle>
|
||||||
Failed to initialize wallet. Please check the console for errors.
|
<CardDescription>
|
||||||
</CardDescription>
|
Failed to initialize wallet. Please check the console for errors.
|
||||||
</CardHeader>
|
</CardDescription>
|
||||||
</Card>
|
</CardHeader>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// RENDER - Main UI
|
// RENDER - Main UI
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container mx-auto py-1 px-3 sm:px-4">
|
<>
|
||||||
|
<LoadingModal />
|
||||||
|
<UninitializedModal />
|
||||||
|
<div className="container mx-auto py-1 px-3 sm:px-4">
|
||||||
|
|
||||||
{/* ========================================================================
|
{/* ========================================================================
|
||||||
SECTION: Balance Display
|
SECTION: Balance Display
|
||||||
@@ -792,8 +841,6 @@ export function CashuWallet() {
|
|||||||
</Button>
|
</Button>
|
||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
<DropdownMenuContent align="center" className="w-72">
|
<DropdownMenuContent align="center" className="w-72">
|
||||||
<DropdownMenuLabel>Connected Mints</DropdownMenuLabel>
|
|
||||||
<DropdownMenuSeparator />
|
|
||||||
{mints.length === 0 && (
|
{mints.length === 0 && (
|
||||||
<DropdownMenuItem disabled>No mints connected</DropdownMenuItem>
|
<DropdownMenuItem disabled>No mints connected</DropdownMenuItem>
|
||||||
)}
|
)}
|
||||||
@@ -808,6 +855,17 @@ export function CashuWallet() {
|
|||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<span className="font-medium">{getCleanMintLabel(mint)}</span>
|
<span className="font-medium">{getCleanMintLabel(mint)}</span>
|
||||||
</div>
|
</div>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
className="h-6 w-6 p-0 hover:bg-destructive hover:text-destructive-foreground"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
handleRemoveMintClick(mint);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<X className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
))}
|
))}
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
@@ -845,21 +903,21 @@ export function CashuWallet() {
|
|||||||
{/* Lightning Address */}
|
{/* Lightning Address */}
|
||||||
{lightningAddress && (
|
{lightningAddress && (
|
||||||
<div className="mt-3 flex items-center justify-center gap-2">
|
<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">
|
<code className="bg-background px-3 py-2 rounded text-sm font-mono border">
|
||||||
{lightningAddress}
|
{lightningAddress}
|
||||||
</code>
|
</code>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={copyLightningAddress}
|
onClick={copyLightningAddress}
|
||||||
className="h-8"
|
className="h-8"
|
||||||
>
|
>
|
||||||
{copiedLightningAddress ? (
|
{copiedLightningAddress ? (
|
||||||
<Check className="h-3 w-3" />
|
<Check className="h-3 w-3" />
|
||||||
) : (
|
) : (
|
||||||
<Copy className="h-3 w-3" />
|
<Copy className="h-3 w-3" />
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
@@ -869,16 +927,16 @@ export function CashuWallet() {
|
|||||||
<QrCode className="h-3 w-3" />
|
<QrCode className="h-3 w-3" />
|
||||||
</Button>
|
</Button>
|
||||||
{unclaimedAmount > 0 && (
|
{unclaimedAmount > 0 && (
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={refreshNpubCash}
|
onClick={refreshNpubCash}
|
||||||
disabled={isNpubCashLoading}
|
disabled={isNpubCashLoading}
|
||||||
className="h-8 text-xs"
|
className="h-8 text-xs"
|
||||||
>
|
>
|
||||||
<RefreshCw className={`h-3 w-3 mr-1 ${isNpubCashLoading ? 'animate-spin' : ''}`} />
|
<RefreshCw className={`h-3 w-3 mr-1 ${isNpubCashLoading ? 'animate-spin' : ''}`} />
|
||||||
{unclaimedAmount} sats
|
{unclaimedAmount} sats
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -895,8 +953,8 @@ export function CashuWallet() {
|
|||||||
<Card className="border-0 shadow-none bg-muted/30">
|
<Card className="border-0 shadow-none bg-muted/30">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="flex items-center">
|
<CardTitle className="flex items-center">
|
||||||
<Download className="h-5 w-5 mr-2" />
|
<Download className="h-5 w-5 mr-2" />
|
||||||
Receive
|
Receive
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
{mode === 'tokens'
|
{mode === 'tokens'
|
||||||
@@ -947,8 +1005,8 @@ export function CashuWallet() {
|
|||||||
<Card className="border-0 shadow-none bg-muted/30">
|
<Card className="border-0 shadow-none bg-muted/30">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="flex items-center">
|
<CardTitle className="flex items-center">
|
||||||
<Send className="h-5 w-5 mr-2" />
|
<Send className="h-5 w-5 mr-2" />
|
||||||
Send
|
Send
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
{mode === 'tokens'
|
{mode === 'tokens'
|
||||||
@@ -1355,60 +1413,44 @@ export function CashuWallet() {
|
|||||||
{/* ========================================================================
|
{/* ========================================================================
|
||||||
SECTION: Lightning Address QR Code Modal
|
SECTION: Lightning Address QR Code Modal
|
||||||
======================================================================== */}
|
======================================================================== */}
|
||||||
{showLightningQR && lightningQRCode && (
|
<QRModal
|
||||||
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
|
isOpen={showLightningQR}
|
||||||
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
|
onClose={() => setShowLightningQR(false)}
|
||||||
<CardHeader className="relative">
|
title="Lightning Address QR Code"
|
||||||
<Button
|
description="Scan to send Lightning payments"
|
||||||
onClick={() => setShowLightningQR(false)}
|
qrCodeUrl={lightningQRCode}
|
||||||
className="absolute top-2 right-2"
|
content={lightningAddress || ''}
|
||||||
size="sm"
|
icon="qr"
|
||||||
variant="ghost"
|
helpText="💡 Share this QR code or address to receive Lightning payments"
|
||||||
>
|
/>
|
||||||
<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
|
SECTION: Lightning Invoice Modal
|
||||||
======================================================================== */}
|
======================================================================== */}
|
||||||
{showInvoiceModal && currentInvoice && (
|
<QRModal
|
||||||
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
|
isOpen={showInvoiceModal}
|
||||||
|
onClose={() => setShowInvoiceModal(false)}
|
||||||
|
title="Lightning Invoice"
|
||||||
|
description={currentInvoice ? `Pay this invoice to receive ${currentInvoice.amount} sats` : ''}
|
||||||
|
qrCodeUrl={currentInvoice?.qrCodeUrl || ''}
|
||||||
|
content={currentInvoice?.invoice || ''}
|
||||||
|
icon="zap"
|
||||||
|
showCopyButton={true}
|
||||||
|
copiedText={copiedInvoice}
|
||||||
|
onCopy={() => currentInvoice && handleCopy(currentInvoice.invoice, 'invoice')}
|
||||||
|
copyButtonText="Copy Invoice"
|
||||||
|
helpText="💡 Coco will automatically claim your tokens when payment is detected"
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* ========================================================================
|
||||||
|
SECTION: Remove Mint Confirmation Modal
|
||||||
|
======================================================================== */}
|
||||||
|
{showRemoveMintModal && mintToRemove && (
|
||||||
|
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-[100] p-4">
|
||||||
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
|
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
|
||||||
<CardHeader className="relative">
|
<CardHeader className="relative">
|
||||||
<Button
|
<Button
|
||||||
onClick={() => setShowInvoiceModal(false)}
|
onClick={() => setShowRemoveMintModal(false)}
|
||||||
className="absolute top-2 right-2"
|
className="absolute top-2 right-2"
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
@@ -1416,57 +1458,56 @@ export function CashuWallet() {
|
|||||||
<X className="h-4 w-4" />
|
<X className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
<CardTitle className="flex items-center pr-8">
|
<CardTitle className="flex items-center pr-8">
|
||||||
<Zap className="h-5 w-5 mr-2" />
|
<AlertTriangle className="h-5 w-5 mr-2 text-destructive" />
|
||||||
Lightning Invoice
|
Remove Mint
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
Pay this invoice to receive {currentInvoice.amount} sats
|
Are you sure you want to remove this mint from your wallet?
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className="space-y-4">
|
<CardContent className="space-y-4">
|
||||||
<div className="flex justify-center">
|
<div className="bg-muted p-4 rounded-lg border">
|
||||||
<div className="bg-white dark:bg-gray-100 p-4 rounded-lg border shadow-sm">
|
<div className="font-medium">{mintToRemove.name || new URL(mintToRemove.url).hostname}</div>
|
||||||
<img
|
<div className="text-sm text-muted-foreground font-mono break-all">
|
||||||
src={currentInvoice.qrCodeUrl}
|
{mintToRemove.url}
|
||||||
alt="Lightning Invoice QR Code"
|
|
||||||
className="w-64 h-64 object-contain"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="text-center">
|
<div className="text-sm text-muted-foreground bg-muted/30 p-3 rounded space-y-2">
|
||||||
<div className="bg-muted p-3 rounded-lg border font-mono text-sm break-all">
|
<div>⚠️ This will remove the mint and all associated data. Any tokens from this mint will no longer be accessible.</div>
|
||||||
{currentInvoice.invoice}
|
<div>💡 You can always add this mint back later if needed.</div>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex gap-2 justify-center">
|
<div className="flex gap-2 justify-end">
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={() => handleCopy(currentInvoice.invoice, 'invoice')}
|
onClick={() => setShowRemoveMintModal(false)}
|
||||||
className="flex items-center gap-2"
|
|
||||||
>
|
>
|
||||||
{copiedInvoice === currentInvoice.invoice ? (
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
onClick={handleConfirmRemoveMint}
|
||||||
|
disabled={isLoading}
|
||||||
|
>
|
||||||
|
{isLoading ? (
|
||||||
<>
|
<>
|
||||||
<Check className="h-4 w-4" />
|
<RefreshCw className="h-4 w-4 mr-2 animate-spin" />
|
||||||
Copied!
|
Removing...
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<Copy className="h-4 w-4" />
|
<X className="h-4 w-4 mr-2" />
|
||||||
Copy Invoice
|
Remove Mint
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</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>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { X, Copy, Check, QrCode, Zap } from 'lucide-react';
|
||||||
|
|
||||||
|
interface QRModalProps {
|
||||||
|
isOpen: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
qrCodeUrl: string;
|
||||||
|
content: string;
|
||||||
|
icon?: 'qr' | 'zap';
|
||||||
|
showCopyButton?: boolean;
|
||||||
|
copiedText?: string | null;
|
||||||
|
onCopy?: () => void;
|
||||||
|
copyButtonText?: string;
|
||||||
|
helpText?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function QRModal({
|
||||||
|
isOpen,
|
||||||
|
onClose,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
qrCodeUrl,
|
||||||
|
content,
|
||||||
|
icon = 'qr',
|
||||||
|
showCopyButton = false,
|
||||||
|
copiedText,
|
||||||
|
onCopy,
|
||||||
|
copyButtonText = 'Copy',
|
||||||
|
helpText,
|
||||||
|
}: QRModalProps) {
|
||||||
|
if (!isOpen) return null;
|
||||||
|
|
||||||
|
const IconComponent = icon === 'zap' ? Zap : QrCode;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-[100] p-4">
|
||||||
|
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
|
||||||
|
<CardHeader className="relative">
|
||||||
|
<Button
|
||||||
|
onClick={onClose}
|
||||||
|
className="absolute top-2 right-2"
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
>
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
<CardTitle className="flex items-center pr-8">
|
||||||
|
<IconComponent className="h-5 w-5 mr-2" />
|
||||||
|
{title}
|
||||||
|
</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
{description}
|
||||||
|
</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={qrCodeUrl}
|
||||||
|
alt={`${title} 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">
|
||||||
|
{content}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{showCopyButton && onCopy && (
|
||||||
|
<div className="flex gap-2 justify-center">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={onCopy}
|
||||||
|
className="flex items-center gap-2"
|
||||||
|
>
|
||||||
|
{copiedText === content ? (
|
||||||
|
<>
|
||||||
|
<Check className="h-4 w-4" />
|
||||||
|
Copied!
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Copy className="h-4 w-4" />
|
||||||
|
{copyButtonText}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{helpText && (
|
||||||
|
<div className="text-xs text-muted-foreground bg-muted/30 p-3 rounded text-center">
|
||||||
|
{helpText}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@ export function useCashuBalances(manager: unknown | null): UseCashuBalancesRetur
|
|||||||
// Throttling refs
|
// Throttling refs
|
||||||
const lastRefreshTimeRef = useRef<number>(0);
|
const lastRefreshTimeRef = useRef<number>(0);
|
||||||
const isRefreshingRef = useRef<boolean>(false);
|
const isRefreshingRef = useRef<boolean>(false);
|
||||||
const REFRESH_THROTTLE_MS = 500;
|
const REFRESH_THROTTLE_MS = 200;
|
||||||
|
|
||||||
const refreshBalances = useCallback(async () => {
|
const refreshBalances = useCallback(async () => {
|
||||||
if (!manager) return;
|
if (!manager) return;
|
||||||
@@ -102,10 +102,46 @@ export function useCashuBalances(manager: unknown | null): UseCashuBalancesRetur
|
|||||||
refreshBalances();
|
refreshBalances();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const unsubscribeSendCreated = (manager as { on: (event: string, handler: (payload: unknown) => void | Promise<void>) => () => void }).on('send:created', () => {
|
||||||
|
console.log('📤 Send created, refreshing balances...');
|
||||||
|
// Bypass throttling for send operations to ensure immediate balance update
|
||||||
|
setTimeout(async () => {
|
||||||
|
if (!manager) return;
|
||||||
|
try {
|
||||||
|
console.log('🔄 send:created: Getting balances from wallet...');
|
||||||
|
const newBalances = await (manager as { wallet: { getBalances: () => Promise<CashuBalance> } }).wallet.getBalances();
|
||||||
|
console.log('💰 send:created: Raw balances from wallet:', newBalances);
|
||||||
|
|
||||||
|
setBalances(prevBalances => {
|
||||||
|
const hasChanged =
|
||||||
|
Object.keys(newBalances).length !== Object.keys(prevBalances).length ||
|
||||||
|
Object.keys(newBalances).some(key => newBalances[key] !== prevBalances[key]);
|
||||||
|
|
||||||
|
if (hasChanged) {
|
||||||
|
console.log('✅ send:created: Balances changed, updating state:', Object.keys(newBalances).length, 'mints');
|
||||||
|
return newBalances;
|
||||||
|
} else {
|
||||||
|
console.log('⏭️ send:created: Balances unchanged, skipping state update');
|
||||||
|
return prevBalances;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error('❌ send:created: Failed to refresh balances:', err);
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
const unsubscribeReceiveCreated = (manager as { on: (event: string, handler: (payload: unknown) => void | Promise<void>) => () => void }).on('receive:created', () => {
|
||||||
|
console.log('📥 Receive created, refreshing balances...');
|
||||||
|
refreshBalances();
|
||||||
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
unsubscribeProofsSaved();
|
unsubscribeProofsSaved();
|
||||||
unsubscribeProofsStateChanged();
|
unsubscribeProofsStateChanged();
|
||||||
unsubscribeMintQuoteRedeemed();
|
unsubscribeMintQuoteRedeemed();
|
||||||
|
unsubscribeSendCreated();
|
||||||
|
unsubscribeReceiveCreated();
|
||||||
};
|
};
|
||||||
}, [manager, refreshBalances]);
|
}, [manager, refreshBalances]);
|
||||||
|
|
||||||
|
|||||||
@@ -78,13 +78,33 @@ export function useCashuMints(manager: unknown | null): UseCashuMintsReturn {
|
|||||||
}
|
}
|
||||||
}, [manager]);
|
}, [manager]);
|
||||||
|
|
||||||
const removeMint = useCallback(async (_mintUrl: string) => {
|
const removeMint = useCallback(async (mintUrl: string) => {
|
||||||
// Note: coco-cashu doesn't have a direct remove mint method
|
if (!manager) throw new CashuWalletError('Wallet not initialized', 'WALLET_NOT_INITIALIZED');
|
||||||
// This would need to be implemented at the repository level
|
|
||||||
const error = new CashuWalletError('Remove mint functionality not yet implemented', 'REMOVE_MINT_NOT_IMPLEMENTED');
|
try {
|
||||||
setError(error);
|
setIsLoading(true);
|
||||||
throw error;
|
setError(null);
|
||||||
}, []);
|
|
||||||
|
// Access the mintService directly through the manager to use deleteMint
|
||||||
|
// The manager has a private mintService property that we can access
|
||||||
|
const mintService = (manager as any).mintService;
|
||||||
|
if (!mintService || typeof mintService.deleteMint !== 'function') {
|
||||||
|
throw new CashuWalletError('Mint service not available', 'MINT_SERVICE_ERROR');
|
||||||
|
}
|
||||||
|
|
||||||
|
await mintService.deleteMint(mintUrl);
|
||||||
|
|
||||||
|
// No need to manually refresh - events will handle it automatically
|
||||||
|
console.log('✅ Mint removed successfully:', mintUrl);
|
||||||
|
} catch (err) {
|
||||||
|
const errorMessage = err instanceof Error ? err.message : 'Failed to remove mint';
|
||||||
|
const cashuError = new CashuWalletError(errorMessage, 'REMOVE_MINT_ERROR', { mintUrl, originalError: err });
|
||||||
|
setError(cashuError);
|
||||||
|
throw cashuError;
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}, [manager]);
|
||||||
|
|
||||||
// Set up event listeners for automatic mint updates using coco events
|
// Set up event listeners for automatic mint updates using coco events
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in new issue
Block a user