aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/routes/stops-$id.tsx
blob: 38f6c5925b14f47295c6bf805b5bb836ef58d5b6 (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
import { CircleHelp, Eye, EyeClosed, Star } from "lucide-react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useLocation, useParams } from "react-router";
import { fetchArrivals } from "~/api/arrivals";
import {
  type Arrival,
  type Position,
  type RouteInfo,
  type StopArrivalsResponse,
} from "~/api/schema";
import { ArrivalList } from "~/components/arrivals/ArrivalList";
import { ErrorDisplay } from "~/components/ErrorDisplay";
import { PullToRefresh } from "~/components/PullToRefresh";
import RouteIcon from "~/components/RouteIcon";
import ServiceAlerts from "~/components/ServiceAlerts";
import { StopHelpModal } from "~/components/stop/StopHelpModal";
import { StopMapModal } from "~/components/stop/StopMapModal";
import { StopUsageChart } from "~/components/stop/StopUsageChart";
import { useJourney } from "~/contexts/JourneyContext";
import { usePageRightNode, usePageTitle } from "~/contexts/PageTitleContext";
import { formatHex } from "~/utils/colours";
import StopDataProvider from "../data/StopDataProvider";
import "../tailwind-full.css";
import "./stops-$id.css";

function StopFavouriteButton({ stopId }: { stopId: string }) {
  const { t } = useTranslation();
  const [favourited, setFavourited] = useState(() =>
    StopDataProvider.isFavourite(stopId)
  );

  const toggle = () => {
    if (favourited) {
      StopDataProvider.removeFavourite(stopId);
      setFavourited(false);
    } else {
      StopDataProvider.addFavourite(stopId);
      setFavourited(true);
    }
  };

  return (
    <button
      onClick={toggle}
      className={`app-header__menu-btn p-2 rounded-full transition-colors ${
        favourited
          ? "text-(--star-color)"
          : "text-slate-500 hover:text-slate-700 dark:text-slate-400 dark:hover:text-slate-200"
      }`}
      aria-label={t("stop.toggle_favourite", "Alternar favorito")}
    >
      <Star className={favourited ? "fill-current" : ""} size={24} />
    </button>
  );
}

export const getArrivalId = (a: Arrival): string => {
  return a.tripId;
};

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

export default function Estimates() {
  const { t } = useTranslation();
  const params = useParams();
  const location = useLocation();
  const stopId = params.id ?? "";
  const stopFeedId = stopId.split(":")[0] || stopId;
  const fallbackStopCode = stopId.split(":")[1] || stopId;
  const [stopName, setStopName] = useState<string | undefined>(undefined);
  const [apiRoutes, setApiRoutes] = useState<RouteInfo[]>([]);
  const [apiLocation, setApiLocation] = useState<Position | undefined>(
    undefined
  );

  // Data state
  const [data, setData] = useState<StopArrivalsResponse | null>(null);
  const [dataDate, setDataDate] = useState<Date | null>(null);
  const [dataLoading, setDataLoading] = useState(true);
  const [dataError, setDataError] = useState<ErrorInfo | null>(null);

  const [isManualRefreshing, setIsManualRefreshing] = useState(false);
  const [isMapModalOpen, setIsMapModalOpen] = useState(false);
  const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);
  const [isReducedView, setIsReducedView] = useState(false);
  const [selectedArrivalId, setSelectedArrivalId] = useState<
    string | undefined
  >(undefined);

  // Journey tracking
  const { activeJourney, startJourney, stopJourney } = useJourney();
  const trackedTripId =
    activeJourney?.stopId === stopId ? activeJourney.tripId : undefined;

  // If navigated from the journey banner, open the map for the tracked trip.
  // Empty dependency array is intentional: we only consume the navigation state
  // once on mount (location.state is fixed for the lifetime of this component
  // instance; setters from useState are stable and don't need to be listed).
  useEffect(() => {
    const state = location.state as
      | { openMap?: boolean; selectedTripId?: string }
      | null
      | undefined;
    if (state?.openMap && state?.selectedTripId) {
      setSelectedArrivalId(state.selectedTripId);
      setIsMapModalOpen(true);
    }
  }, []); // mount-only: see comment above

  const handleTrackArrival = useCallback(
    (arrival: Arrival) => {
      if (activeJourney?.tripId === arrival.tripId) {
        stopJourney();
        return;
      }
      startJourney({
        tripId: arrival.tripId,
        stopId,
        stopName: stopName ?? stopId,
        routeShortName: arrival.route.shortName,
        routeColour: arrival.route.colour,
        routeTextColour: arrival.route.textColour,
        headsignDestination: arrival.headsign.destination,
        initialMinutes: arrival.estimate.minutes,
        notifyAtMinutes: 2,
      });
    },
    [activeJourney, startJourney, stopJourney, stopId, stopName]
  );

  // Helper function to get the display name for the stop
  const getStopDisplayName = useCallback(() => {
    if (stopName) return stopName;
    return `Parada ${stopId}`;
  }, [stopId, stopName]);

  usePageTitle(getStopDisplayName());

  const rightNode = useMemo(
    () => <StopFavouriteButton stopId={stopId} />,
    [stopId]
  );
  usePageRightNode(rightNode);

  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 = useCallback(async () => {
    try {
      setDataError(null);

      const response = await fetchArrivals(stopId, false);
      setData(response);
      setStopName(response.stopName);
      setApiRoutes(response.routes);
      if (response.stopLocation) {
        setApiLocation(response.stopLocation);
      }
      setDataDate(new Date());
    } catch (error) {
      console.error("Error loading arrivals data:", error);
      setDataError(parseError(error));
      setData(null);
      setDataDate(null);
    }
  }, [stopId]);

  const refreshData = useCallback(async () => {
    await Promise.all([loadData()]);
  }, [loadData]);

  const handleManualRefresh = useCallback(async () => {
    try {
      setDataLoading(true);
      setIsManualRefreshing(true);
      await refreshData();
    } finally {
      setIsManualRefreshing(false);
      setDataLoading(false);
    }
  }, [refreshData]);

  useEffect(() => {
    // Initial load
    setDataLoading(true);
    loadData();

    StopDataProvider.pushRecent(stopId);
    setDataLoading(false);
  }, [stopId, loadData]);

  return (
    <PullToRefresh onRefresh={handleManualRefresh}>
      <div className="page-container stops-page flex-1">
        {apiRoutes.length > 0 && (
          <div className={`estimates-lines-container scrollable`}>
            {apiRoutes.map((line) => (
              <div key={line.shortName} className="estimates-line-icon">
                <RouteIcon
                  line={line.shortName}
                  colour={line.colour}
                  textColour={line.textColour}
                  mode="pill"
                />
              </div>
            ))}
          </div>
        )}

        <ServiceAlerts selectorFilter={[`stop#${stopId}`]} />

        <div className="estimates-list-container flex-1">
          {dataLoading ? (
            <>{/*TODO: New loading skeleton*/}</>
          ) : dataError ? (
            <ErrorDisplay
              error={dataError}
              onRetry={loadData}
              title={t(
                "errors.estimates_title",
                "Error al cargar estimaciones"
              )}
            />
          ) : data ? (
            <>
              <div className="flex flex-col gap-3">
                <div className="flex items-center justify-between">
                  <div className="flex flex-col gap-1">
                    <div className="consolidated-circulation-caption m-0 text-xs font-bold uppercase tracking-wider text-muted">
                      {t("estimates.caption", "Estimaciones a las {{time}}", {
                        time: dataDate?.toLocaleTimeString([], {
                          hour: "2-digit",
                          minute: "2-digit",
                        }),
                      })}
                    </div>
                    <div className="flex items-center gap-1.5 text-xs font-mono uppercase text-muted">
                      <span className="flex items-center justify-center rounded bg-slate-200 px-1.5 py-0.5 text-[10px] font-bold leading-none text-slate-700 dark:bg-slate-700 dark:text-slate-300">
                        {stopFeedId}
                      </span>
                      <span>{data.stopCode || fallbackStopCode}</span>
                    </div>
                  </div>

                  <div className="flex items-center gap-2">
                    <button
                      className="p-1.5 rounded-md hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors text-muted"
                      onClick={() => setIsHelpModalOpen(true)}
                    >
                      <CircleHelp className="w-5 h-5" />
                    </button>
                    {isReducedView ? (
                      <button
                        className="p-1.5 rounded-md hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors text-muted"
                        onClick={() => setIsReducedView(false)}
                      >
                        <EyeClosed className="w-5 h-5" />
                      </button>
                    ) : (
                      <button
                        className="p-1.5 rounded-md hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors text-muted"
                        onClick={() => setIsReducedView(true)}
                      >
                        <Eye className="w-5 h-5" />
                      </button>
                    )}
                  </div>
                </div>
              </div>
              <ArrivalList
                arrivals={data.arrivals}
                reduced={isReducedView}
                onArrivalClick={(arrival) => {
                  setSelectedArrivalId(getArrivalId(arrival));
                  setIsMapModalOpen(true);
                }}
                onTrackArrival={handleTrackArrival}
                trackedTripId={trackedTripId}
              />

              {data.usage && data.usage.length > 0 && (
                <div className="mt-8">
                  <StopUsageChart usage={data.usage} />
                </div>
              )}
            </>
          ) : null}
        </div>

        {apiLocation && (
          <StopMapModal
            stop={{
              stopId: stopId,
              name: stopName ?? "",
              latitude: apiLocation?.latitude,
              longitude: apiLocation?.longitude,
              lines: [],
            }}
            circulations={(data?.arrivals ?? []).map((a) => ({
              id: getArrivalId(a),
              currentPosition: a.currentPosition ?? undefined,
              colour: formatHex(a.route.colour),
              textColour: formatHex(a.route.textColour),
              shape: a.shape,
            }))}
            isOpen={isMapModalOpen}
            onClose={() => setIsMapModalOpen(false)}
            selectedCirculationId={selectedArrivalId}
          />
        )}

        <StopHelpModal
          isOpen={isHelpModalOpen}
          onClose={() => setIsHelpModalOpen(false)}
        />
      </div>
    </PullToRefresh>
  );
}