cosmo/frontend/src/hooks/useSpaceData.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

/**
* 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);
2025-11-29 15:10:00 +00:00
// Fetch current position - single point in time at current hour
// Round to current hour (00 minutes, 00 seconds)
const now = new Date();
now.setMinutes(0, 0, 0);
const data = await fetchCelestialPositions(
now.toISOString(),
now.toISOString(), // Same as start - single point in time
'1h' // 1 hour step (though doesn't matter for single point)
);
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 };
}