34 lines
840 B
TypeScript
34 lines
840 B
TypeScript
|
|
import { useState, useEffect } from 'react';
|
||
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||
|
|
import { Transaction } from '../types';
|
||
|
|
|
||
|
|
const STORAGE_KEY = '@finance_transactions';
|
||
|
|
|
||
|
|
export const useTransactions = () => {
|
||
|
|
const [transactions, setTransactions] = useState<Transaction[]>([]);
|
||
|
|
const [loading, setLoading] = useState(true);
|
||
|
|
|
||
|
|
const loadTransactions = async () => {
|
||
|
|
try {
|
||
|
|
setLoading(true);
|
||
|
|
const stored = await AsyncStorage.getItem(STORAGE_KEY);
|
||
|
|
if (stored) {
|
||
|
|
setTransactions(JSON.parse(stored));
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error loading transactions:', error);
|
||
|
|
} finally {
|
||
|
|
setLoading(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
loadTransactions();
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return {
|
||
|
|
transactions,
|
||
|
|
loading,
|
||
|
|
refresh: loadTransactions,
|
||
|
|
};
|
||
|
|
};
|