import { useState, useEffect } from 'react'; type ApiFunction = () => Promise<{ data: T }>; export function useApi(apiFunction: ApiFunction) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { try { const response = await apiFunction(); setData(response.data); } catch (err) { setError(err); } finally { setLoading(false); } }; fetchData(); }, [apiFunction]); return { data, loading, error }; }