aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/routes/timetable-$id.tsx
blob: 1942ce8cfc5ad35a22eae5cee0d08620e60156d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { useEffect, useState, useRef } from "react";
import { useParams, Link } from "react-router";
import StopDataProvider from "../data/StopDataProvider";
import { ArrowLeft, Eye, EyeOff } from "lucide-react";
import { TimetableTable, type TimetableEntry } from "../components/TimetableTable";
import { TimetableSkeleton } from "../components/TimetableSkeleton";
import { ErrorDisplay } from "../components/ErrorDisplay";
import LineIcon from "../components/LineIcon";
import { useTranslation } from "react-i18next";
import { type RegionId, getRegionConfig } from "../data/RegionConfig";
import { useApp } from "../AppContext";
import "./timetable-$id.css";

interface ErrorInfo {
  type: 'network' | 'server' | 'unknown';
  status?: number;
  message?: string;
}

const loadTimetableData = async (region: RegionId, stopId: string): Promise<TimetableEntry[]> => {
  const regionConfig = getRegionConfig(region);

  // Check if timetable is available for this region
  if (!regionConfig.timetableEndpoint) {
    throw new Error("Timetable not available for this region");
  }

  // Add delay to see skeletons in action (remove in production)
  await new Promise(resolve => setTimeout(resolve, 1000));

  const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD format
  const resp = await fetch(`${regionConfig.timetableEndpoint}?date=${today}&stopId=${stopId}`, {
    headers: {
      Accept: "application/json",
    },
  });

  if (!resp.ok) {
    throw new Error(`HTTP ${resp.status}: ${resp.statusText}`);
  }

  return await resp.json();
};

// Utility function to compare times
const timeToMinutes = (time: string): number => {
  const [hours, minutes] = time.split(':').map(Number);
  return hours * 60 + minutes;
};

// Filter past entries (keep only a few recent past ones)
const filterTimetableData = (data: TimetableEntry[], currentTime: string, showPast: boolean = false): TimetableEntry[] => {
  if (showPast) return data;

  const currentMinutes = timeToMinutes(currentTime);
  const sortedData = [...data].sort((a, b) =>
    timeToMinutes(a.departure_time) - timeToMinutes(b.departure_time)
  );

  // Find the current position
  const currentIndex = sortedData.findIndex(entry =>
    timeToMinutes(entry.departure_time) >= currentMinutes
  );

  if (currentIndex === -1) {
    // All entries are in the past, show last 3
    return sortedData.slice(-3);
  }

  // Show 3 past entries + all future entries
  const startIndex = Math.max(0, currentIndex - 3);
  return sortedData.slice(startIndex);
};

// Utility function to parse service ID and get the turn number
const parseServiceId = (serviceId: string): string => {
  const parts = serviceId.split('_');
  if (parts.length === 0) return '';

  const lastPart = parts[parts.length - 1];
  if (lastPart.length < 6) return '';

  const last6 = lastPart.slice(-6);
  const lineCode = last6.slice(0, 3);
  const turnCode = last6.slice(-3);

  // Remove leading zeros from turn
  const turnNumber = parseInt(turnCode, 10).toString();

  // Parse line number with special cases
  const lineNumber = parseInt(lineCode, 10);
  let displayLine: string;

  switch (lineNumber) {
    case 1: displayLine = "C1"; break;
    case 3: displayLine = "C3"; break;
    case 30: displayLine = "N1"; break;
    case 33: displayLine = "N4"; break;
    case 8: displayLine = "A"; break;
    case 101: displayLine = "H"; break;
    case 150: displayLine = "REF"; break;
    case 500: displayLine = "TUR"; break;
    default: displayLine = `L${lineNumber}`;
  }

  return `${displayLine}-${turnNumber}`;
};

export default function Timetable() {
  const { t } = useTranslation();
  const { region } = useApp();
  const params = useParams();
  const stopIdNum = parseInt(params.id ?? "");
  const [timetableData, setTimetableData] = useState<TimetableEntry[]>([]);
  const [customName, setCustomName] = useState<string | undefined>(undefined);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<ErrorInfo | null>(null);
  const [showPastEntries, setShowPastEntries] = useState(false);
  const nextEntryRef = useRef<HTMLDivElement>(null);
  const regionConfig = getRegionConfig(region);

  const currentTime = new Date().toTimeString().slice(0, 8); // HH:MM:SS
  const filteredData = filterTimetableData(timetableData, currentTime, showPastEntries);

  const parseError = (error: any): ErrorInfo => {
    if (!navigator.onLine) {
      return { type: 'network', message: 'No internet connection' };
    }

    if (error.message?.includes('Failed to fetch') || error.message?.includes('NetworkError')) {
      return { type: 'network' };
    }

    if (error.message?.includes('HTTP')) {
      const statusMatch = error.message.match(/HTTP (\d+):/);
      const status = statusMatch ? parseInt(statusMatch[1]) : undefined;
      return { type: 'server', status };
    }

    return { type: 'unknown', message: error.message };
  };

  const loadData = async () => {
    // Check if timetable is available for this region
    if (!regionConfig.timetableEndpoint) {
      setError({
        type: 'server',
        status: 501,
        message: 'Timetable not available for this region'
      });
      setLoading(false);
      return;
    }

    try {
      setLoading(true);
      setError(null);

      const timetableBody = await loadTimetableData(region, params.id!);
      setTimetableData(timetableBody);

      if (timetableBody.length > 0) {
        // Scroll to next entry after a short delay to allow rendering
        setTimeout(() => {
          const currentMinutes = timeToMinutes(currentTime);
          const sortedData = [...timetableBody].sort((a, b) =>
            timeToMinutes(a.departure_time) - timeToMinutes(b.departure_time)
          );

          const nextIndex = sortedData.findIndex(entry =>
            timeToMinutes(entry.departure_time) >= currentMinutes
          );

          if (nextIndex !== -1 && nextEntryRef.current) {
            nextEntryRef.current.scrollIntoView({
              behavior: 'smooth',
              block: 'center'
            });
          }
        }, 500);
      }
    } catch (err) {
      console.error('Error loading timetable data:', err);
      setError(parseError(err));
      setTimetableData([]);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadData();
    setCustomName(StopDataProvider.getCustomName(region, stopIdNum));
  }, [params.id, region]);

  if (loading) {
    return (
      <div className="page-container">
        <div className="timetable-full-header">
          <h1 className="page-title">
            {t("timetable.fullTitle", "Horarios teóricos")} ({params.id})
          </h1>
          <Link to={`/estimates/${params.id}`} className="back-link">
            <ArrowLeft className="back-icon" />
            {t("timetable.backToEstimates", "Volver a estimaciones")}
          </Link>
        </div>

        <div className="timetable-full-content">
          <div className="timetable-controls">
            <button className="past-toggle" disabled>
              <Eye className="toggle-icon" />
              {t("timetable.showPast", "Mostrar todos")}
            </button>
          </div>

          <TimetableSkeleton rows={8} />
        </div>
      </div>
    );
  }

  return (
    <div className="page-container">
      <div className="timetable-full-header">
        <h1 className="page-title">
          {t("timetable.fullTitle", "Horarios teóricos")} ({params.id})
        </h1>
        <Link to={`/estimates/${params.id}`} className="back-link">
          <ArrowLeft className="back-icon" />
          {t("timetable.backToEstimates", "Volver a estimaciones")}
        </Link>
      </div>

      {error ? (
        <div className="timetable-full-content">
          <ErrorDisplay
            error={error}
            onRetry={loadData}
            title={t("errors.timetable_title", "Error al cargar horarios")}
          />
        </div>
      ) : timetableData.length === 0 ? (
        <div className="error-message">
          <p>{t("timetable.noDataAvailable", "No hay datos de horarios disponibles para hoy")}</p>
          <p className="error-detail">
            {t("timetable.errorDetail", "Los horarios teóricos se actualizan diariamente. Inténtalo más tarde.")}
          </p>
        </div>
      ) : (
        <div className="timetable-full-content">
          <div className="timetable-controls">
            <button
              className={`past-toggle ${showPastEntries ? 'active' : ''}`}
              onClick={() => setShowPastEntries(!showPastEntries)}
            >
              {showPastEntries ? (
                <>
                  <EyeOff className="toggle-icon" />
                  {t("timetable.hidePast", "Ocultar pasados")}
                </>
              ) : (
                <>
                  <Eye className="toggle-icon" />
                  {t("timetable.showPast", "Mostrar todos")}
                </>
              )}
            </button>
          </div>

          <TimetableTableWithScroll
            data={filteredData}
            showAll={true}
            currentTime={currentTime}
            nextEntryRef={nextEntryRef}
          />
        </div>
      )}
    </div>
  );
}

// Custom component for the full timetable with scroll reference
const TimetableTableWithScroll: React.FC<{
  data: TimetableEntry[];
  showAll: boolean;
  currentTime: string;
  nextEntryRef: React.RefObject<HTMLDivElement | null>;
}> = ({ data, showAll, currentTime, nextEntryRef }) => {
  const { t } = useTranslation();
  const { region } = useApp();
  const nowMinutes = timeToMinutes(currentTime);

  return (
    <div className="timetable-container">
      <div className="timetable-caption">
        {t("timetable.fullCaption", "Horarios teóricos de la parada")}
      </div>

      <div className="timetable-cards">
        {data.map((entry, index) => {
          const entryMinutes = timeToMinutes(entry.departure_time);
          const isPast = entryMinutes < nowMinutes;
          const isNext = !isPast && (index === 0 || timeToMinutes(data[index - 1]?.departure_time || '00:00:00') < nowMinutes);

          return (
            <div
              key={`${entry.trip.id}-${index}`}
              ref={isNext ? nextEntryRef : null}
              className={`timetable-card${isPast ? " timetable-past" : ""}${isNext ? " timetable-next" : ""}`}
              style={{
                background: isPast
                  ? "var(--surface-past, #f3f3f3)"
                  : isNext
                  ? "var(--surface-next, #e8f5e8)"
                  : "var(--surface-future, #fff)"
              }}
            >
              <div className="card-header">
                <div className="line-info">
                  <LineIcon line={entry.line.name} region={region} />
                </div>

                <div className="destination-info">
                  {entry.trip.headsign && entry.trip.headsign.trim() ? (
                    <strong>{entry.trip.headsign}</strong>
                  ) : (
                    <strong>{t("timetable.noDestination", "Línea")} {entry.line.name}</strong>
                  )}
                </div>

                <div className="time-info">
                  <span className="departure-time">
                    {entry.departure_time.slice(0, 5)}
                  </span>
                  <div className="service-id">
                    {parseServiceId(entry.trip.service_id)}
                  </div>
                </div>
              </div>
              <div className="card-body">
                {!isPast && entry.next_streets.length > 0 && (
                  <div className="route-streets">
                    {entry.next_streets.join(' — ')}
                  </div>
                )}
              </div>
            </div>
          );
        })}
      </div>

      {data.length === 0 && (
        <p className="no-data">{t("timetable.noData", "No hay datos de horarios disponibles")}</p>
      )}
    </div>
  );
};