mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import { useState, useCallback } from 'react';
|
|
import { HistoryEntry, CashuWalletError } from './types';
|
|
|
|
export interface UseCashuHistoryReturn {
|
|
isLoading: boolean;
|
|
error: CashuWalletError | null;
|
|
getHistory: (limit?: number, offset?: number) => Promise<HistoryEntry[]>;
|
|
}
|
|
|
|
export function useCashuHistory(manager: unknown | null): UseCashuHistoryReturn {
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [error, setError] = useState<CashuWalletError | null>(null);
|
|
|
|
const getHistory = useCallback(async (limit: number = 50, offset: number = 0): Promise<HistoryEntry[]> => {
|
|
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<unknown[]> } }).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,
|
|
};
|
|
}
|