aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app
diff options
context:
space:
mode:
authorCopilot <198982749+Copilot@users.noreply.github.com>2025-11-03 10:51:47 +0100
committerGitHub <noreply@github.com>2025-11-03 10:51:47 +0100
commit60f8d79d9e8ccedcd95610a88dd7dc8ec5521aca (patch)
tree4fa59e85859ea94a579970fb38ecd32232e2425c /src/frontend/app
parent73b975b7cff87d1e90ad4d0abadc1f65d62ae2e6 (diff)
Simplify estimates endpoint to return only arrival data (#57)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: arielcostas <94913521+arielcostas@users.noreply.github.com>
Diffstat (limited to 'src/frontend/app')
-rw-r--r--src/frontend/app/components/GroupedTable.tsx10
-rw-r--r--src/frontend/app/components/RegularTable.tsx8
-rw-r--r--src/frontend/app/components/StopSheet.tsx8
-rw-r--r--src/frontend/app/routes/estimates-$id.tsx43
4 files changed, 37 insertions, 32 deletions
diff --git a/src/frontend/app/components/GroupedTable.tsx b/src/frontend/app/components/GroupedTable.tsx
index fd97d5b..9bdd5a0 100644
--- a/src/frontend/app/components/GroupedTable.tsx
+++ b/src/frontend/app/components/GroupedTable.tsx
@@ -1,9 +1,9 @@
-import { type StopDetails } from "../routes/estimates-$id";
+import { type Estimate } from "../routes/estimates-$id";
import LineIcon from "./LineIcon";
import { type RegionConfig } from "../data/RegionConfig";
interface GroupedTable {
- data: StopDetails;
+ data: Estimate[];
dataDate: Date | null;
regionConfig: RegionConfig;
}
@@ -17,7 +17,7 @@ export const GroupedTable: React.FC<GroupedTable> = ({ data, dataDate, regionCon
}
};
- const groupedEstimates = data.estimates.reduce(
+ const groupedEstimates = data.reduce(
(acc, estimate) => {
if (!acc[estimate.line]) {
acc[estimate.line] = [];
@@ -25,7 +25,7 @@ export const GroupedTable: React.FC<GroupedTable> = ({ data, dataDate, regionCon
acc[estimate.line].push(estimate);
return acc;
},
- {} as Record<string, typeof data.estimates>,
+ {} as Record<string, typeof data>,
);
const sortedLines = Object.keys(groupedEstimates).sort((a, b) => {
@@ -72,7 +72,7 @@ export const GroupedTable: React.FC<GroupedTable> = ({ data, dataDate, regionCon
)}
</tbody>
- {data?.estimates.length === 0 && (
+ {data?.length === 0 && (
<tfoot>
<tr>
<td colSpan={regionConfig.showMeters ? 4 : 3}>
diff --git a/src/frontend/app/components/RegularTable.tsx b/src/frontend/app/components/RegularTable.tsx
index 68b732a..868332f 100644
--- a/src/frontend/app/components/RegularTable.tsx
+++ b/src/frontend/app/components/RegularTable.tsx
@@ -1,10 +1,10 @@
import { useTranslation } from "react-i18next";
-import { type StopDetails } from "../routes/estimates-$id";
+import { type Estimate } from "../routes/estimates-$id";
import LineIcon from "./LineIcon";
import { type RegionConfig } from "../data/RegionConfig";
interface RegularTableProps {
- data: StopDetails;
+ data: Estimate[];
dataDate: Date | null;
regionConfig: RegionConfig;
}
@@ -56,7 +56,7 @@ export const RegularTable: React.FC<RegularTableProps> = ({
</thead>
<tbody>
- {data.estimates
+ {data
.sort((a, b) => a.minutes - b.minutes)
.map((estimate, idx) => (
<tr key={idx}>
@@ -80,7 +80,7 @@ export const RegularTable: React.FC<RegularTableProps> = ({
))}
</tbody>
- {data?.estimates.length === 0 && (
+ {data?.length === 0 && (
<tfoot>
<tr>
<td colSpan={regionConfig.showMeters ? 4 : 3}>
diff --git a/src/frontend/app/components/StopSheet.tsx b/src/frontend/app/components/StopSheet.tsx
index 0084c14..4bd75a8 100644
--- a/src/frontend/app/components/StopSheet.tsx
+++ b/src/frontend/app/components/StopSheet.tsx
@@ -6,7 +6,7 @@ import { Clock, ClockFading, Hourglass, RefreshCw } from "lucide-react";
import LineIcon from "./LineIcon";
import { StopSheetSkeleton } from "./StopSheetSkeleton";
import { ErrorDisplay } from "./ErrorDisplay";
-import { type StopDetails } from "../routes/estimates-$id";
+import { type Estimate } from "../routes/estimates-$id";
import { type RegionId, getRegionConfig } from "../data/RegionConfig";
import { useApp } from "../AppContext";
import "./StopSheet.css";
@@ -24,7 +24,7 @@ interface ErrorInfo {
message?: string;
}
-const loadStopData = async (region: RegionId, stopId: number): Promise<StopDetails> => {
+const loadStopData = async (region: RegionId, stopId: number): Promise<Estimate[]> => {
const regionConfig = getRegionConfig(region);
const resp = await fetch(`${regionConfig.estimatesEndpoint}?id=${stopId}`, {
headers: {
@@ -48,7 +48,7 @@ export const StopSheet: React.FC<StopSheetProps> = ({
const { t } = useTranslation();
const { region } = useApp();
const regionConfig = getRegionConfig(region);
- const [data, setData] = useState<StopDetails | null>(null);
+ const [data, setData] = useState<Estimate[] | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<ErrorInfo | null>(null);
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
@@ -120,7 +120,7 @@ export const StopSheet: React.FC<StopSheetProps> = ({
// Show only the next 4 arrivals
const limitedEstimates =
- data?.estimates.sort((a, b) => a.minutes - b.minutes).slice(0, 4) || [];
+ data?.sort((a, b) => a.minutes - b.minutes).slice(0, 4) || [];
return (
<Sheet
diff --git a/src/frontend/app/routes/estimates-$id.tsx b/src/frontend/app/routes/estimates-$id.tsx
index c48932c..60d533a 100644
--- a/src/frontend/app/routes/estimates-$id.tsx
+++ b/src/frontend/app/routes/estimates-$id.tsx
@@ -1,6 +1,6 @@
import { type JSX, useEffect, useState, useCallback } from "react";
import { useParams, Link } from "react-router";
-import StopDataProvider from "../data/StopDataProvider";
+import StopDataProvider, { type Stop } from "../data/StopDataProvider";
import { Star, Edit2, ExternalLink, RefreshCw } from "lucide-react";
import "./estimates-$id.css";
import { RegularTable } from "../components/RegularTable";
@@ -15,19 +15,11 @@ import { PullToRefresh } from "../components/PullToRefresh";
import { useAutoRefresh } from "../hooks/useAutoRefresh";
import { type RegionId, getRegionConfig } from "../data/RegionConfig";
-export interface StopDetails {
- stop: {
- id: number;
- name: string;
- latitude: number;
- longitude: number;
- };
- estimates: {
- line: string;
- route: string;
- minutes: number;
- meters: number;
- }[];
+export interface Estimate {
+ line: string;
+ route: string;
+ minutes: number;
+ meters: number;
}
interface ErrorInfo {
@@ -36,7 +28,7 @@ interface ErrorInfo {
message?: string;
}
-const loadData = async (region: RegionId, stopId: string): Promise<StopDetails> => {
+const loadData = async (region: RegionId, stopId: string): Promise<Estimate[]> => {
const regionConfig = getRegionConfig(region);
const resp = await fetch(`${regionConfig.estimatesEndpoint}?id=${stopId}`, {
headers: {
@@ -78,9 +70,10 @@ export default function Estimates() {
const params = useParams();
const stopIdNum = parseInt(params.id ?? "");
const [customName, setCustomName] = useState<string | undefined>(undefined);
+ const [stopData, setStopData] = useState<Stop | undefined>(undefined);
// Estimates data state
- const [data, setData] = useState<StopDetails | null>(null);
+ const [data, setData] = useState<Estimate[] | null>(null);
const [dataDate, setDataDate] = useState<Date | null>(null);
const [estimatesLoading, setEstimatesLoading] = useState(true);
const [estimatesError, setEstimatesError] = useState<ErrorInfo | null>(null);
@@ -121,6 +114,10 @@ export default function Estimates() {
const body = await loadData(region, params.id!);
setData(body);
setDataDate(new Date());
+
+ // Load stop data from StopDataProvider
+ const stop = await StopDataProvider.getStopById(region, stopIdNum);
+ setStopData(stop);
setCustomName(StopDataProvider.getCustomName(region, stopIdNum));
} catch (error) {
console.error('Error loading estimates data:', error);
@@ -198,8 +195,16 @@ export default function Estimates() {
}
};
+ // Helper function to get the display name for the stop
+ const getStopDisplayName = () => {
+ if (customName) return customName;
+ if (stopData?.name.intersect) return stopData.name.intersect;
+ if (stopData?.name.original) return stopData.name.original;
+ return `Parada ${stopIdNum}`;
+ };
+
const handleRename = () => {
- const current = customName ?? data?.stop.name;
+ const current = getStopDisplayName();
const input = window.prompt("Custom name for this stop:", current);
if (input === null) return; // cancelled
const trimmed = input.trim();
@@ -257,8 +262,8 @@ export default function Estimates() {
onClick={toggleFavourite}
/>
<Edit2 className="edit-icon" onClick={handleRename} />
- {customName ?? data?.stop.name ?? `Parada ${stopIdNum}`}{" "}
- <span className="estimates-stop-id">({data?.stop.id ?? stopIdNum})</span>
+ {getStopDisplayName()}{" "}
+ <span className="estimates-stop-id">({stopIdNum})</span>
</h1>
<button