Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions apps/web/src/components/ElevationChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";

import type { ElevationPoint } from "@/types/route";
import { formatMiles, mToFt } from "@/lib/units";

const W = 600;
const H = 130;
const PAD = { top: 16, right: 16, bottom: 28, left: 52 };
const H = 144;
const PAD = { top: 16, right: 56, bottom: 42, left: 52 };
const INNER_W = W - PAD.left - PAD.right;
const INNER_H = H - PAD.top - PAD.bottom;

Expand Down Expand Up @@ -45,6 +46,7 @@ export function ElevationChart({ profile }: { profile: ElevationPoint[] }) {
}

const yTicks = ticks(minElev, maxElev, 4);
const yTicksFt = ticks(mToFt(minElev), mToFt(maxElev), 4);

const xLabelCount = Math.min(Math.ceil(maxDist) + 1, 8);
const xStep = maxDist / (xLabelCount - 1);
Expand Down Expand Up @@ -77,7 +79,7 @@ export function ElevationChart({ profile }: { profile: ElevationPoint[] }) {
<svg
viewBox={`0 0 ${W} ${H}`}
className="w-full"
style={{ height: 160 }}
style={{ height: 176 }}
aria-label="Elevation profile chart"
>
{yTicks.map((tick) => {
Expand Down Expand Up @@ -107,6 +109,23 @@ export function ElevationChart({ profile }: { profile: ElevationPoint[] }) {
);
})}

{yTicksFt.map((tickFt) => {
const yPos = y(tickFt / mToFt(1));
if (yPos < PAD.top - 4 || yPos > PAD.top + INNER_H + 4) return null;
return (
<text
key={`ft-${tickFt}`}
x={PAD.left + INNER_W + 6}
y={yPos + 4}
textAnchor="start"
fontSize={11}
fill="#78716c"
>
{tickFt} ft
</text>
);
})}

<path d={areaPath} fill="#9ca3af" opacity={0.68} />
<path
d={linePath}
Expand All @@ -127,6 +146,9 @@ export function ElevationChart({ profile }: { profile: ElevationPoint[] }) {
fill="#78716c"
>
{d} km
<tspan x={x(d)} dy={13} fontSize={10} fill="#a8a29e">
{formatMiles(d)}
</tspan>
</text>
))}
</svg>
Expand Down
11 changes: 6 additions & 5 deletions apps/web/src/components/RouteCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bike, Footprints, Mountain, Star } from "lucide-react";
import type { ActivityType, RouteCandidate } from "@/types/route";
import { formatActivity, formatSource } from "@/lib/geoUtils";
import { formatDistance, formatElevation } from "@/lib/units";

const activityIcons: Record<ActivityType, typeof Footprints> = {
running: Footprints,
Expand Down Expand Up @@ -49,21 +50,21 @@ export function RouteCard({
{route.explanation}
</p>
<div className="mt-5 grid grid-cols-2 gap-3 text-sm">
<Metric selected={selected} label="Distance" value={`${route.distanceKm} km`} />
<Metric selected={selected} label="Distance" value={formatDistance(route.distanceKm)} />
<Metric selected={selected} label="Duration" value={`${route.estimatedDurationMin} min`} />
<Metric selected={selected} label="Elevation gain" value={`${route.elevationGainM} m`} />
<Metric selected={selected} label="Elevation gain" value={formatElevation(route.elevationGainM)} />
<Metric selected={selected} label="Difficulty" value={route.difficulty ?? "Moderate"} />
{route.averageSlopePct != null && (
<Metric selected={selected} label="Avg grade" value={`${route.averageSlopePct.toFixed(1)}%`} />
)}
{route.elevDifferenceM != null && (
<Metric selected={selected} label="Elev difference" value={`${route.elevDifferenceM} m`} />
<Metric selected={selected} label="Elev difference" value={formatElevation(route.elevDifferenceM)} />
)}
{route.lowestElevM != null && (
<Metric selected={selected} label="Lowest elev" value={`${route.lowestElevM > 0 ? "+" : ""}${route.lowestElevM} m`} />
<Metric selected={selected} label="Lowest elev" value={formatElevation(route.lowestElevM, { signed: true })} />
)}
{route.highestElevM != null && (
<Metric selected={selected} label="Highest elev" value={`${route.highestElevM > 0 ? "+" : ""}${route.highestElevM} m`} />
<Metric selected={selected} label="Highest elev" value={formatElevation(route.highestElevM, { signed: true })} />
)}
</div>
<div className="mt-5 grid gap-3">
Expand Down
6 changes: 5 additions & 1 deletion apps/web/src/components/RouteGoalSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { LayerGroup, Map as LeafletMap } from "leaflet";
import { formatLatLng } from "@/lib/geoUtils";
import type { LatLng, RouteType, UserPreferences } from "@/types/route";
import { apiUrl } from "@/lib/api-client/routing";
import { formatDistance, formatMiles } from "@/lib/units";

type RouteGoal = Extract<RouteType, "point_to_point" | "loop">;
type TargetMode = "distance" | "time";
Expand Down Expand Up @@ -234,6 +235,9 @@ function PresetRow({
}`}
>
{item.label}
{suffix === "km" && (
<span className="ml-1 font-normal opacity-75">({formatMiles(item.value)})</span>
)}
</button>
))}
<button
Expand All @@ -248,7 +252,7 @@ function PresetRow({
</div>
{isCustom && (
<label className="mt-5 block text-sm font-semibold text-stone-700">
Custom value: {customValue} {suffix}
Custom value: {suffix === "km" ? formatDistance(customValue) : `${customValue} ${suffix}`}
<input
type="range"
min={suffix === "km" ? 2 : 20}
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/components/RouteSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bike, Footprints, Mountain } from "lucide-react";
import type { ActivityType, UserPreferences } from "@/types/route";
import { formatRouteType } from "@/lib/geoUtils";
import { formatDistance } from "@/lib/units";

const activityIcons: Record<ActivityType, typeof Footprints> = {
running: Footprints,
Expand All @@ -15,7 +16,7 @@ const shadeNote = (value: number) =>
export function RouteSummary({ preferences }: { preferences: UserPreferences }) {
const Icon = activityIcons[preferences.activity];
const target = preferences.targetDistanceKm
? `${preferences.targetDistanceKm} km`
? formatDistance(preferences.targetDistanceKm)
: preferences.targetDurationMin
? `${preferences.targetDurationMin} min`
: "flexible distance";
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/SavedRouteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ExportButtons } from "./ExportButtons";
import { formatActivity, formatSource } from "@/lib/geoUtils";
import { useRouteStore } from "@/store/routeStore";
import type { SavedRoute } from "@/types/route";
import { formatDistance, formatElevation } from "@/lib/units";

export function SavedRouteCard({ route }: { route: SavedRoute }) {
const router = useRouter();
Expand All @@ -25,8 +26,8 @@ export function SavedRouteCard({ route }: { route: SavedRoute }) {
</p>
<h2 className="mt-2 text-2xl font-semibold text-stone-950">{route.name}</h2>
<div className="mt-3 flex flex-wrap gap-3 text-sm text-stone-600">
<span>{route.distanceKm} km</span>
<span>{route.elevationGainM} m gain</span>
<span>{formatDistance(route.distanceKm)}</span>
<span>{formatElevation(route.elevationGainM)} gain</span>
<span className="inline-flex items-center gap-1">
<CalendarDays size={15} />
Saved {savedDate}
Expand Down
7 changes: 4 additions & 3 deletions apps/web/src/components/StravaRoutePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { formatActivity } from "@/lib/geoUtils";
import { loadStravaSegments } from "@/lib/api-client/strava";
import { useRouteStore } from "@/store/routeStore";
import type { ExternalRouteMock, LatLng } from "@/types/route";
import { formatDistance, formatElevation } from "@/lib/units";

type StravaFilter = "all" | "running" | "cycling";
type StravaStatus = "loading" | "live" | "mock" | "error";
Expand Down Expand Up @@ -169,7 +170,7 @@ export function StravaRoutePicker() {
setSelectedRouteId(data.segments[0].id);
setStravaStatus("live");
setStravaMessage(
`Showing ${data.segments.length} of up to ${STRAVA_SEGMENT_LIMIT} live Strava segment${data.segments.length === 1 ? "" : "s"} with a path inside ${STRAVA_RADIUS_KM} km.`,
`Showing ${data.segments.length} of up to ${STRAVA_SEGMENT_LIMIT} live Strava segment${data.segments.length === 1 ? "" : "s"} with a path inside ${formatDistance(STRAVA_RADIUS_KM)}.`,
);
return;
}
Expand Down Expand Up @@ -499,8 +500,8 @@ export function StravaRoutePicker() {
</span>
</div>
<dl className="mt-4 grid grid-cols-3 gap-2 text-sm">
<Metric icon={<Activity size={15} />} label="Distance" value={`${route.distanceKm} km`} />
<Metric icon={<Mountain size={15} />} label="Gain" value={`${route.elevationGainM} m`} />
<Metric icon={<Activity size={15} />} label="Distance" value={formatDistance(route.distanceKm)} />
<Metric icon={<Mountain size={15} />} label="Gain" value={formatElevation(route.elevationGainM)} />
<Metric
icon={<Timer size={15} />}
label="Time"
Expand Down
22 changes: 22 additions & 0 deletions apps/web/src/lib/__tests__/units.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import { formatDistance, formatElevation, formatFeet, formatMiles } from "../units";

describe("unit formatting", () => {
it("formats distance in km with miles alongside", () => {
expect(formatDistance(5)).toBe("5 km (3.1 mi)");
expect(formatDistance(42.2)).toBe("42.2 km (26.2 mi)");
expect(formatDistance(0)).toBe("0 km (0 mi)");
expect(formatMiles(200)).toBe("124 mi");
});

it("formats elevation in m with feet alongside", () => {
expect(formatElevation(120)).toBe("120 m (394 ft)");
expect(formatElevation(0)).toBe("0 m (0 ft)");
expect(formatFeet(-10)).toBe("-33 ft");
});

it("prefixes positive signed elevations", () => {
expect(formatElevation(50, { signed: true })).toBe("+50 m (+164 ft)");
expect(formatElevation(-5, { signed: true })).toBe("-5 m (-16 ft)");
});
});
36 changes: 36 additions & 0 deletions apps/web/src/lib/units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const MI_PER_KM = 0.621371;
const FT_PER_M = 3.28084;

export function kmToMi(km: number) {
return km * MI_PER_KM;
}

export function mToFt(m: number) {
return m * FT_PER_M;
}

function trimDecimals(value: number, digits: number) {
return Number(value.toFixed(digits)).toString();
}

/** "5.2 mi" — one decimal under 100 mi, whole miles above. */
export function formatMiles(km: number) {
const mi = kmToMi(km);
return `${trimDecimals(mi, Math.abs(mi) < 100 ? 1 : 0)} mi`;
}

/** "394 ft" — rounded to the nearest foot. */
export function formatFeet(m: number) {
return `${Math.round(mToFt(m))} ft`;
}

/** "8.4 km (5.2 mi)" */
export function formatDistance(km: number) {
return `${km} km (${formatMiles(km)})`;
}

/** "120 m (394 ft)"; pass signed to prefix positive values with "+". */
export function formatElevation(m: number, { signed = false }: { signed?: boolean } = {}) {
const sign = signed && m > 0 ? "+" : "";
return `${sign}${m} m (${sign}${formatFeet(m)})`;
}
Loading