Update station type handling and current time logic for quai display

This commit is contained in:
Ferrovia committed 2026-05-01 21:47:27 +02:00
1 parent e42f6c01ec
commit 503a70bd49
5 files changed
+368 -40

No files matched your search

@@ -1,3 +1,4 @@
import React, { useEffect, useState, useCallback } from 'react';
import './ClassiquesDeparts.css';
import supabase from '../../../lib/supabase';
@@ -101,6 +102,14 @@ export default function ClassiquesDeparts(){
// État pour alterner l'affichage toutes les 3 secondes
const [showStatus, setShowStatus] = useState(false);
// État pour l'heure actuelle, mise à jour chaque seconde pour la logique d'affichage du quai
const [currentTime, setCurrentTime] = useState(new Date());
// Met à jour l'heure actuelle chaque seconde
useEffect(() => {
const id = setInterval(() => setCurrentTime(new Date()), 1000);
return () => clearInterval(id);
}, []);
// Données depuis Supabase
const [horaires, setHoraires] = useState([]);
@@ -245,9 +254,9 @@ export default function ClassiquesDeparts(){
// retourne le threshold (en minutes) pour afficher le quai selon le type de gare
const thresholdForStationType = useCallback((type) => {
if (!type) return 0;
const t = String(type).toLowerCase();
if (t.includes('interurb') || t.includes('interurbaine')) return 360; // 6 heures
if (t.includes('ville')) return 30; // 30 minutes
const t = String(type).trim().toLowerCase();
if (t === 'interurbaine' || t.includes('interurb')) return 360; // 6 heures
if (t === 'ville' || t.includes('ville')) return 30; // 30 minutes
return 0;
}, []);
@@ -456,7 +465,6 @@ export default function ClassiquesDeparts(){
if (typeof base === 'number') displayTime = formatTimeFromHM(base + displayStatus.delayMinutes);
}
}
horaire._stopsStatus = stopsStatus;
// prefer platform from stop data (quai) if available
const stopForStation = (stops || []).find(s => {
@@ -565,7 +573,9 @@ export default function ClassiquesDeparts(){
} catch (err) { /* ignore */ }
}
}
if (mounted) setStationType(fetchedType ? String(fetchedType).toLowerCase() : null);
if (mounted) {
setStationType(fetchedType ? String(fetchedType).toLowerCase() : null);
}
} catch (e) { /* ignore errors reading gares table */ }
// Filtrer par gare : garder uniquement les horaires qui DESSERVENT la gare ciblée
// (soit comme gare_depart, soit comme un arrêt intermédiaire dans `stops` / `gares_desservies`)
@@ -704,18 +714,65 @@ export default function ClassiquesDeparts(){
}
};
const circulatesOn = (h, d) => {
const col = weekdayNameForColumn(d);
if (!col) return true;
const v = h[col];
if (typeof v === 'boolean') return v;
if (typeof v === 'number') return v === 1;
if (typeof v === 'string') {
const s = v.trim().toLowerCase();
return s === 'true' || s === '1' || s === 't' || s === 'y' || s === 'yes';
const isPublicHoliday = (d) => {
const y = d.getFullYear();
const m = d.getMonth() + 1;
const day = d.getDate();
const s = `${y}-${String(m).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
const holidays = [
`${y}-01-01`, // Jour de l'an
`${y}-05-01`, // Fête du Travail
`${y}-05-08`, // Victoire 1945
`${y}-07-14`, // Fête nationale
`${y}-08-15`, // Assomption
`${y}-11-01`, // Toussaint
`${y}-11-11`, // Armistice 1918
`${y}-12-25`, // Noël
];
// Dates variables pour 2026
if (y === 2026) {
holidays.push('2026-04-06'); // Lundi de Pâques
holidays.push('2026-05-14'); // Ascension
holidays.push('2026-05-25'); // Lundi de Pentecôte
}
// si la colonne est undefined ou null, on considère que ça circule (fallback permissif)
return (typeof v === 'undefined' || v === null) ? true : Boolean(v);
return holidays.includes(s);
};
const parseCirculationFlag = (value, defaultValue) => {
if (value === null || typeof value === 'undefined') {
return defaultValue;
}
if (typeof value === 'boolean') {
return value;
}
if (typeof value === 'number') {
return value === 1;
}
if (typeof value === 'string') {
const s = value.trim().toLowerCase();
if (['true', '1', 't', 'y', 'yes', 'oui'].includes(s)) {
return true;
}
if (['false', '0', 'f', 'n', 'no', 'non'].includes(s)) {
return false;
}
}
return defaultValue;
};
const circulatesOn = (h, d) => {
if (isPublicHoliday(d)) {
// On public holidays, circulation is determined SOLELY by `circule_jours_feries`.
// If the flag is true, it runs. Otherwise, it doesn't.
return parseCirculationFlag(h['circule_jours_feries'], false);
}
// On a regular day, check the corresponding weekday column.
const col = weekdayNameForColumn(d);
if (!col) return true; // Should not happen, but permissive.
// For regular weekdays, the default is to run unless specified otherwise.
return parseCirculationFlag(h[col], true);
};
// Construire listes pour aujourd'hui et demain en tenant compte des colonnes circule_*
@@ -952,6 +1009,9 @@ export default function ClassiquesDeparts(){
// prefer precomputed status and display time (synchronisé depuis la BDD) — sinon calculer à la volée
const status = horaire._status || computeStatusForHoraireAtStop(horaire, stationId, stationName, buildPerturbMap(perturbations));
if (status && status.label === 'supprimé' && showStatus) {
time = '';
}
// if the status is 'supprimé' and the component is currently showing the status (showStatus toggles every 3s), hide the time
if (status && status.label === 'supprimé' && showStatus) {
time = '';
@@ -978,8 +1038,7 @@ export default function ClassiquesDeparts(){
// Ne pas afficher le quai si le train est supprimé ou si aucun quai n'est défini
let showPlatform = !(status && status.label === 'supprimé') && Boolean(platform);
if (showPlatform) {
const now = new Date();
const nowHM = now.getHours()*60 + now.getMinutes();
const nowHM = currentTime.getHours()*60 + currentTime.getMinutes(); // Use currentTime
const sched = (typeof horaire._depHM === 'number') ? horaire._depHM : parseTimeHM(horaire.heure_depart || horaire.time || horaire.heure || horaire._rawTime || '');
const threshold = thresholdForStationType(stationType);
if (typeof sched === 'number' && typeof threshold === 'number' && threshold > 0) {
@@ -1034,5 +1093,3 @@ export default function ClassiquesDeparts(){
</div>
)
}