fix active mint determination

This commit is contained in:
lemonknowsall committed 2025-11-03 08:08:34 -08:00
1 parent 28e50a9d7a
commit 7d6ca0f71e
3 files changed
+140 -77

No files matched your search

+1 -40
View File
@@ -1,4 +1,4 @@
import { useState, useCallback, useEffect, useRef } from 'react';
import { useState, useCallback } from 'react';
import { getCleanMintLabel } from './walletUtils';
import type { Manager, Repositories } from 'coco-cashu-core';
import { toast } from '@/hooks/useToast';
@@ -30,7 +30,6 @@ export function useMintManager({
const [mintUrl, setMintUrl] = useState('');
const [mintToRemove, setMintToRemove] = useState<CashuMint | null>(null);
const [showRemoveMintModal, setShowRemoveMintModal] = useState(false);
const isAddingDefaultMintRef = useRef(false);
// Set active mint and optionally sync to npub.cash
const handleSetActiveMint = useCallback(
@@ -205,44 +204,6 @@ export function useMintManager({
[repositories]
);
// Auto-select first mint when mints change, or add default mint if none exist
useEffect(() => {
if (mints.length > 0 && !activeMintUrl) {
// Select first mint initially - user can change via dropdown
// Don't sync to npub.cash on auto-select, only on manual changes
handleSetActiveMint(mints[0].url, false);
} else if (mints.length === 0 && !activeMintUrl && coco && !isAddingDefaultMintRef.current) {
// Add default mint if no mints exist
// Use ref guard to prevent multiple simultaneous additions
isAddingDefaultMintRef.current = true;
const addDefaultMint = async () => {
try {
const defaultMintUrl = 'https://mint.chorus.community';
// Check if mint already exists (check trusted mints since that's what we display)
const trustedMints = await coco.mint.getAllTrustedMints();
const mintExists = trustedMints.some((mint: { mintUrl: string }) =>
mint.mintUrl.toLowerCase() === defaultMintUrl.toLowerCase()
);
if (!mintExists) {
// Add default mint and automatically trust it
await coco.mint.addMint(defaultMintUrl, { trusted: true });
// The mint will be added and refreshMints will be called via event listeners
}
} catch (err) {
console.error('Failed to add default mint:', err);
} finally {
// Reset the guard after a short delay to allow mints to update
setTimeout(() => {
isAddingDefaultMintRef.current = false;
}, 1000);
}
};
addDefaultMint();
}
}, [mints, activeMintUrl, handleSetActiveMint, coco]);
return {
activeMintUrl,
setActiveMintUrl: handleSetActiveMint,