Skip to content

Commit c638e68

Browse files
committed
Fix navigation and api endpoints
1 parent 46737d8 commit c638e68

8 files changed

Lines changed: 62 additions & 58 deletions

File tree

apps/api/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ app.get("/data/init", async (_req, res) => {
5656
}
5757
});
5858

59-
app.get("/api/data/pilot/:id", async (req, res) => {
59+
app.get("/data/pilot/:id", async (req, res) => {
6060
try {
6161
const { id } = req.params;
6262
console.log("Requested pilot:", id);
@@ -115,7 +115,7 @@ app.get("/data/track/:id", async (req, res) => {
115115
}
116116
});
117117

118-
app.get("/api/data/aircraft/:reg", async (req, res) => {
118+
app.get("/data/aircraft/:reg", async (req, res) => {
119119
try {
120120
const { reg } = req.params;
121121
console.log("Requested aircraft:", reg);

apps/web/app/pilot/[id]/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import type { StaticAircraft } from "@sk/types/db";
22
import type { PilotLong } from "@sk/types/vatsim";
33
import PilotPanel from "@/components/Panels/PilotPanel";
44

5-
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001";
5+
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001/api";
66

77
async function fetchPilotLong(id: string): Promise<PilotLong | null> {
8-
const res = await fetch(`${API_URL}/api/data/pilot/${id}`, {
8+
const res = await fetch(`${API_URL}/data/pilot/${id}`, {
99
cache: "no-store",
1010
});
1111
if (!res.ok) return null;
1212
return res.json();
1313
}
1414

1515
async function fetchAircraftByReg(reg: string): Promise<StaticAircraft | null> {
16-
const res = await fetch(`${API_URL}/api/data/aircraft/${reg}`, {
16+
const res = await fetch(`${API_URL}/data/aircraft/${reg}`, {
1717
cache: "no-store",
1818
});
1919
if (!res.ok) return null;

apps/web/components/Map/Map.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33
import { useEffect } from "react";
44
import "./Map.css";
55
import { useRouter } from "next/navigation";
6-
import { onClick, onMoveEnd, onPointerMove, setNavigator } from "./utils/events";
6+
import { onClick, onMoveEnd, onPointerMove } from "./utils/events";
77
import { initMap } from "./utils/init";
88

99
export default function OMap() {
1010
const router = useRouter();
1111

1212
useEffect(() => {
13-
setNavigator((href) => router.push(href));
13+
const onNavigate = (e: Event) => {
14+
const href = (e as CustomEvent<string>).detail;
15+
if (href) router.push(href);
16+
};
17+
window.addEventListener("sr24:navigate", onNavigate);
1418

1519
const map = initMap();
1620
map.on(["moveend"], onMoveEnd);
1721
map.on("pointermove", onPointerMove);
1822
map.on("click", onClick);
1923

2024
return () => {
25+
window.removeEventListener("sr24:navigate", onNavigate);
2126
map.un(["moveend"], onMoveEnd);
2227
map.un("pointermove", onPointerMove);
2328
map.un("click", onClick);

apps/web/components/Map/utils/events.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import { addHighlightedPilot, clearHighlightedPilot } from "./pilotFeatures";
1414
import { initTrackFeatures } from "./trackFeatures";
1515

1616
export type NavigateFn = (href: string) => void;
17-
let navigate: NavigateFn | null = null;
18-
export function setNavigator(fn: NavigateFn) {
19-
navigate = fn;
17+
export function navigateTo(href: string) {
18+
window.dispatchEvent(new CustomEvent("sr24:navigate", { detail: href }));
2019
}
2120

2221
export function onMoveEnd(evt: BaseEvent | Event): void {
@@ -111,7 +110,7 @@ export async function onClick(evt: MapBrowserEvent): Promise<void> {
111110
}
112111

113112
if (!feature) {
114-
navigate?.(`/`);
113+
navigateTo(`/`);
115114
return;
116115
}
117116

@@ -127,7 +126,7 @@ export async function onClick(evt: MapBrowserEvent): Promise<void> {
127126

128127
if (id) {
129128
const strippedId = id.toString().replace(/^pilot_/, "");
130-
navigate?.(`/pilot/${strippedId}`);
129+
navigateTo(`/pilot/${strippedId}`);
131130
addHighlightedPilot(strippedId);
132131
}
133132
}
@@ -384,5 +383,5 @@ export function resetMap(): void {
384383
clickedFeature?.set("clicked", false);
385384
clickedFeature = null;
386385

387-
navigate?.(`/`);
386+
navigateTo(`/`);
388387
}

apps/web/components/Map/utils/init.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,50 @@ import { initSunLayer } from "./sunLayer";
77
let map: OlMap | null = null;
88

99
export function initMap(): OlMap {
10-
const savedView = localStorage.getItem("mapView");
11-
const initialCenter = [0, 0];
12-
const initialZoom = 2;
13-
14-
let center = initialCenter;
15-
let zoom = initialZoom;
16-
17-
if (savedView) {
18-
try {
19-
const parsed = JSON.parse(savedView) as {
20-
center: [number, number];
21-
zoom: number;
22-
};
23-
center = parsed.center;
24-
zoom = parsed.zoom;
25-
} catch {
26-
// fallback to default
27-
}
28-
}
29-
30-
const baseLayer = initBaseLayer();
31-
const sunLayer = initSunLayer();
32-
const dataLayers = initDataLayers();
33-
34-
map = new OlMap({
35-
target: "map",
36-
layers: [baseLayer, sunLayer, ...dataLayers],
37-
view: new View({
38-
center: fromLonLat(center),
39-
zoom,
40-
maxZoom: 18,
41-
minZoom: 3,
42-
extent: transformExtent([-190, -80, 190, 80], "EPSG:4326", "EPSG:3857"),
43-
}),
44-
controls: [],
45-
});
46-
47-
return map;
10+
const savedView = localStorage.getItem("mapView");
11+
const initialCenter = [0, 0];
12+
const initialZoom = 2;
13+
14+
let center = initialCenter;
15+
let zoom = initialZoom;
16+
17+
if (savedView) {
18+
try {
19+
const parsed = JSON.parse(savedView) as {
20+
center: [number, number];
21+
zoom: number;
22+
};
23+
center = parsed.center;
24+
zoom = parsed.zoom;
25+
} catch {
26+
// fallback to default
27+
}
28+
}
29+
30+
const baseLayer = initBaseLayer();
31+
const sunLayer = initSunLayer();
32+
const dataLayers = initDataLayers();
33+
34+
map = new OlMap({
35+
target: "map",
36+
layers: [baseLayer, sunLayer, ...dataLayers],
37+
view: new View({
38+
center: fromLonLat(center),
39+
zoom,
40+
maxZoom: 18,
41+
minZoom: 3,
42+
extent: transformExtent([-190, -80, 190, 80], "EPSG:4326", "EPSG:3857"),
43+
}),
44+
controls: [],
45+
});
46+
47+
return map;
4848
}
4949

5050
export function getMapView(): View | null {
51-
return map?.getView() || null;
51+
return map?.getView() || null;
5252
}
5353

5454
export function getMap(): OlMap | null {
55-
return map;
55+
return map;
5656
}

apps/web/components/Panels/BasePanel.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
line-height: 1rem;
9999
white-space: nowrap;
100100
overflow: hidden;
101-
text-overflow: ellipsis;
101+
text-overflow: ellipsis;
102102
}
103103

104104
.panel-desc-items {

apps/web/components/Panels/PilotPanel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface PilotPanelFetchData {
2424
type AccordionSection = "info" | "charts" | "pilot" | null;
2525
type MapInteraction = "route" | "follow" | null;
2626

27-
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001";
27+
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001/api";
2828

2929
function setHeight(ref: React.RefObject<HTMLDivElement | null>, isOpen: boolean) {
3030
if (!ref.current) return;
@@ -123,7 +123,7 @@ export default function PilotPanel({ initialPilot, aircraft }: { initialPilot: P
123123

124124
const fetchPilot = async () => {
125125
try {
126-
const res = await fetch(`${BASE_URL}/api/data/pilot/${pilot.id}`);
126+
const res = await fetch(`${BASE_URL}/data/pilot/${pilot.id}`);
127127
if (res.ok) {
128128
const updatedPilot: PilotLong = await res.json();
129129
setPilot(updatedPilot);

apps/web/storage/cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { dxGetAirline, dxGetAirport, dxGetFirs, dxGetTracons, dxInitDatabases }
1313

1414
type StatusSetter = (status: Partial<StatusMap> | ((prev: Partial<StatusMap>) => Partial<StatusMap>)) => void;
1515

16-
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001";
16+
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001/api";
1717

1818
let airportsShort: AirportShort[] = [];
1919
let controllersMerged: ControllerMerged[] = [];
@@ -128,7 +128,7 @@ export async function fetchTrackPoints(id: string): Promise<TrackPoint[]> {
128128
return trackPointsPending;
129129
}
130130

131-
trackPointsPending = fetch(`${BASE_URL}/api/data/track/${id}`).then((res) => res.json());
131+
trackPointsPending = fetch(`${BASE_URL}/data/track/${id}`).then((res) => res.json());
132132
trackPointsCache = await trackPointsPending;
133133
trackPointsPending = null;
134134

0 commit comments

Comments
 (0)