import { useState, useCallback } from 'react'; import { HistoryEntry, CashuWalletError } from './types'; export interface UseCashuHistoryReturn { isLoading: boolean; error: CashuWalletError | null; getHistory: (limit?: number, offset?: number) => Promise; } export function useCashuHistory(manager: unknown | null): UseCashuHistoryReturn { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const getHistory = useCallback(async (limit: number = 50, offset: number = 0): Promise => { if (!manager) throw new CashuWalletError('Wallet not initialized', 'WALLET_NOT_INITIALIZED'); try { setIsLoading(true); setError(null); console.log('📜 Getting transaction history...'); const entries = await (manager as { history: { getPaginatedHistory: (offset: number, limit: number) => Promise } }).history.getPaginatedHistory(offset, limit); console.log(`✅ Found ${entries.length} history entries`); return entries as unknown as HistoryEntry[]; } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Failed to get history'; console.error('❌ Error getting history:', err); const cashuError = new CashuWalletError(errorMessage, 'GET_HISTORY_ERROR', { limit, offset, originalError: err }); setError(cashuError); throw cashuError; } finally { setIsLoading(false); } }, [manager]); return { isLoading, error, getHistory, }; }