implement npubcash-sdk to simplify code

This commit is contained in:
lemonknowsall committed 2025-10-15 01:22:12 -07:00
1 parent 2ac097d8de
commit bd956cef3f
5 files changed
+265 -265

No files matched your search

+36 -34
View File
@@ -34,7 +34,15 @@ export function CashuWallet() {
clearError,
} = useCashu();
const { setMint: setNpubCashMint, getUserInfo: getNpubCashUserInfo, isLoggedIn: isNpubCashLoggedIn } = useNpubCash();
// Npub.cash integration with cleaner SDK-based implementation
const {
setMint: setNpubCashMint,
getUserInfo: getNpubCashUserInfo,
isLoggedIn: isNpubCashLoggedIn,
isLoading: isNpubCashLoading,
error: npubCashError,
clearError: clearNpubCashError
} = useNpubCash();
const { user } = useCurrentUser();
@@ -156,21 +164,13 @@ export function CashuWallet() {
* @param syncToNpubCash - Whether to sync this change to npub.cash (default: true for manual changes)
*/
const handleSetActiveMint = useCallback(async (mintUrl: string | null, syncToNpubCash = true) => {
// Set the active mint locally
setActiveMintUrl(mintUrl);
// If user is logged in to npub.cash, a mint is selected, and we should sync
// Sync to npub.cash if user is logged in and we should sync
if (isNpubCashLoggedIn && mintUrl && syncToNpubCash) {
const success = await setNpubCashMint(mintUrl);
if (success) {
toast({
title: "Mint Synced",
description: "Your preferred mint has been updated on npub.cash",
});
}
await setNpubCashMint(mintUrl);
}
}, [isNpubCashLoggedIn, setNpubCashMint, toast]);
}, [isNpubCashLoggedIn, setNpubCashMint]);
// ============================================================================
// HELPER FUNCTIONS - History
@@ -515,53 +515,55 @@ export function CashuWallet() {
}
}, [error, toast]);
// Show npub.cash error toasts
useEffect(() => {
if (npubCashError) {
toast({
variant: "destructive",
title: "Npub.cash Error",
description: npubCashError,
});
clearNpubCashError();
}
}, [npubCashError, clearNpubCashError, toast]);
// Fetch npub.cash user info on mount/login and sync mint
useEffect(() => {
// Reset the check flag if user logs out
if (!isNpubCashLoggedIn) {
hasCheckedNpubCashRef.current = false;
setNpubCashUserInfo(null);
return;
}
// Only fetch if user is logged in, wallet is initialized, and we haven't fetched yet
if (!isInitialized || hasCheckedNpubCashRef.current) return;
if (!isInitialized || hasCheckedNpubCashRef.current || isNpubCashLoading) return;
const fetchUserInfo = async () => {
// Mark that we've checked to prevent multiple fetches
hasCheckedNpubCashRef.current = true;
const userInfo = await getNpubCashUserInfo();
if (!userInfo) return;
setNpubCashUserInfo(userInfo);
// Check if the mint from npub.cash is different from our active mint
// Sync mint from npub.cash if different from active mint
if (userInfo.mint_url && userInfo.mint_url !== activeMintUrl) {
// Check if we have this mint in our list
const mintExists = mints.some(m => m.url === userInfo.mint_url);
if (!mintExists) {
if (!mintExists && coco) {
try {
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) {
await coco.mint.addMint(userInfo.mint_url);
setTimeout(() => handleSetActiveMint(userInfo.mint_url, false), 500);
} catch {
// Silent failure - user can manually add mint if needed
}
} else {
// Mint exists, just set it as active (without syncing back to npub.cash)
} else if (mintExists) {
handleSetActiveMint(userInfo.mint_url, false);
}
}
};
fetchUserInfo();
}, [isNpubCashLoggedIn, isInitialized, getNpubCashUserInfo, activeMintUrl, mints, coco, handleSetActiveMint]);
}, [isNpubCashLoggedIn, isInitialized, isNpubCashLoading, getNpubCashUserInfo, activeMintUrl, mints, coco, handleSetActiveMint]);
// Auto-select active mint when mints change (balances will update automatically via events)
useEffect(() => {
@@ -678,7 +680,7 @@ export function CashuWallet() {
setQuoteAmount={setQuoteAmount}
activeMintUrl={activeMintUrl}
onReceive={handleReceive}
isLoading={isLoading}
isLoading={isLoading || isNpubCashLoading}
/>
<SendCard
@@ -696,7 +698,7 @@ export function CashuWallet() {
activeMintUrl={activeMintUrl}
onSend={handleSend}
isResolvingAddress={isResolvingAddress}
isLoading={isLoading}
isLoading={isLoading || isNpubCashLoading}
/>
</div>
@@ -793,7 +795,7 @@ export function CashuWallet() {
onClose={() => setShowRemoveMintModal(false)}
mint={mintToRemove}
onConfirm={handleConfirmRemoveMint}
isLoading={isLoading}
isLoading={isLoading || isNpubCashLoading}
/>
</>
</div>
+4 -29
View File
@@ -1,6 +1,5 @@
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Loader2, History, ArrowUpRight, ArrowDownLeft, Zap, ChevronDown, ChevronUp, Coins } from 'lucide-react';
import type { HistoryEntry } from 'coco-cashu-core';
@@ -43,32 +42,16 @@ export function TransactionHistory({ entries, isLoading, isExpanded, setIsExpand
const getHistoryLabel = (type: string) => {
switch (type) {
case 'mint':
return 'Mint Tokens';
case 'melt':
return 'Melt Tokens';
case 'send':
return 'Send Tokens';
case 'receive':
return 'Receive Tokens';
return 'Received';
case 'melt':
case 'send':
return 'Sent';
default:
return type;
}
};
const getHistoryStateColor = (state?: string) => {
if (!state) return 'bg-gray-500';
switch (state) {
case 'PAID':
return 'bg-green-500';
case 'UNPAID':
return 'bg-yellow-500';
case 'PENDING':
return 'bg-blue-500';
default:
return 'bg-gray-500';
}
};
return (
<Card className="mt-6 border-0 shadow-none">
<CardHeader>
@@ -129,14 +112,6 @@ export function TransactionHistory({ entries, isLoading, isExpanded, setIsExpand
<span className="font-medium">
{getHistoryLabel(entry.type)}
</span>
{'state' in entry && entry.state && (
<Badge
variant="secondary"
className={`${getHistoryStateColor(entry.state)} text-white text-xs`}
>
{entry.state}
</Badge>
)}
</div>
<div className="text-sm text-muted-foreground mt-1">
{new URL(entry.mintUrl).hostname}