Skip to content

Commit 3b5185b

Browse files
committed
Fix units on tide graph
1 parent a334e38 commit 3b5185b

4 files changed

Lines changed: 19 additions & 14 deletions

File tree

website/src/components/TideHeight.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
const IMPERIAL_LOCALES = ["en-US", "en-LR", "my-MM"];
2-
3-
const locale =
4-
typeof navigator !== "undefined" ? navigator.language : undefined;
5-
6-
function usesImperialUnits(locale: string | undefined): boolean {
7-
return !!locale && IMPERIAL_LOCALES.includes(locale);
8-
}
9-
10-
const imperial = usesImperialUnits(locale);
1+
import { imperial } from "../utils/units";
112

123
const FEET_PER_METER = 3.28084;
134
const MILES_PER_METER = 0.000621371;
@@ -55,5 +46,11 @@ export function TideHeight({
5546
value: number;
5647
precision?: number;
5748
}) {
58-
return <Distance meters={value} precision={precision} />;
49+
const unit = imperial ? "ft" : "m";
50+
return (
51+
<span className="tabular-nums">
52+
{value.toFixed(precision)}
53+
<span className="text-[0.8em]"> {unit}</span>
54+
</span>
55+
);
5956
}

website/src/components/tides/TideGraph.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface Props {
4141
}
4242

4343
export function TideGraph({ station, extremesData, timelineData }: Props) {
44-
const units: "meters" | "feet" = "feet";
44+
const units = timelineData?.units || extremesData?.units || "meters";
4545
const isReference = station.type === "reference";
4646

4747
const data: TideDataPoint[] = useMemo(() => {

website/src/components/tides/TideStation.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Station } from "@neaps/tide-database";
33
import { Temporal } from "@js-temporal/polyfill";
44
import { useNeapsAPI } from "../../utils/useNeapsAPI";
55
import type { ExtremesResponse, TimelineResponse } from "../../utils/tides";
6+
import { preferredUnits } from "../../utils/units";
67
import Today from "./Today";
78
import { TideGraph } from "./TideGraph";
89

@@ -33,12 +34,12 @@ export function TideStation({ station }: Props) {
3334

3435
const { data: extremesData, loading } = useNeapsAPI<ExtremesResponse>(
3536
`/tides/stations/${station.id}/extremes`,
36-
{ start: startDate, end: endDate },
37+
{ start: startDate, end: endDate, units: preferredUnits },
3738
);
3839

3940
const { data: timelineData } = useNeapsAPI<TimelineResponse>(
4041
`/tides/stations/${station.id}/timeline`,
41-
{ start: startDate, end: endDate },
42+
{ start: startDate, end: endDate, units: preferredUnits },
4243
{ skip: !isReference },
4344
);
4445

website/src/utils/units.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const IMPERIAL_LOCALES = ["en-US", "en-LR", "my-MM"];
2+
3+
const locale =
4+
typeof navigator !== "undefined" ? navigator.language : undefined;
5+
6+
export const imperial = !!locale && IMPERIAL_LOCALES.includes(locale);
7+
export const preferredUnits = imperial ? "feet" : "meters";

0 commit comments

Comments
 (0)