mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +00:00
code cleanup round 2
This commit is contained in:
1 parent
aaf12120e6
commit
2ac097d8de
9 files changed
+326
-669
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import { useCashu, type HistoryEntry } from '@/hooks/cashu/useCashu';
|
||||
import { useCashu } from '@/hooks/cashu/useCashu';
|
||||
import type { HistoryEntry } from 'coco-cashu-core';
|
||||
import { useLightningAddress } from '@/hooks/useLightningAddress';
|
||||
import { useNpubCash, type NpubCashUserInfo } from '@/hooks/useNpubCash';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
@@ -8,7 +9,6 @@ import { useToast } from '@/hooks/useToast';
|
||||
import { useQRCode } from '@/hooks/useQRCode';
|
||||
import { useCopyToClipboard } from '@/hooks/useCopyToClipboard';
|
||||
import { QRModal } from '@/components/ui/qr-modal';
|
||||
import { LoadingModal } from './LoadingModal';
|
||||
import { BalanceDisplay } from './BalanceDisplay';
|
||||
import { ReceiveCard } from './ReceiveCard';
|
||||
import { SendCard } from './SendCard';
|
||||
@@ -23,26 +23,15 @@ export function CashuWallet() {
|
||||
// ============================================================================
|
||||
|
||||
const {
|
||||
coco,
|
||||
repositories,
|
||||
isInitialized,
|
||||
isLoading,
|
||||
error,
|
||||
balances,
|
||||
mints,
|
||||
receive,
|
||||
send,
|
||||
getProofs,
|
||||
addMint,
|
||||
removeMint,
|
||||
createMintQuote,
|
||||
createMeltQuote,
|
||||
payMeltQuote,
|
||||
redeemMintQuote,
|
||||
addMintQuote,
|
||||
on,
|
||||
refreshBalances,
|
||||
refreshMints,
|
||||
getEncodedToken,
|
||||
clearError,
|
||||
getHistory,
|
||||
} = useCashu();
|
||||
|
||||
const { setMint: setNpubCashMint, getUserInfo: getNpubCashUserInfo, isLoggedIn: isNpubCashLoggedIn } = useNpubCash();
|
||||
@@ -188,32 +177,38 @@ export function CashuWallet() {
|
||||
// ============================================================================
|
||||
|
||||
const loadHistory = useCallback(async () => {
|
||||
if (!coco) return;
|
||||
|
||||
try {
|
||||
setIsLoadingHistory(true);
|
||||
const entries = await getHistory(50, 0); //TODO: show entire history
|
||||
const entries = await coco.history.getPaginatedHistory(0, 50); //TODO: show entire history
|
||||
setHistoryEntries(entries);
|
||||
} catch (err) {
|
||||
console.error('Failed to load history:', err);
|
||||
} finally {
|
||||
setIsLoadingHistory(false);
|
||||
}
|
||||
}, [getHistory]);
|
||||
}, [coco]);
|
||||
|
||||
// ============================================================================
|
||||
// EVENT HANDLERS - Receive
|
||||
// ============================================================================
|
||||
|
||||
const handleReceive = async () => {
|
||||
if (!coco) return;
|
||||
|
||||
if (mode === 'tokens') {
|
||||
if (!tokenInput.trim()) return;
|
||||
try {
|
||||
await receive(tokenInput);
|
||||
console.log('Attempting to receive token:', tokenInput.substring(0, 20) + '...');
|
||||
await coco.wallet.receive(tokenInput);
|
||||
setTokenInput('');
|
||||
toast({
|
||||
title: "Token Received",
|
||||
description: "Cashu token redeemed successfully!",
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Token receive error:', err);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Receive Failed",
|
||||
@@ -233,7 +228,7 @@ export function CashuWallet() {
|
||||
});
|
||||
return;
|
||||
}
|
||||
const quote = await createMintQuote(activeMintUrl, amount);
|
||||
const quote = await coco.quotes.createMintQuote(activeMintUrl, amount);
|
||||
|
||||
// Generate QR code for the Lightning invoice
|
||||
const qrCodeUrl = await generateQRCode(quote.request);
|
||||
@@ -265,7 +260,7 @@ export function CashuWallet() {
|
||||
const quoteId = quote.quote;
|
||||
const invoiceAmount = amount;
|
||||
|
||||
const unsubscribe = on('mint-quote:redeemed', (payload: unknown) => {
|
||||
const unsubscribe = coco.on('mint-quote:redeemed', (payload: unknown) => {
|
||||
const redeemedQuote = payload as { quoteId?: string };
|
||||
|
||||
// Check if this is the quote we're waiting for
|
||||
@@ -300,6 +295,8 @@ export function CashuWallet() {
|
||||
// ============================================================================
|
||||
|
||||
const handleSend = async () => {
|
||||
if (!coco) return;
|
||||
|
||||
if (mode === 'tokens') {
|
||||
if (!sendAmount || !activeMintUrl) return;
|
||||
try {
|
||||
@@ -312,7 +309,8 @@ export function CashuWallet() {
|
||||
});
|
||||
return;
|
||||
}
|
||||
const encodedToken = await send(activeMintUrl, amount);
|
||||
const token = await coco.wallet.send(activeMintUrl, amount);
|
||||
const encodedToken = getEncodedToken(token);
|
||||
setSentToken(encodedToken);
|
||||
await generateSentTokenQRCode(encodedToken);
|
||||
setSendAmount('');
|
||||
@@ -348,8 +346,8 @@ export function CashuWallet() {
|
||||
amountSats: amount,
|
||||
});
|
||||
|
||||
const meltQuote = await createMeltQuote(activeMintUrl, invoice);
|
||||
await payMeltQuote(activeMintUrl, meltQuote.quote);
|
||||
const meltQuote = await coco.quotes.createMeltQuote(activeMintUrl, invoice);
|
||||
await coco.quotes.payMeltQuote(activeMintUrl, meltQuote.quote);
|
||||
|
||||
toast({
|
||||
title: "Payment Sent",
|
||||
@@ -379,8 +377,8 @@ export function CashuWallet() {
|
||||
} else {
|
||||
// It's a regular Lightning invoice
|
||||
try {
|
||||
const meltQuote = await createMeltQuote(activeMintUrl, input);
|
||||
await payMeltQuote(activeMintUrl, meltQuote.quote);
|
||||
const meltQuote = await coco.quotes.createMeltQuote(activeMintUrl, input);
|
||||
await coco.quotes.payMeltQuote(activeMintUrl, meltQuote.quote);
|
||||
|
||||
toast({
|
||||
title: "Invoice Paid",
|
||||
@@ -412,44 +410,24 @@ export function CashuWallet() {
|
||||
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) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Remove Failed",
|
||||
description: err instanceof Error ? err.message : 'Unknown error',
|
||||
});
|
||||
}
|
||||
}, [mintToRemove, removeMint, refreshMints, activeMintUrl, mints, toast]);
|
||||
// Note: Mint deletion is not available in the current coco-cashu-core API
|
||||
// This would require direct repository access which is not exposed
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Mint Removal Not Supported",
|
||||
description: "Mint removal is not currently supported in this version of coco-cashu-core",
|
||||
});
|
||||
|
||||
setShowRemoveMintModal(false);
|
||||
setMintToRemove(null);
|
||||
}, [mintToRemove, toast]);
|
||||
|
||||
// ============================================================================
|
||||
// EVENT HANDLERS - Mint Management
|
||||
// ============================================================================
|
||||
|
||||
const handleAddMint = async () => {
|
||||
if (!mintUrl.trim()) return;
|
||||
if (!mintUrl.trim() || !coco) return;
|
||||
try {
|
||||
// Normalize URL - add https:// if not present
|
||||
let normalizedUrl = mintUrl.trim();
|
||||
@@ -457,7 +435,7 @@ export function CashuWallet() {
|
||||
normalizedUrl = `https://${normalizedUrl}`;
|
||||
}
|
||||
|
||||
await addMint(normalizedUrl);
|
||||
await coco.mint.addMint(normalizedUrl);
|
||||
setMintUrl('');
|
||||
|
||||
toast({
|
||||
@@ -480,8 +458,11 @@ export function CashuWallet() {
|
||||
|
||||
|
||||
const handleShowDenominations = async (mintUrl: string) => {
|
||||
if (!repositories) return;
|
||||
|
||||
try {
|
||||
const proofs = await getProofs(mintUrl);
|
||||
// Access proofs directly through the proof repository
|
||||
const proofs = await repositories.proofRepository.getReadyProofs(mintUrl);
|
||||
|
||||
if (!proofs || proofs.length === 0) {
|
||||
toast({
|
||||
@@ -562,11 +543,13 @@ export function CashuWallet() {
|
||||
|
||||
if (!mintExists) {
|
||||
try {
|
||||
await addMint(userInfo.mint_url);
|
||||
// Wait a bit for the mint to be added
|
||||
setTimeout(() => {
|
||||
handleSetActiveMint(userInfo.mint_url, false);
|
||||
}, 500);
|
||||
if (coco) {
|
||||
await coco.mint.addMint(userInfo.mint_url);
|
||||
// Wait a bit for the mint to be added
|
||||
setTimeout(() => {
|
||||
handleSetActiveMint(userInfo.mint_url, false);
|
||||
}, 500);
|
||||
}
|
||||
} catch (err) {
|
||||
// Silent failure - user can manually add mint if needed
|
||||
}
|
||||
@@ -578,7 +561,7 @@ export function CashuWallet() {
|
||||
};
|
||||
|
||||
fetchUserInfo();
|
||||
}, [isNpubCashLoggedIn, isInitialized, getNpubCashUserInfo, activeMintUrl, mints, addMint, handleSetActiveMint]);
|
||||
}, [isNpubCashLoggedIn, isInitialized, getNpubCashUserInfo, activeMintUrl, mints, coco, handleSetActiveMint]);
|
||||
|
||||
// Auto-select active mint when mints change (balances will update automatically via events)
|
||||
useEffect(() => {
|
||||
@@ -598,25 +581,25 @@ export function CashuWallet() {
|
||||
|
||||
// Auto-refresh transaction history on new transactions
|
||||
useEffect(() => {
|
||||
if (!isInitialized) return;
|
||||
if (!isInitialized || !coco) return;
|
||||
|
||||
// Listen for history updates (fires on send, receive transactions)
|
||||
const unsubscribeHistoryUpdated = on('history:updated', () => {
|
||||
const unsubscribeHistoryUpdated = coco.on('history:updated', () => {
|
||||
loadHistory();
|
||||
});
|
||||
|
||||
// Listen for melt quote creation (coco-cashu bug: doesn't emit history:updated)
|
||||
const unsubscribeMeltCreated = on('melt-quote:created', () => {
|
||||
const unsubscribeMeltCreated = coco.on('melt-quote:created', () => {
|
||||
loadHistory();
|
||||
});
|
||||
|
||||
// Listen for melt quote paid (backup, in case creation event missed)
|
||||
const unsubscribeMeltPaid = on('melt-quote:paid', () => {
|
||||
const unsubscribeMeltPaid = coco.on('melt-quote:paid', () => {
|
||||
loadHistory();
|
||||
});
|
||||
|
||||
// Listen for mint quote REDEEMED (only after invoice is paid, not created)
|
||||
const unsubscribeMintRedeemed = on('mint-quote:redeemed', () => {
|
||||
const unsubscribeMintRedeemed = coco.on('mint-quote:redeemed', () => {
|
||||
loadHistory();
|
||||
});
|
||||
|
||||
@@ -626,7 +609,7 @@ export function CashuWallet() {
|
||||
unsubscribeMeltPaid();
|
||||
unsubscribeMintRedeemed();
|
||||
};
|
||||
}, [isInitialized, on, loadHistory]);
|
||||
}, [isInitialized, coco, loadHistory]);
|
||||
|
||||
// Cleanup mint quote listener on component unmount
|
||||
useEffect(() => {
|
||||
@@ -645,7 +628,6 @@ export function CashuWallet() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<LoadingModal isLoading={isLoading || !isInitialized} />
|
||||
<div className="max-w-4xl mx-auto py-1 px-3 sm:px-4 relative">
|
||||
|
||||
{/* ========================================================================
|
||||
|
||||
Reference in new issue
Block a user