Fix: add missing services files
This commit is contained in:
1 parent
33e97f2f3f
commit
bf6d8722e0
5 files changed
+25534
No files matched your search
@@ -0,0 +1,78 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { getSourceFromPath, getTimetable, getStationMeta } from '../services/railwayService';
|
||||
|
||||
/**
|
||||
* Hook unifié pour charger les horaires et métadonnées d'affichage
|
||||
* selon la source active (SNCF GTFS ou Supabase Ferrovia)
|
||||
*/
|
||||
export default function useAfficheurData({
|
||||
gareName,
|
||||
gareId = null,
|
||||
mode = 'departs',
|
||||
quai = null,
|
||||
refreshIntervalMs = 30000
|
||||
}) {
|
||||
const [source, setSource] = useState(() => getSourceFromPath(window.location.pathname));
|
||||
const [horaires, setHoraires] = useState([]);
|
||||
const [perturbations, setPerturbations] = useState([]);
|
||||
const [travaux, setTravaux] = useState([]);
|
||||
const [stationType, setStationType] = useState(null);
|
||||
const [resolvedGareId, setResolvedGareId] = useState(gareId);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
setSource(getSourceFromPath(window.location.pathname));
|
||||
}, []);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
// 1. Récupération des métadonnées de la gare
|
||||
const meta = await getStationMeta(gareId || gareName, source);
|
||||
if (meta) {
|
||||
setStationType(meta.type ? String(meta.type).toLowerCase() : null);
|
||||
if (meta.id) setResolvedGareId(meta.id);
|
||||
}
|
||||
|
||||
// 2. Récupération des horaires & perturbations
|
||||
const res = await getTimetable({ gareName, mode, quai, source });
|
||||
if (res.error) {
|
||||
setError(res.error);
|
||||
} else {
|
||||
setHoraires(res.data || []);
|
||||
setPerturbations(res.perturbations || []);
|
||||
setTravaux(res.travaux || []);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[useAfficheurData] Erreur de chargement:', err);
|
||||
setError(err.message || String(err));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [gareName, gareId, mode, quai, source]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
|
||||
// Polling automatique pour actualiser les données en temps réel
|
||||
if (refreshIntervalMs > 0) {
|
||||
const interval = setInterval(fetchData, refreshIntervalMs);
|
||||
return () => clearInterval(interval);
|
||||
}
|
||||
}, [fetchData, refreshIntervalMs]);
|
||||
|
||||
return {
|
||||
source,
|
||||
horaires,
|
||||
perturbations,
|
||||
travaux,
|
||||
stationType,
|
||||
resolvedGareId,
|
||||
loading,
|
||||
error,
|
||||
refetch: fetchData
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user