import { useState, useEffect } from 'react';
import { getUserTransactions, type Transaction } from '@/lib/api/transactions';

export function useTransactions() {
  const [transactions, setTransactions] = useState<Transaction[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const fetchTransactions = async () => {
    setIsLoading(true);
    setError(null);

    const response = await getUserTransactions();

    if (response.success) {
      setTransactions(response.data);
    } else {
      setError(response.message || '获取交易记录失败');
    }

    setIsLoading(false);
  };

  useEffect(() => {
    fetchTransactions();
  }, []);

  return {
    transactions,
    isLoading,
    error,
    refresh: fetchTransactions,
  };
}
