44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
/**
|
|
* Custom hook for fetching space data
|
|
*/
|
|
import { useState, useEffect } from 'react';
|
|
import { fetchCelestialPositions } from '../utils/api';
|
|
import type { CelestialBody } from '../types';
|
|
|
|
export function useSpaceData() {
|
|
const [bodies, setBodies] = useState<CelestialBody[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
async function loadData() {
|
|
try {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
// Fetch current position - single point in time for today (UTC midnight)
|
|
// This ensures we hit the cache if we already have data for today
|
|
const now = new Date();
|
|
now.setUTCHours(0, 0, 0, 0);
|
|
|
|
const data = await fetchCelestialPositions(
|
|
now.toISOString(),
|
|
now.toISOString(), // Same as start - single point in time
|
|
'1d' // Use 1d step for consistency
|
|
);
|
|
|
|
setBodies(data.bodies);
|
|
} catch (err) {
|
|
console.error('Failed to fetch celestial data:', err);
|
|
setError(err instanceof Error ? err.message : 'Unknown error');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
loadData();
|
|
}, []);
|
|
|
|
return { bodies, loading, error };
|
|
}
|